blob: 3c89381f92f9209c16eb47375af2e7672bfbdc37 [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
Chris Bieneman1ca6b632016-08-19 22:22:35 +000010 RESULT_VARIABLE result_process
Chris Bieneman19c84512015-08-13 20:38:16 +000011 OUTPUT_VARIABLE var_internal
12 OUTPUT_STRIP_TRAILING_WHITESPACE
13 ERROR_FILE /dev/null
14 )
Chris Bieneman1ca6b632016-08-19 22:22:35 +000015 if((NOT result_process EQUAL 0) OR "" STREQUAL "${var_internal}")
Chris Bieneman19c84512015-08-13 20:38:16 +000016 execute_process(
17 COMMAND xcodebuild -version -sdk ${sdk_name} Path
Chris Bieneman1ca6b632016-08-19 22:22:35 +000018 RESULT_VARIABLE result_process
Chris Bieneman19c84512015-08-13 20:38:16 +000019 OUTPUT_VARIABLE var_internal
20 OUTPUT_STRIP_TRAILING_WHITESPACE
21 ERROR_FILE /dev/null
22 )
Chris Bienemanb8eab082016-04-26 17:53:25 +000023 else()
24 set(${var}_INTERNAL ${var_internal} PARENT_SCOPE)
Chris Bieneman19c84512015-08-13 20:38:16 +000025 endif()
Chris Bieneman1ca6b632016-08-19 22:22:35 +000026 if(result_process EQUAL 0)
27 set(${var} ${var_internal} PARENT_SCOPE)
28 endif()
Chris Bieneman19c84512015-08-13 20:38:16 +000029endfunction()
30
31# There isn't a clear mapping of what architectures are supported with a given
32# target platform, but ld's version output does list the architectures it can
33# link for.
34function(darwin_get_toolchain_supported_archs output_var)
35 execute_process(
36 COMMAND ld -v
37 ERROR_VARIABLE LINKER_VERSION)
38
39 string(REGEX MATCH "configured to support archs: ([^\n]+)"
40 ARCHES_MATCHED "${LINKER_VERSION}")
41 if(ARCHES_MATCHED)
42 set(ARCHES "${CMAKE_MATCH_1}")
Filipe Cabecinhasd5e075d2015-08-19 16:23:19 +000043 message(STATUS "Got ld supported ARCHES: ${ARCHES}")
Chris Bieneman19c84512015-08-13 20:38:16 +000044 string(REPLACE " " ";" ARCHES ${ARCHES})
45 else()
46 # If auto-detecting fails, fall back to a default set
47 message(WARNING "Detecting supported architectures from 'ld -v' failed. Returning default set.")
48 set(ARCHES "i386;x86_64;armv7;armv7s;arm64")
49 endif()
50
51 set(${output_var} ${ARCHES} PARENT_SCOPE)
52endfunction()
53
54# This function takes an OS and a list of architectures and identifies the
55# subset of the architectures list that the installed toolchain can target.
56function(darwin_test_archs os valid_archs)
Kuba Brecka945fcee2015-12-03 17:02:07 +000057 if(${valid_archs})
58 message(STATUS "Using cached valid architectures for ${os}.")
59 return()
60 endif()
61
Chris Bieneman19c84512015-08-13 20:38:16 +000062 set(archs ${ARGN})
Chris Bieneman4e4d4302016-05-03 19:48:11 +000063 if(NOT TEST_COMPILE_ONLY)
64 message(STATUS "Finding valid architectures for ${os}...")
Chris Bieneman84e44232016-06-22 23:02:49 +000065 set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
66 file(WRITE ${SIMPLE_C} "#include <stdio.h>\nint main() { printf(__FILE__); return 0; }\n")
Chris Bieneman4e4d4302016-05-03 19:48:11 +000067
68 set(os_linker_flags)
Francis Ricci9f931132017-01-10 04:33:04 +000069 foreach(flag ${DARWIN_${os}_LINK_FLAGS})
Chris Bieneman4e4d4302016-05-03 19:48:11 +000070 set(os_linker_flags "${os_linker_flags} ${flag}")
71 endforeach()
72 endif()
Chris Bieneman19c84512015-08-13 20:38:16 +000073
74 # The simple program will build for x86_64h on the simulator because it is
75 # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually
76 # a valid or useful architecture for the iOS simulator we should drop it.
Anna Zaksb83cbf02016-02-02 02:01:17 +000077 if(${os} MATCHES "^(iossim|tvossim|watchossim)$")
Chris Bieneman19c84512015-08-13 20:38:16 +000078 list(REMOVE_ITEM archs "x86_64h")
79 endif()
80
81 set(working_archs)
82 foreach(arch ${archs})
Chris Bieneman4e4d4302016-05-03 19:48:11 +000083
Chris Bieneman19c84512015-08-13 20:38:16 +000084 set(arch_linker_flags "-arch ${arch} ${os_linker_flags}")
Chris Bieneman4e4d4302016-05-03 19:48:11 +000085 if(TEST_COMPILE_ONLY)
86 try_compile_only(CAN_TARGET_${os}_${arch} -v -arch ${arch} ${DARWIN_${os}_CFLAGS})
87 else()
Chris Bieneman84e44232016-06-22 23:02:49 +000088 try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_C}
Chris Bieneman4e4d4302016-05-03 19:48:11 +000089 COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS}
90 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${arch_linker_flags}"
91 OUTPUT_VARIABLE TEST_OUTPUT)
92 endif()
Chris Bieneman19c84512015-08-13 20:38:16 +000093 if(${CAN_TARGET_${os}_${arch}})
94 list(APPEND working_archs ${arch})
Chris Bieneman4c6b6982015-12-10 00:39:57 +000095 else()
96 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
97 "Testing compiler for supporting ${os}-${arch}:\n"
98 "${TEST_OUTPUT}\n")
Chris Bieneman19c84512015-08-13 20:38:16 +000099 endif()
100 endforeach()
Kuba Brecka945fcee2015-12-03 17:02:07 +0000101 set(${valid_archs} ${working_archs}
102 CACHE STRING "List of valid architectures for platform ${os}.")
Chris Bieneman19c84512015-08-13 20:38:16 +0000103endfunction()
Chris Bienemanb0607602015-08-20 17:32:06 +0000104
105# This function checks the host cpusubtype to see if it is post-haswell. Haswell
106# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
107function(darwin_filter_host_archs input output)
Daniel Sandersbfcbe762016-01-27 09:28:01 +0000108 list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
Chris Bienemanb0607602015-08-20 17:32:06 +0000109 execute_process(
110 COMMAND sysctl hw.cpusubtype
111 OUTPUT_VARIABLE SUBTYPE)
112
113 string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
114 SUBTYPE_MATCHED "${SUBTYPE}")
115 set(HASWELL_SUPPORTED Off)
Chris Bieneman76d68912015-08-21 18:05:57 +0000116 if(SUBTYPE_MATCHED)
117 if(${CMAKE_MATCH_1} GREATER 7)
Chris Bienemanb0607602015-08-20 17:32:06 +0000118 set(HASWELL_SUPPORTED On)
119 endif()
120 endif()
121 if(NOT HASWELL_SUPPORTED)
122 list(REMOVE_ITEM tmp_var x86_64h)
123 endif()
124 set(${output} ${tmp_var} PARENT_SCOPE)
125endfunction()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000126
Chris Bieneman66e02f02015-09-23 15:18:17 +0000127# Read and process the exclude file into a list of symbols
Chris Bieneman648eafc2015-09-25 23:29:03 +0000128function(darwin_read_list_from_file output_var file)
129 if(EXISTS ${file})
130 file(READ ${file} EXCLUDES)
131 string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES})
132 set(${output_var} ${EXCLUDES} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000133 endif()
134endfunction()
135
136# this function takes an OS, architecture and minimum version and provides a
137# list of builtin functions to exclude
Chris Bieneman82ebe892015-09-24 21:51:09 +0000138function(darwin_find_excluded_builtins_list output_var)
139 cmake_parse_arguments(LIB
140 ""
141 "OS;ARCH;MIN_VERSION"
142 ""
143 ${ARGN})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000144
Chris Bieneman82ebe892015-09-24 21:51:09 +0000145 if(NOT LIB_OS OR NOT LIB_ARCH)
146 message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!")
147 endif()
148
Chris Bieneman648eafc2015-09-25 23:29:03 +0000149 darwin_read_list_from_file(${LIB_OS}_BUILTINS
150 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt)
151 darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS
152 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt)
Chris Bieneman82ebe892015-09-24 21:51:09 +0000153
154 if(LIB_MIN_VERSION)
155 file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
156 foreach(builtin_list ${builtin_lists})
157 string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
158 if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
159 if(NOT smallest_version)
160 set(smallest_version ${CMAKE_MATCH_1})
161 elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
162 set(smallest_version ${CMAKE_MATCH_1})
163 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000164 endif()
Chris Bieneman82ebe892015-09-24 21:51:09 +0000165 endforeach()
166
167 if(smallest_version)
Chris Bieneman648eafc2015-09-25 23:29:03 +0000168 darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS
169 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000170 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000171 endif()
172
Chris Bieneman82ebe892015-09-24 21:51:09 +0000173 set(${output_var}
174 ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
175 ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
176 ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000177endfunction()
178
179# adds a single builtin library for a single OS & ARCH
Chris Bieneman765d76e2015-09-23 23:05:32 +0000180macro(darwin_add_builtin_library name suffix)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000181 cmake_parse_arguments(LIB
182 ""
183 "PARENT_TARGET;OS;ARCH"
Chris Bieneman765d76e2015-09-23 23:05:32 +0000184 "SOURCES;CFLAGS;DEFS"
Chris Bieneman66e02f02015-09-23 15:18:17 +0000185 ${ARGN})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000186 set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
Chris Bieneman66e02f02015-09-23 15:18:17 +0000187 add_library(${libname} STATIC ${LIB_SOURCES})
Chris Bieneman4e5c2982015-11-10 17:26:40 +0000188 if(DARWIN_${LIB_OS}_SYSROOT)
189 set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
190 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000191 set_target_compile_flags(${libname}
Chris Bieneman4e5c2982015-11-10 17:26:40 +0000192 ${sysroot_flag}
Chris Bieneman66e02f02015-09-23 15:18:17 +0000193 ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
194 ${LIB_CFLAGS})
Chris Bieneman765d76e2015-09-23 23:05:32 +0000195 set_property(TARGET ${libname} APPEND PROPERTY
196 COMPILE_DEFINITIONS ${LIB_DEFS})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000197 set_target_properties(${libname} PROPERTIES
198 OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
199 set_target_properties(${libname} PROPERTIES
200 OSX_ARCHITECTURES ${LIB_ARCH})
201
202 if(LIB_PARENT_TARGET)
203 add_dependencies(${LIB_PARENT_TARGET} ${libname})
204 endif()
Chris Bieneman765d76e2015-09-23 23:05:32 +0000205
Chris Bieneman36235332015-09-25 22:31:17 +0000206 list(APPEND ${LIB_OS}_${suffix}_libs ${libname})
207 list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
Chris Bieneman765d76e2015-09-23 23:05:32 +0000208endmacro()
209
210function(darwin_lipo_libs name)
211 cmake_parse_arguments(LIB
212 ""
Chris Bieneman8a386812015-10-02 22:14:25 +0000213 "PARENT_TARGET;OUTPUT_DIR;INSTALL_DIR"
Chris Bieneman765d76e2015-09-23 23:05:32 +0000214 "LIPO_FLAGS;DEPENDS"
215 ${ARGN})
Chris Bieneman5becf7f2015-11-09 23:37:45 +0000216 if(LIB_DEPENDS AND LIB_LIPO_FLAGS)
Chris Bieneman7e1db732015-11-09 23:07:45 +0000217 add_custom_command(OUTPUT ${LIB_OUTPUT_DIR}/lib${name}.a
218 COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR}
219 COMMAND lipo -output
220 ${LIB_OUTPUT_DIR}/lib${name}.a
221 -create ${LIB_LIPO_FLAGS}
222 DEPENDS ${LIB_DEPENDS}
223 )
224 add_custom_target(${name}
225 DEPENDS ${LIB_OUTPUT_DIR}/lib${name}.a)
226 add_dependencies(${LIB_PARENT_TARGET} ${name})
227 install(FILES ${LIB_OUTPUT_DIR}/lib${name}.a
228 DESTINATION ${LIB_INSTALL_DIR})
229 else()
230 message(WARNING "Not generating lipo target for ${name} because no input libraries exist.")
231 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000232endfunction()
233
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000234# Filter out generic versions of routines that are re-implemented in
235# architecture specific manner. This prevents multiple definitions of the
236# same symbols, making the symbol selection non-deterministic.
Chris Bienemanb850f312015-09-28 17:38:44 +0000237function(darwin_filter_builtin_sources output_var exclude_or_include excluded_list)
238 if(exclude_or_include STREQUAL "EXCLUDE")
239 set(filter_action GREATER)
240 set(filter_value -1)
241 elseif(exclude_or_include STREQUAL "INCLUDE")
242 set(filter_action LESS)
243 set(filter_value 0)
244 else()
245 message(FATAL_ERROR "darwin_filter_builtin_sources called without EXCLUDE|INCLUDE")
246 endif()
247
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000248 set(intermediate ${ARGN})
249 foreach (_file ${intermediate})
250 get_filename_component(_name_we ${_file} NAME_WE)
251 list(FIND ${excluded_list} ${_name_we} _found)
Chris Bienemanb850f312015-09-28 17:38:44 +0000252 if(_found ${filter_action} ${filter_value})
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000253 list(REMOVE_ITEM intermediate ${_file})
Chris Bieneman81fb4f02015-11-06 23:19:29 +0000254 elseif(${_file} MATCHES ".*/.*\\.S" OR ${_file} MATCHES ".*/.*\\.c")
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000255 get_filename_component(_name ${_file} NAME)
256 string(REPLACE ".S" ".c" _cname "${_name}")
257 list(REMOVE_ITEM intermediate ${_cname})
258 endif ()
259 endforeach ()
260 set(${output_var} ${intermediate} PARENT_SCOPE)
261endfunction()
262
Chris Bieneman66e02f02015-09-23 15:18:17 +0000263# Generates builtin libraries for all operating systems specified in ARGN. Each
264# OS library is constructed by lipo-ing together single-architecture libraries.
265macro(darwin_add_builtin_libraries)
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000266 set(DARWIN_EXCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Darwin-excludes)
267
Chris Bieneman44e875b2015-11-12 22:37:03 +0000268 set(CFLAGS "-fPIC -O3 -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer")
Chris Bieneman33dc9102015-11-11 21:38:35 +0000269 set(CMAKE_C_FLAGS "")
270 set(CMAKE_CXX_FLAGS "")
271 set(CMAKE_ASM_FLAGS "")
Chris Bieneman64dab722015-09-24 22:29:58 +0000272
273 set(PROFILE_SOURCES ../profile/InstrProfiling
274 ../profile/InstrProfilingBuffer
Vedant Kumar6ec496a2016-01-07 22:54:46 +0000275 ../profile/InstrProfilingPlatformDarwin
Vedant Kumar7456a7a2016-01-08 00:07:50 +0000276 ../profile/InstrProfilingWriter)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000277 foreach (os ${ARGN})
Daniel Sandersbfcbe762016-01-27 09:28:01 +0000278 list_intersect(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH)
Chris Bieneman3ff8cb22015-09-28 22:20:25 +0000279 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bieneman82ebe892015-09-24 21:51:09 +0000280 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
281 OS ${os}
282 ARCH ${arch}
283 MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
284
Chris Bienemanb850f312015-09-28 17:38:44 +0000285 darwin_filter_builtin_sources(filtered_sources
286 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
287 ${${arch}_SOURCES})
Chris Bieneman66e02f02015-09-23 15:18:17 +0000288
Chris Bieneman765d76e2015-09-23 23:05:32 +0000289 darwin_add_builtin_library(clang_rt builtins
Chris Bieneman66e02f02015-09-23 15:18:17 +0000290 OS ${os}
291 ARCH ${arch}
Chris Bieneman25e1bdf2015-09-24 21:40:06 +0000292 SOURCES ${filtered_sources}
Chris Bieneman33dc9102015-11-11 21:38:35 +0000293 CFLAGS ${CFLAGS} -arch ${arch}
Chris Bieneman66e02f02015-09-23 15:18:17 +0000294 PARENT_TARGET builtins)
Chris Bieneman66e02f02015-09-23 15:18:17 +0000295 endforeach()
296
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000297 # Don't build cc_kext libraries for simulator platforms
Chris Bienemand4b23202015-09-30 20:25:10 +0000298 if(NOT DARWIN_${os}_SKIP_CC_KEXT)
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000299 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bienemanc959a0b2015-09-28 23:09:46 +0000300 # By not specifying MIN_VERSION this only reads the OS and OS-arch lists.
301 # We don't want to filter out the builtins that are present in libSystem
302 # because kexts can't link libSystem.
303 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
304 OS ${os}
305 ARCH ${arch})
306
307 darwin_filter_builtin_sources(filtered_sources
308 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
309 ${${arch}_SOURCES})
310
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000311 # In addition to the builtins cc_kext includes some profile sources
312 darwin_add_builtin_library(clang_rt cc_kext
313 OS ${os}
314 ARCH ${arch}
Chris Bienemanc959a0b2015-09-28 23:09:46 +0000315 SOURCES ${filtered_sources} ${PROFILE_SOURCES}
Chris Bieneman33dc9102015-11-11 21:38:35 +0000316 CFLAGS ${CFLAGS} -arch ${arch} -mkernel
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000317 DEFS KERNEL_USE
318 PARENT_TARGET builtins)
319 endforeach()
Chris Bienemanf0099182015-10-09 18:38:34 +0000320 set(archive_name clang_rt.cc_kext_${os})
321 if(${os} STREQUAL "osx")
322 set(archive_name clang_rt.cc_kext)
323 endif()
324 darwin_lipo_libs(${archive_name}
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000325 PARENT_TARGET builtins
326 LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
327 DEPENDS ${${os}_cc_kext_libs}
Chris Bieneman8a386812015-10-02 22:14:25 +0000328 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
329 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000330 endif()
331 endforeach()
332
333 # We put the x86 sim slices into the archives for their base OS
334 foreach (os ${ARGN})
335 if(NOT ${os} MATCHES ".*sim$")
336 darwin_lipo_libs(clang_rt.${os}
337 PARENT_TARGET builtins
338 LIPO_FLAGS ${${os}_builtins_lipo_flags} ${${os}sim_builtins_lipo_flags}
339 DEPENDS ${${os}_builtins_libs} ${${os}sim_builtins_libs}
Chris Bieneman8a386812015-10-02 22:14:25 +0000340 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
341 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Chris Bienemanf77c6a02015-09-28 22:18:31 +0000342 endif()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000343 endforeach()
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000344 darwin_add_embedded_builtin_libraries()
Chris Bieneman66e02f02015-09-23 15:18:17 +0000345endmacro()
346
Chris Bieneman65b6e052015-11-10 17:26:35 +0000347macro(darwin_add_embedded_builtin_libraries)
Chris Bieneman4ae803e2015-11-10 00:41:18 +0000348 # this is a hacky opt-out. If you can't target both intel and arm
349 # architectures we bail here.
350 set(DARWIN_SOFT_FLOAT_ARCHS armv6m armv7m armv7em armv7)
351 set(DARWIN_HARD_FLOAT_ARCHS armv7em armv7)
Chris Bieneman854e1db2015-11-20 18:45:42 +0000352 if(COMPILER_RT_SUPPORTED_ARCH MATCHES ".*armv.*")
353 list(FIND COMPILER_RT_SUPPORTED_ARCH i386 i386_idx)
354 if(i386_idx GREATER -1)
355 list(APPEND DARWIN_HARD_FLOAT_ARCHS i386)
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000356 endif()
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000357
Chris Bieneman854e1db2015-11-20 18:45:42 +0000358 list(FIND COMPILER_RT_SUPPORTED_ARCH x86_64 x86_64_idx)
359 if(x86_64_idx GREATER -1)
360 list(APPEND DARWIN_HARD_FLOAT_ARCHS x86_64)
361 endif()
362
363 set(MACHO_SYM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/macho_embedded)
364
365 set(CFLAGS "-Oz -Wall -fomit-frame-pointer -ffreestanding")
366 set(CMAKE_C_FLAGS "")
367 set(CMAKE_CXX_FLAGS "")
368 set(CMAKE_ASM_FLAGS "")
369
370 set(SOFT_FLOAT_FLAG -mfloat-abi=soft)
371 set(HARD_FLOAT_FLAG -mfloat-abi=hard)
372
373 set(ENABLE_PIC Off)
374 set(PIC_FLAG -fPIC)
375 set(STATIC_FLAG -static)
376
377 set(DARWIN_macho_embedded_ARCHS armv6m armv7m armv7em armv7 i386 x86_64)
378
379 set(DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR
380 ${COMPILER_RT_OUTPUT_DIR}/lib/macho_embedded)
381 set(DARWIN_macho_embedded_LIBRARY_INSTALL_DIR
382 ${COMPILER_RT_INSTALL_PATH}/lib/macho_embedded)
383
384 set(CFLAGS_armv7 "-target thumbv7-apple-darwin-eabi")
385 set(CFLAGS_i386 "-march=pentium")
386
387 darwin_read_list_from_file(common_FUNCTIONS ${MACHO_SYM_DIR}/common.txt)
388 darwin_read_list_from_file(thumb2_FUNCTIONS ${MACHO_SYM_DIR}/thumb2.txt)
389 darwin_read_list_from_file(thumb2_64_FUNCTIONS ${MACHO_SYM_DIR}/thumb2-64.txt)
390 darwin_read_list_from_file(arm_FUNCTIONS ${MACHO_SYM_DIR}/arm.txt)
391 darwin_read_list_from_file(i386_FUNCTIONS ${MACHO_SYM_DIR}/i386.txt)
392
393
394 set(armv6m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS})
395 set(armv7m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
396 set(armv7em_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
397 set(armv7_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS} ${thumb2_64_FUNCTIONS})
398 set(i386_FUNCTIONS ${common_FUNCTIONS} ${i386_FUNCTIONS})
399 set(x86_64_FUNCTIONS ${common_FUNCTIONS})
400
401 foreach(arch ${DARWIN_macho_embedded_ARCHS})
402 darwin_filter_builtin_sources(${arch}_filtered_sources
403 INCLUDE ${arch}_FUNCTIONS
404 ${${arch}_SOURCES})
405 if(NOT ${arch}_filtered_sources)
406 message("${arch}_SOURCES: ${${arch}_SOURCES}")
407 message("${arch}_FUNCTIONS: ${${arch}_FUNCTIONS}")
408 message(FATAL_ERROR "Empty filtered sources!")
409 endif()
Chris Bienemanbcc4ac92015-09-29 23:13:45 +0000410 endforeach()
Chris Bieneman854e1db2015-11-20 18:45:42 +0000411
412 foreach(float_type SOFT HARD)
413 foreach(type PIC STATIC)
414 string(TOLOWER "${float_type}_${type}" lib_suffix)
415 foreach(arch ${DARWIN_${float_type}_FLOAT_ARCHS})
416 set(DARWIN_macho_embedded_SYSROOT ${DARWIN_osx_SYSROOT})
417 set(float_flag)
418 if(${arch} MATCHES "^arm")
419 # x86 targets are hard float by default, but the complain about the
420 # float ABI flag, so don't pass it unless we're targeting arm.
421 set(float_flag ${${float_type}_FLOAT_FLAG})
422 endif()
423 darwin_add_builtin_library(clang_rt ${lib_suffix}
424 OS macho_embedded
425 ARCH ${arch}
426 SOURCES ${${arch}_filtered_sources}
427 CFLAGS ${CFLAGS} -arch ${arch} ${${type}_FLAG} ${float_flag} ${CFLAGS_${arch}}
428 PARENT_TARGET builtins)
429 endforeach()
430 foreach(lib ${macho_embedded_${lib_suffix}_libs})
431 set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C)
432 endforeach()
433 darwin_lipo_libs(clang_rt.${lib_suffix}
434 PARENT_TARGET builtins
435 LIPO_FLAGS ${macho_embedded_${lib_suffix}_lipo_flags}
436 DEPENDS ${macho_embedded_${lib_suffix}_libs}
437 OUTPUT_DIR ${DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR}
438 INSTALL_DIR ${DARWIN_macho_embedded_LIBRARY_INSTALL_DIR})
439 endforeach()
440 endforeach()
441 endif()
Chris Bieneman65b6e052015-11-10 17:26:35 +0000442endmacro()