Skip to content

Commit 447efaf

Browse files
committed
Make use of jextract build plugin from all gradle builds (TODO: make it a plugin)
1 parent e968053 commit 447efaf

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

Samples/SwiftAndJavaJarSampleLib/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def swiftCheckValid = tasks.register("swift-check-valid", Exec) {
8686
def jextract = tasks.register("jextract", Exec) {
8787
description = "Generate Java wrappers for swift target"
8888
dependsOn swiftCheckValid
89-
// dependsOn compileSwiftJExtractPlugin
9089

9190
// only because we depend on "live developing" the plugin while using this project to test it
9291
inputs.file(new File(rootDir, "Package.swift"))
@@ -96,8 +95,7 @@ def jextract = tasks.register("jextract", Exec) {
9695
inputs.file(new File(projectDir, "Package.swift"))
9796

9897
// monitor all targets/products which depend on the JExtract plugin
99-
// swiftProductsWithJExtractPlugin().each {
100-
["MySwiftLibrary"].each {
98+
swiftProductsWithJExtractPlugin().each {
10199
logger.info("[swift-java:jextract (Gradle)] Swift input target: ${it}")
102100
inputs.dir(new File(layout.projectDirectory.asFile, "Sources/${it}".toString()))
103101
}

Samples/SwiftKitSampleApp/build.gradle

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import groovy.json.JsonSlurper
1516
import org.swift.swiftkit.gradle.BuildUtils
1617

1718
import java.nio.file.*
19+
import kotlinx.serialization.json.*
1820

1921
plugins {
2022
id("build-logic.java-application-conventions")
@@ -34,26 +36,57 @@ java {
3436
}
3537
}
3638

39+
def swiftProductsWithJExtractPlugin() {
40+
def stdout = new ByteArrayOutputStream()
41+
def stderr = new ByteArrayOutputStream()
42+
43+
def result = exec {
44+
commandLine 'swift', 'package', 'describe', '--type', 'json'
45+
standardOutput = stdout
46+
errorOutput = stderr
47+
ignoreExitValue = true
48+
}
49+
50+
def jsonOutput = stdout.toString()
51+
52+
if (result.exitValue == 0) {
53+
def json = new JsonSlurper().parseText(jsonOutput)
54+
def products = json.targets
55+
.findAll { target ->
56+
target.product_dependencies?.contains("JExtractSwiftPlugin")
57+
}
58+
.collectMany { target ->
59+
target.product_memberships ?: []
60+
}
61+
return products
62+
} else {
63+
logger.warn("Command failed: ${stderr.toString()}")
64+
return []
65+
}
66+
}
67+
68+
3769
def swiftCheckValid = tasks.register("swift-check-valid", Exec) {
3870
commandLine "swift"
3971
args("-version")
4072
}
4173

4274
def jextract = tasks.register("jextract", Exec) {
43-
description = "Builds swift sources, including swift-java source generation"
75+
description = "Generate Java wrappers for swift target"
4476
dependsOn swiftCheckValid
45-
// dependsOn compileSwiftJExtractPlugin
4677

4778
// only because we depend on "live developing" the plugin while using this project to test it
4879
inputs.file(new File(rootDir, "Package.swift"))
4980
inputs.dir(new File(rootDir, "Sources"))
5081

82+
// If the package description changes, we should execute jextract again, maybe we added jextract to new targets
5183
inputs.file(new File(projectDir, "Package.swift"))
52-
inputs.dir(new File(projectDir, "Sources"))
5384

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

5992
File baseSwiftPluginOutputsDir = layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile
@@ -69,7 +102,16 @@ def jextract = tasks.register("jextract", Exec) {
69102

70103
workingDir = layout.projectDirectory
71104
commandLine "swift"
72-
args("run", "swift-java", "jextract", "-v", "--log-level", "info") // TODO: pass log level from Gradle build
105+
args("build") // since Swift targets which need to be jextract-ed have the jextract build plugin, we just need to build
106+
// If we wanted to execute a specific subcommand, we can like this:
107+
// args("run",/*
108+
// "swift-java", "jextract",
109+
// "--swift-module", "MySwiftLibrary",
110+
// // java.package is obtained from the swift-java.config in the swift module
111+
// "--output-java", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java").get()}",
112+
// "--output-swift", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources").get()}",
113+
// "--log-level", (logging.level <= LogLevel.INFO ? "debug" : */"info")
114+
// )
73115
}
74116

75117
// Add the java-swift generated Java sources

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public static MemorySegment fullTypeMetadata(MemorySegment typeMetadata) {
7171
public static MemorySegment valueWitnessTable(MemorySegment typeMetadata) {
7272
return fullTypeMetadata(typeMetadata)
7373
.get(SwiftValueLayout.SWIFT_POINTER, SwiftValueWitnessTable.fullTypeMetadata$vwt$offset);
74-
// .get(ValueLayout.ADDRESS, SwiftValueWitnessTable.fullTypeMetadata$vwt$offset);
7574
}
7675

7776

0 commit comments

Comments
 (0)