From fda1265d2ea44c08aa7e72352f4bac82d215b282 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?=
 <jeanmichael.celerier@gmail.com>
Date: Mon, 8 May 2017 12:52:10 +0200
Subject: [PATCH] Add an android-clang-libc++ mkspec

---
 configure.pri                                     |   2 +-
 mkspecs/android-clang-libc++/qmake.conf           |  39 +++++
 mkspecs/android-clang-libc++/qplatformdefs.h      | 180 ++++++++++++++++++++++
 mkspecs/android-clang/qmake.conf                  |   6 +
 mkspecs/android-g++/qmake.conf                    |   6 +
 mkspecs/common/android-base-head.conf             |   3 -
 mkspecs/common/android-base-tail.conf             |   2 +-
 mkspecs/features/android/sdk.prf                  |   2 +-
 src/android/jar/jar.pri                           |   2 +-
 src/plugins/platforms/android/androidjniinput.cpp |   1 +
 10 files changed, 236 insertions(+), 7 deletions(-)
 create mode 100644 mkspecs/android-clang-libc++/qmake.conf
 create mode 100644 mkspecs/android-clang-libc++/qplatformdefs.h

diff --git a/configure.pri b/configure.pri
index c422c73650..9edbc95838 100644
--- a/configure.pri
+++ b/configure.pri
@@ -564,7 +564,7 @@ defineTest(qtConfOutput_prepareOptions) {
 
         platform = $$eval(config.input.android-ndk-platform)
         isEmpty(platform): \
-            platform = android-16  ### the windows configure disagrees ...
+            platform = android-22  ### the windows configure disagrees ...
 
         $${currentConfig}.output.devicePro += \
             "DEFAULT_ANDROID_SDK_ROOT = $$val_escape(sdk_root)" \
diff --git a/mkspecs/android-clang-libc++/qmake.conf b/mkspecs/android-clang-libc++/qmake.conf
new file mode 100644
index 0000000000..cf8e88a8b9
--- /dev/null
+++ b/mkspecs/android-clang-libc++/qmake.conf
@@ -0,0 +1,39 @@
+# qmake configuration for building with android-clang and libc++
+MAKEFILE_GENERATOR      = UNIX
+QMAKE_PLATFORM          = android
+QMAKE_COMPILER          = gcc clang llvm
+
+CONFIG                 += android_install unversioned_soname unversioned_libname plugin_with_soname android_deployment_settings
+
+include(../common/linux.conf)
+include(../common/gcc-base-unix.conf)
+include(../common/clang.conf)
+include(../common/android-base-head.conf)
+
+NDK_LLVM_PATH = $$NDK_ROOT/toolchains/llvm/prebuilt/$$NDK_HOST
+QMAKE_CC      = $$NDK_LLVM_PATH/bin/clang
+QMAKE_CXX     = $$NDK_LLVM_PATH/bin/clang++
+
+equals(ANDROID_TARGET_ARCH, armeabi-v7a): \
+    QMAKE_CFLAGS = -target armv7-none-linux-androideabi
+else: equals(ANDROID_TARGET_ARCH, armeabi): \
+    QMAKE_CFLAGS = -target armv5te-none-linux-androideabi
+else: equals(ANDROID_TARGET_ARCH, arm64-v8a): \
+    QMAKE_CFLAGS = -target aarch64-none-linux-android
+else: equals(ANDROID_TARGET_ARCH, x86): \
+    QMAKE_CFLAGS = -target i686-none-linux-android
+else: equals(ANDROID_TARGET_ARCH, x86_64): \
+    QMAKE_CFLAGS = -target x86_64-none-linux-android
+else: equals(ANDROID_TARGET_ARCH, mips): \
+    QMAKE_CFLAGS += -target mipsel-none-linux-android
+else: equals(ANDROID_TARGET_ARCH, mips64): \
+    QMAKE_CFLAGS = -target mips64el-none-linux-android
+
+QMAKE_LINK = $$QMAKE_CXX $$QMAKE_CFLAGS -gcc-toolchain $$NDK_TOOLCHAIN_PATH
+
+ANDROID_SOURCES_CXX_STL_LIBDIR = $$NDK_ROOT/sources/cxx-stl/llvm-libc++/libs/$$ANDROID_TARGET_ARCH $$ANDROID_SOURCES_CXX_STL_LIBDIR/include
+ANDROID_SOURCES_CXX_STL_INCDIR = $$NDK_ROOT/sources/cxx-stl/llvm-libc++/include
+ 
+ANDROID_CXX_STL_LIBS           = -stdlib=libc++
+
+include(../common/android-base-tail.conf)
diff --git a/mkspecs/android-clang-libc++/qplatformdefs.h b/mkspecs/android-clang-libc++/qplatformdefs.h
new file mode 100644
index 0000000000..017233658e
--- /dev/null
+++ b/mkspecs/android-clang-libc++/qplatformdefs.h
@@ -0,0 +1,180 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the qmake spec of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QPLATFORMDEFS_H
+#define QPLATFORMDEFS_H
+
+// Get Qt defines/settings
+
+#include "qglobal.h"
+
+// Set any POSIX/XOPEN defines at the top of this file to turn on specific APIs
+
+// 1) need to reset default environment if _BSD_SOURCE is defined
+// 2) need to specify POSIX thread interfaces explicitly in glibc 2.0
+// 3) it seems older glibc need this to include the X/Open stuff
+
+#include <unistd.h>
+
+// We are hot - unistd.h should have turned on the specific APIs we requested
+
+#include <features.h>
+#include <pthread.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <grp.h>
+#include <pwd.h>
+#include <signal.h>
+#include <dlfcn.h>
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/ipc.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#ifndef QT_NO_IPV6IFNAME
+#include <net/if.h>
+#endif
+
+#ifndef _GNU_SOURCE
+#  define _GNU_SOURCE
+#endif
+
+#ifdef QT_LARGEFILE_SUPPORT
+#define QT_STATBUF              struct stat64
+#define QT_STATBUF4TSTAT        struct stat64
+#define QT_STAT                 ::stat64
+#define QT_FSTAT                ::fstat64
+#define QT_LSTAT                ::lstat64
+#define QT_OPEN                 ::open64
+#define QT_TRUNCATE             ::truncate64
+#define QT_FTRUNCATE            ::ftruncate64
+#define QT_LSEEK                ::lseek64
+#else
+#define QT_STATBUF              struct stat
+#define QT_STATBUF4TSTAT        struct stat
+#define QT_STAT                 ::stat
+#define QT_FSTAT                ::fstat
+#define QT_LSTAT                ::lstat
+#define QT_OPEN                 ::open
+#define QT_TRUNCATE             ::truncate
+#define QT_FTRUNCATE            ::ftruncate
+#define QT_LSEEK                ::lseek
+#endif
+
+#ifdef QT_LARGEFILE_SUPPORT
+#define QT_FOPEN                ::fopen64
+#define QT_FSEEK                ::fseeko64
+#define QT_FTELL                ::ftello64
+#define QT_FGETPOS              ::fgetpos64
+#define QT_FSETPOS              ::fsetpos64
+#define QT_MMAP                 ::mmap64
+#define QT_FPOS_T               fpos64_t
+#define QT_OFF_T                off64_t
+#else
+#define QT_FOPEN                ::fopen
+#define QT_FSEEK                ::fseek
+#define QT_FTELL                ::ftell
+#define QT_FGETPOS              ::fgetpos
+#define QT_FSETPOS              ::fsetpos
+#define QT_MMAP                 ::mmap
+#define QT_FPOS_T               fpos_t
+#define QT_OFF_T                long
+#endif
+
+#define QT_STAT_REG             S_IFREG
+#define QT_STAT_DIR             S_IFDIR
+#define QT_STAT_MASK            S_IFMT
+#define QT_STAT_LNK             S_IFLNK
+#define QT_SOCKET_CONNECT       ::connect
+#define QT_SOCKET_BIND          ::bind
+#define QT_FILENO               fileno
+#define QT_CLOSE                ::close
+#define QT_READ                 ::read
+#define QT_WRITE                ::write
+#define QT_ACCESS               ::access
+#define QT_GETCWD               ::getcwd
+#define QT_CHDIR                ::chdir
+#define QT_MKDIR                ::mkdir
+#define QT_RMDIR                ::rmdir
+#define QT_OPEN_LARGEFILE       O_LARGEFILE
+#define QT_OPEN_RDONLY          O_RDONLY
+#define QT_OPEN_WRONLY          O_WRONLY
+#define QT_OPEN_RDWR            O_RDWR
+#define QT_OPEN_CREAT           O_CREAT
+#define QT_OPEN_TRUNC           O_TRUNC
+#define QT_OPEN_APPEND          O_APPEND
+
+// Directory iteration
+#define QT_DIR                  DIR
+
+#define QT_OPENDIR              ::opendir
+#define QT_CLOSEDIR             ::closedir
+
+#if defined(QT_LARGEFILE_SUPPORT) \
+        && defined(QT_USE_XOPEN_LFS_EXTENSIONS) \
+        && !defined(QT_NO_READDIR64)
+#define QT_DIRENT               struct dirent64
+#define QT_READDIR              ::readdir64
+#define QT_READDIR_R            ::readdir64_r
+#else
+#define QT_DIRENT               struct dirent
+#define QT_READDIR              ::readdir
+#define QT_READDIR_R            ::readdir_r
+#endif
+
+#define QT_SOCKET_CONNECT       ::connect
+#define QT_SOCKET_BIND          ::bind
+
+
+#define QT_SIGNAL_RETTYPE       void
+#define QT_SIGNAL_ARGS          int
+#define QT_SIGNAL_IGNORE        SIG_IGN
+
+#define QT_SOCKLEN_T            socklen_t
+
+#if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)
+#define QT_SNPRINTF             ::snprintf
+#define QT_VSNPRINTF            ::vsnprintf
+#endif
+
+#endif // QPLATFORMDEFS_H
diff --git a/mkspecs/android-clang/qmake.conf b/mkspecs/android-clang/qmake.conf
index 3e621c7d77..30ea6f08aa 100644
--- a/mkspecs/android-clang/qmake.conf
+++ b/mkspecs/android-clang/qmake.conf
@@ -31,4 +31,10 @@ else: equals(ANDROID_TARGET_ARCH, mips64): \
 
 QMAKE_LINK = $$QMAKE_CXX $$QMAKE_CFLAGS -gcc-toolchain $$NDK_TOOLCHAIN_PATH
 
+
+ANDROID_SOURCES_CXX_STL_LIBDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/libs/$$ANDROID_TARGET_ARCH
+ANDROID_SOURCES_CXX_STL_INCDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/include $$ANDROID_SOURCES_CXX_STL_LIBDIR/include
+
+ANDROID_CXX_STL_LIBS    = -lgnustl_shared -lgcc
+
 include(../common/android-base-tail.conf)
diff --git a/mkspecs/android-g++/qmake.conf b/mkspecs/android-g++/qmake.conf
index 2b81ac58f0..efa2839d49 100644
--- a/mkspecs/android-g++/qmake.conf
+++ b/mkspecs/android-g++/qmake.conf
@@ -15,4 +15,10 @@ QMAKE_CXX               = $$QMAKE_GCC
 QMAKE_CFLAGS            =
 QMAKE_LINK              = $$QMAKE_GCC
 
+
+ANDROID_SOURCES_CXX_STL_LIBDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/libs/$$ANDROID_TARGET_ARCH
+ANDROID_SOURCES_CXX_STL_INCDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/include $$ANDROID_SOURCES_CXX_STL_LIBDIR/include
+
+ANDROID_CXX_STL_LIBS    = -lgnustl_shared -lgcc
+
 include(../common/android-base-tail.conf)
diff --git a/mkspecs/common/android-base-head.conf b/mkspecs/common/android-base-head.conf
index ae4933c453..0ac0e444d1 100644
--- a/mkspecs/common/android-base-head.conf
+++ b/mkspecs/common/android-base-head.conf
@@ -73,9 +73,6 @@ ANDROID_PLATFORM_PATH  = $$ANDROID_PLATFORM_ROOT_PATH/usr
 QMAKE_ANDROID_PLATFORM_INCDIR = $$ANDROID_PLATFORM_PATH/include
 QMAKE_ANDROID_PLATFORM_LIBDIR = $$ANDROID_PLATFORM_PATH/lib
 
-ANDROID_SOURCES_CXX_STL_LIBDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/libs/$$ANDROID_TARGET_ARCH
-ANDROID_SOURCES_CXX_STL_INCDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/include $$ANDROID_SOURCES_CXX_STL_LIBDIR/include
-
 equals(ANDROID_TARGET_ARCH, x86_64)|equals(ANDROID_TARGET_ARCH, mips64): \
     QMAKE_ANDROID_PLATFORM_LIBDIR = $${QMAKE_ANDROID_PLATFORM_LIBDIR}64
 
diff --git a/mkspecs/common/android-base-tail.conf b/mkspecs/common/android-base-tail.conf
index 23bd6696de..a2ba85258c 100644
--- a/mkspecs/common/android-base-tail.conf
+++ b/mkspecs/common/android-base-tail.conf
@@ -91,7 +91,7 @@ QMAKE_LFLAGS_NOUNDEF    = -Wl,--no-undefined
 QMAKE_LFLAGS_RPATH      = -Wl,-rpath=
 QMAKE_LFLAGS_RPATHLINK  = -Wl,-rpath-link=
 
-QMAKE_LIBS_PRIVATE      = -lgnustl_shared -llog -lz -lm -ldl -lc -lgcc
+QMAKE_LIBS_PRIVATE      = $$ANDROID_CXX_STL_LIBS -llog -lz -lm -ldl -lc
 QMAKE_LIBS_X11          =
 QMAKE_LIBS_THREAD       =
 QMAKE_LIBS_EGL          = -lEGL
diff --git a/mkspecs/features/android/sdk.prf b/mkspecs/features/android/sdk.prf
index eee7ac2d5d..07f7c673a9 100644
--- a/mkspecs/features/android/sdk.prf
+++ b/mkspecs/features/android/sdk.prf
@@ -1,6 +1,6 @@
 API_VERSION_TO_USE = $$(ANDROID_API_VERSION)
 isEmpty(API_VERSION_TO_USE): API_VERSION_TO_USE = $$API_VERSION
-isEmpty(API_VERSION_TO_USE): API_VERSION_TO_USE = android-16
+isEmpty(API_VERSION_TO_USE): API_VERSION_TO_USE = android-22
 
 ANDROID_JAR_FILE = $$ANDROID_SDK_ROOT/platforms/$$API_VERSION_TO_USE/android.jar
 !exists($$ANDROID_JAR_FILE) {
diff --git a/src/android/jar/jar.pri b/src/android/jar/jar.pri
index 4535880536..5807787672 100644
--- a/src/android/jar/jar.pri
+++ b/src/android/jar/jar.pri
@@ -1,6 +1,6 @@
 CONFIG += java
 DESTDIR = $$[QT_INSTALL_PREFIX/get]/jar
-API_VERSION = android-16
+API_VERSION = android-22
 
 PATHPREFIX = $$PWD/src/org/qtproject/qt5/android/
 
diff --git a/src/plugins/platforms/android/androidjniinput.cpp b/src/plugins/platforms/android/androidjniinput.cpp
index 8372bf6484..198ff85d74 100644
--- a/src/plugins/platforms/android/androidjniinput.cpp
+++ b/src/plugins/platforms/android/androidjniinput.cpp
@@ -48,6 +48,7 @@
 
 #include <QGuiApplication>
 #include <QDebug>
+#include <QtMath>
 
 QT_BEGIN_NAMESPACE
 
-- 
2.12.2

