|
| 1 | +#[=======================================================================[.rst: |
| 2 | +FindSwiftCxxOverlay |
| 3 | +------------ |
| 4 | +
|
| 5 | +Find SwiftCxxOverlay, deferring to the associated SwiftCxxOverlayConfig.cmake when requested. |
| 6 | +This is meant to find the Cxx overlays to be linked by the Supplemental libraries. |
| 7 | +
|
| 8 | +Imported Targets |
| 9 | +^^^^^^^^^^^^^^^^ |
| 10 | +
|
| 11 | +The following :prop_tgt:`IMPORTED` TARGETS may be defined: |
| 12 | +
|
| 13 | + ``swiftCxx`` |
| 14 | + ``swiftCxxStdlib`` |
| 15 | + ``cxxshims`` (except on Apple platforms) |
| 16 | + ``libstdcxx`` (if libstdc++ is present) |
| 17 | +
|
| 18 | +Hint Variables |
| 19 | +^^^^^^^^^^^^^^ |
| 20 | +
|
| 21 | + ``SDKROOT`` (environment variable) |
| 22 | + Set the path to the Swift SDK Root. |
| 23 | + This only affects Windows and Android builds. |
| 24 | +
|
| 25 | + ``Swift_SDKROOT`` |
| 26 | + Set the path to the Swift SDK installation. |
| 27 | + This affects Linux, Android, and Windows builds. |
| 28 | + Apple builds always use the overlay provided by the SDK. |
| 29 | +
|
| 30 | +#]=======================================================================] |
| 31 | + |
| 32 | +include_guard(GLOBAL) |
| 33 | + |
| 34 | +if(SwiftCxxOverlay_DIR) |
| 35 | + if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED) |
| 36 | + list(APPEND args REQUIRED) |
| 37 | + endif() |
| 38 | + if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY) |
| 39 | + list(APPEND args QUIET) |
| 40 | + endif() |
| 41 | + find_package(SwiftCxxOverlay CONFIG ${args}) |
| 42 | + return() |
| 43 | +endif() |
| 44 | + |
| 45 | +include(FindPackageHandleStandardArgs) |
| 46 | +include(PlatformInfo) |
| 47 | +include(CheckSymbolExists) |
| 48 | + |
| 49 | +if(APPLE) |
| 50 | + # Cxx Interop is not installed in the SDKs, but in the |
| 51 | + # toolchain next to the compiler |
| 52 | + set(target_info_command "${CMAKE_Swift_COMPILER}" -print-target-info) |
| 53 | + if(CMAKE_Swift_COMPILER_TARGET) |
| 54 | + list(APPEND target_info_command -target ${CMAKE_Swift_COMPILER_TARGET}) |
| 55 | + endif() |
| 56 | + execute_process(COMMAND ${target_info_command} OUTPUT_VARIABLE target_info_json) |
| 57 | + message(CONFIGURE_LOG "Swift target info: ${target_info_command}\n" |
| 58 | + "${target_info_json}") |
| 59 | + |
| 60 | + string(JSON runtime_library_import_paths_json GET "${target_info_json}" "paths" "runtimeLibraryImportPaths") |
| 61 | + message(CONFIGURE_LOG "runtime_library_import_paths_json ${runtime_library_import_paths_json}") |
| 62 | + |
| 63 | + string(JSON number_of_runtime_library_import_paths LENGTH "${runtime_library_import_paths_json}") |
| 64 | + math(EXPR index_of_last_runtime_library_import_path "${number_of_runtime_library_import_paths} - 1") |
| 65 | + foreach(index RANGE 0 ${index_of_last_runtime_library_import_path}) |
| 66 | + string(JSON runtime_library_import_path GET "${runtime_library_import_paths_json}" ${index}) |
| 67 | + |
| 68 | + list(APPEND SwiftCxxOverlay_INCLUDE_DIR_HINTS |
| 69 | + "${runtime_library_import_path}") |
| 70 | + list(APPEND SwiftCxxOverlay_LIBRARY_HINTS |
| 71 | + "${runtime_library_import_path}") |
| 72 | + endforeach() |
| 73 | + |
| 74 | + list(APPEND swiftCxx_NAMES libswiftCxx.a) |
| 75 | + list(APPEND swiftCxxStdlib_NAMES libswiftCxxStdlib.a) |
| 76 | +elseif(LINUX) |
| 77 | + list(APPEND SwiftCxxOverlay_INCLUDE_DIR_HINTS |
| 78 | + "${Swift_SDKROOT}/usr/lib/swift/linux/") |
| 79 | + list(APPEND SwiftCxxOverlay_LIBRARY_HINTS |
| 80 | + "${Swift_SDKROOT}/usr/lib/swift/linux") |
| 81 | + list(APPEND swiftCxx_NAMES libswiftCxx.a) |
| 82 | + list(APPEND swiftCxxStdlib_NAMES libswiftCxxStdlib.a) |
| 83 | +elseif(WIN32) |
| 84 | + list(APPEND SwiftCxxOverlay_INCLUDE_DIR_HINTS |
| 85 | + "${Swift_SDKROOT}/usr/lib/swift/windows" |
| 86 | + "$ENV{SDKROOT}/usr/lib/swift/windows") |
| 87 | + list(APPEND SwiftCxxOverlay_LIBRARY_HINTS |
| 88 | + "${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 89 | + "${Swift_SDKROOT}/usr/lib/swift" |
| 90 | + "$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 91 | + "$ENV{SDKROOT}/usr/lib/swift") |
| 92 | + |
| 93 | + list(APPEND swiftCxx_NAMES libswiftCxx.lib) |
| 94 | + list(APPEND swiftCxxStdlib_NAMES libswiftCxxStdlib.lib) |
| 95 | +elseif(ANDROID) |
| 96 | + list(APPEND SwiftCxxOverlay_INCLUDE_DIR_HINTS |
| 97 | + "${Swift_SDKROOT}/usr/lib/swift/android" |
| 98 | + "$ENV{SDKROOT}/usr/lib/swift/android") |
| 99 | + list(APPEND SwiftCxxOverlay_LIBRARY_HINTS |
| 100 | + "${Swift_SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 101 | + "${Swift_SDKROOT}/usr/lib/swift" |
| 102 | + "$ENV{SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 103 | + "$ENV{SDKROOT}/usr/lib/swift") |
| 104 | + list(APPEND swiftCxx_NAMES libswiftCxx.a) |
| 105 | + list(APPEND swiftCxxStdlib_NAMES libswiftCxxStdlib.a) |
| 106 | +else() |
| 107 | + message(FATAL_ERROR "FindSwiftCxxOverlay.cmake module search not implemented for targeted platform\n" |
| 108 | + " Build the Overlays for your platform and set the appropriate `SwiftCxxOverlay_DIR` variable to" |
| 109 | + " the directory containing SwiftCxxOverlayConfig.cmake\n") |
| 110 | +endif() |
| 111 | + |
| 112 | +set(swiftCxx_MODULE_NAME "Cxx.swiftmodule") |
| 113 | +set(swiftCxxStdlib_MODULE_NAME "CxxStdlib.swiftmodule") |
| 114 | +if(NOT APPLE) |
| 115 | + set(cxxshims_MODULE_NAME libcxxshim.modulemap) |
| 116 | +endif() |
| 117 | +check_symbol_exists(__GLIBCXX__ "version" HAVE___GLIBCXX__) |
| 118 | +if(HAVE___GLIBCXX__) |
| 119 | + set(libstdcxx_MODULE_NAME libstdcxx.modulemap) |
| 120 | +endif() |
| 121 | + |
| 122 | +find_path(swiftCxx_INCLUDE_DIR |
| 123 | + ${swiftCxx_MODULE_NAME} |
| 124 | + NO_CMAKE_FIND_ROOT_PATH |
| 125 | + HINTS |
| 126 | + ${SwiftCxxOverlay_INCLUDE_DIR_HINTS}) |
| 127 | +find_library(swiftCxx_LIBRARY |
| 128 | + NAMES |
| 129 | + ${swiftCxx_NAMES} |
| 130 | + NO_CMAKE_FIND_ROOT_PATH |
| 131 | + HINTS |
| 132 | + ${SwiftCxxOverlay_LIBRARY_HINTS}) |
| 133 | + |
| 134 | +add_library(swiftCxx STATIC IMPORTED GLOBAL) |
| 135 | +target_include_directories(swiftCxx INTERFACE |
| 136 | + "${swiftCxx_INCLUDE_DIR}") |
| 137 | +set_target_properties(swiftCxx PROPERTIES |
| 138 | + IMPORTED_LOCATION "${swiftCxx_LIBRARY}") |
| 139 | + |
| 140 | +find_path(swiftCxxStdlib_INCLUDE_DIR |
| 141 | + ${swiftCxxStdlib_MODULE_NAME} |
| 142 | + NO_CMAKE_FIND_ROOT_PATH |
| 143 | + HINTS |
| 144 | + ${SwiftCxxOverlay_INCLUDE_DIR_HINTS}) |
| 145 | +find_library(swiftCxxStdlib_LIBRARY |
| 146 | + NAMES |
| 147 | + ${swiftCxxStdlib_NAMES} |
| 148 | + NO_CMAKE_FIND_ROOT_PATH |
| 149 | + HINTS |
| 150 | + ${SwiftCxxOverlay_LIBRARY_HINTS}) |
| 151 | + |
| 152 | +add_library(swiftCxxStdlib STATIC IMPORTED GLOBAL) |
| 153 | +target_include_directories(swiftCxxStdlib INTERFACE |
| 154 | + "${swiftCxxStdlib_INCLUDE_DIR}") |
| 155 | +set_target_properties(swiftCxxStdlib PROPERTIES |
| 156 | + IMPORTED_LOCATION "${swiftCxxStdlib_LIBRARY}") |
| 157 | + |
| 158 | +if(cxxshims_MODULE_NAME) |
| 159 | + find_path(cxxshims_INCLUDE_DIR |
| 160 | + ${cxxshims_MODULE_NAME} |
| 161 | + NO_CMAKE_FIND_ROOT_PATH |
| 162 | + HINTS |
| 163 | + ${SwiftCxxOverlay_INCLUDE_DIR_HINTS}) |
| 164 | + |
| 165 | + add_library(cxxshims INTERFACE IMPORTED GLOBAL) |
| 166 | + target_compile_options(cxxshims INTERFACE |
| 167 | + "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${cxxshims_INCLUDE_DIR}/${cxxshims_MODULE_NAME}>") |
| 168 | +endif() |
| 169 | +if(libstdcxx_MODULE_NAME) |
| 170 | + find_path(libstdcxx_INCLUDE_DIR |
| 171 | + ${libstdcxx_MODULE_NAME} |
| 172 | + NO_CMAKE_FIND_ROOT_PATH |
| 173 | + HINTS |
| 174 | + ${SwiftCxxOverlay_INCLUDE_DIR_HINTS}) |
| 175 | + |
| 176 | + add_library(libstdcxx INTERFACE IMPORTED GLOBAL) |
| 177 | + target_compile_options(libstdcxx INTERFACE |
| 178 | + "$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${libstdcxx_INCLUDE_DIR}/${libstdcxx_MODULE_NAME}>") |
| 179 | +endif() |
| 180 | + |
| 181 | +set(vars_to_check swiftCxx_LIBRARY swiftCxx_INCLUDE_DIR swiftCxxStdlib_LIBRARY swiftCxxStdlib_INCLUDE_DIR) |
| 182 | +if(cxxshims_MODULE_NAME) |
| 183 | + list(APPEND vars_to_check cxxshims_INCLUDE_DIR) |
| 184 | +endif() |
| 185 | +if(libstdcxx_MODULE_NAME) |
| 186 | + list(APPEND vars_to_check libstdcxx_INCLUDE_DIR) |
| 187 | +endif() |
| 188 | + |
| 189 | +find_package_handle_standard_args(SwiftCxxOverlay DEFAULT_MSG |
| 190 | + ${vars_to_check}) |
0 commit comments