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