Skip to content

Commit d4d5da6

Browse files
committed
replicate impl in build conventions
1 parent 55fca6a commit d4d5da6

File tree

9 files changed

+136
-197
lines changed

9 files changed

+136
-197
lines changed

BuildLogic/build.gradle.kts

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

15-
import org.apache.tools.ant.filters.*
16-
import org.jetbrains.kotlin.ir.backend.js.compile
17-
1815
repositories {
1916
gradlePluginPortal()
2017
mavenCentral()
2118
}
2219

23-
plugins {
24-
`kotlin-dsl`
25-
}
26-
2720
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")
21+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.7.3")
3122
}
3223

33-
34-
kotlin {
35-
jvmToolchain(17)
24+
plugins {
25+
`kotlin-dsl`
3626
}

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import org.swift.swiftjava.gradle.JavaLibraryPathUtils.javaLibraryPaths
15+
import java.util.*
1616
import java.io.*
17+
import kotlin.system.exitProcess
18+
import kotlinx.serialization.json.*
1719

1820
plugins {
1921
java
@@ -43,6 +45,62 @@ tasks.withType(JavaCompile::class).forEach {
4345
it.options.compilerArgs.add("-Xlint:preview")
4446
}
4547

48+
49+
fun getSwiftRuntimeLibraryPaths(): List<String> {
50+
val process = ProcessBuilder("swiftc", "-print-target-info")
51+
.redirectError(ProcessBuilder.Redirect.INHERIT)
52+
.start()
53+
54+
val output = process.inputStream.bufferedReader().use { it.readText() }
55+
val exitCode = process.waitFor()
56+
if (exitCode != 0) {
57+
System.err.println("Error executing swiftc -print-target-info")
58+
exitProcess(exitCode)
59+
}
60+
61+
val json = Json.parseToJsonElement(output)
62+
val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray
63+
return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList()
64+
}
65+
66+
/**
67+
* Find library paths for 'java.library.path' when running or testing projects inside this build.
68+
*/
69+
// TODO: can't figure out how to share this code between BuildLogic/ and buildSrc/
70+
fun javaLibraryPaths(rootDir: File): List<String> {
71+
val osName = System.getProperty("os.name").lowercase(Locale.getDefault())
72+
val osArch = System.getProperty("os.arch")
73+
val isLinux = osName.contains("linux")
74+
val base = rootDir.path.let { "$it/" }
75+
76+
val projectBuildOutputPath =
77+
if (isLinux) {
78+
if (osArch == "amd64" || osArch == "x86_64")
79+
"$base.build/x86_64-unknown-linux-gnu"
80+
else
81+
"$base.build/${osArch}-unknown-linux-gnu"
82+
} else {
83+
if (osArch == "aarch64")
84+
"$base.build/arm64-apple-macosx"
85+
else
86+
"$base.build/${osArch}-apple-macosx"
87+
}
88+
val parentParentBuildOutputPath =
89+
"../../$projectBuildOutputPath"
90+
91+
92+
val swiftBuildOutputPaths = listOf(
93+
projectBuildOutputPath,
94+
parentParentBuildOutputPath
95+
)
96+
97+
val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" }
98+
val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" }
99+
val swiftRuntimePaths = getSwiftRuntimeLibraryPaths()
100+
101+
return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths
102+
}
103+
46104
// Configure paths for native (Swift) libraries
47105
tasks.test {
48106
jvmArgs(

BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt

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

BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt

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

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.swiftjava.gradle.JavaLibraryPathUtils
15+
import org.swift.swiftkit.gradle.BuildUtils
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-
(JavaLibraryPathUtils.javaLibraryPaths(rootDir) +
140-
JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"),
139+
(BuildUtils.javaLibraryPaths(rootDir) +
140+
BuildUtils.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-
(JavaLibraryPathUtils.javaLibraryPaths(rootDir) +
152-
JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"),
151+
(BuildUtils.javaLibraryPaths(rootDir) +
152+
BuildUtils.javaLibraryPaths(project.projectDir)).join(":"),
153153
]
154154
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ 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!"
40-
.formatted(this.owner, Thread.currentThread()));
39+
throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!".formatted(this.owner, Thread.currentThread()));
4140
} else if (this.state.get() < ACTIVE) {
4241
throw new RuntimeException("SwiftArena is already closed!");
4342
}

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

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

15-
plugins {
16-
kotlin("jvm") version "1.9.10"
17-
}
18-
1915
repositories {
2016
mavenCentral()
2117
}
2218

2319
dependencies {
24-
implementation("org.jetbrains.kotlin:kotlin-stdlib")
25-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
20+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.7.3")
2621
}
2722

28-
kotlin {
29-
jvmToolchain(17)
23+
def cleanSwift = tasks.register("cleanSwift", Exec) {
24+
workingDir = layout.projectDirectory
25+
commandLine "swift"
26+
args("package", "clean")
27+
}
28+
tasks.clean {
29+
dependsOn("cleanSwift")
3030
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.swiftkit.gradle
16+
17+
import groovy.json.JsonSlurper
18+
19+
final class BuildUtils {
20+
21+
static List<String> getSwiftRuntimeLibraryPaths() {
22+
def process = ['swiftc', '-print-target-info'].execute()
23+
def output = new StringWriter()
24+
process.consumeProcessOutput(output, System.err)
25+
process.waitFor()
26+
27+
def json = new JsonSlurper().parseText(output.toString())
28+
def runtimeLibraryPaths = json.paths.runtimeLibraryPaths
29+
return runtimeLibraryPaths
30+
}
31+
32+
/// Find library paths for 'java.library.path' when running or testing projects inside this build.
33+
static def javaLibraryPaths(File rootDir) {
34+
def osName = System.getProperty("os.name")
35+
def osArch = System.getProperty("os.arch")
36+
def isLinux = osName.toLowerCase(Locale.getDefault()).contains("linux")
37+
def base = rootDir == null ? "" : "${rootDir}/"
38+
39+
def debugPaths = [
40+
isLinux ?
41+
/* Linux */ (osArch == "amd64" || osArch == "x86_64" ?
42+
"${base}.build/x86_64-unknown-linux-gnu/debug/" :
43+
"${base}.build/${osArch}-unknown-linux-gnu/debug/") :
44+
/* macOS */ (osArch == "aarch64" ?
45+
"${base}.build/arm64-apple-macosx/debug/" :
46+
"${base}.build/${osArch}-apple-macosx/debug/"),
47+
isLinux ?
48+
/* Linux */ (osArch == "amd64" || osArch == "x86_64" ?
49+
"${base}../../.build/x86_64-unknown-linux-gnu/debug/" :
50+
"${base}../../.build/${osArch}-unknown-linux-gnu/debug/") :
51+
/* macOS */ (osArch == "aarch64" ?
52+
"${base}../../.build/arm64-apple-macosx/debug/" :
53+
"${base}../../.build/${osArch}-apple-macosx/debug/"),
54+
]
55+
def releasePaths = debugPaths.collect { it.replaceAll("debug", "release") }
56+
def swiftRuntimePaths = getSwiftRuntimeLibraryPaths()
57+
58+
return releasePaths + debugPaths + swiftRuntimePaths
59+
}
60+
}

0 commit comments

Comments
 (0)