Skip to content

Commit ad1a4f9

Browse files
committed
Rewrite how we get java library paths from build
1 parent 3517bba commit ad1a4f9

File tree

9 files changed

+201
-110
lines changed

9 files changed

+201
-110
lines changed

BuildLogic/build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,25 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import org.apache.tools.ant.filters.*
16+
import org.jetbrains.kotlin.ir.backend.js.compile
17+
1518
repositories {
1619
gradlePluginPortal()
20+
mavenCentral()
1721
}
1822

1923
plugins {
2024
`kotlin-dsl`
2125
}
26+
27+
dependencies {
28+
implementation("org.jetbrains.kotlin:kotlin-stdlib")
29+
// for parsing Swift compiler information
30+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
31+
}
32+
33+
34+
kotlin {
35+
jvmToolchain(17)
36+
}

BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import java.util.*
15+
import org.swift.swiftjava.gradle.JavaLibraryPathUtils.javaLibraryPaths
1616
import java.io.*
1717

1818
plugins {
@@ -43,43 +43,6 @@ tasks.withType(JavaCompile::class).forEach {
4343
it.options.compilerArgs.add("-Xlint:preview")
4444
}
4545

46-
47-
// FIXME: cannot share definition with 'buildSrc' so we duplicated the impl here
48-
fun javaLibraryPaths(dir: File): List<String> {
49-
val osName = System.getProperty("os.name")
50-
val osArch = System.getProperty("os.arch")
51-
val isLinux = osName.lowercase(Locale.getDefault()).contains("linux")
52-
53-
return listOf(
54-
if (isLinux) {
55-
if (osArch.equals("x86_64") || osArch.equals("amd64")) {
56-
"$dir/.build/x86_64-unknown-linux-gnu/debug/"
57-
} else {
58-
"$dir/.build/$osArch-unknown-linux-gnu/debug/"
59-
}
60-
} else {
61-
if (osArch.equals("aarch64")) {
62-
"$dir/.build/arm64-apple-macosx/debug/"
63-
} else {
64-
"$dir/.build/$osArch-apple-macosx/debug/"
65-
}
66-
},
67-
if (isLinux) {
68-
"/usr/lib/swift/linux"
69-
} else {
70-
// assume macOS
71-
"/usr/lib/swift/"
72-
},
73-
if (isLinux) {
74-
System.getProperty("user.home") + "/.local/share/swiftly/toolchains/6.0.2/usr/lib/swift/linux"
75-
} else {
76-
// assume macOS
77-
"/usr/lib/swift/"
78-
}
79-
)
80-
}
81-
82-
8346
// Configure paths for native (Swift) libraries
8447
tasks.test {
8548
jvmArgs(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DO NOT EDIT THIS PACKAGE
2+
3+
It is copied from buildSrc/ during build.
4+
5+
We cannot easily share the sources between BuildLogic and buildSrc so we automated the copy.
6+
If this could be shared in some nicer way, please improve the build to do so :-)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 org.swift.swiftjava.gradle
16+
17+
import java.io.File
18+
import java.util.*
19+
import kotlin.system.exitProcess
20+
import kotlinx.serialization.json.*
21+
22+
object JavaLibraryPathUtils {
23+
24+
private fun getSwiftRuntimeLibraryPaths(): List<String> {
25+
val process = ProcessBuilder("swiftc", "-print-target-info")
26+
.redirectError(ProcessBuilder.Redirect.INHERIT)
27+
.start()
28+
29+
val output = process.inputStream.bufferedReader().use { it.readText() }
30+
val exitCode = process.waitFor()
31+
if (exitCode != 0) {
32+
System.err.println("Error executing swiftc -print-target-info")
33+
exitProcess(exitCode)
34+
}
35+
36+
val json = Json.parseToJsonElement(output)
37+
val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray
38+
return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList()
39+
}
40+
41+
/**
42+
* Find library paths for 'java.library.path' when running or testing projects inside this build.
43+
*/
44+
@JvmStatic
45+
fun javaLibraryPaths(rootDir: File): List<String> {
46+
val osName = System.getProperty("os.name").lowercase(Locale.getDefault())
47+
val osArch = System.getProperty("os.arch")
48+
val isLinux = osName.contains("linux")
49+
val base = rootDir.path.let { "$it/" }
50+
51+
val projectBuildOutputPath =
52+
if (isLinux) {
53+
if (osArch == "amd64" || osArch == "x86_64")
54+
"$base.build/x86_64-unknown-linux-gnu"
55+
else
56+
"$base.build/${osArch}-unknown-linux-gnu"
57+
} else {
58+
if (osArch == "aarch64")
59+
"$base.build/arm64-apple-macosx"
60+
else
61+
"$base.build/${osArch}-apple-macosx"
62+
}
63+
val parentParentBuildOutputPath =
64+
"../../$projectBuildOutputPath"
65+
66+
67+
val swiftBuildOutputPaths = listOf(
68+
projectBuildOutputPath,
69+
parentParentBuildOutputPath
70+
)
71+
72+
val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" }
73+
val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" }
74+
val swiftRuntimePaths = getSwiftRuntimeLibraryPaths()
75+
76+
return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths
77+
}
78+
}
79+
80+
81+
// DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE

Samples/SwiftKitSampleApp/build.gradle

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

15-
import org.swift.swiftkit.gradle.BuildUtils
15+
import org.swift.swiftjava.gradle.JavaLibraryPathUtils
1616

1717
import java.nio.file.*
1818

@@ -136,8 +136,8 @@ application {
136136

137137
// Include the library paths where our dylibs are that we want to load and call
138138
"-Djava.library.path=" +
139-
(BuildUtils.javaLibraryPaths(rootDir) +
140-
BuildUtils.javaLibraryPaths(project.projectDir)).join(":"),
139+
(JavaLibraryPathUtils.javaLibraryPaths(rootDir) +
140+
JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"),
141141

142142

143143
// Enable tracing downcalls (to Swift)
@@ -148,7 +148,7 @@ application {
148148
jmh {
149149
jvmArgsAppend = [
150150
"-Djava.library.path=" +
151-
(BuildUtils.javaLibraryPaths(rootDir) +
152-
BuildUtils.javaLibraryPaths(project.projectDir)).join(":"),
151+
(JavaLibraryPathUtils.javaLibraryPaths(rootDir) +
152+
JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"),
153153
]
154154
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public ConfinedSwiftMemorySession(Thread owner) {
3636

3737
public void checkValid() throws RuntimeException {
3838
if (this.owner != null && this.owner != Thread.currentThread()) {
39-
throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!".formatted(this.owner, Thread.currentThread()));
39+
throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!"
40+
.formatted(this.owner, Thread.currentThread()));
4041
} else if (this.state.get() < ACTIVE) {
4142
throw new RuntimeException("SwiftArena is already closed!");
4243
}

buildSrc/build.gradle renamed to buildSrc/build.gradle.kts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
plugins {
16+
kotlin("jvm") version "1.9.10"
17+
}
18+
1519
repositories {
1620
mavenCentral()
1721
}
1822

19-
def cleanSwift = tasks.register("cleanSwift", Exec) {
20-
workingDir = layout.projectDirectory
21-
commandLine "swift"
22-
args("package", "clean")
23+
dependencies {
24+
implementation("org.jetbrains.kotlin:kotlin-stdlib")
25+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
2326
}
24-
tasks.clean {
25-
dependsOn("cleanSwift")
27+
28+
kotlin {
29+
jvmToolchain(17)
2630
}

buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 org.swift.swiftjava.gradle
16+
17+
import java.io.File
18+
import java.util.*
19+
import kotlin.system.exitProcess
20+
import kotlinx.serialization.json.*
21+
22+
object JavaLibraryPathUtils {
23+
24+
private fun getSwiftRuntimeLibraryPaths(): List<String> {
25+
val process = ProcessBuilder("swiftc", "-print-target-info")
26+
.redirectError(ProcessBuilder.Redirect.INHERIT)
27+
.start()
28+
29+
val output = process.inputStream.bufferedReader().use { it.readText() }
30+
val exitCode = process.waitFor()
31+
if (exitCode != 0) {
32+
System.err.println("Error executing swiftc -print-target-info")
33+
exitProcess(exitCode)
34+
}
35+
36+
val json = Json.parseToJsonElement(output)
37+
val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray
38+
return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList()
39+
}
40+
41+
/**
42+
* Find library paths for 'java.library.path' when running or testing projects inside this build.
43+
*/
44+
@JvmStatic
45+
fun javaLibraryPaths(rootDir: File): List<String> {
46+
val osName = System.getProperty("os.name").lowercase(Locale.getDefault())
47+
val osArch = System.getProperty("os.arch")
48+
val isLinux = osName.contains("linux")
49+
val base = rootDir.path.let { "$it/" }
50+
51+
val projectBuildOutputPath =
52+
if (isLinux) {
53+
if (osArch == "amd64" || osArch == "x86_64")
54+
"$base.build/x86_64-unknown-linux-gnu"
55+
else
56+
"$base.build/${osArch}-unknown-linux-gnu"
57+
} else {
58+
if (osArch == "aarch64")
59+
"$base.build/arm64-apple-macosx"
60+
else
61+
"$base.build/${osArch}-apple-macosx"
62+
}
63+
val parentParentBuildOutputPath =
64+
"../../$projectBuildOutputPath"
65+
66+
67+
val swiftBuildOutputPaths = listOf(
68+
projectBuildOutputPath,
69+
parentParentBuildOutputPath
70+
)
71+
72+
val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" }
73+
val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" }
74+
val swiftRuntimePaths = getSwiftRuntimeLibraryPaths()
75+
76+
return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths
77+
}
78+
}
79+
80+
81+
// DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE

0 commit comments

Comments
 (0)