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: Fixing cross-compiling Swift-Foundation #878

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@
##
##===----------------------------------------------------------------------===##

add_library(FoundationBuildMacros INTERFACE)
export(TARGETS FoundationBuildMacros
FILE
${SwiftFoundation_BINARY_DIR}/cmake/modules/FoundationBuildMacros.cmake)
if(SwiftFoundation_MACRO)
message(STATUS "SwiftFoundation_MACRO provided, using macros in ${SwiftFoundation_MACRO}")
target_compile_options(FoundationBuildMacros INTERFACE
"SHELL:$<BUILD_INTERFACE:$<$<COMPILE_LANGUAGE:Swift>:-plugin-path ${SwiftFoundation_MACRO}>>")
else()
message(STATUS "NO SwiftFoundation_MACRO, building macros from scratch")
include(ExternalProject)
# The macros are required for building Swift-Foundation. Build them for the
# build machine.
ExternalProject_Add(FoundationMacros
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/FoundationMacros"
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/FoundationMacros"
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_Swift_COMPILER=${CMAKE_Swift_COMPILER}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DSwiftFoundation_BUILD_EXECUTABLE_MACROS=YES)
ExternalProject_Get_Property(FoundationMacros INSTALL_DIR)
add_dependencies(FoundationBuildMacros FoundationMacros)
if(CMAKE_HOST_WIN32)
set(_SwiftFoundation_HOST_EXECUTABLE_SUFFIX .exe)
endif()

target_compile_options(FoundationBuildMacros INTERFACE
"SHELL:$<BUILD_INTERFACE:$<$<COMPILE_LANGUAGE:Swift>:-load-plugin-executable ${INSTALL_DIR}/bin/FoundationMacros${_SwiftFoundation_HOST_EXECUTABLE_SUFFIX}#FoundationMacros>>")
unset(_SwiftFoundation_HOST_EXECUTABLE_SUFFIX)
endif()

add_subdirectory(_FoundationCShims)
add_subdirectory(FoundationEssentials)
add_subdirectory(FoundationInternationalization)
24 changes: 1 addition & 23 deletions Sources/FoundationEssentials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,6 @@ add_subdirectory(String)
add_subdirectory(TimeZone)
add_subdirectory(URL)

if(SwiftFoundation_MACRO)
message(STATUS "SwiftFoundation_MACRO provided, using macros in ${SwiftFoundation_MACRO}")
# A path to Foundation macros was provided, so we use that path
target_compile_options(FoundationEssentials PRIVATE
"SHELL:-plugin-path ${SwiftFoundation_MACRO}")
else()
message(STATUS "SwiftFoundation_MACRO not provided, building Foundation macros locally for host")
# No path to Foundation macros was provided, so we must build it ourselves
set(FoundationMacros_BuildLocalExecutable YES)
FetchContent_Declare(FoundationMacros
SOURCE_DIR ${CMAKE_SOURCE_DIR}/Sources/FoundationMacros)
FetchContent_MakeAvailable(FoundationMacros)
add_dependencies(FoundationEssentials FoundationMacros)
get_target_property(MacroDIR FoundationMacros RUNTIME_OUTPUT_DIRECTORY)
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
set(MacroExecutable "${MacroDIR}/FoundationMacros.exe#FoundationMacros")
else()
set(MacroExecutable "${MacroDIR}/FoundationMacros#FoundationMacros")
endif()
target_compile_options(FoundationEssentials PUBLIC
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-load-plugin-executable ${MacroExecutable}>")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
target_compile_options(FoundationEssentials PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -Xcc -Xfrontend -D_GNU_SOURCE>")
Expand All @@ -88,6 +65,7 @@ target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_wasi_libc
target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation")

target_link_libraries(FoundationEssentials PUBLIC
FoundationBuildMacros
_FoundationCShims
_FoundationCollections)
target_link_libraries(FoundationEssentials PUBLIC ${_SwiftFoundation_wasi_libc_libraries})
Expand Down
19 changes: 10 additions & 9 deletions Sources/FoundationMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ endif()
project(FoundationMacros
LANGUAGES Swift)

option(SwiftFoundation_BUILD_EXECUTABLE_MACROS
"Build the FoundationMacros as an executable" NO)

# SwiftSyntax Dependency
find_package(SwiftSyntax QUIET)
if(NOT SwiftSyntax_FOUND)
Expand All @@ -41,13 +44,13 @@ else()
message(STATUS "SwiftSyntax_DIR provided, using swift-syntax from ${SwiftSyntax_DIR}")
endif()

if(NOT FoundationMacros_BuildLocalExecutable)
add_library(FoundationMacros SHARED)
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
else()
if(SwiftFoundation_BUILD_EXECUTABLE_MACROS)
add_executable(FoundationMacros)
target_link_libraries(FoundationMacros PUBLIC
SwiftSyntax::SwiftCompilerPlugin)
else()
add_library(FoundationMacros SHARED)
target_compile_definitions(FoundationMacros PRIVATE FOUNDATION_MACROS_LIBRARY)
endif()

# Parse the module as a library, even if it's an executable, because it uses an `@main` type to define its entry point.
Expand Down Expand Up @@ -82,8 +85,6 @@ set_target_properties(FoundationMacros PROPERTIES
INSTALL_RPATH "$ORIGIN/../../../swift/${SWIFT_SYSTEM_NAME}:$ORIGIN/.."
INSTALL_REMOVE_ENVIRONMENT_RPATH ON)

if(NOT FoundationMacros_BuildLocalExecutable)
install(TARGETS FoundationMacros
LIBRARY DESTINATION lib/swift/host/plugins
RUNTIME DESTINATION bin)
endif()
install(TARGETS FoundationMacros
LIBRARY DESTINATION lib/swift/host/plugins
RUNTIME DESTINATION bin)
1 change: 1 addition & 0 deletions cmake/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
##
##===----------------------------------------------------------------------===##

set(SWIFT_FOUNDATION_BUILD_MACROS_FILE ${CMAKE_CURRENT_BINARY_DIR}/FoundationBuildMacros.cmake)
set(SWIFT_FOUNDATION_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftFoundationExports.cmake)
set(SWIFT_FOUNDATION_ICU_EXPORTS_FILE ${SwiftFoundationICU_BINARY_DIR}/cmake/modules/SwiftFoundationICUExports.cmake)
set(SWIFT_COLLECTIONS_EXPORTS_FILE ${SwiftCollections_BINARY_DIR}/cmake/modules/SwiftCollectionsExports.cmake)
Expand Down
1 change: 1 addition & 0 deletions cmake/modules/SwiftFoundationConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
##===----------------------------------------------------------------------===##

if(NOT TARGET SwiftFoundation)
include(@SWIFT_FOUNDATION_BUILD_MACROS_FILE@)
include(@SWIFT_FOUNDATION_ICU_EXPORTS_FILE@)
include(@SWIFT_COLLECTIONS_EXPORTS_FILE@)
include(@SWIFT_FOUNDATION_EXPORTS_FILE@)
Expand Down