xref: /llvm-project/compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake (revision b07e7b76c5d532a611cf19ae083a94d44bfe0c5d)
1include(CompilerRTUtils)
2include(BuiltinTests)
3
4set(CMAKE_LIPO "lipo" CACHE PATH "path to the lipo tool")
5
6# On OS X SDKs can be installed anywhere on the base system and xcode-select can
7# set the default Xcode to use. This function finds the SDKs that are present in
8# the current Xcode.
9function(find_darwin_sdk_dir var sdk_name)
10  set(DARWIN_${sdk_name}_CACHED_SYSROOT "" CACHE STRING "Darwin SDK path for SDK ${sdk_name}.")
11  set(DARWIN_PREFER_PUBLIC_SDK OFF CACHE BOOL "Prefer Darwin public SDK, even when an internal SDK is present.")
12
13  if(DARWIN_${sdk_name}_CACHED_SYSROOT)
14    set(${var} ${DARWIN_${sdk_name}_CACHED_SYSROOT} PARENT_SCOPE)
15    return()
16  endif()
17  if(NOT DARWIN_PREFER_PUBLIC_SDK)
18    # Let's first try the internal SDK, otherwise use the public SDK.
19    execute_process(
20      COMMAND xcrun --sdk ${sdk_name}.internal --show-sdk-path
21      RESULT_VARIABLE result_process
22      OUTPUT_VARIABLE var_internal
23      OUTPUT_STRIP_TRAILING_WHITESPACE
24      ERROR_FILE /dev/null
25    )
26  endif()
27  if((NOT result_process EQUAL 0) OR "" STREQUAL "${var_internal}")
28    execute_process(
29      COMMAND xcrun --sdk ${sdk_name} --show-sdk-path
30      RESULT_VARIABLE result_process
31      OUTPUT_VARIABLE var_internal
32      OUTPUT_STRIP_TRAILING_WHITESPACE
33      ERROR_FILE /dev/null
34    )
35  else()
36    set(${var}_INTERNAL ${var_internal} PARENT_SCOPE)
37  endif()
38  if(result_process EQUAL 0)
39    set(${var} ${var_internal} PARENT_SCOPE)
40  endif()
41  message(STATUS "Checking DARWIN_${sdk_name}_SYSROOT - '${var_internal}'")
42  set(DARWIN_${sdk_name}_CACHED_SYSROOT ${var_internal} CACHE STRING "Darwin SDK path for SDK ${sdk_name}." FORCE)
43endfunction()
44
45function(find_darwin_sdk_version var sdk_name)
46  if (DARWIN_${sdk_name}_OVERRIDE_SDK_VERSION)
47    message(WARNING "Overriding ${sdk_name} SDK version to ${DARWIN_${sdk_name}_OVERRIDE_SDK_VERSION}")
48    set(${var} "${DARWIN_${sdk_name}_OVERRIDE_SDK_VERSION}" PARENT_SCOPE)
49    return()
50  endif()
51  set(result_process 1)
52  if(NOT DARWIN_PREFER_PUBLIC_SDK)
53    # Let's first try the internal SDK, otherwise use the public SDK.
54    execute_process(
55      COMMAND xcrun --sdk ${sdk_name}.internal --show-sdk-version
56      RESULT_VARIABLE result_process
57      OUTPUT_VARIABLE var_internal
58      OUTPUT_STRIP_TRAILING_WHITESPACE
59      ERROR_FILE /dev/null
60    )
61  endif()
62  if((NOT ${result_process} EQUAL 0) OR "" STREQUAL "${var_internal}")
63    execute_process(
64      COMMAND xcrun --sdk ${sdk_name} --show-sdk-version
65      RESULT_VARIABLE result_process
66      OUTPUT_VARIABLE var_internal
67      OUTPUT_STRIP_TRAILING_WHITESPACE
68      ERROR_FILE /dev/null
69    )
70  endif()
71  if(NOT result_process EQUAL 0)
72    message(FATAL_ERROR
73      "Failed to determine SDK version for \"${sdk_name}\" SDK")
74  endif()
75  # Check reported version looks sane.
76  if (NOT "${var_internal}" MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+)?$")
77    message(FATAL_ERROR
78      "Reported SDK version \"${var_internal}\" does not look like a version")
79  endif()
80  set(${var} ${var_internal} PARENT_SCOPE)
81endfunction()
82
83# There isn't a clear mapping of what architectures are supported with a given
84# target platform, but ld's version output does list the architectures it can
85# link for.
86function(darwin_get_toolchain_supported_archs output_var)
87  execute_process(
88    COMMAND "${CMAKE_LINKER}" -v
89    ERROR_VARIABLE LINKER_VERSION)
90
91  string(REGEX MATCH "configured to support archs: ([^\n]+)"
92         ARCHES_MATCHED "${LINKER_VERSION}")
93  if(ARCHES_MATCHED)
94    set(ARCHES "${CMAKE_MATCH_1}")
95    message(STATUS "Got ld supported ARCHES: ${ARCHES}")
96    string(REPLACE " " ";" ARCHES ${ARCHES})
97  else()
98    # If auto-detecting fails, fall back to a default set
99    message(WARNING "Detecting supported architectures from 'ld -v' failed. Returning default set.")
100    set(ARCHES "i386;x86_64;armv7;armv7s;arm64")
101  endif()
102  set(${output_var} ${ARCHES} PARENT_SCOPE)
103endfunction()
104
105# This function takes an OS and a list of architectures and identifies the
106# subset of the architectures list that the installed toolchain can target.
107function(darwin_test_archs os valid_archs)
108  if(${valid_archs})
109    message(STATUS "Using cached valid architectures for ${os}.")
110    return()
111  endif()
112
113  set(archs ${ARGN})
114  if(NOT TEST_COMPILE_ONLY)
115    message(STATUS "Finding valid architectures for ${os}...")
116    set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
117    file(WRITE ${SIMPLE_C} "#include <stdio.h>\n#include <unistd.h>\nint main(void) { printf(__FILE__); fork(); return 0; }\n")
118
119    set(os_linker_flags)
120    foreach(flag ${DARWIN_${os}_LINK_FLAGS})
121      set(os_linker_flags "${os_linker_flags} ${flag}")
122    endforeach()
123
124    # Disable building for i386 for macOS SDK >= 10.15. The SDK doesn't support
125    # linking for i386 and the corresponding OS doesn't allow running macOS i386
126    # binaries.
127    if ("${os}" STREQUAL "osx")
128      find_darwin_sdk_version(macosx_sdk_version "macosx")
129      if ("${macosx_sdk_version}" VERSION_GREATER 10.15 OR "${macosx_sdk_version}" VERSION_EQUAL 10.15)
130        message(STATUS "Disabling i386 slice for ${valid_archs}")
131        list(REMOVE_ITEM archs "i386")
132      endif()
133    endif()
134  endif()
135
136  # The simple program will build for x86_64h on the simulator because it is
137  # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually
138  # a valid or useful architecture for the simulators. We should drop it.
139  if(${os} MATCHES "^(iossim|tvossim|watchossim)$")
140    list(REMOVE_ITEM archs "x86_64h")
141    if ("i386" IN_LIST archs)
142      list(REMOVE_ITEM archs "i386")
143      message(STATUS "Disabling i386 slice for simulator")
144    endif()
145  endif()
146
147  if(${os} MATCHES "^ios$")
148    message(STATUS "Disabling sanitizers armv7* slice for ios")
149    list(FILTER archs EXCLUDE REGEX "armv7.*")
150  endif()
151
152  set(working_archs)
153  foreach(arch ${archs})
154
155    set(arch_linker_flags "-arch ${arch} ${os_linker_flags}")
156    if(TEST_COMPILE_ONLY)
157      # `-w` is used to surpress compiler warnings which `try_compile_only()` treats as an error.
158      try_compile_only(CAN_TARGET_${os}_${arch} FLAGS -v -arch ${arch} ${DARWIN_${os}_CFLAGS} -w)
159    else()
160      set(SAVED_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
161      set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${arch_linker_flags}")
162      try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_C}
163                  COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS}
164                  OUTPUT_VARIABLE TEST_OUTPUT)
165      set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS})
166    endif()
167    if(${CAN_TARGET_${os}_${arch}})
168      list(APPEND working_archs ${arch})
169    else()
170      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
171        "Testing compiler for supporting ${os}-${arch}:\n"
172        "${TEST_OUTPUT}\n")
173    endif()
174  endforeach()
175  set(${valid_archs} ${working_archs}
176    CACHE STRING "List of valid architectures for platform ${os}." FORCE)
177endfunction()
178
179# This function checks the host cputype/cpusubtype to filter supported
180# architecture for the host OS. This is used to determine which tests are
181# available for the host.
182function(darwin_filter_host_archs input output)
183  list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
184  execute_process(
185    COMMAND sysctl hw.cputype
186    OUTPUT_VARIABLE CPUTYPE)
187  string(REGEX MATCH "hw.cputype: ([0-9]*)"
188         CPUTYPE_MATCHED "${CPUTYPE}")
189  set(ARM_HOST Off)
190  if(CPUTYPE_MATCHED)
191    # ARM cputype is (0x01000000 | 12) and X86(_64) is always 7.
192    if(${CMAKE_MATCH_1} GREATER 11)
193      set(ARM_HOST On)
194    endif()
195  endif()
196
197  if(ARM_HOST)
198    list(REMOVE_ITEM tmp_var i386)
199    list(REMOVE_ITEM tmp_var x86_64)
200    list(REMOVE_ITEM tmp_var x86_64h)
201  else()
202    list(REMOVE_ITEM tmp_var arm64)
203    list(REMOVE_ITEM tmp_var arm64e)
204    execute_process(
205      COMMAND sysctl hw.cpusubtype
206      OUTPUT_VARIABLE SUBTYPE)
207    string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
208           SUBTYPE_MATCHED "${SUBTYPE}")
209
210    set(HASWELL_SUPPORTED Off)
211    if(SUBTYPE_MATCHED)
212      if(${CMAKE_MATCH_1} GREATER 7)
213        set(HASWELL_SUPPORTED On)
214      endif()
215    endif()
216    if(NOT HASWELL_SUPPORTED)
217      list(REMOVE_ITEM tmp_var x86_64h)
218    endif()
219  endif()
220
221  set(${output} ${tmp_var} PARENT_SCOPE)
222endfunction()
223
224# Read and process the exclude file into a list of symbols
225function(darwin_read_list_from_file output_var file)
226  if(EXISTS ${file})
227    file(READ ${file} EXCLUDES)
228    string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES})
229    set(${output_var} ${EXCLUDES} PARENT_SCOPE)
230  endif()
231endfunction()
232
233# this function takes an OS, architecture and minimum version and provides a
234# list of builtin functions to exclude
235function(darwin_find_excluded_builtins_list output_var)
236  cmake_parse_arguments(LIB
237    ""
238    "OS;ARCH;MIN_VERSION"
239    ""
240    ${ARGN})
241
242  if(NOT LIB_OS OR NOT LIB_ARCH)
243    message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!")
244  endif()
245
246  darwin_read_list_from_file(${LIB_OS}_BUILTINS
247    ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt)
248  darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS
249    ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt)
250
251  if(LIB_MIN_VERSION)
252    file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
253    foreach(builtin_list ${builtin_lists})
254      string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
255      if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
256        if(NOT smallest_version)
257          set(smallest_version ${CMAKE_MATCH_1})
258        elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
259          set(smallest_version ${CMAKE_MATCH_1})
260        endif()
261      endif()
262    endforeach()
263
264    if(smallest_version)
265      darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS
266        ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt)
267    endif()
268  endif()
269
270  set(${output_var}
271      ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
272      ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
273      ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
274endfunction()
275
276# adds a single builtin library for a single OS & ARCH
277macro(darwin_add_builtin_library name suffix)
278  cmake_parse_arguments(LIB
279    ""
280    "PARENT_TARGET;OS;ARCH"
281    "SOURCES;CFLAGS;DEFS;INCLUDE_DIRS"
282    ${ARGN})
283  set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
284  add_library(${libname} STATIC ${LIB_SOURCES})
285  if(DARWIN_${LIB_OS}_SYSROOT)
286    set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
287  endif()
288
289  # Make a copy of the compilation flags.
290  set(builtin_cflags ${LIB_CFLAGS})
291
292  # Strip out any inappropriate flags for the target.
293  if("${LIB_ARCH}" MATCHES "^(armv7|armv7k|armv7s)$")
294    set(builtin_cflags "")
295    foreach(cflag "${LIB_CFLAGS}")
296      string(REPLACE "-fomit-frame-pointer" "" cflag "${cflag}")
297      list(APPEND builtin_cflags ${cflag})
298    endforeach(cflag)
299  endif()
300
301  if ("${LIB_OS}" MATCHES ".*sim$")
302    # Pass an explicit -simulator environment to the -target option to ensure
303    # that we don't rely on the architecture to infer whether we're building
304    # for the simulator.
305    string(REGEX REPLACE "sim" "" base_os "${LIB_OS}")
306    list(APPEND builtin_cflags
307         -target "${LIB_ARCH}-apple-${base_os}${DARWIN_${LIBOS}_BUILTIN_MIN_VER}-simulator")
308  endif()
309
310  if ("${COMPILER_RT_ENABLE_MACCATALYST}" AND
311      "${LIB_OS}" MATCHES "^osx$")
312    # Build the macOS builtins with Mac Catalyst support.
313    list(APPEND builtin_cflags
314      "SHELL:-target ${LIB_ARCH}-apple-macos${DARWIN_osx_BUILTIN_MIN_VER} -darwin-target-variant ${LIB_ARCH}-apple-ios13.1-macabi")
315  endif()
316
317  set_target_compile_flags(${libname}
318    ${sysroot_flag}
319    ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
320    ${builtin_cflags})
321  target_include_directories(${libname}
322    PRIVATE ${LIB_INCLUDE_DIRS})
323  set_property(TARGET ${libname} APPEND PROPERTY
324      COMPILE_DEFINITIONS ${LIB_DEFS})
325  set_target_properties(${libname} PROPERTIES
326      OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
327  set_target_properties(${libname} PROPERTIES
328    OSX_ARCHITECTURES ${LIB_ARCH})
329
330  if(LIB_PARENT_TARGET)
331    add_dependencies(${LIB_PARENT_TARGET} ${libname})
332  endif()
333
334  list(APPEND ${LIB_OS}_${suffix}_libs ${libname})
335  list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
336  set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT/Libraries")
337endmacro()
338
339function(darwin_lipo_libs name)
340  cmake_parse_arguments(LIB
341    ""
342    "PARENT_TARGET;OUTPUT_DIR;INSTALL_DIR"
343    "LIPO_FLAGS;DEPENDS"
344    ${ARGN})
345  if(LIB_DEPENDS AND LIB_LIPO_FLAGS)
346    add_custom_command(OUTPUT ${LIB_OUTPUT_DIR}/lib${name}.a
347      COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR}
348      COMMAND ${CMAKE_LIPO} -output
349              ${LIB_OUTPUT_DIR}/lib${name}.a
350              -create ${LIB_LIPO_FLAGS}
351      DEPENDS ${LIB_DEPENDS}
352      )
353    add_custom_target(${name}
354      DEPENDS ${LIB_OUTPUT_DIR}/lib${name}.a)
355    set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT/Misc")
356    add_dependencies(${LIB_PARENT_TARGET} ${name})
357
358    if(CMAKE_CONFIGURATION_TYPES)
359      set(install_component ${LIB_PARENT_TARGET})
360    else()
361      set(install_component ${name})
362    endif()
363    install(FILES ${LIB_OUTPUT_DIR}/lib${name}.a
364      DESTINATION ${LIB_INSTALL_DIR}
365      COMPONENT ${install_component})
366    add_compiler_rt_install_targets(${name} PARENT_TARGET ${LIB_PARENT_TARGET})
367  else()
368    message(WARNING "Not generating lipo target for ${name} because no input libraries exist.")
369  endif()
370endfunction()
371
372# Filter the list of builtin sources for Darwin, then delegate to the generic
373# filtering.
374#
375# `exclude_or_include` must be one of:
376#  - EXCLUDE: remove every item whose name (w/o extension) matches a name in
377#    `excluded_list`.
378#  - INCLUDE: keep only items whose name (w/o extension) matches something
379#    in `excluded_list`.
380function(darwin_filter_builtin_sources output_var name exclude_or_include excluded_list)
381  if(exclude_or_include STREQUAL "EXCLUDE")
382    set(filter_action GREATER)
383    set(filter_value -1)
384  elseif(exclude_or_include STREQUAL "INCLUDE")
385    set(filter_action LESS)
386    set(filter_value 0)
387  else()
388    message(FATAL_ERROR "darwin_filter_builtin_sources called without EXCLUDE|INCLUDE")
389  endif()
390
391  set(intermediate ${ARGN})
392  foreach(_file ${intermediate})
393    get_filename_component(_name_we ${_file} NAME_WE)
394    list(FIND ${excluded_list} ${_name_we} _found)
395    if(_found ${filter_action} ${filter_value})
396      list(REMOVE_ITEM intermediate ${_file})
397    endif()
398  endforeach()
399
400  filter_builtin_sources(intermediate ${name})
401  set(${output_var} ${intermediate} PARENT_SCOPE)
402endfunction()
403
404# Generates builtin libraries for all operating systems specified in ARGN. Each
405# OS library is constructed by lipo-ing together single-architecture libraries.
406macro(darwin_add_builtin_libraries)
407  set(DARWIN_EXCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Darwin-excludes)
408
409  set(CFLAGS -fPIC -O3 -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer)
410  set(CMAKE_C_FLAGS "")
411  set(CMAKE_CXX_FLAGS "")
412  set(CMAKE_ASM_FLAGS "")
413
414  append_list_if(COMPILER_RT_HAS_ASM_LSE -DHAS_ASM_LSE CFLAGS)
415
416  set(PROFILE_SOURCES ../profile/InstrProfiling.c
417                      ../profile/InstrProfilingBuffer.c
418                      ../profile/InstrProfilingPlatformDarwin.c
419                      ../profile/InstrProfilingWriter.c
420                      ../profile/InstrProfilingInternal.c
421                      ../profile/InstrProfilingVersionVar.c)
422  foreach (os ${ARGN})
423    set(macosx_sdk_version 99999)
424    if ("${os}" STREQUAL "osx")
425      find_darwin_sdk_version(macosx_sdk_version "macosx")
426    endif()
427    add_security_warnings(CFLAGS ${macosx_sdk_version})
428
429    list_intersect(DARWIN_BUILTIN_ARCHS DARWIN_${os}_BUILTIN_ARCHS BUILTIN_SUPPORTED_ARCH)
430
431    if((arm64 IN_LIST DARWIN_BUILTIN_ARCHS OR arm64e IN_LIST DARWIN_BUILTIN_ARCHS) AND NOT TARGET lse_builtin_symlinks)
432      add_custom_target(
433        lse_builtin_symlinks
434        BYPRODUCTS ${lse_builtins}
435        ${arm64_lse_commands}
436      )
437
438      set(deps_arm64 lse_builtin_symlinks)
439      set(deps_arm64e lse_builtin_symlinks)
440    endif()
441
442    foreach (arch ${DARWIN_BUILTIN_ARCHS})
443      darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
444                              OS ${os}
445                              ARCH ${arch}
446                              MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
447      check_c_source_compiles("_Float16 foo(_Float16 x) { return x; }"
448                              COMPILER_RT_HAS_${arch}_FLOAT16)
449      append_list_if(COMPILER_RT_HAS_${arch}_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS_${arch})
450      check_c_source_compiles("__bf16 foo(__bf16 x) { return x; }"
451                              COMPILER_RT_HAS_${arch}_BFLOAT16)
452      # Build BF16 files only when "__bf16" is available.
453      if(COMPILER_RT_HAS_${arch}_BFLOAT16)
454        list(APPEND ${arch}_SOURCES ${BF16_SOURCES})
455      endif()
456      darwin_filter_builtin_sources(filtered_sources
457        ${os}_${arch}
458        EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
459        ${${arch}_SOURCES})
460
461      darwin_add_builtin_library(clang_rt builtins
462                              OS ${os}
463                              ARCH ${arch}
464                              DEPS ${deps_${arch}}
465                              SOURCES ${filtered_sources}
466                              CFLAGS ${CFLAGS} -arch ${arch}
467                              PARENT_TARGET builtins)
468    endforeach()
469
470    # Don't build cc_kext libraries for simulator platforms
471    if(NOT DARWIN_${os}_SKIP_CC_KEXT)
472      foreach (arch ${DARWIN_BUILTIN_ARCHS})
473        # By not specifying MIN_VERSION this only reads the OS and OS-arch lists.
474        # We don't want to filter out the builtins that are present in libSystem
475        # because kexts can't link libSystem.
476        darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
477                              OS ${os}
478                              ARCH ${arch})
479
480        darwin_filter_builtin_sources(filtered_sources
481          cc_kext_${os}_${arch}
482          EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
483          ${${arch}_SOURCES})
484
485        # In addition to the builtins cc_kext includes some profile sources
486        darwin_add_builtin_library(clang_rt cc_kext
487                                OS ${os}
488                                ARCH ${arch}
489                                DEPS ${deps_${arch}}
490                                SOURCES ${filtered_sources} ${PROFILE_SOURCES}
491                                CFLAGS ${CFLAGS} -arch ${arch} -mkernel
492                                DEFS KERNEL_USE
493                                INCLUDE_DIRS ../../include
494                                PARENT_TARGET builtins)
495      endforeach()
496      set(archive_name clang_rt.cc_kext_${os})
497      if(${os} STREQUAL "osx")
498        set(archive_name clang_rt.cc_kext)
499      endif()
500      darwin_lipo_libs(${archive_name}
501                      PARENT_TARGET builtins
502                      LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
503                      DEPENDS ${${os}_cc_kext_libs}
504                      OUTPUT_DIR ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
505                      INSTALL_DIR ${COMPILER_RT_INSTALL_LIBRARY_DIR})
506    endif()
507  endforeach()
508
509  foreach (os ${ARGN})
510    darwin_lipo_libs(clang_rt.${os}
511                     PARENT_TARGET builtins
512                     LIPO_FLAGS ${${os}_builtins_lipo_flags}
513                     DEPENDS ${${os}_builtins_libs}
514                     OUTPUT_DIR ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
515                     INSTALL_DIR ${COMPILER_RT_INSTALL_LIBRARY_DIR})
516  endforeach()
517  darwin_add_embedded_builtin_libraries()
518endmacro()
519
520macro(darwin_add_embedded_builtin_libraries)
521  # this is a hacky opt-out. If you can't target both intel and arm
522  # architectures we bail here.
523  set(DARWIN_SOFT_FLOAT_ARCHS armv6m armv7m armv7em armv7)
524  set(DARWIN_HARD_FLOAT_ARCHS armv7em armv7)
525  if(COMPILER_RT_SUPPORTED_ARCH MATCHES ".*armv.*")
526    list(FIND COMPILER_RT_SUPPORTED_ARCH i386 i386_idx)
527    if(i386_idx GREATER -1)
528      list(APPEND DARWIN_HARD_FLOAT_ARCHS i386)
529    endif()
530
531    list(FIND COMPILER_RT_SUPPORTED_ARCH x86_64 x86_64_idx)
532    if(x86_64_idx GREATER -1)
533      list(APPEND DARWIN_HARD_FLOAT_ARCHS x86_64)
534    endif()
535
536    set(MACHO_SYM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/macho_embedded)
537
538    set(CFLAGS -Oz -Wall -fomit-frame-pointer -ffreestanding)
539    set(CMAKE_C_FLAGS "")
540    set(CMAKE_CXX_FLAGS "")
541    set(CMAKE_ASM_FLAGS "")
542
543    set(SOFT_FLOAT_FLAG -mfloat-abi=soft)
544    set(HARD_FLOAT_FLAG -mfloat-abi=hard)
545
546    set(ENABLE_PIC Off)
547    set(PIC_FLAG -fPIC)
548    set(STATIC_FLAG -static)
549
550    set(DARWIN_macho_embedded_ARCHS armv6m armv7m armv7em armv7 i386 x86_64)
551
552    set(DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR
553      ${COMPILER_RT_OUTPUT_LIBRARY_DIR}/macho_embedded)
554    set(DARWIN_macho_embedded_LIBRARY_INSTALL_DIR
555      ${COMPILER_RT_INSTALL_LIBRARY_DIR}/macho_embedded)
556
557    set(CFLAGS_armv7 -target thumbv7-apple-darwin-eabi)
558    set(CFLAGS_i386 -march=pentium)
559
560    darwin_read_list_from_file(common_FUNCTIONS ${MACHO_SYM_DIR}/common.txt)
561    darwin_read_list_from_file(thumb2_FUNCTIONS ${MACHO_SYM_DIR}/thumb2.txt)
562    darwin_read_list_from_file(thumb2_64_FUNCTIONS ${MACHO_SYM_DIR}/thumb2-64.txt)
563    darwin_read_list_from_file(arm_FUNCTIONS ${MACHO_SYM_DIR}/arm.txt)
564    darwin_read_list_from_file(i386_FUNCTIONS ${MACHO_SYM_DIR}/i386.txt)
565
566
567    set(armv6m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS})
568    set(armv7m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
569    set(armv7em_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
570    set(armv7_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS} ${thumb2_64_FUNCTIONS})
571    set(i386_FUNCTIONS ${common_FUNCTIONS} ${i386_FUNCTIONS})
572    set(x86_64_FUNCTIONS ${common_FUNCTIONS})
573
574    foreach(arch ${DARWIN_macho_embedded_ARCHS})
575      darwin_filter_builtin_sources(${arch}_filtered_sources
576        macho_embedded_${arch}
577        INCLUDE ${arch}_FUNCTIONS
578        ${${arch}_SOURCES})
579      if(NOT ${arch}_filtered_sources)
580        message(WARNING "${arch}_SOURCES: ${${arch}_SOURCES}")
581        message(WARNING "${arch}_FUNCTIONS: ${${arch}_FUNCTIONS}")
582        message(FATAL_ERROR "Empty filtered sources!")
583      endif()
584    endforeach()
585
586    foreach(float_type SOFT HARD)
587      foreach(type PIC STATIC)
588        string(TOLOWER "${float_type}_${type}" lib_suffix)
589        foreach(arch ${DARWIN_${float_type}_FLOAT_ARCHS})
590          set(DARWIN_macho_embedded_SYSROOT ${DARWIN_osx_SYSROOT})
591          set(float_flag)
592          if(${arch} MATCHES "^arm")
593            # x86 targets are hard float by default, but the complain about the
594            # float ABI flag, so don't pass it unless we're targeting arm.
595            set(float_flag ${${float_type}_FLOAT_FLAG})
596          endif()
597          darwin_add_builtin_library(clang_rt ${lib_suffix}
598                                OS macho_embedded
599                                ARCH ${arch}
600                                SOURCES ${${arch}_filtered_sources}
601                                CFLAGS ${CFLAGS} -arch ${arch} ${${type}_FLAG} ${float_flag} ${CFLAGS_${arch}}
602                                PARENT_TARGET builtins)
603        endforeach()
604        foreach(lib ${macho_embedded_${lib_suffix}_libs})
605          set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C)
606        endforeach()
607        darwin_lipo_libs(clang_rt.${lib_suffix}
608                      PARENT_TARGET builtins
609                      LIPO_FLAGS ${macho_embedded_${lib_suffix}_lipo_flags}
610                      DEPENDS ${macho_embedded_${lib_suffix}_libs}
611                      OUTPUT_DIR ${DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR}
612                      INSTALL_DIR ${DARWIN_macho_embedded_LIBRARY_INSTALL_DIR})
613      endforeach()
614    endforeach()
615  endif()
616endmacro()
617