Skip to content

Commit b1d3b22

Browse files
committed
wip
1 parent 40d50b5 commit b1d3b22

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ extension FFMSwift2JavaGenerator {
3232
for expectedFileName in self.expectedOutputSwiftFiles {
3333
log.debug("Write SwiftPM-'expected' empty file: \(expectedFileName.bold)")
3434

35-
if expectedFileName.contains("MultiplePublicTypes") {
36-
log.error("MultiplePublicTypes MUST NOT BE EMPTY!") // FIXME: xxx
37-
}
3835

3936
var printer = CodePrinter()
4037
printer.print("// Empty file generated on purpose")

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ extension JNISwift2JavaGenerator {
2626
}
2727

2828
package func writeSwiftExpectedEmptySources() throws {
29+
let pendingFileCount = self.expectedOutputSwiftFiles.count
30+
guard pendingFileCount > 0 else {
31+
return // no need to write any empty files, yay
32+
}
33+
34+
print("[swift-java] Write empty [\(self.expectedOutputSwiftFiles.count)] 'expected' files in: \(swiftOutputDirectory)/")
35+
2936
for expectedFileName in self.expectedOutputSwiftFiles {
30-
logger.trace("Write empty file: \(expectedFileName) ...")
37+
logger.debug("Write SwiftPM-'expected' empty file: \(expectedFileName.bold)")
38+
3139

3240
var printer = CodePrinter()
3341
printer.print("// Empty file generated on purpose")
@@ -52,27 +60,46 @@ extension JNISwift2JavaGenerator {
5260
javaPackagePath: nil,
5361
filename: moduleFilename
5462
) {
55-
print("[swift-java] Generated: \(moduleFilenameBase.bold).swift (at \(outputFile))")
63+
logger.info("Generated: \(moduleFilenameBase.bold).swift (at \(outputFile.absoluteString))")
5664
self.expectedOutputSwiftFiles.remove(moduleFilename)
5765
}
5866

59-
for (_, ty) in self.analysis.importedTypes.sorted(by: { (lhs, rhs) in lhs.key < rhs.key }) {
60-
let fileNameBase = "\(ty.swiftNominal.qualifiedName)+SwiftJava"
61-
let filename = "\(fileNameBase).swift"
62-
logger.debug("Printing contents: \(filename)")
67+
// === All types
68+
// We have to write all types to their corresponding output file that matches the file they were declared in,
69+
// because otherwise SwiftPM plugins will not pick up files apropriately -- we expect 1 output +SwiftJava.swift file for every input.
70+
for group: (key: String, value: [Dictionary<String, ImportedNominalType>.Element]) in Dictionary(grouping: self.analysis.importedTypes, by: { $0.value.sourceFilePath }) {
71+
logger.warning("Writing types in file group: \(group.key): \(group.value.map(\.key))")
6372

64-
do {
65-
try printNominalTypeThunks(&printer, ty)
73+
let importedTypesForThisFile = group.value
74+
.map(\.value)
75+
.sorted(by: { $0.qualifiedName < $1.qualifiedName })
6676

77+
let inputFileName = "\(group.key)".split(separator: "/").last ?? "__Unknown.swift"
78+
let filename = "\(inputFileName)".replacing(".swift", with: "+SwiftJava.swift")
79+
80+
for ty in importedTypesForThisFile {
81+
logger.info("Printing Swift thunks for type: \(ty.qualifiedName.bold)")
82+
printer.printSeparator("Thunks for \(ty.qualifiedName)")
83+
84+
do {
85+
try printNominalTypeThunks(&printer, ty)
86+
} catch {
87+
logger.warning("Failed to print to Swift thunks for type'\(ty.qualifiedName)' to '\(filename)', error: \(error)")
88+
}
89+
90+
}
91+
92+
logger.warning("Write Swift thunks file: \(filename.bold)")
93+
do {
6794
if let outputFile = try printer.writeContents(
6895
outputDirectory: self.swiftOutputDirectory,
6996
javaPackagePath: nil,
7097
filename: filename) {
71-
print("[swift-java] Generated: \(fileNameBase.bold).swift (at \(outputFile))")
98+
logger.info("Done writing Swift thunks to: \(outputFile.absoluteString)")
7299
self.expectedOutputSwiftFiles.remove(filename)
73100
}
74101
} catch {
75-
logger.warning("Failed to write to Swift thunks: \(filename)")
102+
logger.warning("Failed to write to Swift thunks: \(filename), error: \(error)")
76103
}
77104
}
78105
} catch {

0 commit comments

Comments
 (0)