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