-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathbuild.gradle
More file actions
197 lines (161 loc) · 6.32 KB
/
build.gradle
File metadata and controls
197 lines (161 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import groovy.json.JsonSlurper
import org.swift.swiftkit.gradle.BuildUtils
import java.nio.file.*
plugins {
id("build-logic.java-application-conventions")
id("me.champeau.jmh") version "0.7.2"
}
group = "org.swift.swiftkit"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(25))
}
}
def swiftProductsWithJExtractPlugin() {
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
def processBuilder = new ProcessBuilder('swift', 'package', 'describe', '--type', 'json')
def process = processBuilder.start()
process.consumeProcessOutput(stdout, stderr)
process.waitFor()
def exitValue = process.exitValue()
def jsonOutput = stdout.toString()
if (exitValue == 0) {
def json = new JsonSlurper().parseText(jsonOutput)
def products = json.targets
.findAll { target ->
target.product_dependencies?.contains("JExtractSwiftPlugin")
}
.collectMany { target ->
target.product_memberships ?: []
}
return products
} else {
logger.warn("Command failed: ${stderr.toString()}")
return []
}
}
def swiftCheckValid = tasks.register("swift-check-valid", Exec) {
commandLine "swift"
args("-version")
}
def jextract = tasks.register("jextract", Exec) {
description = "Generate Java wrappers for swift target"
dependsOn swiftCheckValid
// only because we depend on "live developing" the plugin while using this project to test it
inputs.file(new File(rootDir, "Package.swift"))
inputs.dir(new File(rootDir, "Sources"))
// If the package description changes, we should execute jextract again, maybe we added jextract to new targets
inputs.file(new File(projectDir, "Package.swift"))
// monitor all targets/products which depend on the JExtract plugin
swiftProductsWithJExtractPlugin().each {
logger.info("[swift-java:jextract (Gradle)] Swift input target: ${it}")
inputs.dir(new File(layout.projectDirectory.asFile, "Sources/${it}".toString()))
}
outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}"))
File baseSwiftPluginOutputsDir = layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile
if (!baseSwiftPluginOutputsDir.exists()) {
baseSwiftPluginOutputsDir.mkdirs()
}
Files.walk(layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile.toPath()).each {
// Add any Java sources generated by the plugin to our sourceSet
if (it.endsWith("JExtractSwiftPlugin/src/generated/java")) {
outputs.dir(it)
}
}
workingDir = layout.projectDirectory
commandLine "swift"
// FIXME: disable prebuilts until swift-syntax isn't broken on 6.2 anymore: https://github.com/swiftlang/swift-java/issues/418
args("build", "--disable-experimental-prebuilts") // since Swift targets which need to be jextract-ed have the jextract build plugin, we just need to build
// If we wanted to execute a specific subcommand, we can like this:
// args("run",/*
// "swift-java", "jextract",
// "--swift-module", "MySwiftLibrary",
// // java.package is obtained from the swift-java.config in the swift module
// "--output-java", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/src/generated/java").get()}",
// "--output-swift", "${layout.buildDirectory.dir(".build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractSwiftPlugin/Sources").get()}",
// "--log-level", (logging.level <= LogLevel.INFO ? "debug" : */"info")
// )
}
// Add the java-swift generated Java sources
sourceSets {
main {
java {
srcDir(jextract)
}
}
test {
java {
srcDir(jextract)
}
}
jmh {
java {
srcDir(jextract)
}
}
}
tasks.build {
dependsOn("jextract")
}
def cleanSwift = tasks.register("cleanSwift", Exec) {
workingDir = layout.projectDirectory
commandLine "swift"
args("package", "clean")
}
tasks.clean {
dependsOn("cleanSwift")
}
dependencies {
implementation(project(':SwiftKitCore'))
implementation(project(':SwiftKitFFM'))
testRuntimeOnly("org.junit.platform:junit-platform-launcher") // TODO: workaround for not finding junit: https://github.com/gradle/gradle/issues/34512 // TODO: workaround for not finding junit: https://github.com/gradle/gradle/issues/34512
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.named('test', Test) {
useJUnitPlatform()
}
application {
mainClass = "com.example.swift.HelloJava2Swift"
applicationDefaultJvmArgs = [
"--enable-native-access=ALL-UNNAMED",
// Include the library paths where our dylibs are that we want to load and call
"-Djava.library.path=" +
(BuildUtils.javaLibraryPaths(rootDir) +
BuildUtils.javaLibraryPaths(project.projectDir)).join(":"),
// Enable tracing downcalls (to Swift)
"-Djextract.trace.downcalls=true"
]
}
String jmhIncludes = findProperty("jmhIncludes")
jmh {
if (jmhIncludes != null) {
includes = [jmhIncludes]
}
jvmArgsAppend = [
"--enable-native-access=ALL-UNNAMED",
"-Djava.library.path=" +
(BuildUtils.javaLibraryPaths(rootDir) +
BuildUtils.javaLibraryPaths(project.projectDir)).join(":"),
// Enable tracing downcalls (to Swift)
"-Djextract.trace.downcalls=false"
]
}