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