blob: 55a398ad412efaf7b3ed42d476d0f38f04b09cd1 [file] [log] [blame]
Chris Bieneman19c84512015-08-13 20:38:16 +00001# 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.
4function(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)
21endfunction()
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.
26function(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}")
Filipe Cabecinhasd5e075d2015-08-19 16:23:19 +000035 message(STATUS "Got ld supported ARCHES: ${ARCHES}")
Chris Bieneman19c84512015-08-13 20:38:16 +000036 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)
44endfunction()
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.
48function(darwin_test_archs os valid_archs)
49 set(archs ${ARGN})
50 message(STATUS "Finding valid architectures for ${os}...")
51 set(SIMPLE_CPP ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.cpp)
Chris Bienemand4626792015-09-16 18:29:52 +000052 file(WRITE ${SIMPLE_CPP} "#include <iostream>\nint main() { std::cout << std::endl; return 0; }\n")
Chris Bieneman19c84512015-08-13 20:38:16 +000053
54 set(os_linker_flags)
55 foreach(flag ${DARWIN_${os}_LINKFLAGS})
56 set(os_linker_flags "${os_linker_flags} ${flag}")
57 endforeach()
58
59 # The simple program will build for x86_64h on the simulator because it is
60 # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually
61 # a valid or useful architecture for the iOS simulator we should drop it.
62 if(${os} STREQUAL "iossim")
63 list(REMOVE_ITEM archs "x86_64h")
64 endif()
65
66 set(working_archs)
67 foreach(arch ${archs})
68
69 set(arch_linker_flags "-arch ${arch} ${os_linker_flags}")
70 try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_CPP}
71 COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS}
72 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${arch_linker_flags}"
73 OUTPUT_VARIABLE TEST_OUTPUT)
74 if(${CAN_TARGET_${os}_${arch}})
75 list(APPEND working_archs ${arch})
76 endif()
77 endforeach()
78 set(${valid_archs} ${working_archs} PARENT_SCOPE)
79endfunction()
Chris Bienemanb0607602015-08-20 17:32:06 +000080
81# This function checks the host cpusubtype to see if it is post-haswell. Haswell
82# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
83function(darwin_filter_host_archs input output)
84 list_union(tmp_var DARWIN_osx_ARCHS ${input})
85 execute_process(
86 COMMAND sysctl hw.cpusubtype
87 OUTPUT_VARIABLE SUBTYPE)
88
89 string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
90 SUBTYPE_MATCHED "${SUBTYPE}")
91 set(HASWELL_SUPPORTED Off)
Chris Bieneman76d68912015-08-21 18:05:57 +000092 if(SUBTYPE_MATCHED)
93 if(${CMAKE_MATCH_1} GREATER 7)
Chris Bienemanb0607602015-08-20 17:32:06 +000094 set(HASWELL_SUPPORTED On)
95 endif()
96 endif()
97 if(NOT HASWELL_SUPPORTED)
98 list(REMOVE_ITEM tmp_var x86_64h)
99 endif()
100 set(${output} ${tmp_var} PARENT_SCOPE)
101endfunction()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000102
103set(DARWIN_EXCLUDE_DIR ${CMAKE_SOURCE_DIR}/lib/builtins/Darwin-excludes)
104
105# Read and process the exclude file into a list of symbols
Chris Bieneman648eafc2015-09-25 23:29:03 +0000106function(darwin_read_list_from_file output_var file)
107 if(EXISTS ${file})
108 file(READ ${file} EXCLUDES)
109 string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES})
110 set(${output_var} ${EXCLUDES} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000111 endif()
112endfunction()
113
114# this function takes an OS, architecture and minimum version and provides a
115# list of builtin functions to exclude
Chris Bieneman82ebe892015-09-24 21:51:09 +0000116function(darwin_find_excluded_builtins_list output_var)
117 cmake_parse_arguments(LIB
118 ""
119 "OS;ARCH;MIN_VERSION"
120 ""
121 ${ARGN})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000122
Chris Bieneman82ebe892015-09-24 21:51:09 +0000123 if(NOT LIB_OS OR NOT LIB_ARCH)
124 message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!")
125 endif()
126
Chris Bieneman648eafc2015-09-25 23:29:03 +0000127 darwin_read_list_from_file(${LIB_OS}_BUILTINS
128 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt)
129 darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS
130 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt)
Chris Bieneman82ebe892015-09-24 21:51:09 +0000131
132 if(LIB_MIN_VERSION)
133 file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
134 foreach(builtin_list ${builtin_lists})
135 string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
136 if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
137 if(NOT smallest_version)
138 set(smallest_version ${CMAKE_MATCH_1})
139 elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
140 set(smallest_version ${CMAKE_MATCH_1})
141 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000142 endif()
Chris Bieneman82ebe892015-09-24 21:51:09 +0000143 endforeach()
144
145 if(smallest_version)
Chris Bieneman648eafc2015-09-25 23:29:03 +0000146 darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS
147 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000148 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000149 endif()
150
Chris Bieneman82ebe892015-09-24 21:51:09 +0000151 set(${output_var}
152 ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
153 ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
154 ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000155endfunction()
156
157# adds a single builtin library for a single OS & ARCH
Chris Bieneman765d76e2015-09-23 23:05:32 +0000158macro(darwin_add_builtin_library name suffix)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000159 cmake_parse_arguments(LIB
160 ""
161 "PARENT_TARGET;OS;ARCH"
Chris Bieneman765d76e2015-09-23 23:05:32 +0000162 "SOURCES;CFLAGS;DEFS"
Chris Bieneman66e02f02015-09-23 15:18:17 +0000163 ${ARGN})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000164 set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
Chris Bieneman66e02f02015-09-23 15:18:17 +0000165 add_library(${libname} STATIC ${LIB_SOURCES})
166 set_target_compile_flags(${libname}
167 -isysroot ${DARWIN_${LIB_OS}_SYSROOT}
168 ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
169 ${LIB_CFLAGS})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000170 set_property(TARGET ${libname} APPEND PROPERTY
171 COMPILE_DEFINITIONS ${LIB_DEFS})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000172 set_target_properties(${libname} PROPERTIES
173 OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
174 set_target_properties(${libname} PROPERTIES
175 OSX_ARCHITECTURES ${LIB_ARCH})
176
177 if(LIB_PARENT_TARGET)
178 add_dependencies(${LIB_PARENT_TARGET} ${libname})
179 endif()
Chris Bieneman765d76e2015-09-23 23:05:32 +0000180
Chris Bieneman36235332015-09-25 22:31:17 +0000181 list(APPEND ${LIB_OS}_${suffix}_libs ${libname})
182 list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
Chris Bieneman765d76e2015-09-23 23:05:32 +0000183endmacro()
184
185function(darwin_lipo_libs name)
186 cmake_parse_arguments(LIB
187 ""
188 "PARENT_TARGET"
189 "LIPO_FLAGS;DEPENDS"
190 ${ARGN})
191 add_custom_command(OUTPUT ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a
192 COMMAND lipo -output
193 ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a
194 -create ${LIB_LIPO_FLAGS}
195 DEPENDS ${LIB_DEPENDS}
196 )
197 add_custom_target(${name}
198 DEPENDS ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a)
199 add_dependencies(${LIB_PARENT_TARGET} ${name})
Chris Bieneman72cc28c2015-09-25 22:39:19 +0000200 install(FILES ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a
201 DESTINATION ${COMPILER_RT_INSTALL_PATH})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000202endfunction()
203
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000204# Filter out generic versions of routines that are re-implemented in
205# architecture specific manner. This prevents multiple definitions of the
206# same symbols, making the symbol selection non-deterministic.
207function(darwin_filter_builtin_sources output_var excluded_list)
208 set(intermediate ${ARGN})
209 foreach (_file ${intermediate})
210 get_filename_component(_name_we ${_file} NAME_WE)
211 list(FIND ${excluded_list} ${_name_we} _found)
212 if(_found GREATER -1)
213 list(REMOVE_ITEM intermediate ${_file})
214 elseif(${_file} MATCHES ".*/.*\\.S")
215 get_filename_component(_name ${_file} NAME)
216 string(REPLACE ".S" ".c" _cname "${_name}")
217 list(REMOVE_ITEM intermediate ${_cname})
218 endif ()
219 endforeach ()
220 set(${output_var} ${intermediate} PARENT_SCOPE)
221endfunction()
222
Chris Bieneman66e02f02015-09-23 15:18:17 +0000223# Generates builtin libraries for all operating systems specified in ARGN. Each
224# OS library is constructed by lipo-ing together single-architecture libraries.
225macro(darwin_add_builtin_libraries)
Chris Bienemana6e02022015-09-24 18:26:34 +0000226 if(CMAKE_CONFIGURATION_TYPES)
227 foreach(type ${CMAKE_CONFIGURATION_TYPES})
228 set(CMAKE_C_FLAGS_${type} -O3)
229 set(CMAKE_CXX_FLAGS_${type} -O3)
230 endforeach()
231 else()
232 set(CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE} -O3)
233 endif()
234
235 set(CMAKE_C_FLAGS "-fPIC -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer")
236 set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
237 set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
Chris Bieneman64dab722015-09-24 22:29:58 +0000238
239 set(PROFILE_SOURCES ../profile/InstrProfiling
240 ../profile/InstrProfilingBuffer
241 ../profile/InstrProfilingPlatformDarwin)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000242 foreach (os ${ARGN})
243 list_union(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000244 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bieneman64dab722015-09-24 22:29:58 +0000245 # In addition to the builtins cc_kext includes some profile sources
Chris Bieneman765d76e2015-09-23 23:05:32 +0000246 darwin_add_builtin_library(clang_rt cc_kext
247 OS ${os}
248 ARCH ${arch}
Chris Bieneman64dab722015-09-24 22:29:58 +0000249 SOURCES ${${arch}_SOURCES} ${PROFILE_SOURCES}
Chris Bienemana6e02022015-09-24 18:26:34 +0000250 CFLAGS -arch ${arch} -mkernel
Chris Bieneman765d76e2015-09-23 23:05:32 +0000251 DEFS KERNEL_USE
252 PARENT_TARGET builtins)
253
Chris Bieneman82ebe892015-09-24 21:51:09 +0000254 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
255 OS ${os}
256 ARCH ${arch}
257 MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
258
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000259 darwin_filter_builtin_sources(filtered_sources ${arch}_${os}_EXCLUDED_BUILTINS ${${arch}_SOURCES})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000260
Chris Bieneman765d76e2015-09-23 23:05:32 +0000261 darwin_add_builtin_library(clang_rt builtins
Chris Bieneman66e02f02015-09-23 15:18:17 +0000262 OS ${os}
263 ARCH ${arch}
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000264 SOURCES ${filtered_sources}
Chris Bienemana6e02022015-09-24 18:26:34 +0000265 CFLAGS -arch ${arch}
Chris Bieneman66e02f02015-09-23 15:18:17 +0000266 PARENT_TARGET builtins)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000267 endforeach()
268
Chris Bieneman765d76e2015-09-23 23:05:32 +0000269 darwin_lipo_libs(clang_rt.cc_kext_${os}
270 PARENT_TARGET builtins
271 LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
272 DEPENDS ${${os}_cc_kext_libs})
273 darwin_lipo_libs(clang_rt.${os}
274 PARENT_TARGET builtins
275 LIPO_FLAGS ${${os}_builtins_lipo_flags}
276 DEPENDS ${${os}_builtins_libs})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000277 endforeach()
278endmacro()
279