Skip to content

Commit 48d93b5

Browse files
committed
wip
1 parent 019c50d commit 48d93b5

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public final class SwiftTypeInSubDirectory {
16+
public init() {}
17+
18+
public func hello() -> Int {
19+
12
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package com.example.swift;
16+
17+
import org.junit.jupiter.api.Test;
18+
import org.swift.swiftkit.core.SwiftArena;
19+
20+
import static org.junit.jupiter.api.Assertions.*;
21+
22+
public class SwiftTypeInSubDirectoryTest {
23+
24+
@Test
25+
void test_SwiftTypeInSubDirectory_hello() {
26+
try (var arena = SwiftArena.ofConfined()) {
27+
SwiftTypeInSubDirectory o = SwiftTypeInSubDirectory.init(arena);
28+
var num = o.hello();
29+
assertEquals(12, num);
30+
}
31+
}
32+
33+
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

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

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

3939

4040
var printer = CodePrinter()
@@ -61,7 +61,7 @@ extension JNISwift2JavaGenerator {
6161
filename: moduleFilename
6262
) {
6363
logger.info("Generated: \(moduleFilenameBase.bold).swift (at \(outputFile.absoluteString))")
64-
self.expectedOutputSwiftFiles.remove(moduleFilename)
64+
self.expectedOutputSwiftFileNames.remove(moduleFilename)
6565
}
6666

6767
// === All types
@@ -96,7 +96,7 @@ extension JNISwift2JavaGenerator {
9696
javaPackagePath: nil,
9797
filename: filename) {
9898
logger.info("Done writing Swift thunks to: \(outputFile.absoluteString)")
99-
self.expectedOutputSwiftFiles.remove(filename)
99+
self.expectedOutputSwiftFileNames.remove(filename)
100100
}
101101
} catch {
102102
logger.warning("Failed to write to Swift thunks: \(filename), error: \(error)")

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ package class JNISwift2JavaGenerator: Swift2JavaGenerator {
4444

4545
/// Because we need to write empty files for SwiftPM, keep track which files we didn't write yet,
4646
/// and write an empty file for those.
47-
var expectedOutputSwiftFiles: Set<String>
47+
///
48+
/// Since Swift files in SwiftPM builds needs to be unique, we use this fact to flatten paths into plain names here.
49+
/// For uniqueness checking "did we write this file already", just checking the name should be sufficient.
50+
var expectedOutputSwiftFileNames: Set<String>
4851

4952
package init(
5053
config: Configuration,
@@ -64,27 +67,34 @@ package class JNISwift2JavaGenerator: Swift2JavaGenerator {
6467
self.javaClassLookupTable = javaClassLookupTable
6568
self.lookupContext = translator.lookupContext
6669

67-
// If we are forced to write empty files, construct the expected outputs
70+
// If we are forced to write empty files, construct the expected outputs.
71+
// It is sufficient to use file names only, since SwiftPM requires names to be unique within a module anyway.
6872
if translator.config.writeEmptyFiles ?? false {
69-
self.expectedOutputSwiftFiles = Set(translator.inputs.compactMap { (input) -> String? in
70-
guard let filePathPart = input.path.split(separator: "/\(translator.swiftModuleName)/").last else {
73+
self.expectedOutputSwiftFileNames = Set(translator.inputs.compactMap { (input) -> String? in
74+
// guard let filePathPart = input.path.split(separator: "/\(translator.swiftModuleName)/").last else {
75+
// return nil
76+
// }
77+
// return String(filePathPart.replacing(".swift", with: "+SwiftJava.swift"))
78+
guard let fileName = input.path.split(separator: PATH_SEPARATOR).last else {
7179
return nil
7280
}
73-
74-
return String(filePathPart.replacing(".swift", with: "+SwiftJava.swift"))
81+
guard fileName.hasSuffix(".swift") else {
82+
return nil
83+
}
84+
return String(fileName.replacing(".swift", with: "+SwiftJava.swift"))
7585
})
76-
self.expectedOutputSwiftFiles.insert("\(translator.swiftModuleName)Module+SwiftJava.swift")
77-
self.expectedOutputSwiftFiles.insert("Foundation+SwiftJava.swift")
86+
self.expectedOutputSwiftFileNames.insert("\(translator.swiftModuleName)Module+SwiftJava.swift")
87+
self.expectedOutputSwiftFileNames.insert("Foundation+SwiftJava.swift")
7888
} else {
79-
self.expectedOutputSwiftFiles = []
89+
self.expectedOutputSwiftFileNames = []
8090
}
8191
}
8292

8393
func generate() throws {
8494
try writeSwiftThunkSources()
8595
try writeExportedJavaSources()
8696

87-
let pendingFileCount = self.expectedOutputSwiftFiles.count
97+
let pendingFileCount = self.expectedOutputSwiftFileNames.count
8898
if pendingFileCount > 0 {
8999
print("[swift-java] Write empty [\(pendingFileCount)] 'expected' files in: \(swiftOutputDirectory)/")
90100
try writeSwiftExpectedEmptySources()

0 commit comments

Comments
 (0)