Skip to content

Commit 38fdd9e

Browse files
authored
Merge pull request #27 from DougGregor/swiftinterface-mangled-names
2 parents 8335fd8 + 482e703 commit 38fdd9e

Some content is hidden

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

43 files changed

+94
-18714
lines changed

Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ BUILD_DIR := .build/$(ARCH_SUBDIR)-apple-macosx
3737
LIB_SUFFIX := dylib
3838
endif
3939

40+
ifeq ($(UNAME), Darwin)
41+
ifeq ("${TOOLCHAINS}", "")
42+
SWIFTC := "xcrun swiftc"
43+
else
44+
SWIFTC := "xcrun ${TOOLCHAINS}/usr/bin/swiftc"
45+
endif
46+
else
47+
ifeq ("${TOOLCHAINS}", "")
48+
SWIFTC := "swiftc"
49+
else
50+
SWIFTC := "${TOOLCHAINS}/usr/bin/swiftc"
51+
endif
52+
endif
53+
4054
SAMPLES_DIR := "Samples"
4155

4256
all: generate-all
@@ -83,11 +97,13 @@ JEXTRACT_BUILD_DIR="$(BUILD_DIR)/jextract"
8397
define make_swiftinterface
8498
$(eval $@_MODULE = $(1))
8599
$(eval $@_FILENAME = $(2))
86-
eval swiftc \
100+
eval ${SWIFTC} \
87101
-emit-module-interface-path ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftinterface \
88102
-emit-module-path ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftmodule \
89103
-enable-library-evolution \
104+
-Xfrontend -abi-comments-in-module-interface \
90105
-module-name ${$@_MODULE} \
106+
-Xfrontend -abi-comments-in-module-interface \
91107
Sources/${$@_MODULE}/${$@_FILENAME}.swift
92108
echo "Generated: ${JEXTRACT_BUILD_DIR}/${$@_MODULE}/${$@_FILENAME}.swiftinterface"
93109
endef

Package.swift

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ let package = Package(
122122
dependencies: [
123123
.package(url: "https://github.com/swiftlang/swift-syntax.git", branch: "main"),
124124
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
125-
.package(url: "https://github.com/apple/swift-system", from: "1.0.0"), // TODO: remove, we should not need 'nm' or process callouts
126125
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMinor(from: "1.1.0")),
127126
],
128127
targets: [
@@ -251,31 +250,6 @@ let package = Package(
251250
]
252251
),
253252

