1include(CMakeParseArguments)
2
3# On OS X SDKs can be installed anywhere on the base system and xcode-select can
4# set the default Xcode to use. This function finds the SDKs that are present in
5# the current Xcode.
6function(find_darwin_sdk_dir var sdk_name)
7  # Let's first try the internal SDK, otherwise use the public SDK.
8  execute_process(
9    COMMAND xcodebuild -version -sdk ${sdk_name}.internal Path
10    RESULT_VARIABLE result_process
11    OUTPUT_VARIABLE var_internal
12    OUTPUT_STRIP_TRAILING_WHITESPACE
13    ERROR_FILE /dev/null
14  )
15  if((NOT result_process EQUAL 0) OR "" STREQUAL "${var_internal}")
16    execute_process(
17      COMMAND xcodebuild -version -sdk ${sdk_name} Path
18      RESULT_VARIABLE result_process
19      OUTPUT_VARIABLE var_internal
20      OUTPUT_STRIP_TRAILING_WHITESPACE
21      ERROR_FILE /dev/null
22    )
23  else()
24    set(${var}_INTERNAL ${var_internal} PARENT_SCOPE)
25  endif()
26  if(result_process EQUAL 0)
27    set(${var} ${var_internal} PARENT_SCOPE)
28  endif()
29endfunction()
30
31# There isn't a clear mapping of what architectures are supported with a given
32# target platform, but ld's version output does list the architectures it can
33# link for.
34function(darwin_get_toolchain_supported_archs output_var)
35  execute_process(
36    COMMAND ld -v
37    ERROR_VARIABLE LINKER_VERSION)
38
39  string(REGEX MATCH "configured to support archs: ([^\n]+)"
40         ARCHES_MATCHED "${LINKER_VERSION}")
41  if(ARCHES_MATCHED)
42    set(ARCHES "${CMAKE_MATCH_1}")
43    message(STATUS "Got ld supported ARCHES: ${ARCHES}")
44    string(REPLACE " " ";" ARCHES ${ARCHES})
45  else()
46    # If auto-detecting fails, fall back to a default set
47    message(WARNING "Detecting supported architectures from 'ld -v' failed. Returning default set.")
48    set(ARCHES "i386;x86_64;armv7;armv7s;arm64")
49  endif()
50
51  set(${output_var} ${ARCHES} PARENT_SCOPE)
52endfunction()
53
54# This function takes an OS and a list of architectures and identifies the
55# subset of the architectures list that the installed toolchain can target.
56function(darwin_test_archs os valid_archs)
57  if(${valid_archs})
58    message(STATUS "Using cached valid architectures for ${os}.")
59    return()
60  endif()
61
62  set(archs ${ARGN})
63  if(NOT TEST_COMPILE_ONLY)
64    message(STATUS "Finding valid architectures for ${os}...")
65    set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
66    file(WRITE ${SIMPLE_C} "#include <stdio.h>\nint main() { printf(__FILE__); return 0; }\n")
67
68    set(os_linker_flags)
69    foreach(flag ${DARWIN_${os}_LINK_FLAGS})
70      set(os_linker_flags "${os_linker_flags} ${flag}")
71    endforeach()
72  endif()
73
74  # The simple program will build for x86_64h on the simulator because it is
75  # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually
76  # a valid or useful architecture for the iOS simulator we should drop it.
77  if(${os} MATCHES "^(iossim|tvossim|watchossim)$")
78    list(REMOVE_ITEM archs "x86_64h")
79  endif()
80
81  set(working_archs)
82  foreach(arch ${archs})
83
84    set(arch_linker_flags "-arch ${arch} ${os_linker_flags}")
85    if(TEST_COMPILE_ONLY)
86      try_compile_only(CAN_TARGET_${os}_${arch} -v -arch ${arch} ${DARWIN_${os}_CFLAGS})
87    else()
88      try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_C}
89                  COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS}
90                  CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${arch_linker_flags}"
91                  OUTPUT_VARIABLE TEST_OUTPUT)
92    endif()
93    if(${CAN_TARGET_${os}_${arch}})
94      list(APPEND working_archs ${arch})
95    else()
96      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
97        "Testing compiler for supporting ${os}-${arch}:\n"
98        "${TEST_OUTPUT}\n")
99    endif()
100  endforeach()
101  set(${valid_archs} ${working_archs}
102    CACHE STRING "List of valid architectures for platform ${os}.")
103endfunction()
104
105# This function checks the host cpusubtype to see if it is post-haswell. Haswell
106# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
107function(darwin_filter_host_archs input output)
108  list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
109  execute_process(
110    COMMAND sysctl hw.cpusubtype
111    OUTPUT_VARIABLE SUBTYPE)
112
113  string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
114         SUBTYPE_MATCHED "${SUBTYPE}")
115  set(HASWELL_SUPPORTED Off)
116  if(SUBTYPE_MATCHED)
117    if(${CMAKE_MATCH_1} GREATER 7)
118      set(HASWELL_SUPPORTED On)
119    endif()
120  endif()
121  if(NOT HASWELL_SUPPORTED)
122    list(REMOVE_ITEM tmp_var x86_64h)
123  endif()
124  set(${output} ${tmp_var} PARENT_SCOPE)
125endfunction()
126
127# Read and process the exclude file into a list of symbols
128function(darwin_read_list_from_file output_var file)
129  if(EXISTS ${file})
130    file(READ ${file} EXCLUDES)
131    string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES})
132    set(${output_var} ${EXCLUDES} PARENT_SCOPE)
133  endif()
134endfunction()
135
136# this function takes an OS, architecture and minimum version and provides a
137# list of builtin functions to exclude
138function(darwin_find_excluded_builtins_list output_var)
139  cmake_parse_arguments(LIB
140    ""
141    "OS;ARCH;MIN_VERSION"
142    ""
143    ${ARGN})
144
145  if(NOT LIB_OS OR NOT LIB_ARCH)
146    message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!")
147  endif()
148
149  darwin_read_list_from_file(${LIB_OS}_BUILTINS
150    ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt)
151  darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS
152    ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt)
153
154  if(LIB_MIN_VERSION)
155    file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
156    foreach(builtin_list ${builtin_lists})
157      string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
158      if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
159        if(NOT smallest_version)
160          set(smallest_version ${CMAKE_MATCH_1})
161        elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
162          set(smallest_version ${CMAKE_MATCH_1})
163        endif()
164      endif()
165    endforeach()
166
167    if(smallest_version)
168      darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS
169        ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt)
170    endif()
171  endif()
172
173  set(${output_var}
174      ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
175      ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
176      ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
177endfunction()
178
179# adds a single builtin library for a single OS & ARCH
180macro(darwin_add_builtin_library name suffix)
181  cmake_parse_arguments(LIB
182    ""
183    "PARENT_TARGET;OS;ARCH"
184    "SOURCES;CFLAGS;DEFS"
185    ${ARGN})
186  set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
187  add_library(${libname} STATIC ${LIB_SOURCES})
188  if(DARWIN_${LIB_OS}_SYSROOT)
189    set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
190  endif()
191  set_target_compile_flags(${libname}
192    ${sysroot_flag}
193    ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
194    ${LIB_CFLAGS})
195  set_property(TARGET ${libname} APPEND PROPERTY
196      COMPILE_DEFINITIONS ${LIB_DEFS})
197  set_target_properties(${libname} PROPERTIES
198      OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
199  set_target_properties(${libname} PROPERTIES
200    OSX_ARCHITECTURES ${LIB_ARCH})
201
202  if(LIB_PARENT_TARGET)
203    add_dependencies(${LIB_PARENT_TARGET} ${libname})
204  endif()
205
206  list(APPEND ${LIB_OS}_${suffix}_libs ${libname})
207  list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
208endmacro()
209
210function(darwin_lipo_libs name)
211  cmake_parse_arguments(LIB
212    ""
213    "PARENT_TARGET;OUTPUT_DIR;INSTALL_DIR"
214    "LIPO_FLAGS;DEPENDS"
215    ${ARGN})
216  if(LIB_DEPENDS AND LIB_LIPO_FLAGS)
217    add_custom_command(OUTPUT ${LIB_OUTPUT_DIR}/lib${name}.a
218      COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR}
219      COMMAND lipo -output
220              ${LIB_OUTPUT_DIR}/lib${name}.a
221              -create ${LIB_LIPO_FLAGS}
222      DEPENDS ${LIB_DEPENDS}
223      )
224    add_custom_target(${name}
225      DEPENDS ${LIB_OUTPUT_DIR}/lib${name}.a)
226    add_dependencies(${LIB_PARENT_TARGET} ${name})
227    install(FILES ${LIB_OUTPUT_DIR}/lib${name}.a
228      DESTINATION ${LIB_INSTALL_DIR})
229  else()
230    message(WARNING "Not generating lipo target for ${name} because no input libraries exist.")
231  endif()
232endfunction()
233
234# Filter out generic versions of routines that are re-implemented in
235# architecture specific manner.  This prevents multiple definitions of the
236# same symbols, making the symbol selection non-deterministic.
237function(darwin_filter_builtin_sources output_var exclude_or_include excluded_list)
238  if(exclude_or_include STREQUAL "EXCLUDE")
239    set(filter_action GREATER)
240    set(filter_value -1)
241  elseif(exclude_or_include STREQUAL "INCLUDE")
242    set(filter_action LESS)
243    set(filter_value 0)
244  else()
245    message(FATAL_ERROR "darwin_filter_builtin_sources called without EXCLUDE|INCLUDE")
246  endif()
247
248  set(intermediate ${ARGN})
249  foreach (_file ${intermediate})
250    get_filename_component(_name_we ${_file} NAME_WE)
251    list(FIND ${excluded_list} ${_name_we} _found)
252    if(_found ${filter_action} ${filter_value})
253      list(REMOVE_ITEM intermediate ${_file})
254    elseif(${_file} MATCHES ".*/.*\\.S" OR ${_file} MATCHES ".*/.*\\.c")
255      get_filename_component(_name ${_file} NAME)
256      string(REPLACE ".S" ".c" _cname "${_name}")
257      list(REMOVE_ITEM intermediate ${_cname})
258    endif ()
259  endforeach ()
260  set(${output_var} ${intermediate} PARENT_SCOPE)
261endfunction()
262
263# Generates builtin libraries for all operating systems specified in ARGN. Each
264# OS library is constructed by lipo-ing together single-architecture libraries.
265macro(darwin_add_builtin_libraries)
266  set(DARWIN_EXCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Darwin-excludes)
267
268  set(CFLAGS "-fPIC -O3 -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer")
269  set(CMAKE_C_FLAGS "")
270  set(CMAKE_CXX_FLAGS "")
271  set(CMAKE_ASM_FLAGS "")
272
273  set(PROFILE_SOURCES ../profile/InstrProfiling
274                      ../profile/InstrProfilingBuffer
275                      ../profile/InstrProfilingPlatformDarwin
276                      ../profile/InstrProfilingWriter)
277  foreach (os ${ARGN})
278    list_intersect(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH)
279    foreach (arch ${DARWIN_BUILTIN_ARCHS})
280      darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
281                              OS ${os}
282                              ARCH ${arch}
283                              MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
284
285      darwin_filter_builtin_sources(filtered_sources
286        EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
287        ${${arch}_SOURCES})
288
289      darwin_add_builtin_library(clang_rt builtins
290                              OS ${os}
291                              ARCH ${arch}
292                              SOURCES ${filtered_sources}
293                              CFLAGS ${CFLAGS} -arch ${arch}
294                              PARENT_TARGET builtins)
295    endforeach()
296
297    # Don't build cc_kext libraries for simulator platforms
298    if(NOT DARWIN_${os}_SKIP_CC_KEXT)
299      foreach (arch ${DARWIN_BUILTIN_ARCHS})
300        # By not specifying MIN_VERSION this only reads the OS and OS-arch lists.
301        # We don't want to filter out the builtins that are present in libSystem
302        # because kexts can't link libSystem.
303        darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
304                              OS ${os}
305                              ARCH ${arch})
306
307        darwin_filter_builtin_sources(filtered_sources
308          EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
309          ${${arch}_SOURCES})
310
311        # In addition to the builtins cc_kext includes some profile sources
312        darwin_add_builtin_library(clang_rt cc_kext
313                                OS ${os}
314                                ARCH ${arch}
315                                SOURCES ${filtered_sources} ${PROFILE_SOURCES}
316                                CFLAGS ${CFLAGS} -arch ${arch} -mkernel
317                                DEFS KERNEL_USE
318                                PARENT_TARGET builtins)
319      endforeach()
320      set(archive_name clang_rt.cc_kext_${os})
321      if(${os} STREQUAL "osx")
322        set(archive_name clang_rt.cc_kext)
323      endif()
324      darwin_lipo_libs(${archive_name}
325                      PARENT_TARGET builtins
326                      LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
327                      DEPENDS ${${os}_cc_kext_libs}
328                      OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
329                      INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
330    endif()
331  endforeach()
332
333  # We put the x86 sim slices into the archives for their base OS
334  foreach (os ${ARGN})
335    if(NOT ${os} MATCHES ".*sim$")
336      darwin_lipo_libs(clang_rt.${os}
337                        PARENT_TARGET builtins
338                        LIPO_FLAGS ${${os}_builtins_lipo_flags} ${${os}sim_builtins_lipo_flags}
339                        DEPENDS ${${os}_builtins_libs} ${${os}sim_builtins_libs}
340                        OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
341                        INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
342    endif()
343  endforeach()
344  darwin_add_embedded_builtin_libraries()
345endmacro()
346
347macro(darwin_add_embedded_builtin_libraries)
348  # this is a hacky opt-out. If you can't target both intel and arm
349  # architectures we bail here.
350  set(DARWIN_SOFT_FLOAT_ARCHS armv6m armv7m armv7em armv7)
351  set(DARWIN_HARD_FLOAT_ARCHS armv7em armv7)
352  if(COMPILER_RT_SUPPORTED_ARCH MATCHES ".*armv.*")
353    list(FIND COMPILER_RT_SUPPORTED_ARCH i386 i386_idx)
354    if(i386_idx GREATER -1)
355      list(APPEND DARWIN_HARD_FLOAT_ARCHS i386)
356    endif()
357
358    list(FIND COMPILER_RT_SUPPORTED_ARCH x86_64 x86_64_idx)
359    if(x86_64_idx GREATER -1)
360      list(APPEND DARWIN_HARD_FLOAT_ARCHS x86_64)
361    endif()
362
363    set(MACHO_SYM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/macho_embedded)
364
365    set(CFLAGS "-Oz -Wall -fomit-frame-pointer -ffreestanding")
366    set(CMAKE_C_FLAGS "")
367    set(CMAKE_CXX_FLAGS "")
368    set(CMAKE_ASM_FLAGS "")
369
370    set(SOFT_FLOAT_FLAG -mfloat-abi=soft)
371    set(HARD_FLOAT_FLAG -mfloat-abi=hard)
372
373    set(ENABLE_PIC Off)
374    set(PIC_FLAG -fPIC)
375    set(STATIC_FLAG -static)
376
377    set(DARWIN_macho_embedded_ARCHS armv6m armv7m armv7em armv7 i386 x86_64)
378
379    set(DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR
380      ${COMPILER_RT_OUTPUT_DIR}/lib/macho_embedded)
381    set(DARWIN_macho_embedded_LIBRARY_INSTALL_DIR
382      ${COMPILER_RT_INSTALL_PATH}/lib/macho_embedded)
383
384    set(CFLAGS_armv7 "-target thumbv7-apple-darwin-eabi")
385    set(CFLAGS_i386 "-march=pentium")
386
387    darwin_read_list_from_file(common_FUNCTIONS ${MACHO_SYM_DIR}/common.txt)
388    darwin_read_list_from_file(thumb2_FUNCTIONS ${MACHO_SYM_DIR}/thumb2.txt)
389    darwin_read_list_from_file(thumb2_64_FUNCTIONS ${MACHO_SYM_DIR}/thumb2-64.txt)
390    darwin_read_list_from_file(arm_FUNCTIONS ${MACHO_SYM_DIR}/arm.txt)
391    darwin_read_list_from_file(i386_FUNCTIONS ${MACHO_SYM_DIR}/i386.txt)
392
393
394    set(armv6m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS})
395    set(armv7m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
396    set(armv7em_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
397    set(armv7_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS} ${thumb2_64_FUNCTIONS})
398    set(i386_FUNCTIONS ${common_FUNCTIONS} ${i386_FUNCTIONS})
399    set(x86_64_FUNCTIONS ${common_FUNCTIONS})
400
401    foreach(arch ${DARWIN_macho_embedded_ARCHS})
402      darwin_filter_builtin_sources(${arch}_filtered_sources
403        INCLUDE ${arch}_FUNCTIONS
404        ${${arch}_SOURCES})
405      if(NOT ${arch}_filtered_sources)
406        message("${arch}_SOURCES: ${${arch}_SOURCES}")
407        message("${arch}_FUNCTIONS: ${${arch}_FUNCTIONS}")
408        message(FATAL_ERROR "Empty filtered sources!")
409      endif()
410    endforeach()
411
412    foreach(float_type SOFT HARD)
413      foreach(type PIC STATIC)
414        string(TOLOWER "${float_type}_${type}" lib_suffix)
415        foreach(arch ${DARWIN_${float_type}_FLOAT_ARCHS})
416          set(DARWIN_macho_embedded_SYSROOT ${DARWIN_osx_SYSROOT})
417          set(float_flag)
418          if(${arch} MATCHES "^arm")
419            # x86 targets are hard float by default, but the complain about the
420            # float ABI flag, so don't pass it unless we're targeting arm.
421            set(float_flag ${${float_type}_FLOAT_FLAG})
422          endif()
423          darwin_add_builtin_library(clang_rt ${lib_suffix}
424                                OS macho_embedded
425                                ARCH ${arch}
426                                SOURCES ${${arch}_filtered_sources}
427                                CFLAGS ${CFLAGS} -arch ${arch} ${${type}_FLAG} ${float_flag} ${CFLAGS_${arch}}
428                                PARENT_TARGET builtins)
429        endforeach()
430        foreach(lib ${macho_embedded_${lib_suffix}_libs})
431          set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C)
432        endforeach()
433        darwin_lipo_libs(clang_rt.${lib_suffix}
434                      PARENT_TARGET builtins
435                      LIPO_FLAGS ${macho_embedded_${lib_suffix}_lipo_flags}
436                      DEPENDS ${macho_embedded_${lib_suffix}_libs}
437                      OUTPUT_DIR ${DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR}
438                      INSTALL_DIR ${DARWIN_macho_embedded_LIBRARY_INSTALL_DIR})
439      endforeach()
440    endforeach()
441  endif()
442endmacro()
443