Skip to content

Commit 2567804

Browse files
authored
Merge pull request #64262 from rintaro/swift-hostlib-module
[ASTGen] Separate JSON serialization module from ASTGen
2 parents a45f233 + 9004fd2 commit 2567804

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
@@ -159,8 +159,15 @@ endfunction()
159159
# STATIC
160160
# Build a static library.
161161
#
162-
# LLVM_LINK_COMPONENTS
163-
# LLVM components this library depends on.
162+
# EMIT_MODULE
163+
# Emit '.swiftmodule' to
164+
#
165+
# DEPENDENCIES
166+
# Target names to pass target_link_library
167+
#
168+
# SWIFT_DEPENDENCIES
169+
# Target names to pass force_target_link_library.
170+
# TODO: Remove this and use DEPENDENCIES when CMake is fixed
164171
#
165172
# source1 ...
166173
# Sources to add into this library.
@@ -173,10 +180,12 @@ function(add_pure_swift_host_library name)
173180
# Option handling
174181
set(options
175182
SHARED
176-
STATIC)
183+
STATIC
184+
EMIT_MODULE)
177185
set(single_parameter_options)
178186
set(multiple_parameter_options
179-
DEPENDENCIES)
187+
DEPENDENCIES
188+
SWIFT_DEPENDENCIES)
180189

181190
cmake_parse_arguments(APSHL
182191
"${options}"
@@ -262,11 +271,51 @@ function(add_pure_swift_host_library name)
262271
target_link_libraries(${name} PUBLIC
263272
${APSHL_DEPENDENCIES}
264273
)
274+
# TODO: Change to target_link_libraries when cmake is fixed
275+
force_target_link_libraries(${name} PUBLIC
276+
${APSHL_SWIFT_DEPENDENCIES}
277+
)
265278

266279
# Make sure we can use the host libraries.
267280
target_include_directories(${name} PUBLIC
268281
${SWIFT_HOST_LIBRARIES_DEST_DIR})
269282

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