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