Skip to content

Commit 4e7bb71

Browse files
committed
[CMake] Add option to set the target namespace
Each target name is prefixed with SWIFTSYNTAX_TARGET_NAMESPACE Also, add SWIFTSYNTAX_EMIT_MODULE setting; 'true' for building library-evolution enabled modules with .swiftinterface, 'false' for building fragile modules, undefined is 'true'
1 parent 9a49bb7 commit 4e7bb71

File tree

6 files changed

+98
-76
lines changed

6 files changed

+98
-76
lines changed

Sources/SwiftLibraryPluginProvider/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
add_swift_syntax_library(SwiftLibraryPluginProvider
1010
LibraryPluginProvider.swift
1111
)
12-
target_link_libraries(SwiftLibraryPluginProvider PRIVATE
12+
target_link_swift_syntax_libraries(SwiftLibraryPluginProvider PRIVATE
1313
_SwiftLibraryPluginProviderCShims
1414
)
1515
target_link_swift_syntax_libraries(SwiftLibraryPluginProvider PUBLIC

Sources/SwiftSyntaxBuilder/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ add_swift_syntax_library(SwiftSyntaxBuilder
2828

2929
# Don't depend on OSLog when we are building for the compiler.
3030
# In that case we don't want any dependencies on the SDK.
31-
target_compile_options(SwiftSyntaxBuilder PRIVATE
31+
target_compile_options(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntaxBuilder PRIVATE
3232
$<$<COMPILE_LANGUAGE:Swift>:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>)
3333

3434
target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC

Sources/VersionMarkerModules/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
# Version Marker Modules.
1010
# See the 'Macro Versioning.md' document for more details.
11-
add_library(SwiftSyntax509 STATIC
11+
add_library(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntax509 STATIC
1212
SwiftSyntax509/Empty.swift)
13-
add_library(SwiftSyntax510 STATIC
13+
add_library(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntax510 STATIC
1414
SwiftSyntax509/Empty.swift)
15-
add_library(SwiftSyntax600 STATIC
15+
add_library(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntax600 STATIC
1616
SwiftSyntax509/Empty.swift)
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
add_library(_SwiftLibraryPluginProviderCShims STATIC
1+
set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}_SwiftLibraryPluginProviderCShims)
2+
add_library(${target} STATIC
23
LoadLibrary.c
34
)
4-
target_include_directories(_SwiftLibraryPluginProviderCShims PUBLIC "include")
5-
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS _SwiftLibraryPluginProviderCShims)
6-
install(TARGETS _SwiftLibraryPluginProviderCShims EXPORT SwiftSyntaxTargets)
5+
target_include_directories(${target} PUBLIC "include")
6+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target})
7+
install(TARGETS ${target} EXPORT SwiftSyntaxTargets)
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
add_library(_SwiftSyntaxCShims INTERFACE)
2-
target_include_directories(_SwiftSyntaxCShims INTERFACE "include")
3-
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS _SwiftSyntaxCShims)
4-
install(TARGETS _SwiftSyntaxCShims EXPORT SwiftSyntaxTargets)
1+
set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}_SwiftSyntaxCShims)
2+
add_library(${target} INTERFACE)
3+
target_include_directories(${target} INTERFACE "include")
4+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target})
5+
install(TARGETS ${target} EXPORT SwiftSyntaxTargets)

cmake/modules/AddSwiftHostLibrary.cmake

