Chris Bieneman | 19c8451 | 2015-08-13 20:38:16 +0000 | [diff] [blame] | 1 | # On OS X SDKs can be installed anywhere on the base system and xcode-select can |
| 2 | # set the default Xcode to use. This function finds the SDKs that are present in |
| 3 | # the current Xcode. |
| 4 | function(find_darwin_sdk_dir var sdk_name) |
| 5 | # Let's first try the internal SDK, otherwise use the public SDK. |
| 6 | execute_process( |
| 7 | COMMAND xcodebuild -version -sdk ${sdk_name}.internal Path |
| 8 | OUTPUT_VARIABLE var_internal |
| 9 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 10 | ERROR_FILE /dev/null |
| 11 | ) |
| 12 | if("" STREQUAL "${var_internal}") |
| 13 | execute_process( |
| 14 | COMMAND xcodebuild -version -sdk ${sdk_name} Path |
| 15 | OUTPUT_VARIABLE var_internal |
| 16 | OUTPUT_STRIP_TRAILING_WHITESPACE |
| 17 | ERROR_FILE /dev/null |
| 18 | ) |
| 19 | endif() |
| 20 | set(${var} ${var_internal} PARENT_SCOPE) |
| 21 | endfunction() |
| 22 | |
| 23 | # There isn't a clear mapping of what architectures are supported with a given |
| 24 | # target platform, but ld's version output does list the architectures it can |
| 25 | # link for. |
| 26 | function(darwin_get_toolchain_supported_archs output_var) |
| 27 | execute_process( |
| 28 | COMMAND ld -v |
| 29 | ERROR_VARIABLE LINKER_VERSION) |
| 30 | |
| 31 | string(REGEX MATCH "configured to support archs: ([^\n]+)" |
| 32 | ARCHES_MATCHED "${LINKER_VERSION}") |
| 33 | if(ARCHES_MATCHED) |
| 34 | set(ARCHES "${CMAKE_MATCH_1}") |
Filipe Cabecinhas | d5e075d | 2015-08-19 16:23:19 +0000 | [diff] [blame] | 35 | message(STATUS "Got ld supported ARCHES: ${ARCHES}") |
Chris Bieneman | 19c8451 | 2015-08-13 20:38:16 +0000 | [diff] [blame] | 36 | string(REPLACE " " ";" ARCHES ${ARCHES}) |
| 37 | else() |
| 38 | # If auto-detecting fails, fall back to a default set |
| 39 | message(WARNING "Detecting supported architectures from 'ld -v' failed. Returning default set.") |
| 40 | set(ARCHES "i386;x86_64;armv7;armv7s;arm64") |
| 41 | endif() |
| 42 | |
| 43 | set(${output_var} ${ARCHES} PARENT_SCOPE) |
| 44 | endfunction() |
| 45 | |
| 46 | # This function takes an OS and a list of architectures and identifies the |
| 47 | # subset of the architectures list that the installed toolchain can target. |
| 48 | function(darwin_test_archs os valid_archs) |
| 49 | set(archs ${ARGN}) |
| 50 | message(STATUS "Finding valid architectures for ${os}...") |
| 51 | set(SIMPLE_CPP ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/src.cpp) |
| 52 | file(WRITE ${SIMPLE_CPP} "#include <iostream>\nint main() { return 0; }\n") |
| 53 | |
| 54 | set(os_linker_flags) |
| 55 | foreach(flag ${DARWIN_${os}_LINKFLAGS}) |
| 56 | set(os_linker_flags "${os_linker_flags} ${flag}") |
| 57 | endforeach() |
| 58 | |
| 59 | # The simple program will build for x86_64h on the simulator because it is |
| 60 | # compatible with x86_64 libraries (mostly), but since x86_64h isn't actually |
| 61 | # a valid or useful architecture for the iOS simulator we should drop it. |
| 62 | if(${os} STREQUAL "iossim") |
| 63 | list(REMOVE_ITEM archs "x86_64h") |
| 64 | endif() |
| 65 | |
| 66 | set(working_archs) |
| 67 | foreach(arch ${archs}) |
| 68 | |
| 69 | set(arch_linker_flags "-arch ${arch} ${os_linker_flags}") |
| 70 | try_compile(CAN_TARGET_${os}_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_CPP} |
| 71 | COMPILE_DEFINITIONS "-v -arch ${arch}" ${DARWIN_${os}_CFLAGS} |
| 72 | CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${arch_linker_flags}" |
| 73 | OUTPUT_VARIABLE TEST_OUTPUT) |
| 74 | if(${CAN_TARGET_${os}_${arch}}) |
| 75 | list(APPEND working_archs ${arch}) |
| 76 | endif() |
| 77 | endforeach() |
| 78 | set(${valid_archs} ${working_archs} PARENT_SCOPE) |
| 79 | endfunction() |
Chris Bieneman | b060760 | 2015-08-20 17:32:06 +0000 | [diff] [blame] | 80 | |
| 81 | # This function checks the host cpusubtype to see if it is post-haswell. Haswell |
| 82 | # and later machines can run x86_64h binaries. Haswell is cpusubtype 8. |
| 83 | function(darwin_filter_host_archs input output) |
| 84 | list_union(tmp_var DARWIN_osx_ARCHS ${input}) |
| 85 | execute_process( |
| 86 | COMMAND sysctl hw.cpusubtype |
| 87 | OUTPUT_VARIABLE SUBTYPE) |
| 88 | |
| 89 | string(REGEX MATCH "hw.cpusubtype: ([0-9]*)" |
| 90 | SUBTYPE_MATCHED "${SUBTYPE}") |
| 91 | set(HASWELL_SUPPORTED Off) |
Chris Bieneman | 76d6891 | 2015-08-21 18:05:57 +0000 | [diff] [blame^] | 92 | if(SUBTYPE_MATCHED) |
| 93 | if(${CMAKE_MATCH_1} GREATER 7) |
Chris Bieneman | b060760 | 2015-08-20 17:32:06 +0000 | [diff] [blame] | 94 | set(HASWELL_SUPPORTED On) |
| 95 | endif() |
| 96 | endif() |
| 97 | if(NOT HASWELL_SUPPORTED) |
| 98 | list(REMOVE_ITEM tmp_var x86_64h) |
| 99 | endif() |
| 100 | set(${output} ${tmp_var} PARENT_SCOPE) |
| 101 | endfunction() |