blob: 18379080c0adf8bd90acde86a6826b681cfe1c3b [file] [log] [blame]
Chris Bieneman257fe3a2016-04-28 21:16:45 +00001include(CMakeParseArguments)
2
Chris Bieneman361231e2015-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)
Kuba Mracek93524342017-07-06 23:09:16 +00007 set(DARWIN_PREFER_PUBLIC_SDK OFF CACHE BOOL "Prefer Darwin public SDK, even when an internal SDK is present.")
8 if(NOT DARWIN_PREFER_PUBLIC_SDK)
9 # Let's first try the internal SDK, otherwise use the public SDK.
10 execute_process(
11 COMMAND xcodebuild -version -sdk ${sdk_name}.internal Path
12 RESULT_VARIABLE result_process
13 OUTPUT_VARIABLE var_internal
14 OUTPUT_STRIP_TRAILING_WHITESPACE
15 ERROR_FILE /dev/null
16 )
17 endif()
Chris Bieneman9b818ac2016-08-19 22:22:35 +000018 if((NOT result_process EQUAL 0) OR "" STREQUAL "${var_internal}")
Chris Bieneman361231e2015-08-13 20:38:16 +000019 execute_process(
20 COMMAND xcodebuild -version -sdk ${sdk_name} Path
Chris Bieneman9b818ac2016-08-19 22:22:35 +000021 RESULT_VARIABLE result_process
Chris Bieneman361231e2015-08-13 20:38:16 +000022 OUTPUT_VARIABLE var_internal
23 OUTPUT_STRIP_TRAILING_WHITESPACE
24 ERROR_FILE /dev/null
25 )
Chris Bieneman380ebd02016-04-26 17:53:25 +000026 else()
27 set(${var}_INTERNAL ${var_internal} PARENT_SCOPE)
Chris Bieneman361231e2015-08-13 20:38:16 +000028 endif()
Chris Bieneman9b818ac2016-08-19 22:22:35 +000029 if(result_process EQUAL 0)
30 set(${var} ${var_internal} PARENT_SCOPE)
31 endif()
Chris Bieneman361231e2015-08-13 20:38:16 +000032endfunction()
33
34# There isn't a clear mapping of what architectures are supported with a given
35# target platform, but ld's version output does list the architectures it can
36# link for.
37function(darwin_get_toolchain_supported_archs output_var)
38 execute_process(
39 COMMAND ld -v
40 ERROR_VARIABLE LINKER_VERSION)
41
42 string(REGEX MATCH "configured to support archs: ([^\n]+)"
43 ARCHES_MATCHED "${LINKER_VERSION}")
44 if(ARCHES_MATCHED)
45 set(ARCHES "${CMAKE_MATCH_1}")
Filipe Cabecinhas3e963672015-08-19 16:23:19 +000046 message(STATUS "Got ld supported ARCHES: ${ARCHES}")
Chris Bieneman361231e2015-08-13 20:38:16 +000047 string(REPLACE " " ";" ARCHES ${ARCHES})
48 else()
49 # If auto-detecting fails, fall back to a default set
50 message(WARNING "Detecting supported architectures from 'ld -v' failed. Returning default set.")
51 set(ARCHES "i386;x86_64;armv7;armv7s;arm64")
52 endif()
53
54 set(${output_var} ${ARCHES} PARENT_SCOPE)
55endfunction()
56
57# This function takes an OS and a list of architectures and identifies the
58# subset of the architectures list that the installed toolchain can target.
59function(darwin_test_archs os valid_archs)
Kuba Brecka3daf7b42015-12-03 17:02:07 +000060 if(${valid_archs})
61 message(STATUS "Using cached valid architectures for ${os}.")
62 return()
63 endif()
64
Chris Bieneman361231e2015-08-13 20:38:16 +000065 set(archs ${ARGN})
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000066 if(NOT TEST_COMPILE_ONLY)
67 message(STATUS "Finding valid architectures for ${os}...")
Chris Bieneman38adadf2016-06-22 23:02:49 +000068 set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c)
69 file(WRITE ${SIMPLE_C} "#include <stdio.h>\nint main() { printf(__FILE__); return 0; }\n")
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000070
71 set(os_linker_flags)
Francis Ricci17781c72017-01-10 04:33:04 +000072 foreach(flag ${DARWIN_${os}_LINK_FLAGS})
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000073 set(os_linker_flags "${os_linker_flags} ${flag}")
74 endforeach()
75 endif()
Chris Bieneman361231e2015-08-13 20:38:16 +000076
77 # The simple program will build for x86_64h on the simulator because it is
78 # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually
79 # a valid or useful architecture for the iOS simulator we should drop it.
Anna Zaksc77a0802016-02-02 02:01:17 +000080 if(${os} MATCHES "^(iossim|tvossim|watchossim)$")
Chris Bieneman361231e2015-08-13 20:38:16 +000081 list(REMOVE_ITEM archs "x86_64h")
82 endif()
83
84 set(working_archs)
85 foreach(arch ${archs})
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000086
Chris Bieneman361231e2015-08-13 20:38:16 +000087 set(arch_linker_flags "-arch ${arch} ${os_linker_flags}")
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000088 if(TEST_COMPILE_ONLY)
89 try_compile_only(CAN_TARGET_${os}_${arch} -v -arch ${arch} ${DARWIN_${os}_CFLAGS})
90 else()
Bob Haarmanccd6ae22017-03-21 18:25:35 +000091 set(SAVED_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
92 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${arch_linker_flags}")
Chris Bieneman38adadf2016-06-22 23:02:49 +000093 try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_C}
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000094 COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS}
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000095 OUTPUT_VARIABLE TEST_OUTPUT)
Bob Haarmanccd6ae22017-03-21 18:25:35 +000096 set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS})
Chris Bienemanaa3d2c12016-05-03 19:48:11 +000097 endif()
Chris Bieneman361231e2015-08-13 20:38:16 +000098 if(${CAN_TARGET_${os}_${arch}})
99 list(APPEND working_archs ${arch})
Chris Bienemana5841582015-12-10 00:39:57 +0000100 else()
101 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
102 "Testing compiler for supporting ${os}-${arch}:\n"
103 "${TEST_OUTPUT}\n")
Chris Bieneman361231e2015-08-13 20:38:16 +0000104 endif()
105 endforeach()
Kuba Brecka3daf7b42015-12-03 17:02:07 +0000106 set(${valid_archs} ${working_archs}
107 CACHE STRING "List of valid architectures for platform ${os}.")
Chris Bieneman361231e2015-08-13 20:38:16 +0000108endfunction()
Chris Bieneman5eae1972015-08-20 17:32:06 +0000109
110# This function checks the host cpusubtype to see if it is post-haswell. Haswell
111# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
112function(darwin_filter_host_archs input output)
Daniel Sandersde098c92016-01-27 09:28:01 +0000113 list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
Chris Bieneman5eae1972015-08-20 17:32:06 +0000114 execute_process(
115 COMMAND sysctl hw.cpusubtype
116 OUTPUT_VARIABLE SUBTYPE)
117
118 string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
119 SUBTYPE_MATCHED "${SUBTYPE}")
120 set(HASWELL_SUPPORTED Off)
Chris Bieneman797f5b32015-08-21 18:05:57 +0000121 if(SUBTYPE_MATCHED)
122 if(${CMAKE_MATCH_1} GREATER 7)
Chris Bieneman5eae1972015-08-20 17:32:06 +0000123 set(HASWELL_SUPPORTED On)
124 endif()
125 endif()
126 if(NOT HASWELL_SUPPORTED)
127 list(REMOVE_ITEM tmp_var x86_64h)
128 endif()
129 set(${output} ${tmp_var} PARENT_SCOPE)
130endfunction()
Chris Bieneman0d427982015-09-23 15:18:17 +0000131
Chris Bieneman0d427982015-09-23 15:18:17 +0000132# Read and process the exclude file into a list of symbols
Chris Bienemancc8c7772015-09-25 23:29:03 +0000133function(darwin_read_list_from_file output_var file)
134 if(EXISTS ${file})
135 file(READ ${file} EXCLUDES)
136 string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES})
137 set(${output_var} ${EXCLUDES} PARENT_SCOPE)
Chris Bieneman0d427982015-09-23 15:18:17 +0000138 endif()
139endfunction()
140
141# this function takes an OS, architecture and minimum version and provides a
142# list of builtin functions to exclude
Chris Bieneman590a48d2015-09-24 21:51:09 +0000143function(darwin_find_excluded_builtins_list output_var)
144 cmake_parse_arguments(LIB
145 ""
146 "OS;ARCH;MIN_VERSION"
147 ""
148 ${ARGN})
Chris Bieneman0d427982015-09-23 15:18:17 +0000149
Chris Bieneman590a48d2015-09-24 21:51:09 +0000150 if(NOT LIB_OS OR NOT LIB_ARCH)
151 message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!")
152 endif()
153
Chris Bienemancc8c7772015-09-25 23:29:03 +0000154 darwin_read_list_from_file(${LIB_OS}_BUILTINS
155 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt)
156 darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS
157 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt)
Chris Bieneman590a48d2015-09-24 21:51:09 +0000158
159 if(LIB_MIN_VERSION)
160 file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt)
161 foreach(builtin_list ${builtin_lists})
162 string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}")
163 if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION)
164 if(NOT smallest_version)
165 set(smallest_version ${CMAKE_MATCH_1})
166 elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version)
167 set(smallest_version ${CMAKE_MATCH_1})
168 endif()
Chris Bieneman0d427982015-09-23 15:18:17 +0000169 endif()
Chris Bieneman590a48d2015-09-24 21:51:09 +0000170 endforeach()
171
172 if(smallest_version)
Chris Bienemancc8c7772015-09-25 23:29:03 +0000173 darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS
174 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt)
Chris Bieneman0d427982015-09-23 15:18:17 +0000175 endif()
Chris Bieneman0d427982015-09-23 15:18:17 +0000176 endif()
177
Chris Bieneman590a48d2015-09-24 21:51:09 +0000178 set(${output_var}
179 ${${LIB_ARCH}_${LIB_OS}_BUILTINS}
180 ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS}
181 ${${LIB_OS}_BUILTINS} PARENT_SCOPE)
Chris Bieneman0d427982015-09-23 15:18:17 +0000182endfunction()
183
184# adds a single builtin library for a single OS & ARCH
Chris Bieneman01ebf002015-09-23 23:05:32 +0000185macro(darwin_add_builtin_library name suffix)
Chris Bieneman0d427982015-09-23 15:18:17 +0000186 cmake_parse_arguments(LIB
187 ""
188 "PARENT_TARGET;OS;ARCH"
Chris Bieneman01ebf002015-09-23 23:05:32 +0000189 "SOURCES;CFLAGS;DEFS"
Chris Bieneman0d427982015-09-23 15:18:17 +0000190 ${ARGN})
Chris Bieneman01ebf002015-09-23 23:05:32 +0000191 set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}")
Chris Bieneman0d427982015-09-23 15:18:17 +0000192 add_library(${libname} STATIC ${LIB_SOURCES})
Chris Bieneman45c40ff2015-11-10 17:26:40 +0000193 if(DARWIN_${LIB_OS}_SYSROOT)
194 set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT})
195 endif()
Chris Bieneman0d427982015-09-23 15:18:17 +0000196 set_target_compile_flags(${libname}
Chris Bieneman45c40ff2015-11-10 17:26:40 +0000197 ${sysroot_flag}
Chris Bieneman0d427982015-09-23 15:18:17 +0000198 ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG}
199 ${LIB_CFLAGS})
Chris Bieneman01ebf002015-09-23 23:05:32 +0000200 set_property(TARGET ${libname} APPEND PROPERTY
201 COMPILE_DEFINITIONS ${LIB_DEFS})
Chris Bieneman0d427982015-09-23 15:18:17 +0000202 set_target_properties(${libname} PROPERTIES
203 OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX})
204 set_target_properties(${libname} PROPERTIES
205 OSX_ARCHITECTURES ${LIB_ARCH})
206
207 if(LIB_PARENT_TARGET)
208 add_dependencies(${LIB_PARENT_TARGET} ${libname})
209 endif()
Chris Bieneman01ebf002015-09-23 23:05:32 +0000210
Chris Bieneman75c7c9c2015-09-25 22:31:17 +0000211 list(APPEND ${LIB_OS}_${suffix}_libs ${libname})
212 list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>)
Chris Bieneman01ebf002015-09-23 23:05:32 +0000213endmacro()
214
215function(darwin_lipo_libs name)
216 cmake_parse_arguments(LIB
217 ""
Chris Bienemanbe8b60b2015-10-02 22:14:25 +0000218 "PARENT_TARGET;OUTPUT_DIR;INSTALL_DIR"
Chris Bieneman01ebf002015-09-23 23:05:32 +0000219 "LIPO_FLAGS;DEPENDS"
220 ${ARGN})
Chris Bieneman7068fc32015-11-09 23:37:45 +0000221 if(LIB_DEPENDS AND LIB_LIPO_FLAGS)
Chris Bieneman7ccc6132015-11-09 23:07:45 +0000222 add_custom_command(OUTPUT ${LIB_OUTPUT_DIR}/lib${name}.a
223 COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR}
224 COMMAND lipo -output
225 ${LIB_OUTPUT_DIR}/lib${name}.a
226 -create ${LIB_LIPO_FLAGS}
227 DEPENDS ${LIB_DEPENDS}
228 )
229 add_custom_target(${name}
230 DEPENDS ${LIB_OUTPUT_DIR}/lib${name}.a)
231 add_dependencies(${LIB_PARENT_TARGET} ${name})
232 install(FILES ${LIB_OUTPUT_DIR}/lib${name}.a
233 DESTINATION ${LIB_INSTALL_DIR})
234 else()
235 message(WARNING "Not generating lipo target for ${name} because no input libraries exist.")
236 endif()
Chris Bieneman0d427982015-09-23 15:18:17 +0000237endfunction()
238
Chris Bieneman5fbaeb92015-09-24 21:40:06 +0000239# Filter out generic versions of routines that are re-implemented in
240# architecture specific manner. This prevents multiple definitions of the
241# same symbols, making the symbol selection non-deterministic.
Chris Bieneman8aba4b02015-09-28 17:38:44 +0000242function(darwin_filter_builtin_sources output_var exclude_or_include excluded_list)
243 if(exclude_or_include STREQUAL "EXCLUDE")
244 set(filter_action GREATER)
245 set(filter_value -1)
246 elseif(exclude_or_include STREQUAL "INCLUDE")
247 set(filter_action LESS)
248 set(filter_value 0)
249 else()
250 message(FATAL_ERROR "darwin_filter_builtin_sources called without EXCLUDE|INCLUDE")
251 endif()
252
Chris Bieneman5fbaeb92015-09-24 21:40:06 +0000253 set(intermediate ${ARGN})
254 foreach (_file ${intermediate})
255 get_filename_component(_name_we ${_file} NAME_WE)
256 list(FIND ${excluded_list} ${_name_we} _found)
Chris Bieneman8aba4b02015-09-28 17:38:44 +0000257 if(_found ${filter_action} ${filter_value})
Chris Bieneman5fbaeb92015-09-24 21:40:06 +0000258 list(REMOVE_ITEM intermediate ${_file})
Chris Bieneman472d9372015-11-06 23:19:29 +0000259 elseif(${_file} MATCHES ".*/.*\\.S" OR ${_file} MATCHES ".*/.*\\.c")
Chris Bieneman5fbaeb92015-09-24 21:40:06 +0000260 get_filename_component(_name ${_file} NAME)
261 string(REPLACE ".S" ".c" _cname "${_name}")
262 list(REMOVE_ITEM intermediate ${_cname})
263 endif ()
264 endforeach ()
265 set(${output_var} ${intermediate} PARENT_SCOPE)
266endfunction()
267
Chris Bieneman0d427982015-09-23 15:18:17 +0000268# Generates builtin libraries for all operating systems specified in ARGN. Each
269# OS library is constructed by lipo-ing together single-architecture libraries.
270macro(darwin_add_builtin_libraries)
Chris Bieneman6e18e1e2015-09-29 23:13:45 +0000271 set(DARWIN_EXCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Darwin-excludes)
272
Chris Bieneman14679e92015-11-12 22:37:03 +0000273 set(CFLAGS "-fPIC -O3 -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer")
Chris Bieneman66c126a2015-11-11 21:38:35 +0000274 set(CMAKE_C_FLAGS "")
275 set(CMAKE_CXX_FLAGS "")
276 set(CMAKE_ASM_FLAGS "")
Chris Bieneman7146f7b2015-09-24 22:29:58 +0000277
278 set(PROFILE_SOURCES ../profile/InstrProfiling
279 ../profile/InstrProfilingBuffer
Vedant Kumarc5b779c2016-01-07 22:54:46 +0000280 ../profile/InstrProfilingPlatformDarwin
Vedant Kumarb6262312016-01-08 00:07:50 +0000281 ../profile/InstrProfilingWriter)
Chris Bieneman0d427982015-09-23 15:18:17 +0000282 foreach (os ${ARGN})
Daniel Sandersde098c92016-01-27 09:28:01 +0000283 list_intersect(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH)
Chris Bieneman88d85342015-09-28 22:20:25 +0000284 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bieneman590a48d2015-09-24 21:51:09 +0000285 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
286 OS ${os}
287 ARCH ${arch}
288 MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})
289
Chris Bieneman8aba4b02015-09-28 17:38:44 +0000290 darwin_filter_builtin_sources(filtered_sources
291 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
292 ${${arch}_SOURCES})
Chris Bieneman0d427982015-09-23 15:18:17 +0000293
Chris Bieneman01ebf002015-09-23 23:05:32 +0000294 darwin_add_builtin_library(clang_rt builtins
Chris Bieneman0d427982015-09-23 15:18:17 +0000295 OS ${os}
296 ARCH ${arch}
Chris Bieneman5fbaeb92015-09-24 21:40:06 +0000297 SOURCES ${filtered_sources}
Chris Bieneman66c126a2015-11-11 21:38:35 +0000298 CFLAGS ${CFLAGS} -arch ${arch}
Chris Bieneman0d427982015-09-23 15:18:17 +0000299 PARENT_TARGET builtins)
Chris Bieneman0d427982015-09-23 15:18:17 +0000300 endforeach()
301
Chris Bieneman13414722015-09-28 22:18:31 +0000302 # Don't build cc_kext libraries for simulator platforms
Chris Bienemand8233022015-09-30 20:25:10 +0000303 if(NOT DARWIN_${os}_SKIP_CC_KEXT)
Chris Bieneman13414722015-09-28 22:18:31 +0000304 foreach (arch ${DARWIN_BUILTIN_ARCHS})
Chris Bieneman5def2a82015-09-28 23:09:46 +0000305 # By not specifying MIN_VERSION this only reads the OS and OS-arch lists.
306 # We don't want to filter out the builtins that are present in libSystem
307 # because kexts can't link libSystem.
308 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS
309 OS ${os}
310 ARCH ${arch})
311
312 darwin_filter_builtin_sources(filtered_sources
313 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
314 ${${arch}_SOURCES})
315
Chris Bieneman13414722015-09-28 22:18:31 +0000316 # In addition to the builtins cc_kext includes some profile sources
317 darwin_add_builtin_library(clang_rt cc_kext
318 OS ${os}
319 ARCH ${arch}
Chris Bieneman5def2a82015-09-28 23:09:46 +0000320 SOURCES ${filtered_sources} ${PROFILE_SOURCES}
Chris Bieneman66c126a2015-11-11 21:38:35 +0000321 CFLAGS ${CFLAGS} -arch ${arch} -mkernel
Chris Bieneman13414722015-09-28 22:18:31 +0000322 DEFS KERNEL_USE
323 PARENT_TARGET builtins)
324 endforeach()
Chris Bieneman2c23a802015-10-09 18:38:34 +0000325 set(archive_name clang_rt.cc_kext_${os})
326 if(${os} STREQUAL "osx")
327 set(archive_name clang_rt.cc_kext)
328 endif()
329 darwin_lipo_libs(${archive_name}
Chris Bieneman13414722015-09-28 22:18:31 +0000330 PARENT_TARGET builtins
331 LIPO_FLAGS ${${os}_cc_kext_lipo_flags}
332 DEPENDS ${${os}_cc_kext_libs}
Chris Bienemanbe8b60b2015-10-02 22:14:25 +0000333 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
334 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Chris Bieneman13414722015-09-28 22:18:31 +0000335 endif()
336 endforeach()
337
338 # We put the x86 sim slices into the archives for their base OS
339 foreach (os ${ARGN})
340 if(NOT ${os} MATCHES ".*sim$")
341 darwin_lipo_libs(clang_rt.${os}
342 PARENT_TARGET builtins
343 LIPO_FLAGS ${${os}_builtins_lipo_flags} ${${os}sim_builtins_lipo_flags}
344 DEPENDS ${${os}_builtins_libs} ${${os}sim_builtins_libs}
Chris Bienemanbe8b60b2015-10-02 22:14:25 +0000345 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
346 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR})
Chris Bieneman13414722015-09-28 22:18:31 +0000347 endif()
Chris Bieneman0d427982015-09-23 15:18:17 +0000348 endforeach()
Chris Bieneman6e18e1e2015-09-29 23:13:45 +0000349 darwin_add_embedded_builtin_libraries()
Chris Bieneman0d427982015-09-23 15:18:17 +0000350endmacro()
351
Chris Bieneman9a6afeb2015-11-10 17:26:35 +0000352macro(darwin_add_embedded_builtin_libraries)
Chris Bieneman7d824da2015-11-10 00:41:18 +0000353 # this is a hacky opt-out. If you can't target both intel and arm
354 # architectures we bail here.
355 set(DARWIN_SOFT_FLOAT_ARCHS armv6m armv7m armv7em armv7)
356 set(DARWIN_HARD_FLOAT_ARCHS armv7em armv7)
Chris Bieneman80854752015-11-20 18:45:42 +0000357 if(COMPILER_RT_SUPPORTED_ARCH MATCHES ".*armv.*")
358 list(FIND COMPILER_RT_SUPPORTED_ARCH i386 i386_idx)
359 if(i386_idx GREATER -1)
360 list(APPEND DARWIN_HARD_FLOAT_ARCHS i386)
Chris Bieneman6e18e1e2015-09-29 23:13:45 +0000361 endif()
Chris Bieneman6e18e1e2015-09-29 23:13:45 +0000362
Chris Bieneman80854752015-11-20 18:45:42 +0000363 list(FIND COMPILER_RT_SUPPORTED_ARCH x86_64 x86_64_idx)
364 if(x86_64_idx GREATER -1)
365 list(APPEND DARWIN_HARD_FLOAT_ARCHS x86_64)
366 endif()
367
368 set(MACHO_SYM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/macho_embedded)
369
370 set(CFLAGS "-Oz -Wall -fomit-frame-pointer -ffreestanding")
371 set(CMAKE_C_FLAGS "")
372 set(CMAKE_CXX_FLAGS "")
373 set(CMAKE_ASM_FLAGS "")
374
375 set(SOFT_FLOAT_FLAG -mfloat-abi=soft)
376 set(HARD_FLOAT_FLAG -mfloat-abi=hard)
377
378 set(ENABLE_PIC Off)
379 set(PIC_FLAG -fPIC)
380 set(STATIC_FLAG -static)
381
382 set(DARWIN_macho_embedded_ARCHS armv6m armv7m armv7em armv7 i386 x86_64)
383
384 set(DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR
385 ${COMPILER_RT_OUTPUT_DIR}/lib/macho_embedded)
386 set(DARWIN_macho_embedded_LIBRARY_INSTALL_DIR
387 ${COMPILER_RT_INSTALL_PATH}/lib/macho_embedded)
388
389 set(CFLAGS_armv7 "-target thumbv7-apple-darwin-eabi")
390 set(CFLAGS_i386 "-march=pentium")
391
392 darwin_read_list_from_file(common_FUNCTIONS ${MACHO_SYM_DIR}/common.txt)
393 darwin_read_list_from_file(thumb2_FUNCTIONS ${MACHO_SYM_DIR}/thumb2.txt)
394 darwin_read_list_from_file(thumb2_64_FUNCTIONS ${MACHO_SYM_DIR}/thumb2-64.txt)
395 darwin_read_list_from_file(arm_FUNCTIONS ${MACHO_SYM_DIR}/arm.txt)
396 darwin_read_list_from_file(i386_FUNCTIONS ${MACHO_SYM_DIR}/i386.txt)
397
398
399 set(armv6m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS})
400 set(armv7m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
401 set(armv7em_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS})
402 set(armv7_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS} ${thumb2_64_FUNCTIONS})
403 set(i386_FUNCTIONS ${common_FUNCTIONS} ${i386_FUNCTIONS})
404 set(x86_64_FUNCTIONS ${common_FUNCTIONS})
405
406 foreach(arch ${DARWIN_macho_embedded_ARCHS})
407 darwin_filter_builtin_sources(${arch}_filtered_sources
408 INCLUDE ${arch}_FUNCTIONS
409 ${${arch}_SOURCES})
410 if(NOT ${arch}_filtered_sources)
411 message("${arch}_SOURCES: ${${arch}_SOURCES}")
412 message("${arch}_FUNCTIONS: ${${arch}_FUNCTIONS}")
413 message(FATAL_ERROR "Empty filtered sources!")
414 endif()
Chris Bieneman6e18e1e2015-09-29 23:13:45 +0000415 endforeach()
Chris Bieneman80854752015-11-20 18:45:42 +0000416
417 foreach(float_type SOFT HARD)
418 foreach(type PIC STATIC)
419 string(TOLOWER "${float_type}_${type}" lib_suffix)
420 foreach(arch ${DARWIN_${float_type}_FLOAT_ARCHS})
421 set(DARWIN_macho_embedded_SYSROOT ${DARWIN_osx_SYSROOT})
422 set(float_flag)
423 if(${arch} MATCHES "^arm")
424 # x86 targets are hard float by default, but the complain about the
425 # float ABI flag, so don't pass it unless we're targeting arm.
426 set(float_flag ${${float_type}_FLOAT_FLAG})
427 endif()
428 darwin_add_builtin_library(clang_rt ${lib_suffix}
429 OS macho_embedded
430 ARCH ${arch}
431 SOURCES ${${arch}_filtered_sources}
432 CFLAGS ${CFLAGS} -arch ${arch} ${${type}_FLAG} ${float_flag} ${CFLAGS_${arch}}
433 PARENT_TARGET builtins)
434 endforeach()
435 foreach(lib ${macho_embedded_${lib_suffix}_libs})
436 set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C)
437 endforeach()
438 darwin_lipo_libs(clang_rt.${lib_suffix}
439 PARENT_TARGET builtins
440 LIPO_FLAGS ${macho_embedded_${lib_suffix}_lipo_flags}
441 DEPENDS ${macho_embedded_${lib_suffix}_libs}
442 OUTPUT_DIR ${DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR}
443 INSTALL_DIR ${DARWIN_macho_embedded_LIBRARY_INSTALL_DIR})
444 endforeach()
445 endforeach()
446 endif()
Chris Bieneman9a6afeb2015-11-10 17:26:35 +0000447endmacro()