• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15# notice, this list of conditions and the following disclaimer.
16#
17# 2. Redistributions in binary form must reproduce the above copyright
18# notice, this list of conditions and the following disclaimer in the
19# documentation and/or other materials provided with the distribution.
20#
21# 3. Neither the name of the copyright holder nor the names of its
22# contributors may be used to endorse or promote products derived from
23# this software without specific prior written permission.
24#
25# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36# POSSIBILITY OF SUCH DAMAGE.
37#
38# This file is based off of the Platform/Darwin.cmake and
39# Platform/UnixPaths.cmake files which are included with CMake 2.8.4
40# It has been altered for iOS development.
41#
42# Updated by Alex Stewart (alexs.mac@gmail.com)
43#
44# *****************************************************************************
45#      Now maintained by Alexander Widerberg (widerbergaren [at] gmail.com)
46#                      under the BSD-3-Clause license
47#                   https://github.com/leetal/ios-cmake
48# *****************************************************************************
49#
50#                           INFORMATION / HELP
51#
52# The following arguments control the behaviour of this toolchain:
53#
54# PLATFORM: (default "OS")
55#    OS = Build for iPhoneOS.
56#    OS64 = Build for arm64 iphoneOS.
57#    OS64COMBINED = Build for arm64 x86_64 iphoneOS. Combined into FAT STATIC lib (supported on 3.14+ of CMakewith "-G Xcode" argument ONLY)
58#    SIMULATOR = Build for x86 i386 iphoneOS Simulator.
59#    SIMULATOR64 = Build for x86_64 iphoneOS Simulator.
60#    TVOS = Build for arm64 tvOS.
61#    TVOSCOMBINED = Build for arm64 x86_64 tvOS. Combined into FAT STATIC lib (supported on 3.14+ of CMake with "-G Xcode" argument ONLY)
62#    SIMULATOR_TVOS = Build for x86_64 tvOS Simulator.
63#    WATCHOS = Build for armv7k arm64_32 for watchOS.
64#    WATCHOSCOMBINED = Build for armv7k arm64_32 x86_64 watchOS. Combined into FAT STATIC lib (supported on 3.14+ of CMake with "-G Xcode" argument ONLY)
65#    SIMULATOR_WATCHOS = Build for x86_64 for watchOS Simulator.
66#
67# CMAKE_OSX_SYSROOT: Path to the SDK to use.  By default this is
68#    automatically determined from PLATFORM and xcodebuild, but
69#    can also be manually specified (although this should not be required).
70#
71# CMAKE_DEVELOPER_ROOT: Path to the Developer directory for the platform
72#    being compiled for.  By default this is automatically determined from
73#    CMAKE_OSX_SYSROOT, but can also be manually specified (although this should
74#    not be required).
75#
76# DEPLOYMENT_TARGET: Minimum SDK version to target. Default 2.0 on watchOS and 9.0 on tvOS+iOS
77#
78# ENABLE_BITCODE: (1|0) Enables or disables bitcode support. Default 1 (true)
79#
80# ENABLE_ARC: (1|0) Enables or disables ARC support. Default 1 (true, ARC enabled by default)
81#
82# ENABLE_VISIBILITY: (1|0) Enables or disables symbol visibility support. Default 0 (false, visibility hidden by default)
83#
84# ENABLE_STRICT_TRY_COMPILE: (1|0) Enables or disables strict try_compile() on all Check* directives (will run linker
85#    to actually check if linking is possible). Default 0 (false, will set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY)
86#
87# ARCHS: (armv7 armv7s armv7k arm64 arm64_32 i386 x86_64) If specified, will override the default architectures for the given PLATFORM
88#    OS = armv7 armv7s arm64 (if applicable)
89#    OS64 = arm64 (if applicable)
90#    SIMULATOR = i386
91#    SIMULATOR64 = x86_64
92#    TVOS = arm64
93#    SIMULATOR_TVOS = x86_64 (i386 has since long been deprecated)
94#    WATCHOS = armv7k arm64_32 (if applicable)
95#    SIMULATOR_WATCHOS = x86_64 (i386 has since long been deprecated)
96#
97# This toolchain defines the following variables for use externally:
98#
99# XCODE_VERSION: Version number (not including Build version) of Xcode detected.
100# SDK_VERSION: Version of SDK being used.
101# CMAKE_OSX_ARCHITECTURES: Architectures being compiled for (generated from PLATFORM).
102#
103# This toolchain defines the following macros for use externally:
104#
105# set_xcode_property (TARGET XCODE_PROPERTY XCODE_VALUE XCODE_VARIANT)
106#   A convenience macro for setting xcode specific properties on targets.
107#   Available variants are: All, Release, RelWithDebInfo, Debug, MinSizeRel
108#   example: set_xcode_property (myioslib IPHONEOS_DEPLOYMENT_TARGET "3.1" "all").
109#
110# find_host_package (PROGRAM ARGS)
111#   A macro used to find executable programs on the host system, not within the
112#   environment.  Thanks to the android-cmake project for providing the
113#   command.
114#
115# ******************************** DEPRECATIONS *******************************
116#
117# IOS_DEPLOYMENT_TARGET: (Deprecated) Alias to DEPLOYMENT_TARGET
118# CMAKE_IOS_DEVELOPER_ROOT: (Deprecated) Alias to CMAKE_DEVELOPER_ROOT
119# IOS_PLATFORM: (Deprecated) Alias to PLATFORM
120# IOS_ARCH: (Deprecated) Alias to ARCHS
121#
122# *****************************************************************************
123#
124
125# Fix for PThread library not in path
126set(CMAKE_THREAD_LIBS_INIT "-lpthread")
127set(CMAKE_HAVE_THREADS_LIBRARY 1)
128set(CMAKE_USE_WIN32_THREADS_INIT 0)
129set(CMAKE_USE_PTHREADS_INIT 1)
130
131# Cache what generator is used
132set(USED_CMAKE_GENERATOR "${CMAKE_GENERATOR}" CACHE STRING "Expose CMAKE_GENERATOR" FORCE)
133
134if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14")
135  set(MODERN_CMAKE YES)
136endif()
137
138# Get the Xcode version being used.
139execute_process(COMMAND xcodebuild -version
140  OUTPUT_VARIABLE XCODE_VERSION
141  ERROR_QUIET
142  OUTPUT_STRIP_TRAILING_WHITESPACE)
143string(REGEX MATCH "Xcode [0-9\\.]+" XCODE_VERSION "${XCODE_VERSION}")
144string(REGEX REPLACE "Xcode ([0-9\\.]+)" "\\1" XCODE_VERSION "${XCODE_VERSION}")
145
146######## ALIASES (DEPRECATION WARNINGS)
147
148if(DEFINED IOS_PLATFORM)
149  set(PLATFORM ${IOS_PLATFORM})
150  message(DEPRECATION "IOS_PLATFORM argument is DEPRECATED. Consider using the new PLATFORM argument instead.")
151endif()
152
153if(DEFINED IOS_DEPLOYMENT_TARGET)
154  set(DEPLOYMENT_TARGET ${IOS_DEPLOYMENT_TARGET})
155  message(DEPRECATION "IOS_DEPLOYMENT_TARGET argument is DEPRECATED. Consider using the new DEPLOYMENT_TARGET argument instead.")
156endif()
157
158if(DEFINED CMAKE_IOS_DEVELOPER_ROOT)
159  set(CMAKE_DEVELOPER_ROOT ${CMAKE_IOS_DEVELOPER_ROOT})
160  message(DEPRECATION "CMAKE_IOS_DEVELOPER_ROOT argument is DEPRECATED. Consider using the new CMAKE_DEVELOPER_ROOT argument instead.")
161endif()
162
163if(DEFINED IOS_ARCH)
164  set(ARCHS ${IOS_ARCH})
165  message(DEPRECATION "IOS_ARCH argument is DEPRECATED. Consider using the new ARCHS argument instead.")
166endif()
167
168######## END ALIASES
169
170# Unset the FORCE on cache variables if in try_compile()
171set(FORCE_CACHE FORCE)
172get_property(_CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
173if(_CMAKE_IN_TRY_COMPILE)
174  unset(FORCE_CACHE)
175endif()
176
177# Default to building for iPhoneOS if not specified otherwise, and we cannot
178# determine the platform from the CMAKE_OSX_ARCHITECTURES variable. The use
179# of CMAKE_OSX_ARCHITECTURES is such that try_compile() projects can correctly
180# determine the value of PLATFORM from the root project, as
181# CMAKE_OSX_ARCHITECTURES is propagated to them by CMake.
182if(NOT DEFINED PLATFORM)
183  if (CMAKE_OSX_ARCHITECTURES)
184    if(CMAKE_OSX_ARCHITECTURES MATCHES ".*arm.*" AND CMAKE_OSX_SYSROOT MATCHES ".*iphoneos.*")
185      set(PLATFORM "OS")
186    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "i386" AND CMAKE_OSX_SYSROOT MATCHES ".*iphonesimulator.*")
187      set(PLATFORM "SIMULATOR")
188    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "x86_64" AND CMAKE_OSX_SYSROOT MATCHES ".*iphonesimulator.*")
189      set(PLATFORM "SIMULATOR64")
190    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "arm64" AND CMAKE_OSX_SYSROOT MATCHES ".*appletvos.*")
191      set(PLATFORM "TVOS")
192    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "x86_64" AND CMAKE_OSX_SYSROOT MATCHES ".*appletvsimulator.*")
193      set(PLATFORM "SIMULATOR_TVOS")
194    elseif(CMAKE_OSX_ARCHITECTURES MATCHES ".*armv7k.*" AND CMAKE_OSX_SYSROOT MATCHES ".*watchos.*")
195      set(PLATFORM "WATCHOS")
196    elseif(CMAKE_OSX_ARCHITECTURES MATCHES "i386" AND CMAKE_OSX_SYSROOT MATCHES ".*watchsimulator.*")
197      set(PLATFORM "SIMULATOR_WATCHOS")
198    endif()
199  endif()
200  if (NOT PLATFORM)
201    set(PLATFORM "OS")
202  endif()
203endif()
204
205set(PLATFORM_INT "${PLATFORM}" CACHE STRING "Type of platform for which the build targets.")
206
207# Handle the case where we are targeting iOS and a version above 10.3.4 (32-bit support dropped officially)
208if(PLATFORM_INT STREQUAL "OS" AND DEPLOYMENT_TARGET VERSION_GREATER_EQUAL 10.3.4)
209  set(PLATFORM_INT "OS64")
210  message(STATUS "Targeting minimum SDK version ${DEPLOYMENT_TARGET}. Dropping 32-bit support.")
211elseif(PLATFORM_INT STREQUAL "SIMULATOR" AND DEPLOYMENT_TARGET VERSION_GREATER_EQUAL 10.3.4)
212  set(PLATFORM_INT "SIMULATOR64")
213  message(STATUS "Targeting minimum SDK version ${DEPLOYMENT_TARGET}. Dropping 32-bit support.")
214endif()
215
216# Determine the platform name and architectures for use in xcodebuild commands
217# from the specified PLATFORM name.
218if(PLATFORM_INT STREQUAL "OS")
219  set(SDK_NAME iphoneos)
220  if(NOT ARCHS)
221    set(ARCHS armv7 armv7s arm64)
222  endif()
223elseif(PLATFORM_INT STREQUAL "OS64")
224  set(SDK_NAME iphoneos)
225  if(NOT ARCHS)
226    if (XCODE_VERSION VERSION_GREATER 10.0)
227      set(ARCHS arm64) # Add arm64e when Apple have fixed the integration issues with it, libarclite_iphoneos.a is currently missung bitcode markers for example
228    else()
229      set(ARCHS arm64)
230    endif()
231  endif()
232elseif(PLATFORM_INT STREQUAL "OS64COMBINED")
233  set(SDK_NAME iphoneos)
234  if(MODERN_CMAKE)
235    if(NOT ARCHS)
236      if (XCODE_VERSION VERSION_GREATER 10.0)
237        set(ARCHS arm64 x86_64) # Add arm64e when Apple have fixed the integration issues with it, libarclite_iphoneos.a is currently missung bitcode markers for example
238      else()
239        set(ARCHS arm64 x86_64)
240      endif()
241    endif()
242  else()
243    message(FATAL_ERROR "Please make sure that you are running CMake 3.14+ to make the OS64COMBINED setting work")
244  endif()
245elseif(PLATFORM_INT STREQUAL "SIMULATOR")
246  set(SDK_NAME iphonesimulator)
247  if(NOT ARCHS)
248    set(ARCHS i386)
249  endif()
250  message(DEPRECATION "SIMULATOR IS DEPRECATED. Consider using SIMULATOR64 instead.")
251elseif(PLATFORM_INT STREQUAL "SIMULATOR64")
252  set(SDK_NAME iphonesimulator)
253  if(NOT ARCHS)
254    set(ARCHS x86_64)
255  endif()
256elseif(PLATFORM_INT STREQUAL "TVOS")
257  set(SDK_NAME appletvos)
258  if(NOT ARCHS)
259    set(ARCHS arm64)
260  endif()
261elseif (PLATFORM_INT STREQUAL "TVOSCOMBINED")
262  set(SDK_NAME appletvos)
263  if(MODERN_CMAKE)
264    if(NOT ARCHS)
265      set(ARCHS arm64 x86_64)
266    endif()
267  else()
268    message(FATAL_ERROR "Please make sure that you are running CMake 3.14+ to make the TVOSCOMBINED setting work")
269  endif()
270elseif(PLATFORM_INT STREQUAL "SIMULATOR_TVOS")
271  set(SDK_NAME appletvsimulator)
272  if(NOT ARCHS)
273    set(ARCHS x86_64)
274  endif()
275elseif(PLATFORM_INT STREQUAL "WATCHOS")
276  set(SDK_NAME watchos)
277  if(NOT ARCHS)
278    if (XCODE_VERSION VERSION_GREATER 10.0)
279      set(ARCHS armv7k arm64_32)
280    else()
281      set(ARCHS armv7k)
282    endif()
283  endif()
284elseif(PLATFORM_INT STREQUAL "WATCHOSCOMBINED")
285  set(SDK_NAME watchos)
286  if(MODERN_CMAKE)
287    if(NOT ARCHS)
288      if (XCODE_VERSION VERSION_GREATER 10.0)
289        set(ARCHS armv7k arm64_32 i386)
290      else()
291        set(ARCHS armv7k i386)
292      endif()
293    endif()
294  else()
295    message(FATAL_ERROR "Please make sure that you are running CMake 3.14+ to make the WATCHOSCOMBINED setting work")
296  endif()
297elseif(PLATFORM_INT STREQUAL "SIMULATOR_WATCHOS")
298  set(SDK_NAME watchsimulator)
299  if(NOT ARCHS)
300    set(ARCHS i386)
301  endif()
302else()
303  message(FATAL_ERROR "Invalid PLATFORM: ${PLATFORM_INT}")
304endif()
305
306if(MODERN_CMAKE AND PLATFORM_INT MATCHES ".*COMBINED" AND NOT USED_CMAKE_GENERATOR MATCHES "Xcode")
307  message(FATAL_ERROR "The COMBINED options only work with Xcode generator, -G Xcode")
308endif()
309
310# If user did not specify the SDK root to use, then query xcodebuild for it.
311execute_process(COMMAND xcodebuild -version -sdk ${SDK_NAME} Path
312    OUTPUT_VARIABLE CMAKE_OSX_SYSROOT_INT
313    ERROR_QUIET
314    OUTPUT_STRIP_TRAILING_WHITESPACE)
315if (NOT DEFINED CMAKE_OSX_SYSROOT_INT AND NOT DEFINED CMAKE_OSX_SYSROOT)
316  message(SEND_ERROR "Please make sure that Xcode is installed and that the toolchain"
317  "is pointing to the correct path. Please run:"
318  "sudo xcode-select -s /Applications/Xcode.app/Contents/Developer"
319  "and see if that fixes the problem for you.")
320  message(FATAL_ERROR "Invalid CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT} "
321  "does not exist.")
322elseif(DEFINED CMAKE_OSX_SYSROOT_INT)
323   set(CMAKE_OSX_SYSROOT "${CMAKE_OSX_SYSROOT_INT}" CACHE INTERNAL "")
324endif()
325
326# Set Xcode property for SDKROOT as well if Xcode generator is used
327if(USED_CMAKE_GENERATOR MATCHES "Xcode")
328  set(CMAKE_OSX_SYSROOT "${SDK_NAME}" CACHE INTERNAL "")
329  if(NOT DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM)
330    set(CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "123456789A" CACHE INTERNAL "")
331  endif()
332endif()
333
334# Specify minimum version of deployment target.
335if(NOT DEFINED DEPLOYMENT_TARGET)
336  if (PLATFORM_INT STREQUAL "WATCHOS" OR PLATFORM_INT STREQUAL "SIMULATOR_WATCHOS")
337    # Unless specified, SDK version 2.0 is used by default as minimum target version (watchOS).
338    set(DEPLOYMENT_TARGET "2.0"
339            CACHE STRING "Minimum SDK version to build for." )
340  else()
341    # Unless specified, SDK version 9.0 is used by default as minimum target version (iOS, tvOS).
342    set(DEPLOYMENT_TARGET "9.0"
343            CACHE STRING "Minimum SDK version to build for." )
344  endif()
345  message(STATUS "Using the default min-version since DEPLOYMENT_TARGET not provided!")
346endif()
347
348# Use bitcode or not
349if(NOT DEFINED ENABLE_BITCODE AND NOT ARCHS MATCHES "((^|;|, )(i386|x86_64))+")
350  # Unless specified, enable bitcode support by default
351  message(STATUS "Enabling bitcode support by default. ENABLE_BITCODE not provided!")
352  set(ENABLE_BITCODE TRUE)
353elseif(NOT DEFINED ENABLE_BITCODE)
354  message(STATUS "Disabling bitcode support by default on simulators. ENABLE_BITCODE not provided for override!")
355  set(ENABLE_BITCODE FALSE)
356endif()
357set(ENABLE_BITCODE_INT ${ENABLE_BITCODE} CACHE BOOL "Whether or not to enable bitcode" ${FORCE_CACHE})
358# Use ARC or not
359if(NOT DEFINED ENABLE_ARC)
360  # Unless specified, enable ARC support by default
361  set(ENABLE_ARC TRUE)
362  message(STATUS "Enabling ARC support by default. ENABLE_ARC not provided!")
363endif()
364set(ENABLE_ARC_INT ${ENABLE_ARC} CACHE BOOL "Whether or not to enable ARC" ${FORCE_CACHE})
365# Use hidden visibility or not
366if(NOT DEFINED ENABLE_VISIBILITY)
367  # Unless specified, disable symbols visibility by default
368  set(ENABLE_VISIBILITY FALSE)
369  message(STATUS "Hiding symbols visibility by default. ENABLE_VISIBILITY not provided!")
370endif()
371set(ENABLE_VISIBILITY_INT ${ENABLE_VISIBILITY} CACHE BOOL "Whether or not to hide symbols (-fvisibility=hidden)" ${FORCE_CACHE})
372# Set strict compiler checks or not
373if(NOT DEFINED ENABLE_STRICT_TRY_COMPILE)
374  # Unless specified, disable strict try_compile()
375  set(ENABLE_STRICT_TRY_COMPILE FALSE)
376  message(STATUS "Using NON-strict compiler checks by default. ENABLE_STRICT_TRY_COMPILE not provided!")
377endif()
378set(ENABLE_STRICT_TRY_COMPILE_INT ${ENABLE_STRICT_TRY_COMPILE} CACHE BOOL "Whether or not to use strict compiler checks" ${FORCE_CACHE})
379# Get the SDK version information.
380execute_process(COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version SDKVersion
381  OUTPUT_VARIABLE SDK_VERSION
382  ERROR_QUIET
383  OUTPUT_STRIP_TRAILING_WHITESPACE)
384
385# Find the Developer root for the specific iOS platform being compiled for
386# from CMAKE_OSX_SYSROOT.  Should be ../../ from SDK specified in
387# CMAKE_OSX_SYSROOT. There does not appear to be a direct way to obtain
388# this information from xcrun or xcodebuild.
389if (NOT DEFINED CMAKE_DEVELOPER_ROOT AND NOT USED_CMAKE_GENERATOR MATCHES "Xcode")
390  get_filename_component(PLATFORM_SDK_DIR ${CMAKE_OSX_SYSROOT} PATH)
391  get_filename_component(CMAKE_DEVELOPER_ROOT ${PLATFORM_SDK_DIR} PATH)
392
393  if (NOT DEFINED CMAKE_DEVELOPER_ROOT)
394    message(FATAL_ERROR "Invalid CMAKE_DEVELOPER_ROOT: "
395      "${CMAKE_DEVELOPER_ROOT} does not exist.")
396  endif()
397endif()
398# Find the C & C++ compilers for the specified SDK.
399if(NOT CMAKE_C_COMPILER)
400  execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang
401    OUTPUT_VARIABLE CMAKE_C_COMPILER
402    ERROR_QUIET
403    OUTPUT_STRIP_TRAILING_WHITESPACE)
404  message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}")
405endif()
406if(NOT CMAKE_CXX_COMPILER)
407  execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang++
408    OUTPUT_VARIABLE CMAKE_CXX_COMPILER
409    ERROR_QUIET
410    OUTPUT_STRIP_TRAILING_WHITESPACE)
411  message(STATUS "Using CXX compiler: ${CMAKE_CXX_COMPILER}")
412endif()
413# Find (Apple's) libtool.
414execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find libtool
415  OUTPUT_VARIABLE BUILD_LIBTOOL
416  ERROR_QUIET
417  OUTPUT_STRIP_TRAILING_WHITESPACE)
418message(STATUS "Using libtool: ${BUILD_LIBTOOL}")
419# Configure libtool to be used instead of ar + ranlib to build static libraries.
420# This is required on Xcode 7+, but should also work on previous versions of
421# Xcode.
422set(CMAKE_C_CREATE_STATIC_LIBRARY
423  "${BUILD_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
424set(CMAKE_CXX_CREATE_STATIC_LIBRARY
425  "${BUILD_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
426# Find the toolchain's provided install_name_tool if none is found on the host
427if(NOT CMAKE_INSTALL_NAME_TOOL)
428  execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find install_name_tool
429      OUTPUT_VARIABLE CMAKE_INSTALL_NAME_TOOL_INT
430      ERROR_QUIET
431      OUTPUT_STRIP_TRAILING_WHITESPACE)
432  set(CMAKE_INSTALL_NAME_TOOL ${CMAKE_INSTALL_NAME_TOOL_INT} CACHE STRING "" ${FORCE_CACHE})
433endif()
434# Get the version of Darwin (OS X) of the host.
435execute_process(COMMAND uname -r
436  OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
437  ERROR_QUIET
438  OUTPUT_STRIP_TRAILING_WHITESPACE)
439if(SDK_NAME MATCHES "iphone")
440  set(CMAKE_SYSTEM_NAME iOS CACHE INTERNAL "" ${FORCE_CACHE})
441endif()
442# CMake 3.14+ support building for iOS, watchOS and tvOS out of the box.
443if(MODERN_CMAKE)
444  if(SDK_NAME MATCHES "appletv")
445    set(CMAKE_SYSTEM_NAME tvOS CACHE INTERNAL "" ${FORCE_CACHE})
446  elseif(SDK_NAME MATCHES "watch")
447    set(CMAKE_SYSTEM_NAME watchOS CACHE INTERNAL "" ${FORCE_CACHE})
448  endif()
449  # Provide flags for a combined FAT library build on newer CMake versions
450  if(PLATFORM_INT MATCHES ".*COMBINED")
451    set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH "NO" CACHE INTERNAL "" ${FORCE_CACHE})
452    set(CMAKE_IOS_INSTALL_COMBINED YES CACHE INTERNAL "" ${FORCE_CACHE})
453    message(STATUS "Will combine built (static) artifacts into FAT lib...")
454  endif()
455elseif(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.10")
456  # Legacy code path prior to CMake 3.14 or fallback if no SDK_NAME specified
457  set(CMAKE_SYSTEM_NAME iOS CACHE INTERNAL "" ${FORCE_CACHE})
458else()
459  # Legacy code path prior to CMake 3.14 or fallback if no SDK_NAME specified
460  set(CMAKE_SYSTEM_NAME Darwin CACHE INTERNAL "" ${FORCE_CACHE})
461endif()
462# Standard settings.
463set(CMAKE_SYSTEM_VERSION ${SDK_VERSION} CACHE INTERNAL "")
464set(UNIX TRUE CACHE BOOL "")
465set(APPLE TRUE CACHE BOOL "")
466set(IOS TRUE CACHE BOOL "")
467set(CMAKE_AR ar CACHE FILEPATH "" FORCE)
468set(CMAKE_RANLIB ranlib CACHE FILEPATH "" FORCE)
469set(CMAKE_STRIP strip CACHE FILEPATH "" FORCE)
470# Set the architectures for which to build.
471set(CMAKE_OSX_ARCHITECTURES ${ARCHS} CACHE STRING "Build architecture for iOS")
472# Change the type of target generated for try_compile() so it'll work when cross-compiling, weak compiler checks
473if(ENABLE_STRICT_TRY_COMPILE_INT)
474  message(STATUS "Using strict compiler checks (default in CMake).")
475else()
476  set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
477endif()
478# All iOS/Darwin specific settings - some may be redundant.
479set(CMAKE_MACOSX_BUNDLE YES)
480set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
481set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
482set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
483set(CMAKE_SHARED_MODULE_PREFIX "lib")
484set(CMAKE_SHARED_MODULE_SUFFIX ".so")
485set(CMAKE_C_COMPILER_ABI ELF)
486set(CMAKE_CXX_COMPILER_ABI ELF)
487set(CMAKE_C_HAS_ISYSROOT 1)
488set(CMAKE_CXX_HAS_ISYSROOT 1)
489set(CMAKE_MODULE_EXISTS 1)
490set(CMAKE_DL_LIBS "")
491set(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
492set(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
493set(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
494set(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
495
496if(ARCHS MATCHES "((^|;|, )(arm64|arm64e|x86_64))+")
497  set(CMAKE_C_SIZEOF_DATA_PTR 8)
498  set(CMAKE_CXX_SIZEOF_DATA_PTR 8)
499  if(ARCHS MATCHES "((^|;|, )(arm64|arm64e))+")
500    set(CMAKE_SYSTEM_PROCESSOR "aarch64")
501  else()
502    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
503  endif()
504else()
505  set(CMAKE_C_SIZEOF_DATA_PTR 4)
506  set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
507  set(CMAKE_SYSTEM_PROCESSOR "arm")
508endif()
509
510# Note that only Xcode 7+ supports the newer more specific:
511# -m${SDK_NAME}-version-min flags, older versions of Xcode use:
512# -m(ios/ios-simulator)-version-min instead.
513if(${CMAKE_VERSION} VERSION_LESS "3.11")
514  if(PLATFORM_INT STREQUAL "OS" OR PLATFORM_INT STREQUAL "OS64")
515    if(XCODE_VERSION VERSION_LESS 7.0)
516      set(SDK_NAME_VERSION_FLAGS
517        "-mios-version-min=${DEPLOYMENT_TARGET}")
518    else()
519      # Xcode 7.0+ uses flags we can build directly from SDK_NAME.
520      set(SDK_NAME_VERSION_FLAGS
521        "-m${SDK_NAME}-version-min=${DEPLOYMENT_TARGET}")
522    endif()
523  elseif(PLATFORM_INT STREQUAL "TVOS")
524    set(SDK_NAME_VERSION_FLAGS
525      "-mtvos-version-min=${DEPLOYMENT_TARGET}")
526  elseif(PLATFORM_INT STREQUAL "SIMULATOR_TVOS")
527    set(SDK_NAME_VERSION_FLAGS
528      "-mtvos-simulator-version-min=${DEPLOYMENT_TARGET}")
529  elseif(PLATFORM_INT STREQUAL "WATCHOS")
530    set(SDK_NAME_VERSION_FLAGS
531      "-mwatchos-version-min=${DEPLOYMENT_TARGET}")
532  elseif(PLATFORM_INT STREQUAL "SIMULATOR_WATCHOS")
533    set(SDK_NAME_VERSION_FLAGS
534      "-mwatchos-simulator-version-min=${DEPLOYMENT_TARGET}")
535  else()
536    # SIMULATOR or SIMULATOR64 both use -mios-simulator-version-min.
537    set(SDK_NAME_VERSION_FLAGS
538      "-mios-simulator-version-min=${DEPLOYMENT_TARGET}")
539  endif()
540else()
541  # Newer versions of CMake sets the version min flags correctly
542  set(CMAKE_OSX_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET} CACHE STRING
543      "Set CMake deployment target" ${FORCE_CACHE})
544endif()
545
546
547if(ENABLE_BITCODE_INT)
548  set(BITCODE "-fembed-bitcode")
549  set(CMAKE_XCODE_ATTRIBUTE_BITCODE_GENERATION_MODE "bitcode" CACHE INTERNAL "")
550  set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "YES" CACHE INTERNAL "")
551else()
552  set(BITCODE "")
553  set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO" CACHE INTERNAL "")
554endif()
555
556if(ENABLE_ARC_INT)
557  set(FOBJC_ARC "-fobjc-arc")
558  set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "YES" CACHE INTERNAL "")
559else()
560  set(FOBJC_ARC "-fno-objc-arc")
561  set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "NO" CACHE INTERNAL "")
562endif()
563
564if(NOT ENABLE_VISIBILITY_INT)
565  set(VISIBILITY "-fvisibility=hidden")
566  set(CMAKE_XCODE_ATTRIBUTE_GCC_SYMBOLS_PRIVATE_EXTERN "YES" CACHE INTERNAL "")
567else()
568  set(VISIBILITY "")
569  set(CMAKE_XCODE_ATTRIBUTE_GCC_SYMBOLS_PRIVATE_EXTERN "NO" CACHE INTERNAL "")
570endif()
571
572if(NOT IOS_TOOLCHAIN_HAS_RUN)
573  #Check if Xcode generator is used, since that will handle these flags automagically
574  if(USED_CMAKE_GENERATOR MATCHES "Xcode")
575    message(STATUS "Not setting any manual command-line buildflags, since Xcode is selected as generator.")
576  else()
577    set(CMAKE_C_FLAGS
578    "${SDK_NAME_VERSION_FLAGS} ${BITCODE} -fobjc-abi-version=2 ${FOBJC_ARC} ${CMAKE_C_FLAGS}")
579    # Hidden visibilty is required for C++ on iOS.
580    set(CMAKE_CXX_FLAGS
581    "${SDK_NAME_VERSION_FLAGS} ${BITCODE} ${VISIBILITY} -fvisibility-inlines-hidden -fobjc-abi-version=2 ${FOBJC_ARC} ${CMAKE_CXX_FLAGS}")
582    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g ${CMAKE_CXX_FLAGS_DEBUG}")
583    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -DNDEBUG -Os -ffast-math ${CMAKE_CXX_FLAGS_MINSIZEREL}")
584    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -DNDEBUG -O2 -g -ffast-math ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
585    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -ffast-math ${CMAKE_CXX_FLAGS_RELEASE}")
586    set(CMAKE_C_LINK_FLAGS "${SDK_NAME_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
587    set(CMAKE_CXX_LINK_FLAGS "${SDK_NAME_VERSION_FLAGS}  -Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
588    set(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")
589
590    # In order to ensure that the updated compiler flags are used in try_compile()
591    # tests, we have to forcibly set them in the CMake cache, not merely set them
592    # in the local scope.
593    set(VARS_TO_FORCE_IN_CACHE
594      CMAKE_C_FLAGS
595      CMAKE_CXX_FLAGS
596      CMAKE_CXX_FLAGS_DEBUG
597      CMAKE_CXX_FLAGS_RELWITHDEBINFO
598      CMAKE_CXX_FLAGS_MINSIZEREL
599      CMAKE_CXX_FLAGS_RELEASE
600      CMAKE_C_LINK_FLAGS
601      CMAKE_CXX_LINK_FLAGS)
602    foreach(VAR_TO_FORCE ${VARS_TO_FORCE_IN_CACHE})
603      set(${VAR_TO_FORCE} "${${VAR_TO_FORCE}}" CACHE STRING "" ${FORCE_CACHE})
604    endforeach()
605  endif()
606
607  ## Print status messages to inform of the current state
608  message(STATUS "Configuring ${SDK_NAME} build for platform: ${PLATFORM_INT}, architecture(s): ${ARCHS}")
609  message(STATUS "Using SDK: ${CMAKE_OSX_SYSROOT_INT}")
610  message(STATUS "Using minimum deployment version: ${DEPLOYMENT_TARGET}"
611      " (SDK version: ${SDK_VERSION})")
612  if(MODERN_CMAKE)
613    message(STATUS "Merging integrated CMake 3.14+ iOS,tvOS,watchOS,macOS toolchain(s) with this toolchain!")
614  endif()
615  if(USED_CMAKE_GENERATOR MATCHES "Xcode")
616    message(STATUS "Using Xcode version: ${XCODE_VERSION}")
617  endif()
618  if(DEFINED SDK_NAME_VERSION_FLAGS)
619    message(STATUS "Using version flags: ${SDK_NAME_VERSION_FLAGS}")
620  endif()
621  message(STATUS "Using a data_ptr size of: ${CMAKE_CXX_SIZEOF_DATA_PTR}")
622  message(STATUS "Using install_name_tool: ${CMAKE_INSTALL_NAME_TOOL}")
623  if(ENABLE_BITCODE_INT)
624    message(STATUS "Enabling bitcode support.")
625  else()
626    message(STATUS "Disabling bitcode support.")
627  endif()
628
629  if(ENABLE_ARC_INT)
630    message(STATUS "Enabling ARC support.")
631  else()
632    message(STATUS "Disabling ARC support.")
633  endif()
634
635  if(NOT ENABLE_VISIBILITY_INT)
636    message(STATUS "Hiding symbols (-fvisibility=hidden).")
637  endif()
638endif()
639
640set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
641set(CMAKE_SHARED_LINKER_FLAGS "-rpath @executable_path/Frameworks -rpath @loader_path/Frameworks")
642set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -Wl,-headerpad_max_install_names")
643set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -Wl,-headerpad_max_install_names")
644set(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
645set(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
646set(CMAKE_FIND_LIBRARY_SUFFIXES ".tbd" ".dylib" ".so" ".a")
647set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-install_name")
648
649# Set the find root to the iOS developer roots and to user defined paths.
650set(CMAKE_FIND_ROOT_PATH ${CMAKE_OSX_SYSROOT_INT} ${CMAKE_PREFIX_PATH} CACHE STRING "Root path that will be prepended
651 to all search paths")
652# Default to searching for frameworks first.
653set(CMAKE_FIND_FRAMEWORK FIRST)
654# Set up the default search directories for frameworks.
655set(CMAKE_FRAMEWORK_PATH
656  ${CMAKE_DEVELOPER_ROOT}/Library/PrivateFrameworks
657  ${CMAKE_OSX_SYSROOT_INT}/System/Library/Frameworks
658  ${CMAKE_FRAMEWORK_PATH} CACHE STRING "Frameworks search paths" ${FORCE_CACHE})
659
660set(IOS_TOOLCHAIN_HAS_RUN TRUE CACHE BOOL "Has the CMake toolchain run already?" ${FORCE_CACHE})
661
662# By default, search both the specified iOS SDK and the remainder of the host filesystem.
663if(NOT CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
664  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH CACHE STRING "" ${FORCE_CACHE})
665endif()
666if(NOT CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
667  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY CACHE STRING "" ${FORCE_CACHE})
668endif()
669if(NOT CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
670  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY CACHE STRING "" ${FORCE_CACHE})
671endif()
672if(NOT CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
673  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY CACHE STRING "" ${FORCE_CACHE})
674endif()
675
676#
677# Some helper-macros below to simplify and beautify the CMakeFile
678#
679
680# This little macro lets you set any Xcode specific property.
681macro(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE XCODE_RELVERSION)
682  set(XCODE_RELVERSION_I "${XCODE_RELVERSION}")
683  if(XCODE_RELVERSION_I STREQUAL "All")
684    set_property(TARGET ${TARGET} PROPERTY
685    XCODE_ATTRIBUTE_${XCODE_PROPERTY} "${XCODE_VALUE}")
686  else()
687    set_property(TARGET ${TARGET} PROPERTY
688    XCODE_ATTRIBUTE_${XCODE_PROPERTY}[variant=${XCODE_RELVERSION_I}] "${XCODE_VALUE}")
689  endif()
690endmacro(set_xcode_property)
691# This macro lets you find executable programs on the host system.
692macro(find_host_package)
693  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
694  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
695  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
696  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
697  set(IOS FALSE)
698  find_package(${ARGN})
699  set(IOS TRUE)
700  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
701  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
702  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
703  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
704endmacro(find_host_package)
705