Skip to content

[Runtimes][CMake] Refactor FindSwiftCore to put focus on targets... #82035

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 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
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
189 changes: 95 additions & 94 deletions Runtimes/Supplemental/cmake/modules/FindSwiftCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The module may set the following variables if `SwiftCore_DIR` is not set.

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

include_guard(GLOBAL)

# 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)
Expand All @@ -51,113 +53,112 @@ if(SwiftCore_DIR)
endif()

include(FindPackageHandleStandardArgs)
include(PlatformInfo)

# This was loosely modelled after other find modules
# (namely FindGLEW), where the equivalent parameter
# is not stored in cache (possibly because we want
# the project importing it to be able to
# it "immediately")
if(NOT DEFINED SwiftCore_USE_STATIC_LIBS)
set(SwiftCore_USE_STATIC_LIBS OFF)
if(NOT BUILD_SHARED_LIBS AND NOT APPLE)
set(SwiftCore_USE_STATIC_LIBS ON)
endif()
endif()

if(APPLE)
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
list(APPEND SwiftCore_LIBRARY_HINTS
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
# 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)
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)
list(APPEND SwiftCore_NAMES libswiftCore.tbd)
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)
if (SwiftCore_USE_STATIC_LIBS)
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
list(APPEND SwiftCore_LIBRARY_HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
list(APPEND SwiftCore_NAMES libswiftCore.a)
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)
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
"${Swift_SDKROOT}/usr/lib/swift/linux")
list(APPEND SwiftCore_LIBRARY_HINTS
"${Swift_SDKROOT}/usr/lib/swift/linux")
list(APPEND SwiftCore_NAMES libswiftCore.so)
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 that folding the cases with generator expressions would be reasonable.

Copy link
Contributor Author

@edymtt edymtt Jun 10, 2025

Choose a reason for hiding this comment

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

I would need to double check if I can use generator expressions in this context -- my understanding is that we can employ those when setting properties on targets

Copy link
Contributor Author

@edymtt edymtt Jun 11, 2025

Choose a reason for hiding this comment

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

I cobbled up a small example that uses a generator expression to provide the file name to find_library, but that does lead to the library not being found.

Do you have any link to documentation or forums that shows how to use generator expressions in this context? From a quick search, the official CMake Documentation only mentions they can be used in target properties and file(GENERATE)

Example CMake and configuration/generation output
cmake_minimum_required(VERSION 3.29)

project(Foo LANGUAGES Swift)

find_path(SwiftCore_INCLUDE_DIR
  "Swift.swiftmodule"
  NO_CMAKE_FIND_ROOT_PATH
  HINTS
    "${CMAKE_OSX_SYSROOT}/usr/lib/swift")
find_library(SwiftCore_LIBRARY
  NAMES
  $<<PLATFORM_ID:Darwin>,libswiftCore.tbd,libswiftCore.so>
  NO_CMAKE_FIND_ROOT_PATH
  HINTS
    "${CMAKE_OSX_SYSROOT}/usr/lib/swift")

MESSAGE(STATUS "Library path for SwiftCore: ${SwiftCore_LIBRARY}")

add_library(swiftCore SHARED IMPORTED GLOBAL)

