Skip to content

[6.0][CMake] Enable package CMO if possible #2740

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
Jul 20, 2024
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
33 changes: 17 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ option(SWIFT_SYNTAX_ENABLE_WMO_PRE_3_26
$<IF:$<AND:$<NOT:$<CONFIG:Debug>>,$<PLATFORM_ID:Darwin>>,YES,NO>)

include(AddSwiftHostLibrary)
include(SwiftCompilerCapability)

# Ensure that we do not link the _StringProcessing module. But we can
# only pass this flag for new-enough compilers that support it.
file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift" "")
execute_process(
COMMAND
"${CMAKE_Swift_COMPILER}"
-Xfrontend -disable-implicit-string-processing-module-import
-Xfrontend -parse-stdlib
-typecheck "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift"
OUTPUT_QUIET ERROR_QUIET
RESULT_VARIABLE
SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
add_compile_options(
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend>
$<$<COMPILE_LANGUAGE:Swift>:-disable-implicit-string-processing-module-import>)
# Don't link with 'string-processing' and 'backtracing'.
swift_supports_implicit_module("string-processing" SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
swift_supports_implicit_module("backtracing" SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
if(SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>")
endif()
if(SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-backtracing-module-import>")
endif()

# SWIFTSYNTAX_EMIT_MODULE is TRUE by default
if(NOT DEFINED SWIFTSYNTAX_EMIT_MODULE)
set(SWIFTSYNTAX_EMIT_MODULE TRUE)
endif()
if(SWIFTSYNTAX_EMIT_MODULE)
swift_get_package_cmo_support(SWIFT_PACKAGE_CMO_SUPPORT)
endif()

# Determine the module triple.
Expand Down
19 changes: 19 additions & 0 deletions cmake/modules/AddSwiftHostLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ function(add_swift_syntax_library name)
-emit-module-interface-path;${module_interface_file};
-emit-private-module-interface-path;${module_private_interface_file}
>)

# Enable package CMO if possible.
if(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "IMPLEMENTED")
target_compile_options("${name}" PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
"SHELL:-Xfrontend -package-cmo"
"SHELL:-Xfrontend -allow-non-resilient-access"
>)
elseif(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "EXPERIMENTAL")
target_compile_options("${name}" PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
"SHELL:-Xfrontend -experimental-package-cmo"
"SHELL:-Xfrontend -experimental-allow-non-resilient-access"
"SHELL:-Xfrontend -experimental-package-bypass-resilience"
>)
endif()

if(SWIFT_MODULE_ABI_NAME_PREFIX)
# ABI name prefix. this can be used to avoid name conflicts.
target_compile_options("${name}" PRIVATE
Expand Down
52 changes: 52 additions & 0 deletions cmake/modules/SwiftCompilerCapability.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# Test if the Swift compiler returns success for supplied compiler arguments....
function(swift_supports_compiler_arguments out_var)
file(WRITE "${CMAKE_BINARY_DIR}/tmp/dummy.swift" "")
execute_process(
COMMAND "${CMAKE_Swift_COMPILER}" -parse ${ARGN} -
INPUT_FILE "${CMAKE_BINARY_DIR}/tmp/dummy.swift"
OUTPUT_QUIET ERROR_QUIET
RESULT_VARIABLE result
)
if(NOT result)
set("${out_var}" "TRUE" PARENT_SCOPE)
else()
set("${out_var}" "FALSE" PARENT_SCOPE)
endif()
endfunction()

# Test if the Swift compiler supports -disable-implicit-<module>-module-import.
macro(swift_supports_implicit_module module_name out_var)
swift_supports_compiler_arguments(${out_var}
-Xfrontend -disable-implicit-${module_name}-module-import
)
endmacro()

# Get "package cross-module-optimization" compiler arguments suitable for the compiler.
function(swift_get_package_cmo_support out_var)
# > 6.0 : Fixed feature.
swift_supports_compiler_arguments(result
-package-name my-package
-Xfrontend -package-cmo
-Xfrontend -allow-non-resilient-access
)
if(result)
set(${out_var} IMPLEMENTED PARENT_SCOPE)
return()
endif()

# == 6.0 : Experimental.
swift_supports_compiler_arguments(result
-package-name my-package
-Xfrontend -experimental-package-cmo
-Xfrontend -experimental-allow-non-resilient-access
-Xfrontend -experimental-package-bypass-resilience
)
if(result)
set(${out_var} EXPERIMENTAL PARENT_SCOPE)
return()
endif()

# < 6.0 : Not supported.
set(${out_var} NO PARENT_SCOPE)
endfunction()