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) |
Chris Bieneman | d462679 | 2015-09-16 18:29:52 +0000 | [diff] [blame] | 52 | file(WRITE ${SIMPLE_CPP} "#include <iostream>\nint main() { std::cout << std::endl; return 0; }\n") |
Chris Bieneman | 19c8451 | 2015-08-13 20:38:16 +0000 | [diff] [blame] | 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() |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 102 | |
| 103 | set(DARWIN_EXCLUDE_DIR ${CMAKE_SOURCE_DIR}/lib/builtins/Darwin-excludes) |
| 104 | |
| 105 | # Read and process the exclude file into a list of symbols |
| 106 | function(darwin_read_exclude_file output_var file) |
| 107 | if(EXISTS ${DARWIN_EXCLUDE_DIR}/${file}.txt) |
| 108 | file(READ ${DARWIN_EXCLUDE_DIR}/${file}.txt ${file}_EXCLUDES) |
| 109 | string(REPLACE "\n" ";" ${file}_EXCLUDES ${${file}_EXCLUDES}) |
| 110 | set(${output_var} ${${file}_EXCLUDES} PARENT_SCOPE) |
| 111 | endif() |
| 112 | endfunction() |
| 113 | |
| 114 | # this function takes an OS, architecture and minimum version and provides a |
| 115 | # list of builtin functions to exclude |
| 116 | function(darwin_find_excluded_builtins_list os arch min_version) |
| 117 | darwin_read_exclude_file(${os}_BUILTINS ${os}) |
| 118 | darwin_read_exclude_file(${os}_${arch}_BASE_BUILTINS ${os}-${arch}) |
| 119 | |
| 120 | file(GLOB builtin_lists ${DARWIN_EXCLUDE_DIR}/${os}*-${arch}.txt) |
| 121 | foreach(builtin_list ${builtin_lists}) |
| 122 | string(REGEX MATCH "${os}([0-9\\.]*)-${arch}.txt" VERSION_MATCHED "${builtin_list}") |
| 123 | if (VERSION_MATCHED AND NOT CMAKE_MATCH_1 VERSION_LESS min_version) |
| 124 | if(NOT smallest_version) |
| 125 | set(smallest_version ${CMAKE_MATCH_1}) |
| 126 | elseif(CMAKE_MATCH_1 VERSION_LESS smallest_version) |
| 127 | set(smallest_version ${CMAKE_MATCH_1}) |
| 128 | endif() |
| 129 | endif() |
| 130 | endforeach() |
| 131 | |
| 132 | if(smallest_version) |
| 133 | darwin_read_exclude_file(${arch}_${os}_BUILTINS ${os}${smallest_version}-${arch}) |
| 134 | endif() |
| 135 | |
| 136 | set(${arch}_${os}_EXCLUDED_BUILTINS |
| 137 | ${${arch}_${os}_BUILTINS} |
| 138 | ${${os}_${arch}_BASE_BUILTINS} |
| 139 | ${${os}_BUILTINS} PARENT_SCOPE) |
| 140 | endfunction() |
| 141 | |
| 142 | # adds a single builtin library for a single OS & ARCH |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 143 | macro(darwin_add_builtin_library name suffix) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 144 | cmake_parse_arguments(LIB |
| 145 | "" |
| 146 | "PARENT_TARGET;OS;ARCH" |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 147 | "SOURCES;CFLAGS;DEFS" |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 148 | ${ARGN}) |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 149 | set(libname "${name}.${suffix}_${LIB_ARCH}_${LIB_OS}") |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 150 | add_library(${libname} STATIC ${LIB_SOURCES}) |
| 151 | set_target_compile_flags(${libname} |
| 152 | -isysroot ${DARWIN_${LIB_OS}_SYSROOT} |
| 153 | ${DARWIN_${LIB_OS}_BUILTIN_MIN_VER_FLAG} |
| 154 | ${LIB_CFLAGS}) |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 155 | set_property(TARGET ${libname} APPEND PROPERTY |
| 156 | COMPILE_DEFINITIONS ${LIB_DEFS}) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 157 | set_target_properties(${libname} PROPERTIES |
| 158 | OUTPUT_NAME ${libname}${COMPILER_RT_OS_SUFFIX}) |
| 159 | set_target_properties(${libname} PROPERTIES |
| 160 | OSX_ARCHITECTURES ${LIB_ARCH}) |
| 161 | |
| 162 | if(LIB_PARENT_TARGET) |
| 163 | add_dependencies(${LIB_PARENT_TARGET} ${libname}) |
| 164 | endif() |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 165 | |
| 166 | list(APPEND ${os}_${suffix}_libs ${libname}) |
| 167 | list(APPEND ${os}_${suffix}_lipo_flags -arch ${arch} $<TARGET_FILE:${libname}>) |
| 168 | endmacro() |
| 169 | |
| 170 | function(darwin_lipo_libs name) |
| 171 | cmake_parse_arguments(LIB |
| 172 | "" |
| 173 | "PARENT_TARGET" |
| 174 | "LIPO_FLAGS;DEPENDS" |
| 175 | ${ARGN}) |
| 176 | add_custom_command(OUTPUT ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a |
| 177 | COMMAND lipo -output |
| 178 | ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a |
| 179 | -create ${LIB_LIPO_FLAGS} |
| 180 | DEPENDS ${LIB_DEPENDS} |
| 181 | ) |
| 182 | add_custom_target(${name} |
| 183 | DEPENDS ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/lib${name}.a) |
| 184 | add_dependencies(${LIB_PARENT_TARGET} ${name}) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 185 | endfunction() |
| 186 | |
| 187 | # Generates builtin libraries for all operating systems specified in ARGN. Each |
| 188 | # OS library is constructed by lipo-ing together single-architecture libraries. |
| 189 | macro(darwin_add_builtin_libraries) |
Chris Bieneman | a6e0202 | 2015-09-24 18:26:34 +0000 | [diff] [blame^] | 190 | if(CMAKE_CONFIGURATION_TYPES) |
| 191 | foreach(type ${CMAKE_CONFIGURATION_TYPES}) |
| 192 | set(CMAKE_C_FLAGS_${type} -O3) |
| 193 | set(CMAKE_CXX_FLAGS_${type} -O3) |
| 194 | endforeach() |
| 195 | else() |
| 196 | set(CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE} -O3) |
| 197 | endif() |
| 198 | |
| 199 | set(CMAKE_C_FLAGS "-fPIC -fvisibility=hidden -DVISIBILITY_HIDDEN -Wall -fomit-frame-pointer") |
| 200 | set(CMAKE_CXX_FLAGS ${CMAKE_C_FLAGS}) |
| 201 | set(CMAKE_ASM_FLAGS ${CMAKE_C_FLAGS}) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 202 | foreach (os ${ARGN}) |
| 203 | list_union(DARWIN_BUILTIN_ARCHS DARWIN_${os}_ARCHS BUILTIN_SUPPORTED_ARCH) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 204 | foreach (arch ${DARWIN_BUILTIN_ARCHS}) |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 205 | # do cc_kext |
| 206 | darwin_add_builtin_library(clang_rt cc_kext |
| 207 | OS ${os} |
| 208 | ARCH ${arch} |
| 209 | SOURCES ${${arch}_SOURCES} |
Chris Bieneman | a6e0202 | 2015-09-24 18:26:34 +0000 | [diff] [blame^] | 210 | CFLAGS -arch ${arch} -mkernel |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 211 | DEFS KERNEL_USE |
| 212 | PARENT_TARGET builtins) |
| 213 | |
| 214 | |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 215 | darwin_find_excluded_builtins_list(${os} ${arch} ${DARWIN_${os}_BUILTIN_MIN_VER}) |
| 216 | # Filter out generic versions of routines that are re-implemented in |
| 217 | # architecture specific manner. This prevents multiple definitions of the |
| 218 | # same symbols, making the symbol selection non-deterministic. |
| 219 | foreach (_file ${${arch}_SOURCES}) |
| 220 | get_filename_component(_name_we ${_file} NAME_WE) |
| 221 | list(FIND ${arch}_${os}_EXCLUDED_BUILTINS ${_name_we} _found) |
| 222 | if(_found GREATER -1) |
| 223 | list(REMOVE_ITEM ${arch}_SOURCES ${_file}) |
| 224 | elseif(${_file} MATCHES ${arch}/*) |
| 225 | get_filename_component(_name ${_file} NAME) |
| 226 | string(REPLACE ".S" ".c" _cname "${_name}") |
| 227 | list(REMOVE_ITEM ${arch}_SOURCES ${_cname}) |
| 228 | endif () |
| 229 | endforeach () |
| 230 | |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 231 | darwin_add_builtin_library(clang_rt builtins |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 232 | OS ${os} |
| 233 | ARCH ${arch} |
| 234 | SOURCES ${${arch}_SOURCES} |
Chris Bieneman | a6e0202 | 2015-09-24 18:26:34 +0000 | [diff] [blame^] | 235 | CFLAGS -arch ${arch} |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 236 | PARENT_TARGET builtins) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 237 | endforeach() |
| 238 | |
Chris Bieneman | 765d76e | 2015-09-23 23:05:32 +0000 | [diff] [blame] | 239 | darwin_lipo_libs(clang_rt.cc_kext_${os} |
| 240 | PARENT_TARGET builtins |
| 241 | LIPO_FLAGS ${${os}_cc_kext_lipo_flags} |
| 242 | DEPENDS ${${os}_cc_kext_libs}) |
| 243 | darwin_lipo_libs(clang_rt.${os} |
| 244 | PARENT_TARGET builtins |
| 245 | LIPO_FLAGS ${${os}_builtins_lipo_flags} |
| 246 | DEPENDS ${${os}_builtins_libs}) |
Chris Bieneman | 66e02f0 | 2015-09-23 15:18:17 +0000 | [diff] [blame] | 247 | endforeach() |
| 248 | endmacro() |
| 249 | |