Skip to content

Commit 0ccc1d9

Browse files
committed
Remove the extraction of mangled names via "nm" and supporting code
The Swift compiler is growing an option to emit mangled names along with various operations. Build example .swiftinterfaces with that option and rely on parsing the mangled name from there instead of 'nm'.
1 parent 12bc2e5 commit 0ccc1d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+40
-18698
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ define make_swiftinterface
7474
-emit-module-interface-path ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftinterface \
7575
-emit-module-path ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftmodule \
7676
-enable-library-evolution \
77+
-Xfrontend -abi-comments-in-module-interface \
7778
-module-name ${$@_MODULE} \
79+
-Xfrontend -abi-comments-in-module-interface \
7880
Sources/${$@_MODULE}/${$@_FILENAME}.swift
7981
echo "Generated: ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftinterface"
8082
endef

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ let package = Package(
9797
dependencies: [
9898
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: "main"),
9999
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
100-
.package(url: "https://github.com/apple/swift-system", from: "1.0.0"),
101100
],
102101
targets: [
103102
.macro(
@@ -195,33 +194,13 @@ let package = Package(
195194
]
196195
),
197196

198-
// FIXME: This is swift-foundation's proposed Subprocess; remove when available
199-
// https://github.com/apple/swift-foundation/pull/439
200-
.target(
201-
name: "_Subprocess",
202-
dependencies: [
203-
"_SubprocessCShims",
204-
.product(name: "SystemPackage", package: "swift-system"),
205-
]
206-
),
207-
.target(
208-
name: "_SubprocessCShims",
209-
cSettings: [
210-
.define(
211-
"_CRT_SECURE_NO_WARNINGS",
212-
.when(platforms: [.windows])
213-
)
214-
]
215-
),
216-
217197
.target(
218198
name: "JExtractSwift",
219199
dependencies: [
220200
.product(name: "SwiftBasicFormat", package: "swift-syntax"),
221201
.product(name: "SwiftSyntax", package: "swift-syntax"),
222202
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
223203
.product(name: "ArgumentParser", package: "swift-argument-parser"),
224-
"_Subprocess",
225204
"JavaTypes",
226205
]
227206
),

Sources/JExtractSwift/Convenience/Collection+Extensions.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,6 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
extension Collection {
15-
16-
@_alwaysEmitIntoClient
17-
public func _mapAsync<T, E>(
18-
_ transform: (Element) async throws(E) -> T
19-
) async throws(E) -> [T] {
20-
let initialCapacity = underestimatedCount
21-
var result = Array<T>()
22-
result.reserveCapacity(initialCapacity)
23-
24-
var iterator = self.makeIterator()
25-
26-
// Add elements up to the initial capacity without checking for regrowth.
27-
for _ in 0..<initialCapacity {
28-
result.append(try await transform(iterator.next()!))
29-
}
30-
// Add remaining elements, if any.
31-
while let element = iterator.next() {
32-
result.append(try await transform(element))
33-
}
34-
return Array(result)
35-
}
36-
}
37-
3814
public extension Dictionary {
3915
/// Same values, corresponding to mapped keys.
4016
func mapKeys<Transformed>(

Sources/JExtractSwift/Swift2Java.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SwiftSyntax
1717
import SwiftSyntaxBuilder
1818

1919
/// Command-line utility, similar to `jextract` to export Swift types to Java.
20-
public struct SwiftToJava: AsyncParsableCommand {
20+
public struct SwiftToJava: ParsableCommand {
2121
public init() {}
2222

2323
public static var _commandName: String {
@@ -40,7 +40,7 @@ public struct SwiftToJava: AsyncParsableCommand {
4040
@Argument(help: "The Swift interface files to export to Java.")
4141
var swiftInterfaceFiles: [String]
4242

43-
public func run() async throws {
43+
public func run() throws {
4444
let interfaceFiles = self.swiftInterfaceFiles.dropFirst()
4545
print("Interface files: \(interfaceFiles)")
4646

@@ -55,7 +55,7 @@ public struct SwiftToJava: AsyncParsableCommand {
5555
print("[\(fileNo)/\(interfaceFiles.count)] Importing module '\(swiftModule)', interface file: \(interfaceFile)")
5656
defer { fileNo += 1 }
5757

58-
try await translator.analyze(swiftInterfacePath: interfaceFile)
58+
try translator.analyze(swiftInterfacePath: interfaceFile)
5959
try translator.writeImportedTypesTo(outputDirectory: outputDirectory)
6060

6161
print("[\(fileNo)/\(interfaceFiles.count)] Imported interface file: \(interfaceFile) " + "done.".green)

Sources/JExtractSwift/Swift2JavaTranslator.swift

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension Swift2JavaTranslator {
6969
public func analyze(
7070
swiftInterfacePath: String,
7171
text: String? = nil
72-
) async throws {
72+
) throws {
7373
if text == nil {
7474
precondition(
7575
swiftInterfacePath.hasSuffix(Self.SWIFT_INTERFACE_SUFFIX),
@@ -84,12 +84,12 @@ extension Swift2JavaTranslator {
8484
log.trace("Analyze: \(swiftInterfacePath)")
8585
let text = try text ?? String(contentsOfFile: swiftInterfacePath)
8686

87-
try await analyzeSwiftInterface(interfaceFilePath: swiftInterfacePath, text: text)
87+
try analyzeSwiftInterface(interfaceFilePath: swiftInterfacePath, text: text)
8888

8989
log.info("Done processing: \(swiftInterfacePath)")
9090
}
9191

92-
package func analyzeSwiftInterface(interfaceFilePath: String, text: String) async throws {
92+
package func analyzeSwiftInterface(interfaceFilePath: String, text: String) throws {
9393
assert(interfaceFilePath.hasSuffix(Self.SWIFT_INTERFACE_SUFFIX))
9494

9595
let sourceFileSyntax = Parser.parse(source: text)
@@ -104,59 +104,6 @@ extension Swift2JavaTranslator {
104104
translator: self
105105
)
106106
visitor.walk(sourceFileSyntax)
107-
108-
try await self.postProcessImportedDecls()
109-
}
110-
111-
public func postProcessImportedDecls() async throws {
112-
log.info(
113-
"Post process imported decls...",
114-
metadata: [
115-
"types": "\(importedTypes.count)",
116-
"global/funcs": "\(importedGlobalFuncs.count)",
117-
]
118-
)
119-
120-
// FIXME: the use of dylibs to get symbols is a hack we need to remove and replace with interfaces containing mangled names
121-
let dylibPath = ".build/arm64-apple-macosx/debug/lib\(swiftModuleName).dylib"
122-
guard var dylib = SwiftDylib(path: dylibPath) else {
123-
log.warning(
124-
"""
125-
Unable to find mangled names for imported symbols. Dylib not found: \(dylibPath) This method of obtaining symbols is a workaround; it will be removed.
126-
"""
127-
)
128-
return
129-
}
130-
131-
importedGlobalFuncs = try await importedGlobalFuncs._mapAsync { funcDecl in
132-
let funcDecl = try await dylib.fillInMethodMangledName(funcDecl)
133-
log.info("Mapped method '\(funcDecl.identifier)' -> '\(funcDecl.swiftMangledName)'")
134-
return funcDecl
135-
}
136-
137-
importedTypes = Dictionary(uniqueKeysWithValues: try await importedTypes._mapAsync { (tyName, tyDecl) in
138-
var tyDecl = tyDecl
139-
log.info("Mapping type: \(tyDecl.swiftTypeName)")
140-
141-
tyDecl = try await dylib.fillInTypeMangledName(tyDecl)
142-
143-
log.info("Mapping members of: \(tyDecl.swiftTypeName)")
144-
tyDecl.initializers = try await tyDecl.initializers._mapAsync { initDecl in
145-
dylib.log.logLevel = .trace
146-
147-
let initDecl = try await dylib.fillInAllocatingInitMangledName(initDecl)
148-
log.info("Mapped initializer '\(initDecl.identifier)' -> '\(initDecl.swiftMangledName)'")
149-
return initDecl
150-
}
151-
152-
tyDecl.methods = try await tyDecl.methods._mapAsync { funcDecl in
153-
let funcDecl = try await dylib.fillInMethodMangledName(funcDecl)
154-
log.info("Mapped method '\(funcDecl.identifier)' -> '\(funcDecl.swiftMangledName)'")
155-
return funcDecl
156-
}
157-
158-
return (tyName, tyDecl)
159-
})
160107
}
161108
}
162109

0 commit comments

Comments
 (0)