blob: baf890bae004bd492194e62c3558ddf4cb6962aa [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
106function(darwin_read_exclude_file output_var file)
107 if(EXISTS ${DARWIN_EXCLUDE_DIR}/${file}.txt)
108 file(READ ${DARWIN_EXCLUDE_DIR}/${file}.txt ${file}_EXCLUDES)
109 string(REPLACE "\n" ";" ${file}_EXCLUDES ${${file}_EXCLUDES})
110 set(${output_var} ${${file}_EXCLUDES} PARENT_SCOPE)
111 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
127 darwin_read_exclude_file(${LIB_OS}_BUILTINS ${LIB_OS})
128 darwin_read_exclude_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS ${LIB_OS}-${LIB_ARCH})
129
130 if(LIB_MIN_VERSION)
131 file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
132 foreach(builtin_list ${builtin_lists})
133 string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
134 if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
135 if(NOT smallest_version)
136 set(smallest_version ${CMAKE_MATCH_1})
137 elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
138 set(smallest_version ${CMAKE_MATCH_1})
139 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000140 endif()
Chris Bieneman82ebe892015-09-24 21:51:09 +0000141 endforeach()
142
143 if(smallest_version)
144 darwin_read_exclude_file(${LIB_ARCH}_${LIB_OS}_BUILTINS ${LIB_OS}${smallest_version}-${LIB_ARCH})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000145 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000146 endif()
147
Chris Bieneman82ebe892015-09-24 21:51:09 +0000148 set(${output_var}
149 ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
150 ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
151 ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000152endfunction()
153
154# adds a single builtin library for a single OS & ARCH
Chris Bieneman765d76e2015-09-23 23:05:32 +0000155macro(darwin_add_builtin_library name suffix)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000156 cmake_parse_arguments(LIB
157 ""
158 "PARENT_TARGET;OS;ARCH"
Chris Bieneman765d76e2015-09-23 23:05:32 +0000159 "SOURCES;CFLAGS;DEFS"
Chris Bieneman66e02f02015-09-23 15:18:17 +0000160 ${ARGN})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000161 set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
Chris Bieneman66e02f02015-09-23 15:18:17 +0000162 add_library(${libname} STATIC ${LIB_SOURCES})
163 set_target_compile_flags(${libname}
164 -isysroot ${DARWIN_${LIB_OS}_SYSROOT}
165 ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
166 ${LIB_CFLAGS})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000167 set_property(TARGET ${libname} APPEND PROPERTY
168 COMPILE_DEFINITIONS ${LIB_DEFS})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000169 set_target_properties(${libname} PROPERTIES
170 OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
171 set_target_properties(${libname} PROPERTIES
172 OSX_ARCHITECTURES ${LIB_ARCH})
173
174 if(LIB_PARENT_TARGET)
175 add_dependencies(${LIB_PARENT_TARGET} ${libname})
176 endif()
Chris Bieneman765d76e2015-09-23 23:05:32 +0000177
178 list(APPEND ${os}_${suffix}_libs ${libname})
179 list(APPEND ${os}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
180endmacro()
181
182function(darwin_lipo_libs name)
183 cmake_parse_arguments(LIB
184 ""
185 "PARENT_TARGET"
186 "LIPO_FLAGS;DEPENDS"
187 ${ARGN})
188 add_custom_command(OUTPUT ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a
189 COMMAND lipo -output
190 ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a
191 -create ${LIB_LIPO_FLAGS}
192 DEPENDS ${LIB_DEPENDS}
193 )
194 add_custom_target(${name}
195 DEPENDS ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a)
196 add_dependencies(${LIB_PARENT_TARGET} ${name})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000197endfunction()
198
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000199# Filter out generic versions of routines that are re-implemented in
200# architecture specific manner. This prevents multiple definitions of the
201# same symbols, making the symbol selection non-deterministic.
202function(darwin_filter_builtin_sources output_var excluded_list)
203 set(intermediate ${ARGN})
204 foreach (_file ${intermediate})
205 get_filename_component(_name_we ${_file} NAME_WE)
206 list(FIND ${excluded_list} ${_name_we} _found)
207 if(_found GREATER -1)
208 list(REMOVE_ITEM intermediate ${_file})
209 elseif(${_file} MATCHES ".*/.*\\.S")
210 get_filename_component(_name ${_file} NAME)
211 string(REPLACE ".S" ".c" _cname "${_name}")
212 list(REMOVE_ITEM intermediate ${_cname})
213 endif ()
214 endforeach ()
215 set(${output_var} ${intermediate} PARENT_SCOPE)
216endfunction()
217
Chris Bieneman66e02f02015-09-23 15:18:17 +0000218# Generates builtin libraries for all operating systems specified in ARGN. Each
219# OS library is constructed by lipo-ing together single-architecture libraries.
220macro(darwin_add_builtin_libraries)
Chris Bienemana6e02022015-09-24 18:26:34 +0000221 if(CMAKE_CONFIGURATION_TYPES)
222 foreach(type ${CMAKE_CONFIGURATION_TYPES})
223 set(CMAKE_C_FLAGS_${type} -O3)
224 set(CMAKE_CXX_FLAGS_${type} -O3)
225 endforeach()
226 else()
227 set(CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE} -O3)
228 endif()
229
230 set(CMAKE_C_FLAGS "-fPIC -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer")
231 set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS})
232 set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS})
Chris Bieneman64dab722015-09-24 22:29:58 +0000233
234 set(PROFILE_SOURCES ../profile/InstrProfiling
235 ../profile/InstrProfilingBuffer
236 ../profile/InstrProfilingPlatformDarwin)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000237 foreach (os ${ARGN})
238 list_union(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000239 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bieneman64dab722015-09-24 22:29:58 +0000240 # In addition to the builtins cc_kext includes some profile sources
Chris Bieneman765d76e2015-09-23 23:05:32 +0000241 darwin_add_builtin_library(clang_rt cc_kext
242 OS ${os}
243 ARCH ${arch}
Chris Bieneman64dab722015-09-24 22:29:58 +0000244 SOURCES ${${arch}_SOURCES} ${PROFILE_SOURCES}
Chris Bienemana6e02022015-09-24 18:26:34 +0000245 CFLAGS -arch ${arch} -mkernel
Chris Bieneman765d76e2015-09-23 23:05:32 +0000246 DEFS KERNEL_USE
247 PARENT_TARGET builtins)
248
Chris Bieneman82ebe892015-09-24 21:51:09 +0000249 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
250 OS ${os}
251 ARCH ${arch}
252 MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
253
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000254 darwin_filter_builtin_sources(filtered_sources ${arch}_${os}_EXCLUDED_BUILTINS ${${arch}_SOURCES})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000255
Chris Bieneman765d76e2015-09-23 23:05:32 +0000256 darwin_add_builtin_library(clang_rt builtins
Chris Bieneman66e02f02015-09-23 15:18:17 +0000257 OS ${os}
258 ARCH ${arch}
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000259 SOURCES ${filtered_sources}
Chris Bienemana6e02022015-09-24 18:26:34 +0000260 CFLAGS -arch ${arch}
Chris Bieneman66e02f02015-09-23 15:18:17 +0000261 PARENT_TARGET builtins)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000262 endforeach()
263
Chris Bieneman765d76e2015-09-23 23:05:32 +0000264 darwin_lipo_libs(clang_rt.cc_kext_${os}
265 PARENT_TARGET builtins
266 LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
267 DEPENDS ${${os}_cc_kext_libs})
268 darwin_lipo_libs(clang_rt.${os}
269 PARENT_TARGET builtins
270 LIPO_FLAGS ${${os}_builtins_lipo_flags}
271 DEPENDS ${${os}_builtins_libs})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000272 endforeach()
273endmacro()
274