Changeset View
Changeset View
Standalone View
Standalone View
toolchain/Android.cmake
1 | #.rst: | 1 | #.rst: | ||
---|---|---|---|---|---|
2 | # AndroidToolchain | 2 | # AndroidToolchain | ||
3 | # ---------------- | 3 | # ---------------- | ||
4 | # | 4 | # | ||
5 | # Enable easy compilation of cmake projects on Android. | 5 | # Enable easy compilation of cmake projects on Android. | ||
6 | # | 6 | # | ||
7 | # By using this android toolchain, the projects will be set up to compile the | 7 | # By using this android toolchain, the projects will be set up to compile the | ||
8 | # specified project targeting an Android platform, depending on its input. | 8 | # specified project targeting an Android platform, depending on its input. | ||
9 | # Furthermore, if desired, an APK can be directly generated by using the | 9 | # Furthermore, if desired, an APK can be directly generated by using the | ||
10 | # `androiddeployqt <http://doc.qt.io/qt-5/deployment-android.html>`_ tool. | 10 | # `androiddeployqt <http://doc.qt.io/qt-5/deployment-android.html>`_ tool. | ||
11 | # | 11 | # | ||
12 | # CMake upstream has Android support now. This module will still give us some | ||||
13 | # useful features offering androiddeployqt integration and adequate executables | ||||
14 | # format for our Android applications. | ||||
15 | # | ||||
16 | # Since we are using CMake Android support, any information from CMake documentation | ||||
17 | # still applies: | ||||
18 | # https://cmake.org/cmake/help/v3.7/manual/cmake-toolchains.7.html#cross-compiling-for-android | ||||
19 | # | ||||
12 | # .. note:: | 20 | # .. note:: | ||
13 | # | 21 | # | ||
14 | # This module requires CMake 3.1. | 22 | # This module requires CMake 3.7. | ||
15 | # | 23 | # | ||
16 | # Since 1.7.0. | 24 | # Since 1.7.0. | ||
17 | # | 25 | # | ||
18 | # Usage | 26 | # Usage | ||
19 | # ===== | 27 | # ===== | ||
20 | # | 28 | # | ||
21 | # To use this file, you need to set the ``CMAKE_TOOLCHAIN_FILE`` to point to | 29 | # To use this file, you need to set the ``CMAKE_TOOLCHAIN_FILE`` to point to | ||
22 | # ``AndroidToolchain.cmake`` on the command line:: | 30 | # ``AndroidToolchain.cmake`` on the command line:: | ||
23 | # | 31 | # | ||
24 | # cmake -DCMAKE_TOOLCHAIN_FILE=/usr/share/ECM/toolchain/AndroidToolchain.cmake | 32 | # cmake -DCMAKE_TOOLCHAIN_FILE=/usr/share/ECM/toolchain/AndroidToolchain.cmake | ||
25 | # | 33 | # | ||
26 | # You will also need to provide the locations of the Android NDK and SDK. This | 34 | # You will also need to provide the locations of the Android NDK and SDK. This | ||
27 | # can be done on the commandline or with environment variables; in either case | 35 | # can be done on the commandline or with environment variables; in either case | ||
28 | # the variable names are: | 36 | # the variable names are: | ||
29 | # | 37 | # | ||
30 | # ``ANDROID_NDK`` | 38 | # ``CMAKE_ANDROID_NDK`` | ||
31 | # The NDK root path. | 39 | # The NDK root path. | ||
32 | # ``ANDROID_SDK_ROOT`` | 40 | # ``ANDROID_SDK_ROOT`` | ||
33 | # The SDK root path. | 41 | # The SDK root path. | ||
34 | # | 42 | # | ||
35 | # Additional options are specified as cache variables (eg: on the command line): | 43 | # Additional options are specified as cache variables (eg: on the command line): | ||
36 | # | 44 | # | ||
37 | # ``ANDROID_ARCHITECTURE`` | | |||
38 | # The architecture to compile for. Default: ``arm``. | | |||
39 | # ``ANDROID_TOOLCHAIN`` | 45 | # ``ANDROID_TOOLCHAIN`` | ||
40 | # The toolchain to use. See the ``toolchains`` directory of the NDK. | 46 | # The toolchain to use. See the ``toolchains`` directory of the NDK. | ||
41 | # Default: ``arm-linux-androideabi``. | 47 | # Default: ``arm-linux-androideabi``. | ||
42 | # ``ANDROID_COMPILER_PREFIX`` | 48 | # ``ANDROID_COMPILER_PREFIX`` | ||
43 | # The prefix for the gcc and g++ executables. See the ``toolchains/$ANDROID_TOOLCHAIN/prebuilt/*/bin/*-gcc`` in the NDK. | 49 | # The prefix for the gcc and g++ executables. See the ``toolchains/$ANDROID_TOOLCHAIN/prebuilt/*/bin/*-gcc`` in the NDK. | ||
44 | # Default: ``arm-linux-androideabi``. | 50 | # Default: ``arm-linux-androideabi``. | ||
45 | # ``ANDROID_ABI`` | 51 | # ``ANDROID_ABI`` | ||
46 | # The ABI to use. See the ``sources/cxx-stl/gnu-libstdc++/*/libs`` | 52 | # The ABI to use. See the ``sources/cxx-stl/gnu-libstdc++/*/libs`` | ||
47 | # directories in the NDK. Default: ``armeabi-v7a``. | 53 | # directories in the NDK. Default: ``armeabi-v7a``. | ||
48 | # ``ANDROID_GCC_VERSION`` | | |||
49 | # The GCC version to use. Default: ``4.9``. | | |||
50 | # ``ANDROID_API_LEVEL`` | | |||
51 | # The `API level | | |||
52 | # <http://developer.android.com/guide/topics/manifest/uses-sdk-element.html>`_ | | |||
53 | # to require. Default: ``14``. | | |||
54 | # ``ANDROID_SDK_BUILD_TOOLS_REVISION`` | 54 | # ``ANDROID_SDK_BUILD_TOOLS_REVISION`` | ||
55 | # The build tools version to use. Default: ``21.1.1``. | 55 | # The build tools version to use. Default: ``21.1.1``. | ||
56 | # | 56 | # | ||
57 | # For integrating other libraries which are not part of the Android toolchain, | 57 | # For integrating other libraries which are not part of the Android toolchain, | ||
58 | # like Qt5, and installed to a separate prefix on the host system, the install | 58 | # like Qt5, and installed to a separate prefix on the host system, the install | ||
59 | # prefixes of those libraries would be passed as alternative roots as list via | 59 | # prefixes of those libraries would be passed as alternative roots as list via | ||
60 | # ``ECM_ADDITIONAL_FIND_ROOT_PATH``. Since 5.30.0. | 60 | # ``ECM_ADDITIONAL_FIND_ROOT_PATH``. Since 5.30.0. | ||
61 | # | 61 | # | ||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | |||||
130 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 130 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
131 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 131 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
132 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 132 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
133 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 133 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
134 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 134 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
135 | 135 | | |||
136 | cmake_minimum_required(VERSION "3.7") | 136 | cmake_minimum_required(VERSION "3.7") | ||
137 | 137 | | |||
138 | #input | 138 | macro(set_deprecated_variable actual_variable deprecated_variable default_value) | ||
139 | set(ANDROID_NDK "$ENV{ANDROID_NDK}" CACHE path "Android NDK path") | 139 | set(${deprecated_variable} "${default_value}" CACHE STRING "Deprecated. Use ${actual_variable}") | ||
140 | if (NOT DEFINED ${${actual_variable}}) | ||||
141 | set(${actual_variable} ${${deprecated_variable}}) | ||||
142 | endif() | ||||
143 | endmacro() | ||||
144 | | ||||
145 | set_deprecated_variable(CMAKE_ANDROID_NDK ANDROID_NDK "$ENV{ANDROID_NDK}") | ||||
146 | set_deprecated_variable(CMAKE_ANDROID_ARCH ANDROID_ARCHITECTURE "arm") | ||||
147 | set_deprecated_variable(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION ANDROID_GCC_VERSION "4.9") | ||||
148 | set_deprecated_variable(CMAKE_ANDROID_ARCH_ABI ANDROID_ABI "armeabi-v7a") | ||||
149 | set_deprecated_variable(CMAKE_ANDROID_API ANDROID_API_LEVEL "14") | ||||
150 | | ||||
140 | set(ANDROID_SDK_ROOT "$ENV{ANDROID_SDK_ROOT}" CACHE path "Android SDK path") | 151 | set(ANDROID_SDK_ROOT "$ENV{ANDROID_SDK_ROOT}" CACHE path "Android SDK path") | ||
141 | set(ANDROID_ARCHITECTURE "arm" CACHE string "Used Architecture, related to the ABI and TOOLCHAIN") | | |||
142 | set(ANDROID_TOOLCHAIN "arm-linux-androideabi" CACHE string "Used SDK") | 152 | set(ANDROID_TOOLCHAIN "arm-linux-androideabi" CACHE string "Used SDK") | ||
143 | set(ANDROID_COMPILER_PREFIX "arm-linux-androideabi" CACHE string "Prefix for gcc/g++/etc") | 153 | set(ANDROID_COMPILER_PREFIX "arm-linux-androideabi" CACHE string "Prefix for gcc/g++/etc") | ||
144 | set(ANDROID_ABI "armeabi-v7a" CACHE string "Used ABI") | | |||
145 | set(ANDROID_GCC_VERSION "4.9" CACHE string "Used GCC version" ) | | |||
146 | set(ANDROID_API_LEVEL "14" CACHE string "Android API Level") | | |||
147 | set(ANDROID_SDK_BUILD_TOOLS_REVISION "21.1.1" CACHE string "Android API Level") | 154 | set(ANDROID_SDK_BUILD_TOOLS_REVISION "21.1.1" CACHE string "Android API Level") | ||
148 | 155 | | |||
149 | 156 | set(CMAKE_SYSTEM_VERSION ${CMAKE_ANDROID_API}) | |||
150 | set(CMAKE_ANDROID_NDK ${ANDROID_NDK}) | | |||
151 | set(CMAKE_SYSTEM_VERSION ${ANDROID_API_LEVEL}) | | |||
152 | set(CMAKE_ANDROID_ARCH ${ANDROID_ARCHITECTURE}) | | |||
153 | set(CMAKE_ANDROID_ARCH_ABI ${ANDROID_ABI}) | | |||
154 | set(CMAKE_SYSTEM_NAME Android) | 157 | set(CMAKE_SYSTEM_NAME Android) | ||
155 | if (NOT CMAKE_ANDROID_STL_TYPE) | 158 | if (NOT CMAKE_ANDROID_STL_TYPE) | ||
156 | set(CMAKE_ANDROID_STL_TYPE gnustl_shared) | 159 | set(CMAKE_ANDROID_STL_TYPE gnustl_shared) | ||
157 | endif() | 160 | endif() | ||
158 | 161 | | |||
159 | include(${CMAKE_ROOT}/Modules/Platform/Android-GNU.cmake REQUIRED) | 162 | include(${CMAKE_ROOT}/Modules/Platform/Android-GNU.cmake REQUIRED) | ||
160 | include(${CMAKE_ROOT}/Modules/Platform/Android-Initialize.cmake REQUIRED) | 163 | include(${CMAKE_ROOT}/Modules/Platform/Android-Initialize.cmake REQUIRED) | ||
161 | 164 | | |||
162 | if (NOT DEFINED ECM_ADDITIONAL_FIND_ROOT_PATH) | 165 | if (NOT DEFINED ECM_ADDITIONAL_FIND_ROOT_PATH) | ||
163 | SET(ECM_ADDITIONAL_FIND_ROOT_PATH ${CMAKE_PREFIX_PATH}) | 166 | SET(ECM_ADDITIONAL_FIND_ROOT_PATH ${CMAKE_PREFIX_PATH}) | ||
164 | endif() | 167 | endif() | ||
165 | SET(CMAKE_FIND_ROOT_PATH ${ANDROID_NDK} ${ANDROID_NDK}/sysroot ${CMAKE_SYSROOT} ${ECM_ADDITIONAL_FIND_ROOT_PATH}) | 168 | SET(CMAKE_FIND_ROOT_PATH ${CMAKE_ANDROID_NDK} ${CMAKE_ANDROID_NDK}/sysroot ${CMAKE_SYSROOT} ${ECM_ADDITIONAL_FIND_ROOT_PATH}) | ||
166 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) | 169 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) | ||
167 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) | 170 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) | ||
168 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) | 171 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) | ||
169 | set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) | 172 | set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) | ||
170 | 173 | | |||
171 | #we want executables to be shared libraries, hooks will invoke the exported cmake function | 174 | #we want executables to be shared libraries, hooks will invoke the exported cmake function | ||
172 | set(CMAKE_CXX_LINK_EXECUTABLE | 175 | set(CMAKE_CXX_LINK_EXECUTABLE | ||
173 | "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" | 176 | "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>" | ||
Show All 13 Lines | 189 | if(NOT DEFINED ANDROID_APK_DIR) | |||
187 | message(FATAL_ERROR "Define an apk dir to initialize from using -DANDROID_APK_DIR=<path>. The specified directory must contain the AndroidManifest.xml file.") | 190 | message(FATAL_ERROR "Define an apk dir to initialize from using -DANDROID_APK_DIR=<path>. The specified directory must contain the AndroidManifest.xml file.") | ||
188 | elseif(NOT EXISTS "${ANDROID_APK_DIR}/AndroidManifest.xml") | 191 | elseif(NOT EXISTS "${ANDROID_APK_DIR}/AndroidManifest.xml") | ||
189 | message(FATAL_ERROR "Cannot find ${ANDROID_APK_DIR}/AndroidManifest.xml according to ANDROID_APK_DIR") | 192 | message(FATAL_ERROR "Cannot find ${ANDROID_APK_DIR}/AndroidManifest.xml according to ANDROID_APK_DIR") | ||
190 | endif() | 193 | endif() | ||
191 | 194 | | |||
192 | find_package(Qt5Core REQUIRED) | 195 | find_package(Qt5Core REQUIRED) | ||
193 | 196 | | |||
194 | set(EXPORT_DIR "${CMAKE_BINARY_DIR}/${QTANDROID_EXPORTED_TARGET}_build_apk/") | 197 | set(EXPORT_DIR "${CMAKE_BINARY_DIR}/${QTANDROID_EXPORTED_TARGET}_build_apk/") | ||
195 | set(EXECUTABLE_DESTINATION_PATH "${EXPORT_DIR}/libs/${ANDROID_ABI}/lib${QTANDROID_EXPORTED_TARGET}.so") | 198 | set(EXECUTABLE_DESTINATION_PATH "${EXPORT_DIR}/libs/${CMAKE_ANDROID_ARCH_ABI}/lib${QTANDROID_EXPORTED_TARGET}.so") | ||
196 | set(QML_IMPORT_PATHS "") | 199 | set(QML_IMPORT_PATHS "") | ||
197 | foreach(prefix ${ECM_ADDITIONAL_FIND_ROOT_PATH}) | 200 | foreach(prefix ${ECM_ADDITIONAL_FIND_ROOT_PATH}) | ||
198 | # qmlimportscanner chokes on symlinks, so we need to resolve those first | 201 | # qmlimportscanner chokes on symlinks, so we need to resolve those first | ||
199 | get_filename_component(qml_path "${prefix}/lib/qml" REALPATH) | 202 | get_filename_component(qml_path "${prefix}/lib/qml" REALPATH) | ||
200 | if(EXISTS ${qml_path}) | 203 | if(EXISTS ${qml_path}) | ||
201 | if (QML_IMPORT_PATHS) | 204 | if (QML_IMPORT_PATHS) | ||
202 | set(QML_IMPORT_PATHS "${QML_IMPORT_PATHS},${qml_path}") | 205 | set(QML_IMPORT_PATHS "${QML_IMPORT_PATHS},${qml_path}") | ||
203 | else() | 206 | else() | ||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |