Skip to content

[cmake] add FindSwiftCore module for supplemental libraries #81215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions Runtimes/Supplemental/cmake/modules/FindSwiftCore.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#[=======================================================================[.rst:
FindSwiftCore
------------

Find SwiftCore, deferring to SwiftCoreConfig.cmake when requested.
This is meant to find the core library to be linked by the Supplemental libraries.

Imported Targets
^^^^^^^^^^^^^^^^

The following :prop_tgt:`IMPORTED` TARGETS may be defined:

``SwiftCore``

Hint Variables
^^^^^^^^^^^^^^

``SDKROOT`` (environment variable)
Set the path to the Swift SDK Root.
This only affects Windows builds.

``Swift_SDKROOT``
Set the path to the Swift SDK installation.
This only affects Linux and Windows builds.
Apple builds always use the library provided by the SDK.

Result Variables
^^^^^^^^^^^^^^^^

The module may set the following variables if `SwiftCore_DIR` is not set.

``SwiftCore_FOUND``
true if core was found

``SwiftCore_INCLUDE_DIR``
the directory containing the Swift.swiftmodule folder

#]=======================================================================]

# If the SwiftCore_DIR_FLAG is specified, look there instead. The cmake-generated
# config file is more accurate, but requires that the SDK has one available.
if(SwiftCore_DIR)
if(SwiftCore_FIND_REQUIRED)
list(APPEND args REQUIRED)
endif()
if(SwiftCore_FIND_QUIETLY)
list(APPEND args QUIET)
endif()
find_package(SwiftCore NO_MODULE ${args})
return()
endif()

include(FindPackageHandleStandardArgs)

if(APPLE)
# When building for Apple platforms, SwiftCore always comes from within the
# SDK as a tbd for a shared library in the shared cache.
find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
HINTS
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
find_library(SwiftCore_IMPLIB
NAMES "libswiftCore.tbd"
HINTS
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
add_library(SwiftCore SHARED IMPORTED GLOBAL)
Copy link
Contributor

@edymtt edymtt May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When experimenting at desk I noticed that the case of the target does not match with the one we use in the build system

add_library(swiftCore
Algorithm.swift
ArrayBody.swift

target_link_libraries(swiftSynchronization PRIVATE
swiftCore
$<$<PLATFORM_ID:Darwin>:swiftDarwin>)

meaning we are relying on search paths known to the compiler to find this library, instead of CMake providing the explicit path to the tbd/so/lib file

Suggested change
add_library(SwiftCore SHARED IMPORTED GLOBAL)
add_library(swiftCore SHARED IMPORTED GLOBAL)

set_target_properties(SwiftCore PROPERTIES
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}"
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_IMPLIB SwiftCore_INCLUDE_DIR)
elseif(LINUX)
if (NOT BUILD_SHARED_LIBS)
find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
find_library(SwiftCore_LIBRARY
NAMES "libswiftCore.a"
HINTS "${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
add_library(SwiftCore STATIC IMPORTED GLOBAL)
else()
find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
HINTS
"${Swift_SDKROOT}/usr/lib/swift/linux")
find_library(SwiftCore_LIBRARY
NAMES "libswiftCore.so"
HINTS "${Swift_SDKROOT}/usr/lib/swift/linux")
add_library(SwiftCore SHARED IMPORTED GLOBAL)
endif()
set_target_properties(SwiftCore PROPERTIES
IMPORTED_LOCATION "${SwiftCore_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
elseif(WIN32)
find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
HINTS
"${Swift_SDKROOT}/usr/lib/swift/windows"
"$ENV{SDKROOT}/usr/lib/swift/windows")
find_library(SwiftCore_LIBRARY
NAMES "libswiftCore.lib"
HINTS
"${Swift_SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
"${Swift_SDKROOT}/usr/lib/swift"
"$ENV{SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
"$ENV{SDKROOT}/usr/lib/swift")

add_library(SwiftCore SHARED IMPORTED GLOBAL)
set_target_properties(SwiftCore PROPERTIES
IMPORTED_IMPLIB "${SwiftCore_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
else()
message(FATAL_ERROR "FindSwiftCore.cmake module search not implemented for targeted platform\n"
" Build Core for your platform and set `SwiftCore_DIR` to"
" the directory containing SwiftCoreConfig.cmake\n")
endif()