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] Enable CMP0157 NEW if available #76587

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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()

# Enable Swift_COMPILATION_MODE property.
set(CMP0157_IS_NEW FALSE)
if(POLICY CMP0157)
cmake_policy(SET CMP0157 NEW)
set(CMP0157_IS_NEW TRUE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of setting this variable, we should probably use cmake_policy(GET...) at the locations where we are querying for it so that it can't get out of sync and there's only one source of truth.

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 didn't use cmake_policy(GET...) because it would require a temporary variable.

cmake_policy(GET CMP0157 CMP0157_value)
if(NOT CMP0157_value STREQUAL NEW)
  ...
endif()

Is there a way to check the value in if(...) condition?

endif()

# Add path for custom CMake modules.
list(APPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
Expand Down
67 changes: 38 additions & 29 deletions cmake/modules/AddPureSwift.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
include(macCatalystUtils)

# Workaround a cmake bug, see the corresponding function in swift-syntax
function(force_add_dependencies TARGET)
foreach(DEPENDENCY ${ARGN})
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
Expand All @@ -16,12 +15,18 @@ endfunction()
function(force_target_link_libraries TARGET)
target_link_libraries(${TARGET} ${ARGN})

cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
force_add_dependencies(${TARGET} ${ARGS_UNPARSED_ARGUMENTS})
if(NOT CMP0157_IS_NEW)
Copy link
Contributor

Choose a reason for hiding this comment

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

Didn't this get fixed in 3.26 or something?
We can use CMAKE_VERSION with one of VERSION_LESS, VERSION_GREATER, VERSION_EQUAL, VERSION_LESS_EQUAL, or VERSION_GREATER_EQUAL to do the comparison directly on the version itself rather than using the presence of the CMP0157 policy.

Copy link
Member Author

@rintaro rintaro Sep 19, 2024

Choose a reason for hiding this comment

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

I'm not really sure, tbh I didn't tried just removing this without enabling CMP0157.

Is just checking CMAKE_VERSION enough? As far as I tried, CMP0157 had to be set NEW to remove these workarounds. Though I didn't tried removing each workaround individually .

Copy link
Contributor

Choose a reason for hiding this comment

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

I fixed the missing dependency edge on the swiftmodule here: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/8084.
It should be fixed in anything newer than CMake 3.26. Versions older that 3.29 may have unnecessary rebuilds though, but that shouldn't be different than the forced rebuild anyway.

# Workaround a cmake bug, see the corresponding function in swift-syntax
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
force_add_dependencies(${TARGET} ${ARGS_UNPARSED_ARGUMENTS})
endif()
endfunction()

# Add compile options shared between libraries and executables.
function(_add_host_swift_compile_options name)
set_property(TARGET ${name} PROPERTY
Swift_COMPILATION_MODE "$<IF:$<CONFIG:Release,RelWithDebInfo>,wholemodule,incremental>")

# Avoid introducing an implicit dependency on the string-processing library.
if(SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
target_compile_options(${name} PRIVATE
Expand Down Expand Up @@ -296,14 +301,16 @@ function(add_pure_swift_host_library name)
# NOTE: workaround for CMake not setting up include flags.
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${module_file}"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of library outputs workaround")
if(NOT CMP0157_IS_NEW)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${module_file}"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of library outputs workaround")
endif()

# Downstream linking should include the swiftmodule in debug builds to allow lldb to
# work correctly. Only do this on Darwin since neither gold (currently used by default
Expand Down Expand Up @@ -439,24 +446,26 @@ function(add_pure_swift_host_tool name)
"SHELL:-Xlinker --build-id=sha1")
endif()

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}>
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of executable outputs workaround")

# Even worse hack - ${name}.swiftmodule is added as an output, even though
# this is an executable target. Just touch it all the time to avoid having
# to rebuild it every time.
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_CURRENT_BINARY_DIR}/${name}.swiftmodule"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of executable outputs workaround")
if(NOT CMP0157_IS_NEW)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

# Workaround to touch the library and its objects so that we don't
# continually rebuild (again, see corresponding change in swift-syntax).
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}>
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of executable outputs workaround")

# Even worse hack - ${name}.swiftmodule is added as an output, even though
# this is an executable target. Just touch it all the time to avoid having
# to rebuild it every time.
add_custom_command(
TARGET ${name}
POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_CURRENT_BINARY_DIR}/${name}.swiftmodule"
COMMAND_EXPAND_LISTS
COMMENT "Update mtime of executable outputs workaround")
endif()

if(NOT APSHT_SWIFT_COMPONENT STREQUAL no_component)
add_dependencies(${APSHT_SWIFT_COMPONENT} ${name})
Expand Down
4 changes: 4 additions & 0 deletions tools/swift-plugin-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ if (SWIFT_BUILD_SWIFT_SYNTAX)
PACKAGE_NAME Toolchain
)

# 'swift-plugin-server' is not a valid module name.
set_target_properties(swift-plugin-server PROPERTIES
Swift_MODULE_NAME SwiftPluginServer)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}")
add_pure_swift_host_library(SwiftInProcPluginServer SHARED
Expand Down