Skip to content
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

[CMake] Explicitly link Testing to Foundation #693

Merged
merged 2 commits into from
Sep 20, 2024

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Sep 16, 2024

Explicitly link to Foundation in CMake builds

If Foundation_DIR is passed, find_package finds the Foundation target and target_link_libraries adds required -I and -l/-L. If not, find_package fails and target_link_libraries just assumes it's a system library name and just adds -lFoundation.

@@ -96,6 +96,8 @@ add_library(Testing
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestingInternals)
target_link_libraries(Testing PUBLIC
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this fail if for some reason we can't find Foundation?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it should fail with linker errors.

@grynspan grynspan added bug Something isn't working build Affects the project's build configuration or process labels Sep 16, 2024
@grynspan grynspan added this to the Swift 6.1 milestone Sep 16, 2024
@rintaro rintaro force-pushed the cmake-link-foundation branch 2 times, most recently from 8072ea4 to 8aa2643 Compare September 16, 2024 19:42
CMakeLists.txt Outdated
@@ -15,6 +15,11 @@ endif()
project(SwiftTesting
LANGUAGES CXX Swift)

if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
if(NOT APPLE)

@@ -96,6 +96,11 @@ add_library(Testing
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestingInternals)
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
if(NOT APPLE)

CMakeLists.txt Outdated
@@ -15,6 +15,11 @@ endif()
project(SwiftTesting
LANGUAGES CXX Swift)

if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
find_package(dispatch)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we go this route instead of using the build-directory configurations, we'll need to include a Finddispatch.cmake and FindFoundation.cmake in our modules directory and move these down below the list(APPEND CMAKE_MODUKE_PATH ... on line 25.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think find_package() tries Module mode first then falls backs to Config mode IIUC.

When the basic signature is used, the command searches in Module mode first. If the package is not found, the search falls back to Config mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, but I think we still would want the Find<PackageName>.cmake modules or I don't really see much point in not just using the CONFIG form.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's just use CONFIG then.

@rintaro rintaro force-pushed the cmake-link-foundation branch from 8aa2643 to 79a443a Compare September 16, 2024 22:49
@@ -96,6 +96,11 @@ add_library(Testing
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestingInternals)
if(NOT APPLE)
target_link_libraries(Testing PUBLIC
dispatch
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 optionalize this dispatch link as WASI doesn't support libdispatch? You can ensure the change doesn't break WASI build by @swift-ci test WebAssembly in cross repo testing.

Copy link
Member Author

Choose a reason for hiding this comment

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

What would be the condition? if(NOT WASI)?

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI).

Copy link
Member Author

@rintaro rintaro Sep 17, 2024

Choose a reason for hiding this comment

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

👍 Since WASI is equivalent to CMAKE_SYSTEM_NAME STREQUAL WASI, I went with WASI

Copy link
Member

Choose a reason for hiding this comment

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

WASI is available only in the bleeding edge CMakes (https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9659), so can we stick on CMAKE_SYSTEM_NAME STREQUAL WASI for now?

WASI doesn't support `libdispatch`
@rintaro rintaro force-pushed the cmake-link-foundation branch from fa43fb6 to c2f8ccc Compare September 17, 2024 15:33
CMakeLists.txt Show resolved Hide resolved
Sources/Testing/CMakeLists.txt Show resolved Hide resolved
@@ -96,6 +96,14 @@ add_library(Testing
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestingInternals)
if(NOT APPLE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious why we need the if(NOT APPLE) guard for these target_link_libraries? I get why it's needed for find_package, but don't we want to link Foundation even on Darwin?

Copy link
Member Author

Choose a reason for hiding this comment

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

In Darwin, Foundation is a framework, so -lFoundation doesn't work.
Yes it links Foundation, but that's done via auto-linking. I.e. import Foundation automatically sets required linker flags.

Copy link
Contributor

@stmontgomery stmontgomery left a comment

Choose a reason for hiding this comment

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

Had one question but LGTM regardless

@rintaro rintaro merged commit e20774a into swiftlang:main Sep 20, 2024
grynspan pushed a commit that referenced this pull request Sep 24, 2024
Cherry-pick #693 into `release/6.0`

* **Explanation**: Previously in CMake builds, when `Foundation` was not
in the regular search directory (e.g. resource directory or SDK search
paths) `#if canImport(Foundation)` used to fail, and the functionalities
are not included. This patch provides a way to provide `Foundation_DIR`
for `find_packgage(Foundation CONFIG)`, so that clients can correctly
link Testing to Foundation
* **Scope**: CMake builds
* **Risk**: Low. No actual code changes.
* **Testing**: Passes current test suite Also manually tested the build
toolchain
* **Issues**: N/A
* **Reviewer**: @stmontgomery
grynspan added a commit that referenced this pull request Sep 24, 2024
Cherry-pick #693 into `release/6.0.2`

* **Explanation**: Previously in CMake builds, when `Foundation` was not
in the regular search directory (e.g. resource directory or SDK search
paths) `#if canImport(Foundation)` used to fail, and the functionalities
are not included. This patch provides a way to provide `Foundation_DIR`
for `find_packgage(Foundation CONFIG)`, so that clients can correctly
link Testing to Foundation
* **Scope**: CMake builds
* **Risk**: Low. No actual code changes.
* **Testing**: Passes current test suite Also manually tested the build
toolchain
* **Issues**: N/A
* **Reviewer**: @stmontgomery

---------

Co-authored-by: Jonathan Grynspan <jgrynspan@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working build Affects the project's build configuration or process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants