Skip to content

Commit 6a4f5d8

Browse files
authored
[CMake] Enable package CMO if possible (#2740)
Package cross-module-optimization allows non-resilient access between modules as long as they share the same package name. (cherry picked from commit 5a090ce)
1 parent 5d4b04d commit 6a4f5d8

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
lines changed

CMakeLists.txt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,24 @@ option(SWIFT_SYNTAX_ENABLE_WMO_PRE_3_26
4242
$<IF:$<AND:$<NOT:$<CONFIG:Debug>>,$<PLATFORM_ID:Darwin>>,YES,NO>)
4343

4444
include(AddSwiftHostLibrary)
45+
include(SwiftCompilerCapability)
4546

46-
# Ensure that we do not link the _StringProcessing module. But we can
47-
# only pass this flag for new-enough compilers that support it.
48-
file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift" "")
49-
execute_process(
50-
COMMAND
51-
"${CMAKE_Swift_COMPILER}"
52-
-Xfrontend -disable-implicit-string-processing-module-import
53-
-Xfrontend -parse-stdlib
54-
-typecheck "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift"
55-
OUTPUT_QUIET ERROR_QUIET
56-
RESULT_VARIABLE
57-
SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
58-
if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
59-
add_compile_options(
60-
$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend>
61-
$<$<COMPILE_LANGUAGE:Swift>:-disable-implicit-string-processing-module-import>)
47+
# Don't link with 'string-processing' and 'backtracing'.
48+
swift_supports_implicit_module("string-processing" SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
49+
swift_supports_implicit_module("backtracing" SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
50+
if(SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT)
51+
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>")
52+
endif()
53+
if(SWIFT_SUPPORTS_DISABLE_IMPLICIT_BACKTRACING_MODULE_IMPORT)
54+
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-backtracing-module-import>")
55+
endif()
56+
57+
# SWIFTSYNTAX_EMIT_MODULE is TRUE by default
58+
if(NOT DEFINED SWIFTSYNTAX_EMIT_MODULE)
59+
set(SWIFTSYNTAX_EMIT_MODULE TRUE)
60+
endif()
61+
if(SWIFTSYNTAX_EMIT_MODULE)
62+
swift_get_package_cmo_support(SWIFT_PACKAGE_CMO_SUPPORT)
6263
endif()
6364

6465
# Determine the module triple.

cmake/modules/AddSwiftHostLibrary.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ function(add_swift_syntax_library name)
8787
-emit-module-interface-path;${module_interface_file};
8888
-emit-private-module-interface-path;${module_private_interface_file}
8989
>)
90+
91+
# Enable package CMO if possible.
92+
if(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "IMPLEMENTED")
93+
target_compile_options("${name}" PRIVATE
94+
$<$<COMPILE_LANGUAGE:Swift>:
95+
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
96+
"SHELL:-Xfrontend -package-cmo"
97+
"SHELL:-Xfrontend -allow-non-resilient-access"
98+
>)
99+
elseif(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "EXPERIMENTAL")
100+
target_compile_options("${name}" PRIVATE
101+
$<$<COMPILE_LANGUAGE:Swift>:
102+
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
103+
"SHELL:-Xfrontend -experimental-package-cmo"
104+
"SHELL:-Xfrontend -experimental-allow-non-resilient-access"
105+
"SHELL:-Xfrontend -experimental-package-bypass-resilience"
106+
>)
107+
endif()
108+
90109
if(SWIFT_MODULE_ABI_NAME_PREFIX)
91110
# ABI name prefix. this can be used to avoid name conflicts.
92111
target_compile_options("${name}" PRIVATE
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# Test if the Swift compiler returns success for supplied compiler arguments....
3+
function(swift_supports_compiler_arguments out_var)
4+
file(WRITE "${CMAKE_BINARY_DIR}/tmp/dummy.swift" "")
5+
execute_process(
6+
COMMAND "${CMAKE_Swift_COMPILER}" -parse ${ARGN} -
7+
INPUT_FILE "${CMAKE_BINARY_DIR}/tmp/dummy.swift"
8+
OUTPUT_QUIET ERROR_QUIET
9+
RESULT_VARIABLE result
10+
)
11+
if(NOT result)
12+
set("${out_var}" "TRUE" PARENT_SCOPE)
13+
else()
14+
set("${out_var}" "FALSE" PARENT_SCOPE)
15+
endif()
16+
endfunction()
17+
18+
# Test if the Swift compiler supports -disable-implicit-<module>-module-import.
19+
macro(swift_supports_implicit_module module_name out_var)
20+
swift_supports_compiler_arguments(${out_var}
21+
-Xfrontend -disable-implicit-${module_name}-module-import
22+
)
23+
endmacro()
24+
25+
# Get "package cross-module-optimization" compiler arguments suitable for the compiler.
26+
function(swift_get_package_cmo_support out_var)
27+
# > 6.0 : Fixed feature.
28+
swift_supports_compiler_arguments(result
29+
-package-name my-package
30+
-Xfrontend -package-cmo
31+
-Xfrontend -allow-non-resilient-access
32+
)
33+
if(result)
34+
set(${out_var} IMPLEMENTED PARENT_SCOPE)
35+
return()
36+
endif()
37+
38+
# == 6.0 : Experimental.
39+
swift_supports_compiler_arguments(result
40+
-package-name my-package
41+
-Xfrontend -experimental-package-cmo
42+
-Xfrontend -experimental-allow-non-resilient-access
43+
-Xfrontend -experimental-package-bypass-resilience
44+
)
45+
if(result)
46+
set(${out_var} EXPERIMENTAL PARENT_SCOPE)
47+
return()
48+
endif()
49+
50+
# < 6.0 : Not supported.
51+
set(${out_var} NO PARENT_SCOPE)
52+
endfunction()

0 commit comments

Comments
 (0)