set_target_properties(swiftCore PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
set_target_properties(swiftCore PROPERTIES
    IMPORTED_IMPLIB "${SwiftCore_LIBRARY}")

add_library(Foo foo.swift)
target_link_libraries(Foo PRIVATE swiftCore)
...
-- Library path for SwiftCore: SwiftCore_LIBRARY-NOTFOUND
-- Configuring done (0.8s)
-- Generating done (0.0s)
-- Build files have been written to: .../generator_expressions_with_find_library_sandbox/build_dynamic
ninja: Entering directory `build_dynamic'
ninja: error: 'SwiftCore_LIBRARY-NOTFOUND', needed by 'libFoo.dylib', missing and no known rule to make it

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)
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
"${Swift_SDKROOT}/usr/lib/swift/windows"
"$ENV{SDKROOT}/usr/lib/swift/windows")
list(APPEND SwiftCore_LIBRARY_HINTS
"${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
"${Swift_SDKROOT}/usr/lib/swift"
"$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
"$ENV{SDKROOT}/usr/lib/swift")
if (SwiftCore_USE_STATIC_LIBS)
list(APPEND SwiftCore_NAMES libswiftCore.lib)
else()
list(APPEND SwiftCore_NAMES swiftCore.lib)
endif()
elseif(ANDROID)
if(BUILD_SHARED_LIBS)
find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
NO_CMAKE_FIND_ROOT_PATH
HINTS
"${Swift_SDKROOT}/usr/lib/swift/android"
"$ENV{SDKROOT}/usr/lib/swift/android")
find_library(SwiftCore_LIBRARY
NO_CMAKE_FIND_ROOT_PATH
NAMES "libswiftCore.so"
HINTS
"${Swift_SDKROOT}/usr/lib/swift/android/${SwiftCore_ARCH_SUBDIR}"
"${Swift_SDKROOT}/usr/lib/swift"
"$ENV{SDKROOT}/usr/lib/swift/android/${SwiftCore_ARCH_SUBDIR}"
"$ENV{SDKROOT}/usr/lib/swift")

add_library(swiftCore SHARED IMPORTED GLOBAL)
if (SwiftCore_USE_STATIC_LIBS)
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/android"
"$ENV{SDKROOT}/usr/lib/swift_static/android")
list(APPEND SwiftCore_LIBRARY_HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
"${Swift_SDKROOT}/usr/lib/swift_static"
"$ENV{SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
"$ENV{SDKROOT}/usr/lib/swift_static")
list(APPEND SwiftCore_NAMES libswiftCore.a)
else()
find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
NO_CMAKE_FIND_ROOT_PATH
HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/android"
"$ENV{SDKROOT}/usr/lib/swift_static/android")
find_library(SwiftCore_LIBRARY
NO_CMAKE_FIND_ROOT_PATH
NAMES "libswiftCore.a"
HINTS
"${Swift_SDKROOT}/usr/lib/swift_static/android/${SwiftCore_ARCH_SUBDIR}"
"${Swift_SDKROOT}/usr/lib/swift_static"
"$ENV{SDKROOT}/usr/lib/swift_static/android/${SwiftCore_ARCH_SUBDIR}"
"$ENV{SDKROOT}/usr/lib/swift_static")

add_library(swiftCore STATIC IMPORTED GLOBAL)
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
"${Swift_SDKROOT}/usr/lib/swift/android"
"$ENV{SDKROOT}/usr/lib/swift/android")
list(APPEND SwiftCore_LIBRARY_HINTS
"${Swift_SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
"${Swift_SDKROOT}/usr/lib/swift"
"$ENV{SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
"$ENV{SDKROOT}/usr/lib/swift")
list(APPEND SwiftCore_NAMES libswiftCore.so)
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)
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()

find_path(SwiftCore_INCLUDE_DIR
"Swift.swiftmodule"
NO_CMAKE_FIND_ROOT_PATH
HINTS
${SwiftCore_INCLUDE_DIR_HINTS})
find_library(SwiftCore_LIBRARY
NAMES
${SwiftCore_NAMES}
NO_CMAKE_FIND_ROOT_PATH
HINTS
${SwiftCore_LIBRARY_HINTS})

if(SwiftCore_USE_STATIC_LIBS)
add_library(swiftCore STATIC IMPORTED GLOBAL)
else()
add_library(swiftCore SHARED IMPORTED GLOBAL)
endif()

set_target_properties(swiftCore PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")

if(LINUX OR ANDROID)
set_target_properties(swiftCore PROPERTIES
IMPORTED_LOCATION "${SwiftCore_LIBRARY}")
else()
set_target_properties(swiftCore PROPERTIES
IMPORTED_IMPLIB "${SwiftCore_LIBRARY}")
endif()

find_package_handle_standard_args(SwiftCore DEFAULT_MSG
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
2 changes: 2 additions & 0 deletions Runtimes/Supplemental/cmake/modules/PlatformInfo.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include_guard(GLOBAL)

if(NOT ${PROJECT_NAME}_SIZEOF_POINTER)
set(${PROJECT_NAME}_SIZEOF_POINTER "${CMAKE_SIZEOF_VOID_P}" CACHE STRING "Size of a pointer in bytes")
message(CONFIGURE_LOG "Stdlib Pointer size: ${CMAKE_SIZEOF_VOID_P}")
Expand Down