254-
// FIXME: This is swift-foundation's proposed Subprocess; remove when available
255-
// https://github.com/apple/swift-foundation/pull/439
256-
.target(
257-
name: "_Subprocess",
258-
dependencies: [
259-
"_SubprocessCShims",
260-
.product(name: "SystemPackage", package: "swift-system"),
261-
],
262-
swiftSettings: [
263-
.swiftLanguageMode(.v5)
264-
]
265-
),
266-
.target(
267-
name: "_SubprocessCShims",
268-
cSettings: [
269-
.define(
270-
"_CRT_SECURE_NO_WARNINGS",
271-
.when(platforms: [.windows])
272-
)
273-
],
274-
swiftSettings: [
275-
.swiftLanguageMode(.v5)
276-
]
277-
),
278-
279253
.target(
280254
name: "JExtractSwift",
281255
dependencies: [
@@ -284,7 +258,6 @@ let package = Package(
284258
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
285259
.product(name: "ArgumentParser", package: "swift-argument-parser"),
286260
.product(name: "Collections", package: "swift-collections"),
287-
"_Subprocess",
288261
"JavaTypes",
289262
],
290263
swiftSettings: [

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ The primary purpose of this repository is to create an environment for collabora
1616

1717
## Dependencies
1818

19+
### Required Swift Development Toolchains
20+
21+
To build and use this project, currently, you will need to download a custom toolchain which includes some improvements in Swift that this project relies on:
22+
23+
**Required toolchain download:**
24+
25+
- Go to https://www.swift.org/download/
26+
- Find the "latest" `Trunk Development (main)` toolchain for your OS
27+
28+
If these are too old, you can resort to one of these fallback toolchains:
29+
30+
Fallback development toolchain on **macOS**:
31+
32+
- https://ci.swift.org/job/swift-PR-toolchain-macos/1539/artifact/branch-main/swift-PR-76905-1539-osx.tar.gz
33+
34+
Fallback development toolchain on **Linux (Ubuntu 22.04)**:
35+
36+
```
37+
URL=$(curl -s "https://ci.swift.org/job/oss-swift-package-ubuntu-22_04/lastSuccessfulBuild/consoleText" | grep 'Toolchain: ' | sed 's/Toolchain: //g')
38+
wget ${URL}
39+
```
40+
41+
or just use the provided docker image (explained below).
42+
43+
https://www.swift.org/download/
44+
45+
46+
### Required JDK versions
47+
1948
This project consists of different modules which have different Swift and Java runtime requirements.
2049

2150
**JavaKit** – the Swift macros allowing the invocation of Java libraries from Swift

Samples/SwiftKitSampleApp/src/test/java/com/example/swift/generated/GeneratedJavaKitExampleModuleTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ static void beforeAll() {
3434
}
3535

3636
@Test
37-
@DisabledOnOs(OS.LINUX) // FIXME: enable on Linux when we get new compiler with mangled names in swift interfaces
3837
void call_helloWorld() {
3938
ExampleSwiftLibrary.helloWorld();
4039

4140
assertNotNull(ExampleSwiftLibrary.helloWorld$address());
4241
}
4342

4443
@Test
45-
@DisabledOnOs(OS.LINUX) // FIXME: enable on Linux when we get new compiler with mangled names in swift interfaces
4644
void call_globalTakeInt() {
4745
ExampleSwiftLibrary.globalTakeInt(12);
4846

Samples/SwiftKitSampleApp/src/test/java/com/example/swift/generated/MySwiftClassTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,19 @@ static void beforeAll() {
3535
}
3636

3737
@Test
38-
@DisabledOnOs(OS.LINUX) // FIXME: enable on Linux when we get new compiler with mangled names in swift interfaces
3938
void test_MySwiftClass_voidMethod() {
4039
MySwiftClass o = new MySwiftClass(12, 42);
4140
o.voidMethod();
4241
}
4342

4443
@Test
45-
@DisabledOnOs(OS.LINUX) // FIXME: enable on Linux when we get new compiler with mangled names in swift interfaces
4644
void test_MySwiftClass_makeIntMethod() {
4745
MySwiftClass o = new MySwiftClass(12, 42);
4846
var got = o.makeIntMethod();
4947
assertEquals(12, got);
5048
}
5149

5250
@Test
53-
@DisabledOnOs(OS.LINUX) // FIXME: enable on Linux when we get new compiler with mangled names in swift interfaces
5451
void test_MySwiftClass_property_len() {
5552
MySwiftClass o = new MySwiftClass(12, 42);
5653
var got = o.makeIntMethod();

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/MySwiftClassTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ static void beforeAll() {
3636
}
3737

3838
@Test
39-
@DisabledOnOs(OS.LINUX) // FIXME: enable on Linux when we get new compiler with mangled names in swift interfaces
4039
void call_retain_retainCount_release() {
4140
var obj = new MySwiftClass(1, 2);
4241

Sources/JExtractSwift/Convenience/Collection+Extensions.swift

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

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

Sources/JExtractSwift/Swift2Java.swift

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

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

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

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

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

59-
try await translator.analyze(swiftInterfacePath: interfaceFile)
59+
try translator.analyze(swiftInterfacePath: interfaceFile)
6060
try translator.writeImportedTypesTo(outputDirectory: outputDirectory)
6161

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

Sources/JExtractSwift/Swift2JavaTranslator.swift

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Swift2JavaTranslator {
7070
public func analyze(
7171
swiftInterfacePath: String,
7272
text: String? = nil
73-
) async throws {
73+
) throws {
7474
if text == nil {
7575
precondition(
7676
swiftInterfacePath.hasSuffix(Self.SWIFT_INTERFACE_SUFFIX),
@@ -85,12 +85,12 @@ extension Swift2JavaTranslator {
8585
log.trace("Analyze: \(swiftInterfacePath)")
8686
let text = try text ?? String(contentsOfFile: swiftInterfacePath)
8787

88-
try await analyzeSwiftInterface(interfaceFilePath: swiftInterfacePath, text: text)
88+
try analyzeSwiftInterface(interfaceFilePath: swiftInterfacePath, text: text)
8989

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

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

9696
let sourceFileSyntax = Parser.parse(source: text)
@@ -105,60 +105,8 @@ extension Swift2JavaTranslator {
105105
translator: self
106106
)
107107
visitor.walk(sourceFileSyntax)
108-
109-
try await self.postProcessImportedDecls()
110108
}
111109

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

164112
// ===== --------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)