forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
209 lines (171 loc) · 6.24 KB
/
build.gradle
File metadata and controls
209 lines (171 loc) · 6.24 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
198
199
200
201
202
203
204
205
206
207
208
209
//===----------------------------------------------------------------------===//
//
// 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-library-conventions")
id "com.google.osdetector" version "1.7.3"
id("maven-publish")
}
group = "org.swift.swiftkit"
version = "1.0-SNAPSHOT"
def swiftBuildConfiguration() {
"release"
}
repositories {
mavenLocal()
mavenCentral()
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(24))
}
}
dependencies {
implementation(project(':SwiftKitCore'))
implementation(project(':SwiftKitFFM'))
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
def swiftProductsWithJExtractPlugin() {
def stdout = new ByteArrayOutputStream()
def stderr = new ByteArrayOutputStream()
def result = exec {
commandLine 'swift', 'package', 'describe', '--type', 'json'
standardOutput = stdout
errorOutput = stderr
ignoreExitValue = true
}
def jsonOutput = stdout.toString()
if (result.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"
args("build") // 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)
}
}
}
tasks.build {
dependsOn("jextract")
}
tasks.named('test', Test) {
useJUnitPlatform()
}
// ==== Jar publishing
List<String> swiftProductDylibPaths() {
def process = ['swift', 'package', 'describe', '--type', 'json'].execute()
process.waitFor()
if (process.exitValue() != 0) {
throw new RuntimeException("[swift describe] command failed with exit code: ${process.exitValue()}. Cannot find products! Output: ${process.err.text}")
}
def json = new JsonSlurper().parseText(process.text)
// TODO: require that we depend on swift-java
// TODO: all the products where the targets depend on swift-java plugin
def products =
json.targets.collect { target ->
target.product_memberships
}.flatten()
def productDylibPaths = products.collect {
logger.info("[swift-java] Include Swift product: '${it}' in product resource paths.")
"${layout.projectDirectory}/.build/${swiftBuildConfiguration()}/lib${it}.dylib"
}
return productDylibPaths
}
processResources {
dependsOn "jextract"
def dylibs = [
"${layout.projectDirectory}/.build/${swiftBuildConfiguration()}/libSwiftKitSwift.dylib"
]
dylibs.addAll(swiftProductDylibPaths())
from(dylibs)
}
jar {
archiveClassifier = osdetector.classifier
}
base {
archivesName = "swift-and-java-jar-sample-lib"
}
publishing {
publications {
maven(MavenPublication) {
artifactId = "swift-and-java-jar-sample-lib"
from components.java
}
}
repositories {
mavenLocal()
}
}