1include(CMakeParseArguments) 2include(CompilerRTUtils) 3 4# On OS X SDKs can be installed anywhere on the base system and xcode-select can 5# set the default Xcode to use. This function finds the SDKs that are present in 6# the current Xcode. 7function(find_darwin_sdk_dir var sdk_name) 8 set(DARWIN_${sdk_name}_CACHED_SYSROOT "" CACHE STRING "Darwin SDK path for SDK ${sdk_name}.") 9 set(DARWIN_PREFER_PUBLIC_SDK OFF CACHE BOOL "Prefer Darwin public SDK, even when an internal SDK is present.") 10 11 if(DARWIN_${sdk_name}_CACHED_SYSROOT) 12 set(${var} ${DARWIN_${sdk_name}_CACHED_SYSROOT} PARENT_SCOPE) 13 return() 14 endif() 15 if(NOT DARWIN_PREFER_PUBLIC_SDK) 16 # Let's first try the internal SDK, otherwise use the public SDK. 17 execute_process( 18 COMMAND xcodebuild -version -sdk ${sdk_name}.internal Path 19 RESULT_VARIABLE result_process 20 OUTPUT_VARIABLE var_internal 21 OUTPUT_STRIP_TRAILING_WHITESPACE 22 ERROR_FILE /dev/null 23 ) 24 endif() 25 if((NOT result_process EQUAL 0) OR "" STREQUAL "${var_internal}") 26 execute_process( 27 COMMAND xcodebuild -version -sdk ${sdk_name} Path 28 RESULT_VARIABLE result_process 29 OUTPUT_VARIABLE var_internal 30 OUTPUT_STRIP_TRAILING_WHITESPACE 31 ERROR_FILE /dev/null 32 ) 33 else() 34 set(${var}_INTERNAL ${var_internal} PARENT_SCOPE) 35 endif() 36 if(result_process EQUAL 0) 37 set(${var} ${var_internal} PARENT_SCOPE) 38 endif() 39 set(DARWIN_${sdk_name}_CACHED_SYSROOT ${var_internal} CACHE STRING "Darwin SDK path for SDK ${sdk_name}." FORCE) 40endfunction() 41 42# There isn't a clear mapping of what architectures are supported with a given 43# target platform, but ld's version output does list the architectures it can 44# link for. 45function(darwin_get_toolchain_supported_archs output_var) 46 execute_process( 47 COMMAND "${CMAKE_LINKER}" -v 48 ERROR_VARIABLE LINKER_VERSION) 49 50 string(REGEX MATCH "configured to support archs: ([^\n]+)" 51 ARCHES_MATCHED "${LINKER_VERSION}") 52 if(ARCHES_MATCHED) 53 set(ARCHES "${CMAKE_MATCH_1}") 54 message(STATUS "Got ld supported ARCHES: ${ARCHES}") 55 string(REPLACE " " ";" ARCHES ${ARCHES}) 56 else() 57 # If auto-detecting fails, fall back to a default set 58 message(WARNING "Detecting supported architectures from 'ld -v' failed. Returning default set.") 59 set(ARCHES "i386;x86_64;armv7;armv7s;arm64") 60 endif() 61 62 set(${output_var} ${ARCHES} PARENT_SCOPE) 63endfunction() 64 65# This function takes an OS and a list of architectures and identifies the 66# subset of the architectures list that the installed toolchain can target. 67function(darwin_test_archs os valid_archs) 68 if(${valid_archs}) 69 message(STATUS "Using cached valid architectures for ${os}.") 70 return() 71 endif() 72 73 set(archs ${ARGN}) 74 if(NOT TEST_COMPILE_ONLY) 75 message(STATUS "Finding valid architectures for ${os}...") 76 set(SIMPLE_C ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.c) 77 file(WRITE ${SIMPLE_C} "#include <stdio.h>\nint main() { printf(__FILE__); return 0; }\n") 78 79 set(os_linker_flags) 80 foreach(flag ${DARWIN_${os}_LINK_FLAGS}) 81 set(os_linker_flags "${os_linker_flags} ${flag}") 82 endforeach() 83 endif() 84 85 # The simple program will build for x86_64h on the simulator because it is 86 # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually 87 # a valid or useful architecture for the iOS simulator we should drop it. 88 if(${os} MATCHES "^(iossim|tvossim|watchossim)$") 89 list(REMOVE_ITEM archs "x86_64h") 90 endif() 91 92 set(working_archs) 93 foreach(arch ${archs}) 94 95 set(arch_linker_flags "-arch ${arch} ${os_linker_flags}") 96 if(TEST_COMPILE_ONLY) 97 # `-w` is used to surpress compiler warnings which `try_compile_only()` treats as an error. 98 try_compile_only(CAN_TARGET_${os}_${arch} FLAGS -v -arch ${arch} ${DARWIN_${os}_CFLAGS} -w) 99 else() 100 set(SAVED_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}) 101 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${arch_linker_flags}") 102 try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_C} 103 COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS} 104 OUTPUT_VARIABLE TEST_OUTPUT) 105 set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS}) 106 endif() 107 if(${CAN_TARGET_${os}_${arch}}) 108 list(APPEND working_archs ${arch}) 109 else() 110 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 111 "Testing compiler for supporting ${os}-${arch}:\n" 112 "${TEST_OUTPUT}\n") 113 endif() 114 endforeach() 115 set(${valid_archs} ${working_archs} 116 CACHE STRING "List of valid architectures for platform ${os}." FORCE) 117endfunction() 118 119# This function checks the host cpusubtype to see if it is post-haswell. Haswell 120# and later machines can run x86_64h binaries. Haswell is cpusubtype 8. 121function(darwin_filter_host_archs input output) 122 list_intersect(tmp_var DARWIN_osx_ARCHS ${input}) 123 execute_process( 124 COMMAND sysctl hw.cpusubtype 125 OUTPUT_VARIABLE SUBTYPE) 126 127 string(REGEX MATCH "hw.cpusubtype: ([0-9]*)" 128 SUBTYPE_MATCHED "${SUBTYPE}") 129 set(HASWELL_SUPPORTED Off) 130 if(SUBTYPE_MATCHED) 131 if(${CMAKE_MATCH_1} GREATER 7) 132 set(HASWELL_SUPPORTED On) 133 endif() 134 endif() 135 if(NOT HASWELL_SUPPORTED) 136 list(REMOVE_ITEM tmp_var x86_64h) 137 endif() 138 set(${output} ${tmp_var} PARENT_SCOPE) 139endfunction() 140 141# Read and process the exclude file into a list of symbols 142function(darwin_read_list_from_file output_var file) 143 if(EXISTS ${file}) 144 file(READ ${file} EXCLUDES) 145 string(REPLACE "\n" ";" EXCLUDES ${EXCLUDES}) 146 set(${output_var} ${EXCLUDES} PARENT_SCOPE) 147 endif() 148endfunction() 149 150# this function takes an OS, architecture and minimum version and provides a 151# list of builtin functions to exclude 152function(darwin_find_excluded_builtins_list output_var) 153 cmake_parse_arguments(LIB 154 "" 155 "OS;ARCH;MIN_VERSION" 156 "" 157 ${ARGN}) 158 159 if(NOT LIB_OS OR NOT LIB_ARCH) 160 message(FATAL_ERROR "Must specify OS and ARCH to darwin_find_excluded_builtins_list!") 161 endif() 162 163 darwin_read_list_from_file(${LIB_OS}_BUILTINS 164 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}.txt) 165 darwin_read_list_from_file(${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS 166 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}-${LIB_ARCH}.txt) 167 168 if(LIB_MIN_VERSION) 169 file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${LIB_OS}*-${LIB_ARCH}.txt) 170 foreach(builtin_list ${builtin_lists}) 171 string(REGEX MATCH "${LIB_OS}([0-9\\.]*)-${LIB_ARCH}.txt" VERSION_MATCHED "${builtin_list}") 172 if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS LIB_MIN_VERSION) 173 if(NOT smallest_version) 174 set(smallest_version ${CMAKE_MATCH_1}) 175 elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version) 176 set(smallest_version ${CMAKE_MATCH_1}) 177 endif() 178 endif() 179 endforeach() 180 181 if(smallest_version) 182 darwin_read_list_from_file(${LIB_ARCH}_${LIB_OS}_BUILTINS 183 ${DARWIN_EXCLUDE_DIR}/${LIB_OS}${smallest_version}-${LIB_ARCH}.txt) 184 endif() 185 endif() 186 187 set(${output_var} 188 ${${LIB_ARCH}_${LIB_OS}_BUILTINS} 189 ${${LIB_OS}_${LIB_ARCH}_BASE_BUILTINS} 190 ${${LIB_OS}_BUILTINS} PARENT_SCOPE) 191endfunction() 192 193# adds a single builtin library for a single OS & ARCH 194macro(darwin_add_builtin_library name suffix) 195 cmake_parse_arguments(LIB 196 "" 197 "PARENT_TARGET;OS;ARCH" 198 "SOURCES;CFLAGS;DEFS" 199 ${ARGN}) 200 set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}") 201 add_library(${libname} STATIC ${LIB_SOURCES}) 202 if(DARWIN_${LIB_OS}_SYSROOT) 203 set(sysroot_flag -isysroot ${DARWIN_${LIB_OS}_SYSROOT}) 204 endif() 205 206 # Make a copy of the compilation flags. 207 set(builtin_cflags ${LIB_CFLAGS}) 208 209 # Strip out any inappropriate flags for the target. 210 if("${LIB_ARCH}" MATCHES "^(armv7|armv7k|armv7s)$") 211 set(builtin_cflags "") 212 foreach(cflag "${LIB_CFLAGS}") 213 string(REPLACE "-fomit-frame-pointer" "" cflag "${cflag}") 214 list(APPEND builtin_cflags ${cflag}) 215 endforeach(cflag) 216 endif() 217 218 set_target_compile_flags(${libname} 219 ${sysroot_flag} 220 ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG} 221 ${builtin_cflags}) 222 set_property(TARGET ${libname} APPEND PROPERTY 223 COMPILE_DEFINITIONS ${LIB_DEFS}) 224 set_target_properties(${libname} PROPERTIES 225 OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX}) 226 set_target_properties(${libname} PROPERTIES 227 OSX_ARCHITECTURES ${LIB_ARCH}) 228 229 if(LIB_PARENT_TARGET) 230 add_dependencies(${LIB_PARENT_TARGET} ${libname}) 231 endif() 232 233 list(APPEND ${LIB_OS}_${suffix}_libs ${libname}) 234 list(APPEND ${LIB_OS}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>) 235 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries") 236endmacro() 237 238function(darwin_lipo_libs name) 239 cmake_parse_arguments(LIB 240 "" 241 "PARENT_TARGET;OUTPUT_DIR;INSTALL_DIR" 242 "LIPO_FLAGS;DEPENDS" 243 ${ARGN}) 244 if(LIB_DEPENDS AND LIB_LIPO_FLAGS) 245 add_custom_command(OUTPUT ${LIB_OUTPUT_DIR}/lib${name}.a 246 COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR} 247 COMMAND lipo -output 248 ${LIB_OUTPUT_DIR}/lib${name}.a 249 -create ${LIB_LIPO_FLAGS} 250 DEPENDS ${LIB_DEPENDS} 251 ) 252 add_custom_target(${name} 253 DEPENDS ${LIB_OUTPUT_DIR}/lib${name}.a) 254 set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc") 255 add_dependencies(${LIB_PARENT_TARGET} ${name}) 256 257 if(CMAKE_CONFIGURATION_TYPES) 258 set(install_component ${LIB_PARENT_TARGET}) 259 else() 260 set(install_component ${name}) 261 endif() 262 install(FILES ${LIB_OUTPUT_DIR}/lib${name}.a 263 DESTINATION ${LIB_INSTALL_DIR} 264 COMPONENT ${install_component}) 265 add_compiler_rt_install_targets(${name} PARENT_TARGET ${LIB_PARENT_TARGET}) 266 else() 267 message(WARNING "Not generating lipo target for ${name} because no input libraries exist.") 268 endif() 269endfunction() 270 271# Generates builtin libraries for all operating systems specified in ARGN. Each 272# OS library is constructed by lipo-ing together single-architecture libraries. 273macro(darwin_add_builtin_libraries) 274 set(DARWIN_EXCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Darwin-excludes) 275 276 set(CFLAGS "-fPIC -O3 -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer") 277 set(CMAKE_C_FLAGS "") 278 set(CMAKE_CXX_FLAGS "") 279 set(CMAKE_ASM_FLAGS "") 280 281 set(PROFILE_SOURCES ../profile/InstrProfiling 282 ../profile/InstrProfilingBuffer 283 ../profile/InstrProfilingPlatformDarwin 284 ../profile/InstrProfilingWriter) 285 foreach (os ${ARGN}) 286 list_intersect(DARWIN_BUILTIN_ARCHS DARWIN_${os}_BUILTIN_ARCHS BUILTIN_SUPPORTED_ARCH) 287 foreach (arch ${DARWIN_BUILTIN_ARCHS}) 288 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS 289 OS ${os} 290 ARCH ${arch} 291 MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER}) 292 293 filter_builtin_sources(filtered_sources 294 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS 295 ${${arch}_SOURCES}) 296 297 darwin_add_builtin_library(clang_rt builtins 298 OS ${os} 299 ARCH ${arch} 300 SOURCES ${filtered_sources} 301 CFLAGS ${CFLAGS} -arch ${arch} 302 PARENT_TARGET builtins) 303 endforeach() 304 305 # Don't build cc_kext libraries for simulator platforms 306 if(NOT DARWIN_${os}_SKIP_CC_KEXT) 307 foreach (arch ${DARWIN_BUILTIN_ARCHS}) 308 # By not specifying MIN_VERSION this only reads the OS and OS-arch lists. 309 # We don't want to filter out the builtins that are present in libSystem 310 # because kexts can't link libSystem. 311 darwin_find_excluded_builtins_list(${arch}_${os}_EXCLUDED_BUILTINS 312 OS ${os} 313 ARCH ${arch}) 314 315 filter_builtin_sources(filtered_sources 316 EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS 317 ${${arch}_SOURCES}) 318 319 # In addition to the builtins cc_kext includes some profile sources 320 darwin_add_builtin_library(clang_rt cc_kext 321 OS ${os} 322 ARCH ${arch} 323 SOURCES ${filtered_sources} ${PROFILE_SOURCES} 324 CFLAGS ${CFLAGS} -arch ${arch} -mkernel 325 DEFS KERNEL_USE 326 PARENT_TARGET builtins) 327 endforeach() 328 set(archive_name clang_rt.cc_kext_${os}) 329 if(${os} STREQUAL "osx") 330 set(archive_name clang_rt.cc_kext) 331 endif() 332 darwin_lipo_libs(${archive_name} 333 PARENT_TARGET builtins 334 LIPO_FLAGS ${${os}_cc_kext_lipo_flags} 335 DEPENDS ${${os}_cc_kext_libs} 336 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR} 337 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 338 endif() 339 endforeach() 340 341 # We put the x86 sim slices into the archives for their base OS 342 foreach (os ${ARGN}) 343 if(NOT ${os} MATCHES ".*sim$") 344 darwin_lipo_libs(clang_rt.${os} 345 PARENT_TARGET builtins 346 LIPO_FLAGS ${${os}_builtins_lipo_flags} ${${os}sim_builtins_lipo_flags} 347 DEPENDS ${${os}_builtins_libs} ${${os}sim_builtins_libs} 348 OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR} 349 INSTALL_DIR ${COMPILER_RT_LIBRARY_INSTALL_DIR}) 350 endif() 351 endforeach() 352 darwin_add_embedded_builtin_libraries() 353endmacro() 354 355macro(darwin_add_embedded_builtin_libraries) 356 # this is a hacky opt-out. If you can't target both intel and arm 357 # architectures we bail here. 358 set(DARWIN_SOFT_FLOAT_ARCHS armv6m armv7m armv7em armv7) 359 set(DARWIN_HARD_FLOAT_ARCHS armv7em armv7) 360 if(COMPILER_RT_SUPPORTED_ARCH MATCHES ".*armv.*") 361 list(FIND COMPILER_RT_SUPPORTED_ARCH i386 i386_idx) 362 if(i386_idx GREATER -1) 363 list(APPEND DARWIN_HARD_FLOAT_ARCHS i386) 364 endif() 365 366 list(FIND COMPILER_RT_SUPPORTED_ARCH x86_64 x86_64_idx) 367 if(x86_64_idx GREATER -1) 368 list(APPEND DARWIN_HARD_FLOAT_ARCHS x86_64) 369 endif() 370 371 set(MACHO_SYM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/macho_embedded) 372 373 set(CFLAGS "-Oz -Wall -fomit-frame-pointer -ffreestanding") 374 set(CMAKE_C_FLAGS "") 375 set(CMAKE_CXX_FLAGS "") 376 set(CMAKE_ASM_FLAGS "") 377 378 set(SOFT_FLOAT_FLAG -mfloat-abi=soft) 379 set(HARD_FLOAT_FLAG -mfloat-abi=hard) 380 381 set(ENABLE_PIC Off) 382 set(PIC_FLAG -fPIC) 383 set(STATIC_FLAG -static) 384 385 set(DARWIN_macho_embedded_ARCHS armv6m armv7m armv7em armv7 i386 x86_64) 386 387 set(DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR 388 ${COMPILER_RT_OUTPUT_DIR}/lib/macho_embedded) 389 set(DARWIN_macho_embedded_LIBRARY_INSTALL_DIR 390 ${COMPILER_RT_INSTALL_PATH}/lib/macho_embedded) 391 392 set(CFLAGS_armv7 "-target thumbv7-apple-darwin-eabi") 393 set(CFLAGS_i386 "-march=pentium") 394 395 darwin_read_list_from_file(common_FUNCTIONS ${MACHO_SYM_DIR}/common.txt) 396 darwin_read_list_from_file(thumb2_FUNCTIONS ${MACHO_SYM_DIR}/thumb2.txt) 397 darwin_read_list_from_file(thumb2_64_FUNCTIONS ${MACHO_SYM_DIR}/thumb2-64.txt) 398 darwin_read_list_from_file(arm_FUNCTIONS ${MACHO_SYM_DIR}/arm.txt) 399 darwin_read_list_from_file(i386_FUNCTIONS ${MACHO_SYM_DIR}/i386.txt) 400 401 402 set(armv6m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS}) 403 set(armv7m_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS}) 404 set(armv7em_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS}) 405 set(armv7_FUNCTIONS ${common_FUNCTIONS} ${arm_FUNCTIONS} ${thumb2_FUNCTIONS} ${thumb2_64_FUNCTIONS}) 406 set(i386_FUNCTIONS ${common_FUNCTIONS} ${i386_FUNCTIONS}) 407 set(x86_64_FUNCTIONS ${common_FUNCTIONS}) 408 409 foreach(arch ${DARWIN_macho_embedded_ARCHS}) 410 filter_builtin_sources(${arch}_filtered_sources 411 INCLUDE ${arch}_FUNCTIONS 412 ${${arch}_SOURCES}) 413 if(NOT ${arch}_filtered_sources) 414 message(WARNING "${arch}_SOURCES: ${${arch}_SOURCES}") 415 message(WARNING "${arch}_FUNCTIONS: ${${arch}_FUNCTIONS}") 416 message(FATAL_ERROR "Empty filtered sources!") 417 endif() 418 endforeach() 419 420 foreach(float_type SOFT HARD) 421 foreach(type PIC STATIC) 422 string(TOLOWER "${float_type}_${type}" lib_suffix) 423 foreach(arch ${DARWIN_${float_type}_FLOAT_ARCHS}) 424 set(DARWIN_macho_embedded_SYSROOT ${DARWIN_osx_SYSROOT}) 425 set(float_flag) 426 if(${arch} MATCHES "^arm") 427 # x86 targets are hard float by default, but the complain about the 428 # float ABI flag, so don't pass it unless we're targeting arm. 429 set(float_flag ${${float_type}_FLOAT_FLAG}) 430 endif() 431 darwin_add_builtin_library(clang_rt ${lib_suffix} 432 OS macho_embedded 433 ARCH ${arch} 434 SOURCES ${${arch}_filtered_sources} 435 CFLAGS ${CFLAGS} -arch ${arch} ${${type}_FLAG} ${float_flag} ${CFLAGS_${arch}} 436 PARENT_TARGET builtins) 437 endforeach() 438 foreach(lib ${macho_embedded_${lib_suffix}_libs}) 439 set_target_properties(${lib} PROPERTIES LINKER_LANGUAGE C) 440 endforeach() 441 darwin_lipo_libs(clang_rt.${lib_suffix} 442 PARENT_TARGET builtins 443 LIPO_FLAGS ${macho_embedded_${lib_suffix}_lipo_flags} 444 DEPENDS ${macho_embedded_${lib_suffix}_libs} 445 OUTPUT_DIR ${DARWIN_macho_embedded_LIBRARY_OUTPUT_DIR} 446 INSTALL_DIR ${DARWIN_macho_embedded_LIBRARY_INSTALL_DIR}) 447 endforeach() 448 endforeach() 449 endif() 450endmacro() 451