Skip to content

Commit 9004fd2

Browse files
committed
[ASTGen] Separate JSON serialization module from ASTGen
For future usage from other host libraries written in Swift For CMake: * Explicitly specify LINKER_LANGAGE to CXX in existing components so that 'swiftc' is not used when linking with 'swiftASTGen' * Add 'EMIT_MODULE' argument to 'add_pure_swift_host_library' to emit .swiftmodule usable from other Swift libraries.
1 parent a05853b commit 9004fd2

File tree

9 files changed

+98
-29
lines changed

9 files changed

+98
-29
lines changed

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function(add_swift_unittest test_dirname)
1010
# function defined by AddLLVM.cmake.
1111
add_unittest(SwiftUnitTests ${test_dirname} ${ARGN})
1212

13+
set_target_properties(${test_dirname} PROPERTIES LINKER_LANGUAGE CXX)
14+
1315
# TODO: _add_variant_c_compile_link_flags and these tests should share some
1416
# sort of logic.
1517
#

lib/ASTGen/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
add_pure_swift_host_library(swiftLLVMJSON STATIC EMIT_MODULE
2+
Sources/LLVMJSON/LLVMJSON.swift
3+
4+
DEPENDENCIES
5+
swiftBasic
6+
)
7+
18
add_pure_swift_host_library(swiftASTGen STATIC
29
Sources/ASTGen/ASTGen.swift
310
Sources/ASTGen/Decls.swift
411
Sources/ASTGen/Diagnostics.swift
512
Sources/ASTGen/Exprs.swift
613
Sources/ASTGen/Generics.swift
714
Sources/ASTGen/Literals.swift
8-
Sources/ASTGen/LLVMJSON.swift
915
Sources/ASTGen/Macros.swift
1016
Sources/ASTGen/Misc.swift
1117
Sources/ASTGen/PluginHost.swift
@@ -16,5 +22,8 @@ add_pure_swift_host_library(swiftASTGen STATIC
1622
Sources/ASTGen/Stmts.swift
1723
Sources/ASTGen/Types.swift
1824

19-
DEPENDENCIES swiftAST
25+
DEPENDENCIES
26+
swiftAST
27+
SWIFT_DEPENDENCIES
28+
swiftLLVMJSON
2029
)

lib/ASTGen/Package.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@
99

1010
import PackageDescription
1111

12+
let swiftSetttings: [SwiftSetting] = [
13+
.unsafeFlags([
14+
"-I", "../../include/swift/",
15+
"-I", "../../include",
16+
])
17+
]
18+
1219
let package = Package(
13-
name: "ASTGen",
20+
name: "swiftSwiftCompiler",
1421
platforms: [
1522
.macOS(.v10_15)
1623
],
@@ -29,14 +36,17 @@ let package = Package(
2936
.product(name: "SwiftOperators", package: "swift-syntax"),
3037
.product(name: "SwiftParser", package: "swift-syntax"),
3138
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
39+
"swiftLLVMJSON"
3240
],
33-
path: ".",
41+
path: "Sources/ASTGen",
3442
exclude: ["CMakeLists.txt"],
35-
swiftSettings: [
36-
.unsafeFlags([
37-
"-I", "../../include/swift/",
38-
"-I", "../../include",
39-
])
40-
])
43+
swiftSettings: swiftSetttings
44+
),
45+
.target(
46+
name: "swiftLLVMJSON",
47+
dependencies: [],
48+
path: "Sources/LLVMJSON",
49+
swiftSettings: swiftSetttings
50+
),
4151
]
4252
)

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import CASTBridging
1414
import CBasicBridging
1515
import SwiftSyntax
16+
import swiftLLVMJSON
1617

1718
enum PluginError: Error {
1819
case failedToSendMessage

lib/ASTGen/Sources/ASTGen/LLVMJSON.swift renamed to lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ extension String {
2121
}
2222
}
2323

