Skip to content

Commit 5a090ce

Browse files
committed
[CMake] Enable package CMO if possible
Package cross-module-optimization allows non-resilient access between modules as long as they share the same package name.
1 parent 248dcef commit 5a090ce

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function(add_swift_syntax_library name)
5353
# Create the library target.
5454
add_library(${target} ${ASHL_SOURCES})
5555

56-
if(NOT DEFINED SWIFTSYNTAX_EMIT_MODULE OR SWIFTSYNTAX_EMIT_MODULE)
56+
if(SWIFTSYNTAX_EMIT_MODULE)
5757
# Determine where Swift modules will be built and installed.
5858
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
5959
set(module_base "${module_dir}/${name}.swiftmodule")
@@ -79,6 +79,25 @@ function(add_swift_syntax_library name)
7979
-emit-module-interface-path;${module_interface_file};
8080
-emit-private-module-interface-path;${module_private_interface_file}
8181
>)
82+
83+
# Enable package CMO if possible.
84+
if(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "IMPLEMENTED")
85+
target_compile_options("${target}" PRIVATE
86+
$<$<COMPILE_LANGUAGE:Swift>:
87+
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
88+
"SHELL:-Xfrontend -package-cmo"
89+
"SHELL:-Xfrontend -allow-non-resilient-access"
90+
>)
91+
elseif(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "EXPERIMENTAL")
92+
target_compile_options("${target}" PRIVATE
93+
$<$<COMPILE_LANGUAGE:Swift>:
94+
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
95+
"SHELL:-Xfrontend -experimental-package-cmo"
96+
"SHELL:-Xfrontend -experimental-allow-non-resilient-access"
97+
"SHELL:-Xfrontend -experimental-package-bypass-resilience"
98+
>)
99+
endif()
100+
82101
else()
83102
set(module_dir ${CMAKE_CURRENT_BINARY_DIR})
84103
set(module_base "${module_dir}/${name}.swiftmodule")
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)