blob: bb42802509a5f2c0a600c5b9b0e8944c2c73b667 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001# This file is part of the ios-cmake project. It was retrieved from
2# https://github.com/cristeab/ios-cmake.git, which is a fork of
3# https://code.google.com/p/ios-cmake/. Which in turn is based off of
4# the Platform/Darwin.cmake and Platform/UnixPaths.cmake files which
5# are included with CMake 2.8.4
6#
7# The ios-cmake project is licensed under the new BSD license.
8#
9# Copyright (c) 2014, Bogdan Cristea and LTE Engineering Software,
10# Kitware, Inc., Insight Software Consortium. All rights reserved.
11
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions
14# are met:
15
16# 1. Redistributions of source code must retain the above copyright
17# notice, this list of conditions and the following disclaimer.
18#
19# 2. Redistributions in binary form must reproduce the above copyright
20# notice, this list of conditions and the following disclaimer in the
21# documentation and/or other materials provided with the distribution.
22#
23# 3. Neither the name of the copyright holder nor the names of its
24# contributors may be used to endorse or promote products derived from
25# this software without specific prior written permission.
26#
27# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38# POSSIBILITY OF SUCH DAMAGE.
39#
40# This file is based off of the Platform/Darwin.cmake and
41# Platform/UnixPaths.cmake files which are included with CMake 2.8.4
42# It has been altered for iOS development.
43#
44# Updated by Alex Stewart (alexs.mac@gmail.com).
45
46# The following variables control the behaviour of this toolchain:
47#
48# IOS_PLATFORM: OS (default) or SIMULATOR or SIMULATOR64
49# OS = Build for iPhoneOS.
50# SIMULATOR = Build for x86 i386 iPhone Simulator.
51# SIMULATOR64 = Build for x86 x86_64 iPhone Simulator.
52# CMAKE_OSX_SYSROOT: Path to the iOS SDK to use. By default this is
53# automatically determined from IOS_PLATFORM and xcodebuild, but
54# can also be manually specified (although this should not be required).
55# CMAKE_IOS_DEVELOPER_ROOT: Path to the Developer directory for the iOS platform
56# being compiled for. By default this is automatically determined from
57# CMAKE_OSX_SYSROOT, but can also be manually specified (although this should
58# not be required).
59#
60# This toolchain defines the following variables for use externally:
61#
62# XCODE_VERSION: Version number (not including Build version) of Xcode detected.
63# IOS_SDK_VERSION: Version of iOS SDK being used.
64# CMAKE_OSX_ARCHITECTURES: Architectures being compiled for (generated from
65# IOS_PLATFORM).
66#
67# This toolchain defines the following macros for use externally:
68#
69# set_xcode_property (TARGET XCODE_PROPERTY XCODE_VALUE)
70# A convenience macro for setting xcode specific properties on targets
71# example: set_xcode_property (myioslib IPHONEOS_DEPLOYMENT_TARGET "3.1").
72#
73# find_host_package (PROGRAM ARGS)
74# A macro used to find executable programs on the host system, not within the
75# iOS environment. Thanks to the android-cmake project for providing the
76# command.
77
78# Get the Xcode version being used.
79execute_process(COMMAND xcodebuild -version
80 OUTPUT_VARIABLE XCODE_VERSION
81 ERROR_QUIET
82 OUTPUT_STRIP_TRAILING_WHITESPACE)
83string(REGEX MATCH "Xcode [0-9\\.]+" XCODE_VERSION "${XCODE_VERSION}")
84string(REGEX REPLACE "Xcode ([0-9\\.]+)" "\\1" XCODE_VERSION "${XCODE_VERSION}")
85message(STATUS "Building with Xcode version: ${XCODE_VERSION}")
86
87# Default to building for iPhoneOS if not specified otherwise, and we cannot
88# determine the platform from the CMAKE_OSX_ARCHITECTURES variable. The use
89# of CMAKE_OSX_ARCHITECTURES is such that try_compile() projects can correctly
90# determine the value of IOS_PLATFORM from the root project, as
91# CMAKE_OSX_ARCHITECTURES is propagated to them by CMake.
92if (NOT DEFINED IOS_PLATFORM)
93 if (CMAKE_OSX_ARCHITECTURES)
94 if (CMAKE_OSX_ARCHITECTURES MATCHES ".*arm.*")
95 set(IOS_PLATFORM "OS")
96 elseif (CMAKE_OSX_ARCHITECTURES MATCHES "i386")
97 set(IOS_PLATFORM "SIMULATOR")
98 elseif (CMAKE_OSX_ARCHITECTURES MATCHES "x86_64")
99 set(IOS_PLATFORM "SIMULATOR64")
100 endif()
101 endif()
102 if (NOT IOS_PLATFORM)
103 set(IOS_PLATFORM "OS")
104 endif()
105endif()
106set(IOS_PLATFORM ${IOS_PLATFORM} CACHE STRING
107 "Type of iOS platform for which to build.")
108
109# Determine the platform name and architectures for use in xcodebuild commands
110# from the specified IOS_PLATFORM name.
111if (IOS_PLATFORM STREQUAL "OS")
112 set(XCODE_IOS_PLATFORM iphoneos)
113 set(IOS_ARCH armv7 armv7s arm64)
114elseif (IOS_PLATFORM STREQUAL "SIMULATOR")
115 set(XCODE_IOS_PLATFORM iphonesimulator)
116 set(IOS_ARCH i386)
117elseif(IOS_PLATFORM STREQUAL "SIMULATOR64")
118 set(XCODE_IOS_PLATFORM iphonesimulator)
119 set(IOS_ARCH x86_64)
120else()
121 message(FATAL_ERROR "Invalid IOS_PLATFORM: ${IOS_PLATFORM}")
122endif()
123
124# If user did not specify the SDK root to use, then query xcodebuild for it.
125if (NOT CMAKE_OSX_SYSROOT)
126 execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
127 OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
128 ERROR_QUIET
129 OUTPUT_STRIP_TRAILING_WHITESPACE)
130 message(STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
131endif()
132if (NOT EXISTS ${CMAKE_OSX_SYSROOT})
133 message(FATAL_ERROR "Invalid CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT} "
134 "does not exist.")
135endif()
136# Get the SDK version information.
137execute_process(COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version SDKVersion
138 OUTPUT_VARIABLE IOS_SDK_VERSION
139 ERROR_QUIET
140 OUTPUT_STRIP_TRAILING_WHITESPACE)
141
142# Find the Developer root for the specific iOS platform being compiled for
143# from CMAKE_OSX_SYSROOT. Should be ../../ from SDK specified in
144# CMAKE_OSX_SYSROOT. There does not appear to be a direct way to obtain
145# this information from xcrun or xcodebuild.
146if (NOT CMAKE_IOS_DEVELOPER_ROOT)
147 get_filename_component(IOS_PLATFORM_SDK_DIR ${CMAKE_OSX_SYSROOT} PATH)
148 get_filename_component(CMAKE_IOS_DEVELOPER_ROOT ${IOS_PLATFORM_SDK_DIR} PATH)
149endif()
150if (NOT EXISTS ${CMAKE_IOS_DEVELOPER_ROOT})
151 message(FATAL_ERROR "Invalid CMAKE_IOS_DEVELOPER_ROOT: "
152 "${CMAKE_IOS_DEVELOPER_ROOT} does not exist.")
153endif()
154
155# Find the C & C++ compilers for the specified SDK.
156if (NOT CMAKE_C_COMPILER)
157 execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang
158 OUTPUT_VARIABLE CMAKE_C_COMPILER
159 ERROR_QUIET
160 OUTPUT_STRIP_TRAILING_WHITESPACE)
161 message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}")
162endif()
163if (NOT CMAKE_CXX_COMPILER)
164 execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang++
165 OUTPUT_VARIABLE CMAKE_CXX_COMPILER
166 ERROR_QUIET
167 OUTPUT_STRIP_TRAILING_WHITESPACE)
168 message(STATUS "Using CXX compiler: ${CMAKE_CXX_COMPILER}")
169endif()
170
171# Find (Apple's) libtool.
172execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find libtool
173 OUTPUT_VARIABLE IOS_LIBTOOL
174 ERROR_QUIET
175 OUTPUT_STRIP_TRAILING_WHITESPACE)
176message(STATUS "Using libtool: ${IOS_LIBTOOL}")
177# Configure libtool to be used instead of ar + ranlib to build static libraries.
178# This is required on Xcode 7+, but should also work on previous versions of
179# Xcode.
180set(CMAKE_C_CREATE_STATIC_LIBRARY
181 "${IOS_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
182set(CMAKE_CXX_CREATE_STATIC_LIBRARY
183 "${IOS_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
184
185# Get the version of Darwin (OS X) of the host.
186execute_process(COMMAND uname -r
187 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
188 ERROR_QUIET
189 OUTPUT_STRIP_TRAILING_WHITESPACE)
190
191# Specify minimum version of deployment target.
192# Unless specified, the latest SDK version is used by default.
193set(IOS_DEPLOYMENT_TARGET "${IOS_SDK_VERSION}"
194 CACHE STRING "Minimum iOS version to build for." )
195message(STATUS "Building for minimum iOS version: ${IOS_DEPLOYMENT_TARGET}"
196 " (SDK version: ${IOS_SDK_VERSION})")
197if (NOT IOS_DEPLOYMENT_TARGET VERSION_LESS 11.0)
198 # iOS 11+ does not support 32-bit architectures (armv7).
199 foreach(ARCH ${IOS_ARCH})
200 if (ARCH MATCHES "armv7*")
201 message(STATUS "Removing iOS architecture: ${ARCH} from build as it is "
202 "not supported by the minimum iOS version to build for: "
203 "${IOS_DEPLOYMENT_TARGET} (iOS >= 11 requires 64-bit).")
204 else()
205 list(APPEND VALID_IOS_ARCH_FOR_SDK_VERSION ${ARCH})
206 endif()
207 endforeach()
208 set(IOS_ARCH ${VALID_IOS_ARCH_FOR_SDK_VERSION})
209endif()
210
211message(STATUS "Configuring iOS build for platform: ${IOS_PLATFORM}, "
212 "architecture(s): ${IOS_ARCH}")
213
214# Standard settings.
215set(CMAKE_SYSTEM_NAME Darwin)
216set(CMAKE_SYSTEM_VERSION ${IOS_SDK_VERSION})
217set(UNIX TRUE)
218set(APPLE TRUE)
219set(IOS TRUE)
220# Force unset of OS X-specific deployment target (otherwise autopopulated),
221# required as of cmake 2.8.10.
222set(CMAKE_OSX_DEPLOYMENT_TARGET "" CACHE STRING
223 "Must be empty for iOS builds." FORCE)
224# Set the architectures for which to build.
225set(CMAKE_OSX_ARCHITECTURES ${IOS_ARCH} CACHE STRING "Build architecture for iOS")
226
227# All iOS/Darwin specific settings - some may be redundant.
228set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
229set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
230set(CMAKE_SHARED_MODULE_PREFIX "lib")
231set(CMAKE_SHARED_MODULE_SUFFIX ".so")
232set(CMAKE_MODULE_EXISTS 1)
233set(CMAKE_DL_LIBS "")
234
235set(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
236set(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
237set(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
238set(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
239
240# Note that only Xcode 7+ supports the newer more specific:
241# -m${XCODE_IOS_PLATFORM}-version-min flags, older versions of Xcode use:
242# -m(ios/ios-simulator)-version-min instead.
243if (XCODE_VERSION VERSION_LESS 7.0)
244 if (IOS_PLATFORM STREQUAL "OS")
245 set(XCODE_IOS_PLATFORM_VERSION_FLAGS
246 "-mios-version-min=${IOS_DEPLOYMENT_TARGET}")
247 else()
248 # SIMULATOR or SIMULATOR64 both use -mios-simulator-version-min.
249 set(XCODE_IOS_PLATFORM_VERSION_FLAGS
250 "-mios-simulator-version-min=${IOS_DEPLOYMENT_TARGET}")
251 endif()
252else()
253 # Xcode 7.0+ uses flags we can build directly from XCODE_IOS_PLATFORM.
254 set(XCODE_IOS_PLATFORM_VERSION_FLAGS
255 "-m${XCODE_IOS_PLATFORM}-version-min=${IOS_DEPLOYMENT_TARGET}")
256endif()
257
258set(CMAKE_C_FLAGS
259 "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -fobjc-abi-version=2 -fobjc-arc ${CMAKE_C_FLAGS}")
260# Hidden visibilty is required for C++ on iOS.
261set(CMAKE_CXX_FLAGS
262 "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -fobjc-abi-version=2 -fobjc-arc ${CMAKE_CXX_FLAGS}")
263set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O3 -fomit-frame-pointer -ffast-math ${CMAKE_CXX_FLAGS_RELEASE}")
264
265set(CMAKE_C_LINK_FLAGS "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
266set(CMAKE_CXX_LINK_FLAGS "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
267
268# In order to ensure that the updated compiler flags are used in try_compile()
269# tests, we have to forcibly set them in the CMake cache, not merely set them
270# in the local scope.
271list(APPEND VARS_TO_FORCE_IN_CACHE
272 CMAKE_C_FLAGS
273 CMAKE_CXX_FLAGS
274 CMAKE_CXX_RELEASE
275 CMAKE_C_LINK_FLAGS
276 CMAKE_CXX_LINK_FLAGS)
277foreach(VAR_TO_FORCE ${VARS_TO_FORCE_IN_CACHE})
278 set(${VAR_TO_FORCE} "${${VAR_TO_FORCE}}" CACHE STRING "" FORCE)
279endforeach()
280
281set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
282set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
283set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
284set(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
285set(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
286set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
287
288# Hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old
289# build tree (where install_name_tool was hardcoded) and where
290# CMAKE_INSTALL_NAME_TOOL isn't in the cache and still cmake didn't fail in
291# CMakeFindBinUtils.cmake (because it isn't rerun) hardcode
292# CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did
293# before, Alex.
294if (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
295 find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
296endif (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
297
298# Set the find root to the iOS developer roots and to user defined paths.
299set(CMAKE_FIND_ROOT_PATH ${CMAKE_IOS_DEVELOPER_ROOT} ${CMAKE_OSX_SYSROOT}
300 ${CMAKE_PREFIX_PATH} CACHE string "iOS find search path root" FORCE)
301
302# Default to searching for frameworks first.
303set(CMAKE_FIND_FRAMEWORK FIRST)
304
305# Set up the default search directories for frameworks.
306set(CMAKE_SYSTEM_FRAMEWORK_PATH
307 ${CMAKE_OSX_SYSROOT}/System/Library/Frameworks
308 ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks
309 ${CMAKE_OSX_SYSROOT}/Developer/Library/Frameworks)
310
311# Only search the specified iOS SDK, not the remainder of the host filesystem.
312set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
313set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
314set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
315set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
316
317# This little macro lets you set any XCode specific property.
318macro(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
319 set_property(TARGET ${TARGET} PROPERTY
320 XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
321endmacro(set_xcode_property)
322
323# This macro lets you find executable programs on the host system.
324macro(find_host_package)
325 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
326 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
327 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
328 set(IOS FALSE)
329
330 find_package(${ARGN})
331
332 set(IOS TRUE)
333 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
334 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
335 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
336endmacro(find_host_package)