Skip to content

Introduce Cmake support for SwiftFoundation #573

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 17, 2024

Conversation

iCharlesHu
Copy link
Contributor

@iCharlesHu iCharlesHu commented Apr 30, 2024

Add Cmake support for SwiftFoundation which includes

  • Adding CMakeLists.txt in major directories;
  • Adding module.modulemap for _CShims

Note: I had to change uuid.h's header guard because it's somehow conflicting with system ones while using Cmake to build.


Testing Notes

In order to build SwiftFoundation with Cmake you will need to install cmake and ninja: brew install cmake && brew install ninja

Currently the Cmake build for SwiftFoundationICU (SwiftFoundation's dependency) is hitting two compiler crashes. To work around these issues, first download the nightly Swift snapshot newer than 4/25's build. Then add the following lines in the main CMakeLists.txt file, right after cmake_minimum_required:

set(CMAKE_BUILD_TYPE "RelWithDebInfo")
set(CMAKE_Swift_COMPILER "/Library/Developer/Toolchains/<Toolchain you downloaded>/usr/bin/swiftc")

As of now, the main CMakeLists.txt does not fetch dependencies for you (yet). You'll have to manually checkout SwiftFoundation's two dependencies:

Next, go to each directory and manually build them: cmake -S . -B ./build -G Ninja && cd ./build && ninja

Finally, add these two lines to SwiftFoundation's main CMakeLists.txt, right below the two lines you just added:

set(SwiftCollections_DIR "/path/to/SwiftCollections/build/cmake/modules")
set(SwiftFoundationICU_DIR "/path/to/SwiftFoundationICU/build/cmake/modules")

Make sure to use the build folder above. This will instruct Cmake to use the dependencies you just checked out.

Now you can finally build SwiftFoundation with Cmake:

cmake -S . -B ./build -G Ninja && cd ./build && ninja

@iCharlesHu
Copy link
Contributor Author

@swift-ci please test

@iCharlesHu
Copy link
Contributor Author

@swift-ci please build toolchian

##
##===----------------------------------------------------------------------===##

file(GLOB_RECURSE _FoundationPreviewSources "*.swift")
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not build the "FoundationPreview" package in this mode.

Copy link
Member

Choose a reason for hiding this comment

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

You are aware of the limitation here that if you add a new file or remove a file you will need to do a clean build as the added/removed source will not be reflected in the build.

##
##===----------------------------------------------------------------------===##

file(GLOB_RECURSE _FoundationPreviewSources "*.swift")
Copy link
Member

Choose a reason for hiding this comment

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

You are aware of the limitation here that if you add a new file or remove a file you will need to do a clean build as the added/removed source will not be reflected in the build.

@iCharlesHu iCharlesHu force-pushed the charles/cmake-support branch 2 times, most recently from 8a45d5b to 5fafa69 Compare June 3, 2024 17:10
#
# If the current architecture is supported by Swift, sets ${result_var_name}
# with the sanitized host architecture name derived from CMAKE_SYSTEM_PROCESSOR.
function(get_swift_host_arch result_var_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than hard-code each architecture, we can ask the compiler what the module triple is based on either the default target triple for the toolchain or if someone is trying to cross-compile, based on CMAKE_Swift_COMPILER_TARGET:

if(NOT SwiftFoundation_MODULE_TRIPLE)
  # TODO: This logic should migrate to CMake once CMake supports installing swiftmodules
  set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)
  if(CMAKE_Swift_COMPILER_TARGET)
    list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET})
  endif()
  execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
  string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple")
  set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files")
  mark_as_advanced(SwiftFoundation_MODULE_TRIPLE)
endif()

https://github.com/etcwilde/swift/blob/e10563e336f2fdd7bde5b4b5f1c2a181aa2fe024/stdlib/cmake/modules/PlatformInfo.cmake#L16-L26

$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
endif()
endfunction()
Copy link
Contributor

Choose a reason for hiding this comment

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

missing trailing newline

@iCharlesHu iCharlesHu force-pushed the charles/cmake-support branch 2 times, most recently from e26766c to 06d706e Compare June 5, 2024 18:22
@iCharlesHu iCharlesHu force-pushed the charles/cmake-support branch from 943535f to 9b51995 Compare June 11, 2024 22:52
@iCharlesHu
Copy link
Contributor Author

Merged all commits. Now this PR is ready for review

@iCharlesHu
Copy link
Contributor Author

@swift-ci please test

# If building at desk, check out and link against the SwiftSyntax repo's targets
FetchContent_Declare(SwiftSyntax
GIT_REPOSITORY https://github.com/apple/swift-syntax.git
GIT_TAG 510.0.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't do much about it at the moment, but it's unfortunate that this tag doesn't have the changes for picking up the SWIFT_HOST_MODULE_TRIPLE automatically. So just as a heads up, you're likely to get folks forgetting to set that and asking about an the error message:

CMake Error at build/_deps/swiftsyntax-src/CMakeLists.txt:80 (message):
  Host module triple required

The correct response will be to run swiftc -print-target-info manually and pull out the triple under target > moduleTriple and pass that to their CMake invocation e.g. cmake -G 'Ninja' -DSWIFT_HOST_MODULE_TRIPLE=x86_64-unknown-linux-gnu ....

@iCharlesHu iCharlesHu force-pushed the charles/cmake-support branch 2 times, most recently from dd38a58 to e776427 Compare June 14, 2024 18:00
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
RENAME ${swift_arch}.swiftdoc)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

SwiftFoundation_MODULE_TRIPLE.swiftdock

Comment on lines 88 to 93
else()
install(FILES
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
endif()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove and only use if for all platforms.

@iCharlesHu
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@etcwilde etcwilde left a comment

Choose a reason for hiding this comment

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

I think this will work. I was able to successfully build and run code against this build locally.

In the string_shims.h, it might be worth figuring out where we're pulling that header in in C++ mode and wrapping the declarations in extern "C" {} to ensure that they get mangled correctly everywhere. If they don't, we'll get a linker error saying that some _Z... C++ symbol doesn't exist because it should actually be looking for the C mangle.


set(BUILD_TESTING NO)

set(COLLECTIONS_SINGLE_MODULE YES)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm okay to leave them for now, but we should probably move these settings into a separate CMake cache file so that if folks need to cross-compile SwiftFoundation for use outside of the prebuilt SDK, they can do so in a way that is configurable.

@iCharlesHu iCharlesHu force-pushed the charles/cmake-support branch from daad9de to 8a8c4e3 Compare June 15, 2024 03:27
@iCharlesHu iCharlesHu force-pushed the charles/cmake-support branch from 8a8c4e3 to 6c41fba Compare June 15, 2024 04:55
@iCharlesHu
Copy link
Contributor Author

@swift-ci please test

@iCharlesHu iCharlesHu merged commit 8976fc4 into swiftlang:main Jun 17, 2024
2 checks passed
@iCharlesHu iCharlesHu deleted the charles/cmake-support branch June 17, 2024 05:21
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.

6 participants