Lines changed: 83 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,33 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
# cmake generation for Swift adds an order only dependency, which matches how C-family languages
10-
# works. In that case, however, ninja (and presumably other generators) will rebuild on header
11-
# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
12-
# ABI/API of B changes.
13-
#
14-
# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
15-
# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
16-
# seem to be being tracked.
17-
#
18-
# Remove once rdar://102202478 is fixed.
199
function(target_link_swift_syntax_libraries TARGET)
20-
target_link_libraries(${TARGET} ${ARGN})
21-
2210
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
23-
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
11+
set(link_type)
12+
if(ARGS_PUBLIC)
13+
set(link_type PUBLIC)
14+
elseif(ARGS_PRIVATE)
15+
set(link_type PRIVATE)
16+
elseif(ARGS_INTERFACE)
17+
set(link_type INTERFACE)
18+
endif()
19+
20+
string(PREPEND TARGET ${SWIFTSYNTAX_TARGET_NAMESPACE})
21+
list(TRANSFORM ARGS_UNPARSED_ARGUMENTS PREPEND "${SWIFTSYNTAX_TARGET_NAMESPACE}" OUTPUT_VARIABLE dependencies)
22+
23+
target_link_libraries(${TARGET} ${link_type} ${dependencies})
24+
25+
# cmake generation for Swift adds an order only dependency, which matches how C-family languages
26+
# works. In that case, however, ninja (and presumably other generators) will rebuild on header
27+
# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
28+
# ABI/API of B changes.
29+
#
30+
# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
31+
# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
32+
# seem to be being tracked.
33+
#
34+
# Remove once rdar://102202478 is fixed.
35+
foreach(DEPENDENCY ${dependencies})
2436
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
2537
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
2638
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
@@ -36,103 +48,111 @@ endfunction()
3648
function(add_swift_syntax_library name)
3749
set(ASHL_SOURCES ${ARGN})
3850

39-
# Create the library target.
40-
add_library(${name} ${ASHL_SOURCES})
41-
42-
# Determine where Swift modules will be built and installed.
43-
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
44-
set(module_base "${module_dir}/${name}.swiftmodule")
45-
set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule")
46-
set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface")
47-
set(module_private_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.private.swiftinterface")
48-
set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo")
51+
set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}${name})
4952

