Skip to content

Commit b5cd7c9

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 b5cd7c9

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
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 'backtracking'.
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: 22 additions & 2 deletions
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")
@@ -77,8 +77,28 @@ function(add_swift_syntax_library name)
7777
-emit-module-path;${module_file};
7878
-emit-module-source-info-path;${module_sourceinfo_file};
7979
-emit-module-interface-path;${module_interface_file};
80-
-emit-private-module-interface-path;${module_private_interface_file}
80+
-emit-private-module-interface-path;${module_private_interface_file};
81+
-no-verify-emitted-module-interface
8182
>)
83+
84+
# Enable package CMO if possible.
85+
if(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "IMPLEMENTED")
86+
target_compile_options("${target}" PRIVATE
87+
$<$<COMPILE_LANGUAGE:Swift>:
88+
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
89+
"SHELL:-Xfrontend -package-cmo"
90+
"SHELL:-Xfrontend -allow-non-resilient-access"
91+
>)
92+
elseif(SWIFT_PACKAGE_CMO_SUPPORT STREQUAL "EXPERIMENTAL")
93+
target_compile_options("${target}" PRIVATE
94+
$<$<COMPILE_LANGUAGE:Swift>:
95+
"SHELL:-package-name ${SWIFT_MODULE_ABI_NAME_PREFIX}${PROJECT_NAME}"
96+
"SHELL:-Xfrontend -experimental-package-cmo"
97+
"SHELL:-Xfrontend -experimental-allow-non-resilient-access"
98+
"SHELL:-Xfrontend -experimental-package-bypass-resilience"
99+
>)
100+
endif()
101+
82102
else()
83103
set(module_dir ${CMAKE_CURRENT_BINARY_DIR})
84104
set(module_base "${module_dir}/${name}.swiftmodule")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
RESULT_VARIABLE result
9+
)
10+
if(NOT result)
11+
set("${out_var}" "TRUE" PARENT_SCOPE)
12+
else()
13+
set("${out_var}" "FALSE" PARENT_SCOPE)
14+
endif()
15+
endfunction()
16+
17+
# Test if the Swift compiler supports -disable-implicit-<module>-module-import.
18+
macro(swift_supports_implicit_module module_name out_var)
19+
swift_supports_compiler_arguments(${out_var}
20+
-Xfrontend -disable-implicit-${module_name}-module-import
21+
)
22+
endmacro()
23+
24+
# Get "package cross-module-optimization" compiler arguments suitable for the compiler.
25+
function(swift_get_package_cmo_support out_var)
26+
# > 6.0 : Fixed feature.
27+
swift_supports_compiler_arguments(result
28+
-package-name my-package
29+
-Xfrontend -package-cmo
30+
-Xfrontend -allow-non-resilient-access
31+
)
32+
if(result)
33+
set(${out_var} IMPLEMENTED PARENT_SCOPE)
34+
return()
35+
endif()
36+
37+
# == 6.0 : Experimental.
38+
swift_supports_compiler_arguments(result
39+
-package-name my-package
40+
-Xfrontend -experimental-package-cmo
41+
-Xfrontend -experimental-allow-non-resilient-access
42+
-Xfrontend -experimental-package-bypass-resilience
43+
)
44+
if(result)
45+
set(${out_var} EXPERIMENTAL PARENT_SCOPE)
46+
return()
47+
endif()
48+
49+
# < 6.0 : Not supported.
50+
set(${out_var} NO PARENT_SCOPE)
51+
endfunction()

0 commit comments

Comments
 (0)