Skip to content

Commit 26ae5e9

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 26ae5e9

File tree

6 files changed

+87
-66
lines changed

6 files changed

+87
-66
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: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@
1717
#
1818
# Remove once rdar://102202478 is fixed.
1919
function(target_link_swift_syntax_libraries TARGET)
20-
target_link_libraries(${TARGET} ${ARGN})
21-
2220
cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN})
23-
foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS})
21+
set(link_type)
22+
if(ARGS_PUBLIC)
23+
set(link_type PUBLIC)
24+
elseif(ARGS_PRIVATE)
25+
set(link_type PRIVATE)
26+
elseif(ARGS_INTERFACE)
27+
set(link_type INTERFACE)
28+
endif()
29+
30+
string(PREPEND TARGET ${SWIFTSYNTAX_TARGET_NAMESPACE})
31+
list(TRANSFORM ARGS_UNPARSED_ARGUMENTS PREPEND "${SWIFTSYNTAX_TARGET_NAMESPACE}" OUTPUT_VARIABLE dependencies)
32+
33+
target_link_libraries(${TARGET} ${link_type} ${dependencies})
34+
foreach(DEPENDENCY ${dependencies})
2435
string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY})
2536
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
2637
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift
@@ -36,103 +47,111 @@ endfunction()
3647
function(add_swift_syntax_library name)
3748
set(ASHL_SOURCES ${ARGN})
3849

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")
50+
set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}${name})
4951

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

5787
# Touch the library and objects to workaround their mtime not being updated
5888
# when there are no real changes (eg. a file was updated with a comment).
5989
# Ideally this should be done in the driver, which could only update the
6090
# files that have changed.
6191
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}
92+
TARGET ${target}
93+
POST_BUILD
94+
COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $<TARGET_FILE:${target}> $<TARGET_OBJECTS:${target}> "${module_base}"
95+
COMMAND_EXPAND_LISTS
96+
COMMENT "Update mtime of library outputs workaround")
97+
98+
set_target_properties(${target} PROPERTIES
99+
Swift_MODULE_NAME ${name}
100+
Swift_MODULE_DIRECTORY ${module_dir}
101+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
71102
)
72103

73104
# Configure the emission of the Swift module files.
74-
target_compile_options("${name}" PRIVATE
105+
target_compile_options("${target}" PRIVATE
75106
$<$<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}
107+
"SHELL:-module-name ${name}"
108+
"SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name}"
83109
>)
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()
91110

92111
if(CMAKE_VERSION VERSION_LESS 3.26.0 AND SWIFT_SYNTAX_ENABLE_WMO_PRE_3_26)
93-
target_compile_options(${name} PRIVATE
112+
target_compile_options(${target} PRIVATE
94113
$<$<COMPILE_LANGUAGE:Swift>:-wmo>)
95114
endif()
96115

97-
target_compile_options(${name} PRIVATE
116+
target_compile_options(${target} PRIVATE
98117
$<$<COMPILE_LANGUAGE:Swift>:-color-diagnostics>
99118
)
100119

101120
if(LLVM_USE_LINKER)
102-
target_link_options(${name} PRIVATE
121+
target_link_options(${target} PRIVATE
103122
"-use-ld=${LLVM_USE_LINKER}"
104123
)
105124
endif()
106125

107126
# NOTE: workaround for CMake not setting up include flags yet
108-
set_target_properties(${name} PROPERTIES
127+
set_target_properties(${target} PROPERTIES
109128
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
110129
)
111130

112-
set_target_properties(${name} PROPERTIES
131+
set_target_properties(${target} PROPERTIES
113132
BUILD_WITH_INSTALL_RPATH YES
114133
)
115134

116135
if(SWIFT_HOST_LIBRARIES_RPATH)
117136
# Don't add builder's stdlib RPATH automatically.
118-
target_compile_options(${name} PRIVATE -no-toolchain-stdlib-rpath)
119-
set_property(TARGET ${name}
137+
target_compile_options(${target} PRIVATE -no-toolchain-stdlib-rpath)
138+
set_property(TARGET ${target}
120139
PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}"
121140
)
122141
endif()
123142

124-
get_target_property(lib_type ${name} TYPE)
143+
get_target_property(lib_type ${target} TYPE)
125144
if(lib_type STREQUAL SHARED_LIBRARY)
126145
if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
127146
# Allow install_name_tool to update paths (for rdar://109473564)
128-
set_property(TARGET ${name} APPEND_STRING PROPERTY
147+
set_property(TARGET ${target} APPEND_STRING PROPERTY
129148
LINK_FLAGS " -Xlinker -headerpad_max_install_names")
130149
endif()
131150
endif()
132151

133152
if(PROJECT_IS_TOP_LEVEL OR SWIFT_SYNTAX_INSTALL_TARGETS)
134153
# Install this target
135-
install(TARGETS ${name}
154+
install(TARGETS ${target}
136155
EXPORT SwiftSyntaxTargets
137156
ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
138157
LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
@@ -146,7 +165,7 @@ function(add_swift_syntax_library name)
146165
FILES_MATCHING PATTERN "*.swiftinterface"
147166
)
148167
else()
149-
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
168+
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target})
150169
endif()
151-
add_library(SwiftSyntax::${name} ALIAS ${name})
170+
add_library(SwiftSyntax::${target} ALIAS ${target})
152171
endfunction()

0 commit comments

Comments
 (0)