50-
# Add a custom target to create the module directory.
51-
add_custom_command(
52-
TARGET ${name}
53+
# Create the library target.
54+
add_library(${target} ${ASHL_SOURCES})
55+
56+
if(NOT DEFINED SWIFTSYNTAX_EMIT_MODULE OR SWIFTSYNTAX_EMIT_MODULE)
57+
# Determine where Swift modules will be built and installed.
58+
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
59+
set(module_base "${module_dir}/${name}.swiftmodule")
60+
set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule")
61+
set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface")
62+
set(module_private_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.private.swiftinterface")
63+
set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo")
64+
65+
# Add a custom target to create the module directory.
66+
add_custom_command(
67+
TARGET ${target}
5368
PRE_BUILD
5469
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
55-
COMMENT "Generating module directory for ${name}")
70+
COMMENT "Generating module directory for ${target}")
71+
72+
# Configure the emission of the Swift module files.
73+
target_compile_options("${target}" PRIVATE
74+
$<$<COMPILE_LANGUAGE:Swift>:
75+
-DRESILIENT_LIBRARIES;
76+
-enable-library-evolution;
77+
-emit-module-path;${module_file};
78+
-emit-module-source-info-path;${module_sourceinfo_file};
79+
-emit-module-interface-path;${module_interface_file};
80+
-emit-private-module-interface-path;${module_private_interface_file}
81+
>)
82+
else()
83+
set(module_dir ${CMAKE_CURRENT_BINARY_DIR})
84+
set(module_base "${module_dir}/${name}.swiftmodule")
85+
set(module_file "${module_file}")
86+
endif()
5687

5788
# Touch the library and objects to workaround their mtime not being updated
5889
# when there are no real changes (eg. a file was updated with a comment).
5990
# Ideally this should be done in the driver, which could only update the
6091
# files that have changed.
6192
add_custom_command(
62-
TARGET ${name}
63-
POST_BUILD
64-
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${name}> $<TARGET_OBJECTS:${name}> "${module_base}"
65-
COMMAND_EXPAND_LISTS
66-
COMMENT "Update mtime of library outputs workaround")
67-
68-
# Install the Swift module into the appropriate location.
69-
set_target_properties(${name}
70-
PROPERTIES Swift_MODULE_DIRECTORY ${module_dir}
93+
TARGET ${target}
94+
POST_BUILD
95+
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${target}> $<TARGET_OBJECTS:${target}> "${module_base}"
96+
COMMAND_EXPAND_LISTS
97+
COMMENT "Update mtime of library outputs workaround")
98+
99+
set_target_properties(${target} PROPERTIES
100+
Swift_MODULE_NAME ${name}
101+
Swift_MODULE_DIRECTORY ${module_dir}
102+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
71103
)
72104

73105
# Configure the emission of the Swift module files.
74-
target_compile_options("${name}" PRIVATE
106+
target_compile_options("${target}" PRIVATE
75107
$<$<COMPILE_LANGUAGE:Swift>:
76-
-DRESILIENT_LIBRARIES;
77-
-module-name;${name};
78-
-enable-library-evolution;
79-
-emit-module-path;${module_file};
80-
-emit-module-source-info-path;${module_sourceinfo_file};
81-
-emit-module-interface-path;${module_interface_file};
82-
-emit-private-module-interface-path;${module_private_interface_file}
108+
"SHELL:-module-name ${name}"
109+
"SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name}"
83110
>)
84-
if(SWIFT_MODULE_ABI_NAME_PREFIX)
85-
# ABI name prefix. this can be used to avoid name conflicts.
86-
target_compile_options("${name}" PRIVATE
87-
$<$<COMPILE_LANGUAGE:Swift>:
88-
"SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name}"
89-
>)
90-
endif()
91111

92112
if(CMAKE_VERSION VERSION_LESS 3.26.0 AND SWIFT_SYNTAX_ENABLE_WMO_PRE_3_26)
93-
target_compile_options(${name} PRIVATE
113+
target_compile_options(${target} PRIVATE
94114
$<$<COMPILE_LANGUAGE:Swift>:-wmo>)
95115
endif()
96116

97-
target_compile_options(${name} PRIVATE
117+
target_compile_options(${target} PRIVATE
98118
$<$<COMPILE_LANGUAGE:Swift>:-color-diagnostics>
99119
)
100120

101121
if(LLVM_USE_LINKER)
102-
target_link_options(${name} PRIVATE
122+
target_link_options(${target} PRIVATE
103123
"-use-ld=${LLVM_USE_LINKER}"
104124
)
105125
endif()
106126

107127
# NOTE: workaround for CMake not setting up include flags yet
108-
set_target_properties(${name} PROPERTIES
128+
set_target_properties(${target} PROPERTIES
109129
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
110130
)
111131

112-
set_target_properties(${name} PROPERTIES
132+
set_target_properties(${target} PROPERTIES
113133
BUILD_WITH_INSTALL_RPATH YES
114134
)
115135

116136
if(SWIFT_HOST_LIBRARIES_RPATH)
117137
# Don't add builder's stdlib RPATH automatically.
118-
target_compile_options(${name} PRIVATE -no-toolchain-stdlib-rpath)
119-
set_property(TARGET ${name}
138+
target_compile_options(${target} PRIVATE -no-toolchain-stdlib-rpath)
139+
set_property(TARGET ${target}
120140
PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}"
121141
)
122142
endif()
123143

124-
get_target_property(lib_type ${name} TYPE)
144+
get_target_property(lib_type ${target} TYPE)
125145
if(lib_type STREQUAL SHARED_LIBRARY)
126146
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
127147
# Allow install_name_tool to update paths (for rdar://109473564)
128-
set_property(TARGET ${name} APPEND_STRING PROPERTY
148+
set_property(TARGET ${target} APPEND_STRING PROPERTY
129149
LINK_FLAGS " -Xlinker -headerpad_max_install_names")
130150
endif()
131151
endif()
132152

133153
if(PROJECT_IS_TOP_LEVEL OR SWIFT_SYNTAX_INSTALL_TARGETS)
134154
# Install this target
135-
install(TARGETS ${name}
155+
install(TARGETS ${target}
136156
EXPORT SwiftSyntaxTargets
137157
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
138158
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
@@ -146,7 +166,7 @@ function(add_swift_syntax_library name)
146166
FILES_MATCHING PATTERN "*.swiftinterface"
147167
)
148168
else()
149-
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
169+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target})
150170
endif()
151-
add_library(SwiftSyntax::${name} ALIAS ${name})
171+
add_library(SwiftSyntax::${target} ALIAS ${target})
152172
endfunction()

0 commit comments

Comments
 (0)