24-
struct LLVMJSON {
24+
public struct LLVMJSON {
2525
/// Encode an `Encodable` value to JSON data, and call `body` is the buffer.
2626
/// Note that the buffer is valid onlu in `body`.
27-
static func encoding<T: Encodable, R>(_ value: T, body: (UnsafeBufferPointer<Int8>) -> R) throws -> R {
27+
public static func encoding<T: Encodable, R>(_ value: T, body: (UnsafeBufferPointer<Int8>) -> R) throws -> R {
2828
let valuePtr = JSON_newValue()
2929
defer { JSON_value_delete(valuePtr) }
3030

@@ -40,7 +40,7 @@ struct LLVMJSON {
4040
}
4141

4242
/// Decode a JSON data to a Swift value.
43-
static func decode<T: Decodable>(_ type: T.Type, from json: UnsafeBufferPointer<Int8>) throws -> T {
43+
public static func decode<T: Decodable>(_ type: T.Type, from json: UnsafeBufferPointer<Int8>) throws -> T {
4444
let data = BridgedData(baseAddress: json.baseAddress, size: json.count)
4545
let valuePtr = JSON_deserializedValue(data)
4646
defer { JSON_value_delete(valuePtr) }

lib/CMakeLists.txt

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,15 @@ endfunction()
151151
# STATIC
152152
# Build a static library.
153153
#
154-
# LLVM_LINK_COMPONENTS
155-
# LLVM components this library depends on.
154+
# EMIT_MODULE
155+
# Emit '.swiftmodule' to
156+
#
157+
# DEPENDENCIES
158+
# Target names to pass target_link_library
159+
#
160+
# SWIFT_DEPENDENCIES
161+
# Target names to pass force_target_link_library.
162+
# TODO: Remove this and use DEPENDENCIES when CMake is fixed
156163
#
157164
# source1 ...
158165
# Sources to add into this library.
@@ -165,10 +172,12 @@ function(add_pure_swift_host_library name)
165172
# Option handling
166173
set(options
167174
SHARED
168-
STATIC)
175+
STATIC
176+
EMIT_MODULE)
169177
set(single_parameter_options)
170178
set(multiple_parameter_options
171-
DEPENDENCIES)
179+
DEPENDENCIES
180+
SWIFT_DEPENDENCIES)
172181

173182
cmake_parse_arguments(APSHL
174183
"${options}"
@@ -254,11 +263,51 @@ function(add_pure_swift_host_library name)
254263
target_link_libraries(${name} PUBLIC
255264
${APSHL_DEPENDENCIES}
256265
)
266+
# TODO: Change to target_link_libraries when cmake is fixed
267+
force_target_link_libraries(${name} PUBLIC
268+
${APSHL_SWIFT_DEPENDENCIES}
269+
)
257270

258271
# Make sure we can use the host libraries.
259272
target_include_directories(${name} PUBLIC
260273
${SWIFT_HOST_LIBRARIES_DEST_DIR})
261274

275+
if(APSHL_EMIT_MODULE)
276+
# Determine where Swift modules will be built and installed.
277+
278+
set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE})
279+
set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR})
280+
set(module_base "${module_dir}/${name}.swiftmodule")
281+
set(module_file "${module_base}/${module_triple}.swiftmodule")
282+
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
283+
set(module_sourceinfo_file "${module_base}/${module_triple}.swiftsourceinfo")
284+
285+
set_target_properties(${name} PROPERTIES
286+
# Set the default module name to the target name.
287+
Swift_MODULE_NAME ${name}
288+
# Install the Swift module into the appropriate location.
289+
Swift_MODULE_DIRECTORY ${module_dir}
290+
# NOTE: workaround for CMake not setting up include flags.
291+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir})
292+
293+
# Create the module directory.
294+
add_custom_command(
295+
TARGET ${name}
296+
PRE_BUILD
297+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
298+
COMMENT "Generating module directory for ${name}")
299+
300+
# Configure the emission of the Swift module files.
301+
target_compile_options("${name}" PRIVATE
302+
$<$<COMPILE_LANGUAGE:Swift>:
303+
-module-name;$<TARGET_PROPERTY:${name},Swift_MODULE_NAME>;
304+
-enable-library-evolution;
305+
-emit-module-path;${module_file};
306+
-emit-module-source-info-path;${module_sourceinfo_file};
307+
-emit-module-interface-path;${module_interface_file}
308+
>)
309+
endif()
310+
262311
# Export this target.
263312
set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name})
264313
endfunction()

lib/Parse/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ target_link_libraries(swiftParse PRIVATE
2626
)
2727

2828
if (SWIFT_SWIFT_PARSER)
29-
# Link against the SwiftSyntax parser and libraries it depends on. The actual
30-
# formulation of this is a hack to work around a CMake bug in Ninja file
31-
# generation that results in multiple Ninja targets producing the same file in
32-
# a downstream SourceKit target. This should be expressed as:
33-
#
34-
# target_link_libraries(swiftParse
35-
# PRIVATE
36-
# SwiftSyntax::SwiftParser
37-
# ...
38-
# )
3929
target_link_libraries(swiftParse
4030
PRIVATE
4131
SwiftSyntax::SwiftBasicFormat
@@ -46,7 +36,7 @@ if (SWIFT_SWIFT_PARSER)
4636
SwiftSyntax::SwiftOperators
4737
SwiftSyntax::SwiftSyntaxBuilder
4838
SwiftSyntax::SwiftSyntaxMacros
49-
$<TARGET_OBJECTS:swiftASTGen>
39+
swiftASTGen
5040
)
5141

5242
add_dependencies(swiftParse

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ if (SWIFT_SWIFT_PARSER)
9393
SWIFT_SWIFT_PARSER
9494
)
9595
target_link_libraries(swiftSema PRIVATE
96-
$<TARGET_OBJECTS:swiftASTGen>)
96+
swiftASTGen)
9797
endif()
9898

9999
set_swift_llvm_is_available(swiftSema)

tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ macro(add_sourcekit_library name)
233233
endif()
234234
llvm_update_compile_flags(${name})
235235

236+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
237+
236238
set_output_directory(${name}
237239
BINARY_DIR ${SOURCEKIT_RUNTIME_OUTPUT_INTDIR}
238240
LIBRARY_DIR ${SOURCEKIT_LIBRARY_OUTPUT_INTDIR})
@@ -334,6 +336,8 @@ macro(add_sourcekit_executable name)
334336
set_target_properties(${name} PROPERTIES FOLDER "SourceKit executables")
335337
add_sourcekit_default_compiler_flags("${name}")
336338

339+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
340+
337341
if(SWIFT_SWIFT_PARSER)
338342
set(SKEXEC_HAS_SWIFT_MODULES TRUE)
339343
else()
@@ -387,6 +391,8 @@ macro(add_sourcekit_framework name)
387391
add_library(${name} SHARED ${srcs})
388392
llvm_update_compile_flags(${name})
389393

394+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
395+
390396
set(headers)
391397
foreach(src ${srcs})
392398
get_filename_component(extension ${src} EXT)
@@ -552,6 +558,8 @@ macro(add_sourcekit_xpc_service name framework_target)
552558
swift_common_llvm_config(${name} ${SOURCEKITXPC_LLVM_LINK_COMPONENTS})
553559
target_link_libraries(${name} PRIVATE ${LLVM_COMMON_LIBS})
554560

561+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
562+
555563
add_dependencies(${framework_target} ${name})
556564

557565
set(RPATH_LIST)

0 commit comments

Comments
 (0)