|
| 1 | +#[=======================================================================[.rst: |
| 2 | +FindSwiftOverlay |
| 3 | +------------ |
| 4 | +
|
| 5 | +Find SwiftOverlay, deferring to the associated SwiftOverlayConfig.cmake when requested. |
| 6 | +This is meant to find the platform 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 | + ``SwiftOverlay`` |
| 14 | +
|
| 15 | +Hint Variables |
| 16 | +^^^^^^^^^^^^^^ |
| 17 | +
|
| 18 | + ``SDKROOT`` (environment variable) |
| 19 | + Set the path to the Swift SDK Root. |
| 20 | + This only affects Windows and Android builds. |
| 21 | +
|
| 22 | + ``Swift_SDKROOT`` |
| 23 | + Set the path to the Swift SDK installation. |
| 24 | + This affects Linux, Android, and Windows builds. |
| 25 | + Apple builds always use the overlay provided by the SDK. |
| 26 | +
|
| 27 | +#]=======================================================================] |
| 28 | + |
| 29 | +include_guard(GLOBAL) |
| 30 | + |
| 31 | +# This was loosely modelled after other find modules |
| 32 | +# (namely FindGLEW), where the equivalent parameter |
| 33 | +# is not stored in cache (possibly because we want |
| 34 | +# the project importing it to be able to |
| 35 | +# it "immediately") |
| 36 | +if(NOT DEFINED SwiftOverlay_USE_STATIC_LIBS) |
| 37 | + set(SwiftOverlay_USE_STATIC_LIBS OFF) |
| 38 | + if(NOT BUILD_SHARED_LIBS AND NOT APPLE) |
| 39 | + set(SwiftOverlay_USE_STATIC_LIBS ON) |
| 40 | + endif() |
| 41 | +endif() |
| 42 | + |
| 43 | +if(SwiftOverlay_DIR) |
| 44 | + if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED) |
| 45 | + list(APPEND args REQUIRED) |
| 46 | + endif() |
| 47 | + if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY) |
| 48 | + list(APPEND args QUIET) |
| 49 | + endif() |
| 50 | + find_package(SwiftOverlay CONFIG ${args}) |
| 51 | + return() |
| 52 | +endif() |
| 53 | + |
| 54 | +include(FindPackageHandleStandardArgs) |
| 55 | +include(PlatformInfo) |
| 56 | + |
| 57 | +if(APPLE) |
| 58 | + list(APPEND OVERLAY_TARGET_NAMES "swiftDarwin") |
| 59 | + # Look in the SDK |
| 60 | + list(APPEND swiftDarwin_INCLUDE_DIR_HINTS |
| 61 | + "${CMAKE_OSX_SYSROOT}/usr/lib/swift") |
| 62 | + list(APPEND swiftDarwin_LIBRARY_HINTS |
| 63 | + "${CMAKE_OSX_SYSROOT}/usr/lib/swift") |
| 64 | + # When building for Apple platforms, swiftDarwin always comes from within the |
| 65 | + # SDK as a tbd for a shared library in the shared cache. |
| 66 | + list(APPEND swiftDarwin_NAMES libswiftDarwin.tbd) |
| 67 | + set(swiftDarwin_MODULE_NAME "Darwin.swiftmodule") |
| 68 | +elseif(LINUX) |
| 69 | + #ToDo(swiftlang/swift/issues/83014): Handle the static MUSL SDK case |
| 70 | + list(APPEND OVERLAY_TARGET_NAMES "swiftGlibc") |
| 71 | + |
| 72 | + # Look in the SDK |
| 73 | + if (SwiftOverlay_USE_STATIC_LIBS) |
| 74 | + list(APPEND swiftGlibc_INCLUDE_DIR_HINTS |
| 75 | + "${Swift_SDKROOT}/usr/lib/swift_static/linux") |
| 76 | + list(APPEND swiftGlibc_LIBRARY_HINTS |
| 77 | + "${Swift_SDKROOT}/usr/lib/swift_static/linux") |
| 78 | + list(APPEND swiftGlibc_NAMES libswiftGlibc.a) |
| 79 | + else() |
| 80 | + list(APPEND swiftGlibc_INCLUDE_DIR_HINTS |
| 81 | + "${Swift_SDKROOT}/usr/lib/swift/linux/") |
| 82 | + list(APPEND swiftGlibc_LIBRARY_HINTS |
| 83 | + "${Swift_SDKROOT}/usr/lib/swift/linux") |
| 84 | + list(APPEND swiftGlibc_NAMES libswiftGlibc.so) |
| 85 | + endif() |
| 86 | + set(swiftGlibc_MODULE_NAME "Glibc.swiftmodule") |
| 87 | +elseif(WIN32) |
| 88 | + list(APPEND OVERLAY_TARGET_NAMES "swiftWinSDK") |
| 89 | + list(APPEND OVERLAY_TARGET_NAMES "swiftCRT") |
| 90 | + |
| 91 | + list(APPEND swiftWinSDK_INCLUDE_DIR_HINTS |
| 92 | + "${Swift_SDKROOT}/usr/lib/swift/windows" |
| 93 | + "$ENV{SDKROOT}/usr/lib/swift/windows") |
| 94 | + list(APPEND swiftWinSDK_LIBRARY_HINTS |
| 95 | + "${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 96 | + "${Swift_SDKROOT}/usr/lib/swift" |
| 97 | + "$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 98 | + "$ENV{SDKROOT}/usr/lib/swift") |
| 99 | + |
| 100 | + if(SwiftOverlay_USE_STATIC_LIBS) |
| 101 | + list(APPEND swiftWinSDK_NAMES libswiftWinSDK.lib) |
| 102 | + else() |
| 103 | + list(APPEND swiftWinSDK_NAMES swiftWinSDK.lib) |
| 104 | + endif() |
| 105 | + list(APPEND swiftWinSDK_MODULE_NAME "WinSDK.swiftmodule") |
| 106 | + |
| 107 | + list(APPEND swiftCRT_INCLUDE_DIR_HINTS |
| 108 | + "${Swift_SDKROOT}/usr/lib/swift/windows" |
| 109 | + "$ENV{SDKROOT}/usr/lib/swift/windows") |
| 110 | + list(APPEND swiftCRT_LIBRARY_HINTS |
| 111 | + "${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 112 | + "${Swift_SDKROOT}/usr/lib/swift" |
| 113 | + "$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 114 | + "$ENV{SDKROOT}/usr/lib/swift") |
| 115 | + |
| 116 | + if(SwiftOverlay_USE_STATIC_LIBS) |
| 117 | + list(APPEND swiftCRT_NAMES libswiftCRT.lib) |
| 118 | + else() |
| 119 | + list(APPEND swiftCRT_NAMES swiftCRT.lib) |
| 120 | + endif() |
| 121 | + set(swiftCRT_MODULE_NAME "CRT.swiftmodule") |
| 122 | +elseif(ANDROID) |
| 123 | + list(APPEND OVERLAY_TARGET_NAMES "swiftAndroid") |
| 124 | + |
| 125 | + if (SwiftOverlay_USE_STATIC_LIBS) |
| 126 | + list(APPEND swiftAndroid_INCLUDE_DIR_HINTS |
| 127 | + "${Swift_SDKROOT}/usr/lib/swift_static/android" |
| 128 | + "$ENV{SDKROOT}/usr/lib/swift_static/android") |
| 129 | + list(APPEND swiftAndroid_LIBRARY_HINTS |
| 130 | + "${Swift_SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 131 | + "${Swift_SDKROOT}/usr/lib/swift_static" |
| 132 | + "$ENV{SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 133 | + "$ENV{SDKROOT}/usr/lib/swift_static") |
| 134 | + list(APPEND swiftAndroid_NAMES libswiftAndroid.a) |
| 135 | + else() |
| 136 | + list(APPEND swiftAndroid_INCLUDE_DIR_HINTS |
| 137 | + "${Swift_SDKROOT}/usr/lib/swift/android" |
| 138 | + "$ENV{SDKROOT}/usr/lib/swift/android") |
| 139 | + list(APPEND swiftAndroid_LIBRARY_HINTS |
| 140 | + "${Swift_SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 141 | + "${Swift_SDKROOT}/usr/lib/swift" |
| 142 | + "$ENV{SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}" |
| 143 | + "$ENV{SDKROOT}/usr/lib/swift") |
| 144 | + list(APPEND swiftAndroid_NAMES libswiftAndroid.so) |
| 145 | + endif() |
| 146 | + set(swiftAndroid_MODULE_NAME "Android.swiftmodule") |
| 147 | +else() |
| 148 | + message(FATAL_ERROR "FindSwiftOverlay.cmake module search not implemented for targeted platform\n" |
| 149 | + " Build the Overlays for your platform and set the appropriate `SwiftOverlay_DIR` variable to" |
| 150 | + " the directory containing SwiftOverlayConfig.cmake\n") |
| 151 | +endif() |
| 152 | + |
| 153 | +# Setup SwiftOverlay interface library and link it against the explicit |
| 154 | +# overlay targets |
| 155 | +add_library(SwiftOverlay INTERFACE) |
| 156 | + |
| 157 | +foreach(OVERLAY_TARGET ${OVERLAY_TARGET_NAMES}) |
| 158 | + find_path(${OVERLAY_TARGET}_INCLUDE_DIR |
| 159 | + ${${OVERLAY_TARGET}_MODULE_NAME} |
| 160 | + NO_CMAKE_FIND_ROOT_PATH |
| 161 | + HINTS |
| 162 | + ${${OVERLAY_TARGET}_INCLUDE_DIR_HINTS}) |
| 163 | + find_library(${OVERLAY_TARGET}_LIBRARY |
| 164 | + NAMES |
| 165 | + ${${OVERLAY_TARGET}_NAMES} |
| 166 | + NO_CMAKE_FIND_ROOT_PATH |
| 167 | + HINTS |
| 168 | + ${${OVERLAY_TARGET}_LIBRARY_HINTS}) |
| 169 | + |
| 170 | + if(SwiftOverlay_USE_STATIC_LIBS) |
| 171 | + add_library(${OVERLAY_TARGET} STATIC IMPORTED GLOBAL) |
| 172 | + else() |
| 173 | + add_library(${OVERLAY_TARGET} SHARED IMPORTED GLOBAL) |
| 174 | + endif() |
| 175 | + |
| 176 | + target_include_directories(${OVERLAY_TARGET} INTERFACE |
| 177 | + "${${OVERLAY_TARGET}_INCLUDE_DIR}") |
| 178 | + |
| 179 | + if(LINUX OR ANDROID) |
| 180 | + set_target_properties(${OVERLAY_TARGET} PROPERTIES |
| 181 | + IMPORTED_LOCATION "${${OVERLAY_TARGET}_LIBRARY}") |
| 182 | + else() |
| 183 | + set_target_properties(${OVERLAY_TARGET} PROPERTIES |
| 184 | + IMPORTED_IMPLIB "${${OVERLAY_TARGET}_LIBRARY}") |
| 185 | + endif() |
| 186 | + |
| 187 | + target_link_libraries(SwiftOverlay INTERFACE |
| 188 | + ${OVERLAY_TARGET}) |
| 189 | +endforeach() |
| 190 | + |
| 191 | +foreach(OVERLAY_TARGET ${OVERLAY_TARGET_NAMES}) |
| 192 | + list(APPEND vars_to_check "${OVERLAY_TARGET}_LIBRARY" "${OVERLAY_TARGET}_INCLUDE_DIR") |
| 193 | +endforeach() |
| 194 | + |
| 195 | +find_package_handle_standard_args(SwiftOverlay DEFAULT_MSG |
| 196 | + ${vars_to_check}) |
0 commit comments