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

Conversation

justice-adams-apple
Copy link
Contributor

Add FindSwiftCore to Supplemental cmake modules so we can link against libswiftCore in a given end users SDK if SwiftCore_DIR is not provided

@justice-adams-apple justice-adams-apple force-pushed the jadams/add-find-swiftcore branch from 085630a to 6b5befc Compare May 5, 2025 21:29
@justice-adams-apple
Copy link
Contributor Author

@swift-ci please smoke test

Comment on lines 18 to 20
``SDKROOT``
Set the path to the Swift SDK Root.
This only affects MacOS builds.
Copy link
Contributor

Choose a reason for hiding this comment

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

This description seems stale now -- this seems to be now an environment variable sensed when building on Windows

Copy link
Member

Choose a reason for hiding this comment

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

The SDKROOT env var gets set up by the Windows toolchain installation. It's less of a hint and more meant as a fallback to avoid requiring the variable be set when building for Windows. If it's important, it should likely be set via the Swift_SDKROOT variable directly.

find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
elseif(WIN32)
set(SwiftCore_INCLUDE_DIR "${Swift_SDKROOT}/usr/lib/swift_static/linux-static/Swift.swiftmodule" CACHE STRING "")
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a left over from iterating on the code -- if I interpret this comment correctly, the line below should be enough

Suggested change
set(SwiftCore_INCLUDE_DIR "${Swift_SDKROOT}/usr/lib/swift_static/linux-static/Swift.swiftmodule" CACHE STRING "")

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's use find_path to search for the appropriate Swift.swiftmoule. We should prefer to search via Swift_SDKROOT, but then can fall back on $ENV{SDKROOT}. If we're building for Windows, we shouldn't be trying to find the swiftmodule from the linux-static SDK.

Comment on lines 18 to 20
``SDKROOT``
Set the path to the Swift SDK Root.
This only affects MacOS builds.
Copy link
Member

Choose a reason for hiding this comment

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

The SDKROOT env var gets set up by the Windows toolchain installation. It's less of a hint and more meant as a fallback to avoid requiring the variable be set when building for Windows. If it's important, it should likely be set via the Swift_SDKROOT variable directly.

Set the path to the Swift SDK Root.
This only affects MacOS builds.

``swift_SDKROOT``
Copy link
Member

Choose a reason for hiding this comment

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

The variable below uses a capital S on "Swift".

Suggested change
``swift_SDKROOT``
``Swift_SDKROOT``


``SwiftCore_LIBRARIES`` OR ``SwiftCore_IMPLIB``
the libraries to be linked

Copy link
Member

Choose a reason for hiding this comment

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

SwiftCore_INCLUDE_DIR is also set.

Copy link
Member

Choose a reason for hiding this comment

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

This can also be a property on the imported library target.

find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
elseif(WIN32)
set(SwiftCore_INCLUDE_DIR "${Swift_SDKROOT}/usr/lib/swift_static/linux-static/Swift.swiftmodule" CACHE STRING "")
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's use find_path to search for the appropriate Swift.swiftmoule. We should prefer to search via Swift_SDKROOT, but then can fall back on $ENV{SDKROOT}. If we're building for Windows, we shouldn't be trying to find the swiftmodule from the linux-static SDK.

@justice-adams-apple justice-adams-apple force-pushed the jadams/add-find-swiftcore branch from 6b5befc to c874044 Compare May 6, 2025 18:38
@justice-adams-apple
Copy link
Contributor Author

@swift-ci please smoke test

``SwiftCore_FOUND``
true if core was found

``SwiftCore_LIBRARIES`` OR ``SwiftCore_IMPLIB``
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that we should be doing these variables. Instead we should create a SwiftCore target that is an imported library (static or shared). On Windows, we can associate an IMPORTED_IMPLIB property on the shared library target. This description does seem to match what we are doing below.


``SwiftCore_LIBRARIES`` OR ``SwiftCore_IMPLIB``
the libraries to be linked

Copy link
Member

Choose a reason for hiding this comment

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

This can also be a property on the imported library target.

find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_IMPLIB SwiftCore_INCLUDE_DIR)
elseif(LINUX)
if(SwiftCore_STATIC)
Copy link
Member

Choose a reason for hiding this comment

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

Can we detect static or dynamic by any chance?

@justice-adams-apple
Copy link
Contributor Author

@swift-ci please smoke test

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

``SDKROOT``
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: we may want to state this is not a CMake variable

Suggested change
``SDKROOT``
``SDKROOT`` (environment variable)

"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
add_library(SwiftCore SHARED IMPORTED GLOBAL)
set_target_properties(SwiftCore PROPERTIES
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}")
Copy link
Contributor

@edymtt edymtt May 13, 2025

Choose a reason for hiding this comment

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

I believe this target needs to have the path to the swiftmodules as well like its siblings:

Suggested change
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}")
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}"
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")

@justice-adams-apple
Copy link
Contributor Author

@swift-ci please smoke test

@justice-adams-apple justice-adams-apple merged commit 758ae18 into main May 15, 2025
3 checks passed
@justice-adams-apple justice-adams-apple deleted the jadams/add-find-swiftcore branch May 15, 2025 15:55
Catfish-Man pushed a commit that referenced this pull request May 15, 2025
Add FindSwiftCore to Supplemental cmake modules so we can link against
libswiftCore in a given end users SDK if `SwiftCore_DIR ` is not
provided
justice-adams-apple added a commit that referenced this pull request May 16, 2025
Addresses rdar://150300769

Add Synchronization library to new build system

Land #81215 first
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)

hamishknight added a commit to hamishknight/swift that referenced this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants