Skip to content

Commit 46d44e0

Browse files
committed
Correct the SwiftAndJavaJarSampleLib example and actually verify created
jar
1 parent 8e628a3 commit 46d44e0

File tree

9 files changed

+63
-238
lines changed

9 files changed

+63
-238
lines changed

Package.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,6 @@ let package = Package(
175175
"JExtractSwiftPlugin"
176176
]
177177
),
178-
// .plugin(
179-
// name: "JExtractSwiftCommandPlugin",
180-
// targets: [
181-
// "JExtractSwiftCommandPlugin"
182-
// ]
183-
// ),
184178

185179
// ==== Examples
186180

@@ -429,16 +423,6 @@ let package = Package(
429423
"SwiftJavaTool"
430424
]
431425
),
432-
// .plugin(
433-
// name: "JExtractSwiftCommandPlugin",
434-
// capability: .command(
435-
// intent: .custom(verb: "jextract", description: "Extract Java accessors from Swift module"),
436-
// permissions: [
437-
// ]),
438-
// dependencies: [
439-
// "SwiftJavaTool"
440-
// ]
441-
// ),
442426

443427
.testTarget(
444428
name: "JavaKitTests",

Plugins/JExtractSwiftCommandPlugin/JExtractSwiftCommandPlugin.swift

Lines changed: 0 additions & 159 deletions
This file was deleted.

Plugins/JExtractSwiftCommandPlugin/_PluginsShared

Lines changed: 0 additions & 1 deletion
This file was deleted.

Plugins/JExtractSwiftPlugin/JExtractSwiftPlugin.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,6 @@ struct JExtractSwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
108108
inputFiles: [ configFile ] + swiftFiles,
109109
outputFiles: outputSwiftFiles
110110
)
111-
// .prebuildCommand(
112-
// displayName: "Generate Java wrappers for Swift types",
113-
// executable: toolURL,
114-
// arguments: arguments,
115-
// // inputFiles: [ configFile ] + swiftFiles,
116-
// // outputFiles: outputJavaFiles
117-
// outputFilesDirectory: outputSwiftDirectory
118-
// )
119111
]
120112
}
121113
}

Samples/SwiftAndJavaJarSampleLib/build.gradle

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,34 @@ dependencies {
4949
testImplementation("org.junit.jupiter:junit-jupiter")
5050
}
5151

52-
//// This is for development, when we edit the Swift swift-java project, the outputs of the generated sources may change.
53-
//// Thus, we also need to watch and re-build the top level project.
54-
//def compileSwiftJExtractPlugin = tasks.register("compileSwiftJava", Exec) {
55-
// description = "Rebuild the swift-java root project (" + swiftBuildConfiguration() + ")"
56-
//
57-
// inputs.file(new File(rootDir, "Package.swift"))
58-
// inputs.dir(new File(rootDir, "Sources"))
59-
// outputs.dir(new File(rootDir, ".build"))
60-
//
61-
// workingDir = rootDir
62-
// commandLine "swift"
63-
// args("build",
64-
// "-c", swiftBuildConfiguration(),
65-
// "--product", "swift-java")
66-
//}
52+
def swiftProductsWithJExtractPlugin() {
53+
def stdout = new ByteArrayOutputStream()
54+
def stderr = new ByteArrayOutputStream()
55+
56+
def result = exec {
57+
commandLine 'swift', 'package', 'describe', '--type', 'json'
58+
standardOutput = stdout
59+
errorOutput = stderr
60+
ignoreExitValue = true
61+
}
62+
63+
def jsonOutput = stdout.toString()
64+
65+
if (result.exitValue == 0) {
66+
def json = new JsonSlurper().parseText(jsonOutput)
67+
def products = json.targets
68+
.findAll { target ->
69+
target.product_dependencies?.contains("JExtractSwiftPlugin")
70+
}
71+
.collectMany { target ->
72+
target.product_memberships ?: []
73+
}
74+
return products
75+
} else {
76+
logger.warn("Command failed: ${stderr.toString()}")
77+
return []
78+
}
79+
}
6780

6881
def jextract = tasks.register("jextract", Exec) {
6982
description = "Generate Java wrappers for swift target"
@@ -73,12 +86,14 @@ def jextract = tasks.register("jextract", Exec) {
7386
inputs.file(new File(rootDir, "Package.swift"))
7487
inputs.dir(new File(rootDir, "Sources"))
7588

89+
// If the package description changes, we should execute jextract again, maybe we added jextract to new targets
7690
inputs.file(new File(projectDir, "Package.swift"))
77-
inputs.dir(new File(projectDir, "Sources"))
7891

79-
// TODO: we can use package describe --type json to figure out which targets depend on JExtractSwiftPlugin and will produce outputs
80-
// Avoid adding this directory, but create the expected one specifically for all targets
81-
// which WILL produce sources because they have the plugin
92+
// monitor all targets/products which depend on the JExtract plugin
93+
swiftProductsWithJExtractPlugin().each {
94+
logger.info("[swift-java:jextract (Gradle)] Swift input target: ${it}")
95+
inputs.dir(new File(layout.projectDirectory.asFile, "Sources/${it}".toString()))
96+
}
8297
outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}"))
8398

8499
File baseSwiftPluginOutputsDir = layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile
@@ -94,16 +109,16 @@ def jextract = tasks.register("jextract", Exec) {
94109

95110
workingDir = layout.projectDirectory
96111
commandLine "swift"
97-
args("run",
98-
"swift-java", "jextract",
99-
"--swift-module", "MySwiftLibrary",
100-
"--java-module", "MyJavaLibrary",
101-
// "--java-package", "com.example.myjava",
102-
"--java-output", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java")}",
103-
"--swift-output", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources")}",
104-
// "-v",
105-
// "--log-level", "info" // TODO: pass log level from Gradle build
106-
)
112+
args("build") // since Swift targets which need to be jextract-ed have the jextract build plugin, we just need to build
113+
// If we wanted to execute a specific subcommand, we can like this:
114+
// args("run",/*
115+
// "swift-java", "jextract",
116+
// "--swift-module", "MySwiftLibrary",
117+
// // java.package is obtained from the swift-java.config in the swift module
118+
// "--output-java", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java").get()}",
119+
// "--output-swift", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources").get()}",
120+
// "--log-level", (logging.level <= LogLevel.INFO ? "debug" : */"info")
121+
// )
107122
}
108123

109124

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -x
5+
6+
./gradlew jar
7+
8+
# check if we can compile a plain Example file that uses the generated Java bindings that should be in the generated jar
9+
javac -cp bin/default/build/libs/*jar Example.java
10+
11+
# Can we run the example?
12+
# - find libswiftCore.dylib
13+
SWIFT_DYLIB_PATHS=$(find "$(swiftly use --print-location)" | grep dylib$ | grep libswiftCore | grep macos | xargs dirname)
14+
# - find our library dylib
15+
SWIFT_DYLIB_PATHS="${SWIFT_DYLIB_PATHS}:$(find . | grep libMySwiftLibrary.dylib$ | sort | head -n1 | xargs dirname)"
16+
java -Djava.library.path="${SWIFT_DYLIB_PATHS}" -cp ".:bin/default/build/libs/*jar:../../SwiftKit/build/libs/*jar" Example

Samples/SwiftKitSampleApp/build.gradle

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,6 @@ java {
3434
}
3535
}
3636

37-
38-
// This is for development, when we edit the Swift swift-java project, the outputs of the generated sources may change.
39-
// Thus, we also need to watch and re-build the top level project.
40-
def compileSwiftJExtractPlugin = tasks.register("compileSwiftJExtractPlugin", Exec) {
41-
description = "Rebuild the swift-java root project"
42-
43-
inputs.file(new File(rootDir, "Package.swift"))
44-
inputs.dir(new File(rootDir, "Sources"))
45-
outputs.dir(new File(rootDir, ".build"))
46-
47-
workingDir = rootDir
48-
commandLine "swift"
49-
args("build",
50-
"--product", "SwiftKitSwift",
51-
"--product", "JExtractSwiftPlugin",
52-
"--product", "JExtractSwiftCommandPlugin")
53-
}
54-
5537
def jextract = tasks.register("jextract", Exec) {
5638
description = "Builds swift sources, including swift-java source generation"
5739
dependsOn compileSwiftJExtractPlugin

Sources/SwiftJavaTool/SwiftJava.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import JavaKitShared
2727

2828
/// Command-line utility to drive the export of Java classes into Swift types.
2929
@main
30-
struct SwiftJava: AsyncParsableCommand { // FIXME: this is just a normal async command, no parsing happening here
30+
struct SwiftJava: AsyncParsableCommand {
3131
static var _commandName: String { "swift-java" }
3232

3333
static let configuration = CommandConfiguration(
@@ -39,10 +39,6 @@ struct SwiftJava: AsyncParsableCommand { // FIXME: this is just a normal async c
3939
JExtractCommand.self
4040
])
4141

42-
var effectiveSwiftModule: String {
43-
fatalError("SHOULD NOT BE USED")
44-
}
45-
4642
mutating func run() async throws {
4743
guard CommandLine.arguments.count >= 2 else {
4844
// there's no "default" command, print USAGE when no arguments/parameters are passed.

SwiftKit/src/main/java/org/swift/swiftkit/SwiftValueLayout.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static long addressByteSize() {
4141
public static final ValueLayout.OfLong SWIFT_INT64 = ValueLayout.JAVA_LONG;
4242
public static final ValueLayout.OfFloat SWIFT_FLOAT = ValueLayout.JAVA_FLOAT;
4343
public static final ValueLayout.OfDouble SWIFT_DOUBLE = ValueLayout.JAVA_DOUBLE;
44-
public static final AddressLayout SWIFT_POINTER = ValueLayout.ADDRESS
45-
.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, JAVA_BYTE));
44+
public static final AddressLayout SWIFT_POINTER = ValueLayout.ADDRESS;
45+
// .withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, JAVA_BYTE));
4646
public static final SequenceLayout SWIFT_BYTE_ARRAY = MemoryLayout.sequenceLayout(8, ValueLayout.JAVA_BYTE);
4747

4848
/**

0 commit comments

Comments
 (0)