blob: 5f35c6a45205f5c11892fcdc87532f70a7a9a89c [file] [log] [blame]
Chris Bieneman9e256d02016-04-28 21:16:45 +00001include(CMakeParseArguments)
2
Chris Bieneman19c84512015-08-13 20:38:16 +00003# 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 )
Chris Bienemanb8eab082016-04-26 17:53:25 +000021 else()
22 set(${var}_INTERNAL ${var_internal} PARENT_SCOPE)
Chris Bieneman19c84512015-08-13 20:38:16 +000023 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}")
Filipe Cabecinhasd5e075d2015-08-19 16:23:19 +000039 message(STATUS "Got ld supported ARCHES: ${ARCHES}")
Chris Bieneman19c84512015-08-13 20:38:16 +000040 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)
Kuba Brecka945fcee2015-12-03 17:02:07 +000053 if(${valid_archs})
54 message(STATUS "Using cached valid architectures for ${os}.")
55 return()
56 endif()
57
Chris Bieneman19c84512015-08-13 20:38:16 +000058 set(archs ${ARGN})
59 message(STATUS "Finding valid architectures for ${os}...")
60 set(SIMPLE_CPP ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.cpp)
Chris Bienemand4626792015-09-16 18:29:52 +000061 file(WRITE ${SIMPLE_CPP} "#include <iostream>\nint main() { std::cout << std::endl; return 0; }\n")
Chris Bieneman19c84512015-08-13 20:38:16 +000062
63 set(os_linker_flags)
64 foreach(flag ${DARWIN_${os}_LINKFLAGS})
65 set(os_linker_flags "${os_linker_flags} ${flag}")
66 endforeach()
67
68 # The simple program will build for x86_64h on the simulator because it is
69 # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually
70 # a valid or useful architecture for the iOS simulator we should drop it.
Anna Zaksb83cbf02016-02-02 02:01:17 +000071 if(${os} MATCHES "^(iossim|tvossim|watchossim)$")
Chris Bieneman19c84512015-08-13 20:38:16 +000072 list(REMOVE_ITEM archs "x86_64h")
73 endif()
74
75 set(working_archs)
76 foreach(arch ${archs})
77
78 set(arch_linker_flags "-arch ${arch} ${os_linker_flags}")
79 try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_CPP}
80 COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS}
81 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${arch_linker_flags}"
82 OUTPUT_VARIABLE TEST_OUTPUT)
83 if(${CAN_TARGET_${os}_${arch}})
84 list(APPEND working_archs ${arch})
Chris Bieneman4c6b6982015-12-10 00:39:57 +000085 else()
86 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
87 "Testing compiler for supporting ${os}-${arch}:\n"
88 "${TEST_OUTPUT}\n")
Chris Bieneman19c84512015-08-13 20:38:16 +000089 endif()
90 endforeach()
Kuba Brecka945fcee2015-12-03 17:02:07 +000091 set(${valid_archs} ${working_archs}
92 CACHE STRING "List of valid architectures for platform ${os}.")
Chris Bieneman19c84512015-08-13 20:38:16 +000093endfunction()
Chris Bienemanb0607602015-08-20 17:32:06 +000094
95# This function checks the host cpusubtype to see if it is post-haswell. Haswell
96# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
97function(darwin_filter_host_archs input output)
Daniel Sandersbfcbe762016-01-27 09:28:01 +000098 list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
Chris Bienemanb0607602015-08-20 17:32:06 +000099 execute_process(
100 COMMAND sysctl hw.cpusubtype
101 OUTPUT_VARIABLE SUBTYPE)
102
103 string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
104 SUBTYPE_MATCHED "${SUBTYPE}")
105 set(HASWELL_SUPPORTED Off)
Chris Bieneman76d68912015-08-21 18:05:57 +0000106 if(SUBTYPE_MATCHED)
107 if(${CMAKE_MATCH_1} GREATER 7)
Chris Bienemanb0607602015-08-20 17:32:06 +0000108 set(HASWELL_SUPPORTED On)
109 endif()
110 endif()
111 if(NOT HASWELL_SUPPORTED)
112 list(REMOVE_ITEM tmp_var x86_64h)
113 endif()
114 set(${output} ${tmp_var} PARENT_SCOPE)
115endfunction()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000116
Chris Bieneman66e02f02015-09-23 15:18:17 +0000117# Read and process the exclude file into a list of symbols
Chris Bieneman648eafc2015-09-25 23:29:03 +0000118function(darwin_read_list_from_file output_var file)
119 if(EXISTS ${file})
120 file(READ ${file} EXCLUDES)
121 string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES})
122 set(${output_var} ${EXCLUDES} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000123 endif()
124endfunction()
125
126# this function takes an OS, architecture and minimum version and provides a
127# list of builtin functions to exclude
Chris Bieneman82ebe892015-09-24 21:51:09 +0000128function(darwin_find_excluded_builtins_list output_var)
129 cmake_parse_arguments(LIB
130 ""
131 "OS;ARCH;MIN_VERSION"
132 ""
133 ${ARGN})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000134
Chris Bieneman82ebe892015-09-24 21:51:09 +0000135 if(NOT LIB_OS OR NOT LIB_ARCH)
136 message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!")
137 endif()
138
Chris Bieneman648eafc2015-09-25 23:29:03 +0000139 darwin_read_list_from_file(${LIB_OS}_BUILTINS
140 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt)
141 darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS
142 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt)
Chris Bieneman82ebe892015-09-24 21:51:09 +0000143
144 if(LIB_MIN_VERSION)
145 file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
146 foreach(builtin_list ${builtin_lists})
147 string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
148 if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
149 if(NOT smallest_version)
150 set(smallest_version ${CMAKE_MATCH_1})
151 elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
152 set(smallest_version ${CMAKE_MATCH_1})
153 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000154 endif()
Chris Bieneman82ebe892015-09-24 21:51:09 +0000155 endforeach()
156
157 if(smallest_version)
Chris Bieneman648eafc2015-09-25 23:29:03 +0000158 darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS
159 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000160 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000161 endif()
162
Chris Bieneman82ebe892015-09-24 21:51:09 +0000163 set(${output_var}
164 ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
165 ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
166 ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000167endfunction()
168
169# adds a single builtin library for a single OS & ARCH
Chris Bieneman765d76e2015-09-23 23:05:32 +0000170macro(darwin_add_builtin_library name suffix)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000171 cmake_parse_arguments(LIB
172 ""
173 "PARENT_TARGET;OS;ARCH"
Chris Bieneman765d76e2015-09-23 23:05:32 +0000174 "SOURCES;CFLAGS;DEFS"
Chris Bieneman66e02f02015-09-23 15:18:17 +0000175 ${ARGN})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000176 set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
Chris Bieneman66e02f02015-09-23 15:18:17 +0000177 add_library(${libname} STATIC ${LIB_SOURCES})
Chris Bieneman4e5c2982015-11-10 17:26:40 +0000178 if(DARWIN_${LIB_OS}_SYSROOT)
179 set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
180 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000181 set_target_compile_flags(${libname}
Chris Bieneman4e5c2982015-11-10 17:26:40 +0000182 ${sysroot_flag}
Chris Bieneman66e02f02015-09-23 15:18:17 +0000183 ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
184 ${LIB_CFLAGS})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000185 set_property(TARGET ${libname} APPEND PROPERTY
186 COMPILE_DEFINITIONS ${LIB_DEFS})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000187 set_target_properties(${libname} PROPERTIES
188 OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
189 set_target_properties(${libname} PROPERTIES
190 OSX_ARCHITECTURES ${LIB_ARCH})
191
192 if(LIB_PARENT_TARGET)
193 add_dependencies(${LIB_PARENT_TARGET} ${libname})
194 endif()
Chris Bieneman765d76e2015-09-23 23:05:32 +0000195
Chris Bieneman36235332015-09-25 22:31:17 +0000196 list(APPEND ${LIB_OS}_${suffix}_libs ${libname})
197 list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
Chris Bieneman765d76e2015-09-23 23:05:32 +0000198endmacro()
199
200function(darwin_lipo_libs name)
201 cmake_parse_arguments(LIB
202 ""
Chris Bieneman8a386812015-10-02 22:14:25 +0000203 "PARENT_TARGET;OUTPUT_DIR;INSTALL_DIR"
Chris Bieneman765d76e2015-09-23 23:05:32 +0000204 "LIPO_FLAGS;DEPENDS"
205 ${ARGN})
Chris Bieneman5becf7f2015-11-09 23:37:45 +0000206 if(LIB_DEPENDS AND LIB_LIPO_FLAGS)
Chris Bieneman7e1db732015-11-09 23:07:45 +0000207 add_custom_command(OUTPUT ${LIB_OUTPUT_DIR}/lib${name}.a
208 COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR}
209 COMMAND lipo -output
210 ${LIB_OUTPUT_DIR}/lib${name}.a
211 -create ${LIB_LIPO_FLAGS}
212 DEPENDS ${LIB_DEPENDS}
213 )
214 add_custom_target(${name}
215 DEPENDS ${LIB_OUTPUT_DIR}/lib${name}.a)
216 add_dependencies(${LIB_PARENT_TARGET} ${name})
217 install(FILES ${LIB_OUTPUT_DIR}/lib${name}.a
218 DESTINATION ${LIB_INSTALL_DIR})
219 else()
220 message(WARNING "Not generating lipo target for ${name} because no input libraries exist.")
221 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000222endfunction()
223
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000224# Filter out generic versions of routines that are re-implemented in
225# architecture specific manner. This prevents multiple definitions of the
226# same symbols, making the symbol selection non-deterministic.
Chris Bienemanb850f312015-09-28 17:38:44 +0000227function(darwin_filter_builtin_sources output_var exclude_or_include excluded_list)
228 if(exclude_or_include STREQUAL "EXCLUDE")
229 set(filter_action GREATER)
230 set(filter_value -1)
231 elseif(exclude_or_include STREQUAL "INCLUDE")
232 set(filter_action LESS)
233 set(filter_value 0)
234 else()
235 message(FATAL_ERROR "darwin_filter_builtin_sources called without EXCLUDE|INCLUDE")
236 endif()
237
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000238 set(intermediate ${ARGN})
239 foreach (_file ${intermediate})
240 get_filename_component(_name_we ${_file} NAME_WE)
241 list(FIND ${excluded_list} ${_name_we} _found)
Chris Bienemanb850f312015-09-28 17:38:44 +0000242 if(_found ${filter_action} ${filter_value})
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000243 list(REMOVE_ITEM intermediate ${_file})
Chris Bieneman81fb4f02015-11-06 23:19:29 +0000244 elseif(${_file} MATCHES ".*/.*\\.S" OR ${_file} MATCHES ".*/.*\\.c")
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000245 get_filename_component(_name ${_file} NAME)
246 string(REPLACE ".S" ".c" _cname "${_name}")
247 list(REMOVE_ITEM intermediate ${_cname})
248 endif ()
249 endforeach ()
250 set(${output_var} ${intermediate} PARENT_SCOPE)
251endfunction()
252
Chris Bienemand52171b2015-10-09 22:40:50 +0000253function(darwin_add_eprintf_library)
Chris Bienemancb224502015-11-25 19:32:32 +0000254 cmake_parse_arguments(LIB
255 ""
256 ""
257 "CFLAGS"
258 ${ARGN})
259
Chris Bienemand52171b2015-10-09 22:40:50 +0000260 add_library(clang_rt.eprintf STATIC eprintf.c)
261 set_target_compile_flags(clang_rt.eprintf
262 -isysroot ${DARWIN_osx_SYSROOT}
263 ${DARWIN_osx_BUILTIN_MIN_VER_FLAG}
Chris Bienemancb224502015-11-25 19:32:32 +0000264 -arch i386
265 ${LIB_CFLAGS})
Chris Bienemand52171b2015-10-09 22:40:50 +0000266 set_target_properties(clang_rt.eprintf PROPERTIES
267 OUTPUT_NAME clang_rt.eprintf${COMPILER_RT_OS_SUFFIX})
268 set_target_properties(clang_rt.eprintf PROPERTIES
269 OSX_ARCHITECTURES i386)
270 add_dependencies(builtins clang_rt.eprintf)
271 set_target_properties(clang_rt.eprintf PROPERTIES
272 ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
273 install(TARGETS clang_rt.eprintf
274 ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
275endfunction()
276
Chris Bieneman66e02f02015-09-23 15:18:17 +0000277# Generates builtin libraries for all operating systems specified in ARGN. Each
278# OS library is constructed by lipo-ing together single-architecture libraries.
279macro(darwin_add_builtin_libraries)
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000280 set(DARWIN_EXCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Darwin-excludes)
281
Chris Bieneman44e875b2015-11-12 22:37:03 +0000282 set(CFLAGS "-fPIC -O3 -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer")
Chris Bieneman33dc9102015-11-11 21:38:35 +0000283 set(CMAKE_C_FLAGS "")
284 set(CMAKE_CXX_FLAGS "")
285 set(CMAKE_ASM_FLAGS "")
Chris Bieneman64dab722015-09-24 22:29:58 +0000286
287 set(PROFILE_SOURCES ../profile/InstrProfiling
288 ../profile/InstrProfilingBuffer
Vedant Kumar6ec496a2016-01-07 22:54:46 +0000289 ../profile/InstrProfilingPlatformDarwin
Vedant Kumar7456a7a2016-01-08 00:07:50 +0000290 ../profile/InstrProfilingWriter)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000291 foreach (os ${ARGN})
Daniel Sandersbfcbe762016-01-27 09:28:01 +0000292 list_intersect(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH)
Chris Bieneman3ff8cb22015-09-28 22:20:25 +0000293 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bieneman82ebe892015-09-24 21:51:09 +0000294 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
295 OS ${os}
296 ARCH ${arch}
297 MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
298
Chris Bienemanb850f312015-09-28 17:38:44 +0000299 darwin_filter_builtin_sources(filtered_sources
300 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
301 ${${arch}_SOURCES})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000302
Chris Bieneman765d76e2015-09-23 23:05:32 +0000303 darwin_add_builtin_library(clang_rt builtins
Chris Bieneman66e02f02015-09-23 15:18:17 +0000304 OS ${os}
305 ARCH ${arch}
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000306 SOURCES ${filtered_sources}
Chris Bieneman33dc9102015-11-11 21:38:35 +0000307 CFLAGS ${CFLAGS} -arch ${arch}
Chris Bieneman66e02f02015-09-23 15:18:17 +0000308 PARENT_TARGET builtins)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000309 endforeach()
310
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000311 # Don't build cc_kext libraries for simulator platforms
Chris Bienemand4b23202015-09-30 20:25:10 +0000312 if(NOT DARWIN_${os}_SKIP_CC_KEXT)
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000313 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bienemanc959a0b2015-09-28 23:09:46 +0000314 # By not specifying MIN_VERSION this only reads the OS and OS-arch lists.
315 # We don't want to filter out the builtins that are present in libSystem
316 # because kexts can't link libSystem.
317 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
318 OS ${os}
319 ARCH ${arch})
320
321 darwin_filter_builtin_sources(filtered_sources
322 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
323 ${${arch}_SOURCES})
324
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000325 # In addition to the builtins cc_kext includes some profile sources
326 darwin_add_builtin_library(clang_rt cc_kext
327 OS ${os}
328 ARCH ${arch}
Chris Bienemanc959a0b2015-09-28 23:09:46 +0000329 SOURCES ${filtered_sources} ${PROFILE_SOURCES}
Chris Bieneman33dc9102015-11-11 21:38:35 +0000330 CFLAGS ${CFLAGS} -arch ${arch} -mkernel
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000331 DEFS KERNEL_USE
332 PARENT_TARGET builtins)
333 endforeach()
Chris Bienemanf0099182015-10-09 18:38:34 +0000334 set(archive_name clang_rt.cc_kext_${os})
335 if(${os} STREQUAL "osx")
336 set(archive_name clang_rt.cc_kext)
337 endif()
338 darwin_lipo_libs(${archive_name}
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000339 PARENT_TARGET builtins
340 LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
341 DEPENDS ${${os}_cc_kext_libs}
Chris Bieneman8a386812015-10-02 22:14:25 +0000342 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
343 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000344 endif()
345 endforeach()
346
Chris Bienemancb224502015-11-25 19:32:32 +0000347 darwin_add_eprintf_library(CFLAGS ${CFLAGS})
Chris Bienemand52171b2015-10-09 22:40:50 +0000348
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000349 # We put the x86 sim slices into the archives for their base OS
350 foreach (os ${ARGN})
351 if(NOT ${os} MATCHES ".*sim$")
352 darwin_lipo_libs(clang_rt.${os}
353 PARENT_TARGET builtins
354 LIPO_FLAGS ${${os}_builtins_lipo_flags} ${${os}sim_builtins_lipo_flags}
355 DEPENDS ${${os}_builtins_libs} ${${os}sim_builtins_libs}
Chris Bieneman8a386812015-10-02 22:14:25 +0000356 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
357 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000358 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000359 endforeach()
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000360 darwin_add_embedded_builtin_libraries()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000361endmacro()
362
Chris Bieneman65b6e052015-11-10 17:26:35 +0000363macro(darwin_add_embedded_builtin_libraries)
Chris Bieneman4ae803e2015-11-10 00:41:18 +0000364 # this is a hacky opt-out. If you can't target both intel and arm
365 # architectures we bail here.
366 set(DARWIN_SOFT_FLOAT_ARCHS armv6m armv7m armv7em armv7)
367 set(DARWIN_HARD_FLOAT_ARCHS armv7em armv7)
Chris Bieneman854e1db2015-11-20 18:45:42 +0000368 if(COMPILER_RT_SUPPORTED_ARCH MATCHES ".*armv.*")
369 list(FIND COMPILER_RT_SUPPORTED_ARCH i386 i386_idx)
370 if(i386_idx GREATER -1)
371 list(APPEND DARWIN_HARD_FLOAT_ARCHS i386)
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000372 endif()
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000373
Chris Bieneman854e1db2015-11-20 18:45:42 +0000374 list(FIND COMPILER_RT_SUPPORTED_ARCH x86_64 x86_64_idx)
375 if(x86_64_idx GREATER -1)
376 list(APPEND DARWIN_HARD_FLOAT_ARCHS x86_64)
377 endif()
378
379 set(MACHO_SYM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/macho_embedded)
380
381 set(CFLAGS "-Oz -Wall -fomit-frame-pointer -ffreestanding")
382 set(CMAKE_C_FLAGS "")
383 set(CMAKE_CXX_FLAGS "")
384 set(CMAKE_ASM_FLAGS "")
385
386 set(SOFT_FLOAT_FLAG -mfloat-abi=soft)
387 set(HARD_FLOAT_FLAG -mfloat-abi=hard)
388
389 set(ENABLE_PIC Off)
390 set(PIC_FLAG -fPIC)
391 set(STATIC_FLAG -static)
392
393 set(DARWIN_macho_embedded_ARCHS armv6m armv7m armv7em armv7 i386 x86_64)
394
395 set(DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR
396 ${COMPILER_RT_OUTPUT_DIR}/lib/macho_embedded)
397 set(DARWIN_macho_embedded_LIBRARY_INSTALL_DIR
398 ${COMPILER_RT_INSTALL_PATH}/lib/macho_embedded)
399
400 set(CFLAGS_armv7 "-target thumbv7-apple-darwin-eabi")
401 set(CFLAGS_i386 "-march=pentium")
402
403 darwin_read_list_from_file(common_FUNCTIONS ${MACHO_SYM_DIR}/common.txt)
404 darwin_read_list_from_file(thumb2_FUNCTIONS ${MACHO_SYM_DIR}/thumb2.txt)
405 darwin_read_list_from_file(thumb2_64_FUNCTIONS ${MACHO_SYM_DIR}/thumb2-64.txt)
406 darwin_read_list_from_file(arm_FUNCTIONS ${MACHO_SYM_DIR}/arm.txt)
407 darwin_read_list_from_file(i386_FUNCTIONS ${MACHO_SYM_DIR}/i386.txt)
408
409
410 set(armv6m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS})
411 set(armv7m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
412 set(armv7em_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
413 set(armv7_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS} ${thumb2_64_FUNCTIONS})
414 set(i386_FUNCTIONS ${common_FUNCTIONS} ${i386_FUNCTIONS})
415 set(x86_64_FUNCTIONS ${common_FUNCTIONS})
416
417 foreach(arch ${DARWIN_macho_embedded_ARCHS})
418 darwin_filter_builtin_sources(${arch}_filtered_sources
419 INCLUDE ${arch}_FUNCTIONS
420 ${${arch}_SOURCES})
421 if(NOT ${arch}_filtered_sources)
422 message("${arch}_SOURCES: ${${arch}_SOURCES}")
423 message("${arch}_FUNCTIONS: ${${arch}_FUNCTIONS}")
424 message(FATAL_ERROR "Empty filtered sources!")
425 endif()
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000426 endforeach()
Chris Bieneman854e1db2015-11-20 18:45:42 +0000427
428 foreach(float_type SOFT HARD)
429 foreach(type PIC STATIC)
430 string(TOLOWER "${float_type}_${type}" lib_suffix)
431 foreach(arch ${DARWIN_${float_type}_FLOAT_ARCHS})
432 set(DARWIN_macho_embedded_SYSROOT ${DARWIN_osx_SYSROOT})
433 set(float_flag)
434 if(${arch} MATCHES "^arm")
435 # x86 targets are hard float by default, but the complain about the
436 # float ABI flag, so don't pass it unless we're targeting arm.
437 set(float_flag ${${float_type}_FLOAT_FLAG})
438 endif()
439 darwin_add_builtin_library(clang_rt ${lib_suffix}
440 OS macho_embedded
441 ARCH ${arch}
442 SOURCES ${${arch}_filtered_sources}
443 CFLAGS ${CFLAGS} -arch ${arch} ${${type}_FLAG} ${float_flag} ${CFLAGS_${arch}}
444 PARENT_TARGET builtins)
445 endforeach()
446 foreach(lib ${macho_embedded_${lib_suffix}_libs})
447 set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C)
448 endforeach()
449 darwin_lipo_libs(clang_rt.${lib_suffix}
450 PARENT_TARGET builtins
451 LIPO_FLAGS ${macho_embedded_${lib_suffix}_lipo_flags}
452 DEPENDS ${macho_embedded_${lib_suffix}_libs}
453 OUTPUT_DIR ${DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR}
454 INSTALL_DIR ${DARWIN_macho_embedded_LIBRARY_INSTALL_DIR})
455 endforeach()
456 endforeach()
457 endif()
Chris Bieneman65b6e052015-11-10 17:26:35 +0000458endmacro()