1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4# When CMAKE_SYSTEM_NAME is "Android", CMakeSystemSpecificInitialize loads this
5# module.
6
7# Include the NDK hook.
8# It can be used by NDK to inject necessary fixes for an earlier cmake.
9if(CMAKE_ANDROID_NDK)
10  include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/pre/Android-Initialize.cmake OPTIONAL)
11endif()
12
13# Support for NVIDIA Nsight Tegra Visual Studio Edition was previously
14# implemented in the CMake VS IDE generators.  Avoid interfering with
15# that functionality for now.
16if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
17  return()
18endif()
19
20# Commonly used Android toolchain files that pre-date CMake upstream support
21# set CMAKE_SYSTEM_VERSION to 1.  Avoid interfering with them.
22if(CMAKE_SYSTEM_VERSION EQUAL 1)
23  return()
24endif()
25
26set(CMAKE_BUILD_TYPE_INIT "RelWithDebInfo")
27
28if(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED)
29  # Tell CMake not to search host sysroots for headers/libraries.
30
31  # All paths added to CMAKE_SYSTEM_*_PATH below will be rerooted under
32  # CMAKE_FIND_ROOT_PATH. This is set because:
33  # 1. Users may structure their libraries in a way similar to NDK. When they do that,
34  #    they can simply append another path to CMAKE_FIND_ROOT_PATH.
35  # 2. CMAKE_FIND_ROOT_PATH must be non-empty for CMAKE_FIND_ROOT_PATH_MODE_* == ONLY
36  #    to be meaningful. https://github.com/android-ndk/ndk/issues/890
37  list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot")
38
39  # Allow users to override these values in case they want more strict behaviors.
40  # For example, they may want to prevent the NDK's libz from being picked up so
41  # they can use their own.
42  # https://github.com/android-ndk/ndk/issues/517
43  if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
44    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
45  endif()
46
47  if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
48    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
49  endif()
50
51  if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
52    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
53  endif()
54
55  if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
56    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
57  endif()
58
59  # Allows CMake to find headers in the architecture-specific include directories.
60  set(CMAKE_LIBRARY_ARCHITECTURE "${CMAKE_ANDROID_ARCH_TRIPLE}")
61
62  # Instructs CMake to search the correct API level for libraries.
63  # Besides the paths like <root>/<prefix>/lib/<arch>, cmake also searches <root>/<prefix>.
64  # So we can add the API level specific directory directly.
65  # https://github.com/android/ndk/issues/929
66  list(PREPEND CMAKE_SYSTEM_PREFIX_PATH
67    "/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/${CMAKE_SYSTEM_VERSION}"
68    )
69
70  list(APPEND CMAKE_SYSTEM_PROGRAM_PATH "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/bin")
71endif()
72
73# Skip sysroot selection if the NDK has a unified toolchain.
74if(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED)
75  return()
76endif()
77
78# Natively compiling on an Android host doesn't use the NDK cross-compilation
79# tools.
80if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Android")
81  return()
82endif()
83
84if(NOT CMAKE_SYSROOT)
85  if(CMAKE_ANDROID_NDK)
86    set(CMAKE_SYSROOT "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}/arch-${CMAKE_ANDROID_ARCH}")
87    if(NOT CMAKE_ANDROID_NDK_DEPRECATED_HEADERS)
88      set(CMAKE_SYSROOT_COMPILE "${CMAKE_ANDROID_NDK}/sysroot")
89    endif()
90  elseif(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
91    set(CMAKE_SYSROOT "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot")
92  endif()
93endif()
94
95if(CMAKE_SYSROOT)
96  if(NOT IS_DIRECTORY "${CMAKE_SYSROOT}")
97    message(FATAL_ERROR
98      "Android: The system root directory needed for the selected Android version and architecture does not exist:\n"
99      "  ${CMAKE_SYSROOT}\n"
100      )
101  endif()
102else()
103  message(FATAL_ERROR
104    "Android: No CMAKE_SYSROOT was selected."
105    )
106endif()
107
108# Include the NDK hook.
109# It can be used by NDK to inject necessary fixes for an earlier cmake.
110if(CMAKE_ANDROID_NDK)
111  include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/post/Android-Initialize.cmake OPTIONAL)
112endif()
113