From 272d159ff55ce4ddf44f0c66bf033d81523a964e Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 14 Nov 2024 23:28:45 +0900 Subject: [PATCH 1/3] use -print-target-info to get swift library paths reliably --- BuildLogic/build.gradle.kts | 15 ++++ ...d-logic.java-common-conventions.gradle.kts | 39 +-------- .../swift/swiftjava/gradle/DO_NOT_EDIT.txt | 6 ++ .../swiftjava/gradle/JavaLibraryPathUtils.kt | 81 +++++++++++++++++++ Samples/SwiftKitSampleApp/build.gradle | 10 +-- .../swiftkit/ConfinedSwiftMemorySession.java | 3 +- buildSrc/{build.gradle => build.gradle.kts} | 16 ++-- .../swift/swiftkit/gradle/BuildUtils.groovy | 58 ------------- .../swiftjava/gradle/JavaLibraryPathUtils.kt | 81 +++++++++++++++++++ 9 files changed, 201 insertions(+), 108 deletions(-) create mode 100644 BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt create mode 100644 BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt rename buildSrc/{build.gradle => build.gradle.kts} (70%) delete mode 100644 buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy create mode 100644 buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt diff --git a/BuildLogic/build.gradle.kts b/BuildLogic/build.gradle.kts index 142b7f7c..5d2bf545 100644 --- a/BuildLogic/build.gradle.kts +++ b/BuildLogic/build.gradle.kts @@ -12,10 +12,25 @@ // //===----------------------------------------------------------------------===// +import org.apache.tools.ant.filters.* +import org.jetbrains.kotlin.ir.backend.js.compile + repositories { gradlePluginPortal() + mavenCentral() } plugins { `kotlin-dsl` } + +dependencies { + implementation("org.jetbrains.kotlin:kotlin-stdlib") + // for parsing Swift compiler information + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0") +} + + +kotlin { + jvmToolchain(17) +} diff --git a/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts b/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts index 136da7d3..d8313a31 100644 --- a/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts +++ b/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -import java.util.* +import org.swift.swiftjava.gradle.JavaLibraryPathUtils.javaLibraryPaths import java.io.* plugins { @@ -43,43 +43,6 @@ tasks.withType(JavaCompile::class).forEach { it.options.compilerArgs.add("-Xlint:preview") } - -// FIXME: cannot share definition with 'buildSrc' so we duplicated the impl here -fun javaLibraryPaths(dir: File): List { - val osName = System.getProperty("os.name") - val osArch = System.getProperty("os.arch") - val isLinux = osName.lowercase(Locale.getDefault()).contains("linux") - - return listOf( - if (isLinux) { - if (osArch.equals("x86_64") || osArch.equals("amd64")) { - "$dir/.build/x86_64-unknown-linux-gnu/debug/" - } else { - "$dir/.build/$osArch-unknown-linux-gnu/debug/" - } - } else { - if (osArch.equals("aarch64")) { - "$dir/.build/arm64-apple-macosx/debug/" - } else { - "$dir/.build/$osArch-apple-macosx/debug/" - } - }, - if (isLinux) { - "/usr/lib/swift/linux" - } else { - // assume macOS - "/usr/lib/swift/" - }, - if (isLinux) { - System.getProperty("user.home") + "/.local/share/swiftly/toolchains/6.0.2/usr/lib/swift/linux" - } else { - // assume macOS - "/usr/lib/swift/" - } - ) -} - - // Configure paths for native (Swift) libraries tasks.test { jvmArgs( diff --git a/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt b/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt new file mode 100644 index 00000000..64e78850 --- /dev/null +++ b/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt @@ -0,0 +1,6 @@ +DO NOT EDIT THIS PACKAGE + +It is copied from buildSrc/ during build. + +We cannot easily share the sources between BuildLogic and buildSrc so we automated the copy. +If this could be shared in some nicer way, please improve the build to do so :-) diff --git a/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt b/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt new file mode 100644 index 00000000..d9faff2a --- /dev/null +++ b/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +package org.swift.swiftjava.gradle + +import java.io.File +import java.util.* +import kotlin.system.exitProcess +import kotlinx.serialization.json.* + +object JavaLibraryPathUtils { + + private fun getSwiftRuntimeLibraryPaths(): List { + val process = ProcessBuilder("swiftc", "-print-target-info") + .redirectError(ProcessBuilder.Redirect.INHERIT) + .start() + + val output = process.inputStream.bufferedReader().use { it.readText() } + val exitCode = process.waitFor() + if (exitCode != 0) { + System.err.println("Error executing swiftc -print-target-info") + exitProcess(exitCode) + } + + val json = Json.parseToJsonElement(output) + val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray + return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList() + } + + /** + * Find library paths for 'java.library.path' when running or testing projects inside this build. + */ + @JvmStatic + fun javaLibraryPaths(rootDir: File): List { + val osName = System.getProperty("os.name").lowercase(Locale.getDefault()) + val osArch = System.getProperty("os.arch") + val isLinux = osName.contains("linux") + val base = rootDir.path.let { "$it/" } + + val projectBuildOutputPath = + if (isLinux) { + if (osArch == "amd64" || osArch == "x86_64") + "$base.build/x86_64-unknown-linux-gnu" + else + "$base.build/${osArch}-unknown-linux-gnu" + } else { + if (osArch == "aarch64") + "$base.build/arm64-apple-macosx" + else + "$base.build/${osArch}-apple-macosx" + } + val parentParentBuildOutputPath = + "../../$projectBuildOutputPath" + + + val swiftBuildOutputPaths = listOf( + projectBuildOutputPath, + parentParentBuildOutputPath + ) + + val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" } + val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" } + val swiftRuntimePaths = getSwiftRuntimeLibraryPaths() + + return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths + } +} + + +// DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE diff --git a/Samples/SwiftKitSampleApp/build.gradle b/Samples/SwiftKitSampleApp/build.gradle index ff9e1837..4215b1b4 100644 --- a/Samples/SwiftKitSampleApp/build.gradle +++ b/Samples/SwiftKitSampleApp/build.gradle @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -import org.swift.swiftkit.gradle.BuildUtils +import org.swift.swiftjava.gradle.JavaLibraryPathUtils import java.nio.file.* @@ -136,8 +136,8 @@ application { // 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(":"), + (JavaLibraryPathUtils.javaLibraryPaths(rootDir) + + JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"), // Enable tracing downcalls (to Swift) @@ -148,7 +148,7 @@ application { jmh { jvmArgsAppend = [ "-Djava.library.path=" + - (BuildUtils.javaLibraryPaths(rootDir) + - BuildUtils.javaLibraryPaths(project.projectDir)).join(":"), + (JavaLibraryPathUtils.javaLibraryPaths(rootDir) + + JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"), ] } diff --git a/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java b/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java index e727f5db..36d7be77 100644 --- a/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java +++ b/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java @@ -36,7 +36,8 @@ public ConfinedSwiftMemorySession(Thread owner) { public void checkValid() throws RuntimeException { if (this.owner != null && this.owner != Thread.currentThread()) { - throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!".formatted(this.owner, Thread.currentThread())); + throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!" + .formatted(this.owner, Thread.currentThread())); } else if (this.state.get() < ACTIVE) { throw new RuntimeException("SwiftArena is already closed!"); } diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle.kts similarity index 70% rename from buildSrc/build.gradle rename to buildSrc/build.gradle.kts index c18f30c1..0595b3ad 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle.kts @@ -12,15 +12,19 @@ // //===----------------------------------------------------------------------===// +plugins { + kotlin("jvm") version "1.9.10" +} + repositories { mavenCentral() } -def cleanSwift = tasks.register("cleanSwift", Exec) { - workingDir = layout.projectDirectory - commandLine "swift" - args("package", "clean") +dependencies { + implementation("org.jetbrains.kotlin:kotlin-stdlib") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0") } -tasks.clean { - dependsOn("cleanSwift") + +kotlin { + jvmToolchain(17) } diff --git a/buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy b/buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy deleted file mode 100644 index 4fcddbee..00000000 --- a/buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy +++ /dev/null @@ -1,58 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -package org.swift.swiftkit.gradle - -final class BuildUtils { - - /// Find library paths for 'java.library.path' when running or testing projects inside this build. - static def javaLibraryPaths(File rootDir) { - def osName = System.getProperty("os.name") - def osArch = System.getProperty("os.arch") - def isLinux = osName.toLowerCase(Locale.getDefault()).contains("linux") - def base = rootDir == null ? "" : "${rootDir}/" - - def debugPaths = [ - isLinux ? - /* Linux */ (osArch == "amd64" || osArch == "x86_64" ? - "${base}.build/x86_64-unknown-linux-gnu/debug/" : - "${base}.build/${osArch}-unknown-linux-gnu/debug/") : - /* macOS */ (osArch == "aarch64" ? - "${base}.build/arm64-apple-macosx/debug/" : - "${base}.build/${osArch}-apple-macosx/debug/"), - isLinux ? - /* Linux */ (osArch == "amd64" || osArch == "x86_64" ? - "${base}../../.build/x86_64-unknown-linux-gnu/debug/" : - "${base}../../.build/${osArch}-unknown-linux-gnu/debug/") : - /* macOS */ (osArch == "aarch64" ? - "${base}../../.build/arm64-apple-macosx/debug/" : - "${base}../../.build/${osArch}-apple-macosx/debug/"), - ] - def releasePaths = debugPaths.collect { it.replaceAll("debug", "release") } - def systemPaths = - // system paths - isLinux ? - [ - "/usr/lib/swift/linux", - // TODO: should we be Swiftly aware and then use the currently used path? - System.getProperty("user.home") + "/.local/share/swiftly/toolchains/6.0.2/usr/lib/swift/linux" - ] : - [ - // assume macOS - "/usr/lib/swift/" - ] - - return releasePaths + debugPaths + systemPaths - } -} diff --git a/buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt b/buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt new file mode 100644 index 00000000..d9faff2a --- /dev/null +++ b/buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +package org.swift.swiftjava.gradle + +import java.io.File +import java.util.* +import kotlin.system.exitProcess +import kotlinx.serialization.json.* + +object JavaLibraryPathUtils { + + private fun getSwiftRuntimeLibraryPaths(): List { + val process = ProcessBuilder("swiftc", "-print-target-info") + .redirectError(ProcessBuilder.Redirect.INHERIT) + .start() + + val output = process.inputStream.bufferedReader().use { it.readText() } + val exitCode = process.waitFor() + if (exitCode != 0) { + System.err.println("Error executing swiftc -print-target-info") + exitProcess(exitCode) + } + + val json = Json.parseToJsonElement(output) + val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray + return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList() + } + + /** + * Find library paths for 'java.library.path' when running or testing projects inside this build. + */ + @JvmStatic + fun javaLibraryPaths(rootDir: File): List { + val osName = System.getProperty("os.name").lowercase(Locale.getDefault()) + val osArch = System.getProperty("os.arch") + val isLinux = osName.contains("linux") + val base = rootDir.path.let { "$it/" } + + val projectBuildOutputPath = + if (isLinux) { + if (osArch == "amd64" || osArch == "x86_64") + "$base.build/x86_64-unknown-linux-gnu" + else + "$base.build/${osArch}-unknown-linux-gnu" + } else { + if (osArch == "aarch64") + "$base.build/arm64-apple-macosx" + else + "$base.build/${osArch}-apple-macosx" + } + val parentParentBuildOutputPath = + "../../$projectBuildOutputPath" + + + val swiftBuildOutputPaths = listOf( + projectBuildOutputPath, + parentParentBuildOutputPath + ) + + val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" } + val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" } + val swiftRuntimePaths = getSwiftRuntimeLibraryPaths() + + return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths + } +} + + +// DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE From 55fca6a8c8a5dd5da9e8ccb23aada29eb19de3c8 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Fri, 15 Nov 2024 15:56:38 +0900 Subject: [PATCH 2/3] fix license --- .licenseignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.licenseignore b/.licenseignore index c5dfe3d2..04a64766 100644 --- a/.licenseignore +++ b/.licenseignore @@ -40,3 +40,4 @@ gradlew.bat **/gradlew **/gradlew.bat **/ci-validate.sh +**/DO_NOT_EDIT.txt From d4d5da6004401368f0e33529ca4e4c3e1c4529a9 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Fri, 15 Nov 2024 16:28:31 +0900 Subject: [PATCH 3/3] replicate impl in build conventions --- BuildLogic/build.gradle.kts | 16 +--- ...d-logic.java-common-conventions.gradle.kts | 60 +++++++++++++- .../swift/swiftjava/gradle/DO_NOT_EDIT.txt | 6 -- .../swiftjava/gradle/JavaLibraryPathUtils.kt | 81 ------------------- Samples/SwiftKitSampleApp/build.gradle | 10 +-- .../swiftkit/ConfinedSwiftMemorySession.java | 3 +- buildSrc/{build.gradle.kts => build.gradle} | 16 ++-- .../swift/swiftkit/gradle/BuildUtils.groovy | 60 ++++++++++++++ .../swiftjava/gradle/JavaLibraryPathUtils.kt | 81 ------------------- 9 files changed, 136 insertions(+), 197 deletions(-) delete mode 100644 BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt delete mode 100644 BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt rename buildSrc/{build.gradle.kts => build.gradle} (74%) create mode 100644 buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy delete mode 100644 buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt diff --git a/BuildLogic/build.gradle.kts b/BuildLogic/build.gradle.kts index 5d2bf545..f64d3e94 100644 --- a/BuildLogic/build.gradle.kts +++ b/BuildLogic/build.gradle.kts @@ -12,25 +12,15 @@ // //===----------------------------------------------------------------------===// -import org.apache.tools.ant.filters.* -import org.jetbrains.kotlin.ir.backend.js.compile - repositories { gradlePluginPortal() mavenCentral() } -plugins { - `kotlin-dsl` -} - dependencies { - implementation("org.jetbrains.kotlin:kotlin-stdlib") - // for parsing Swift compiler information - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.7.3") } - -kotlin { - jvmToolchain(17) +plugins { + `kotlin-dsl` } diff --git a/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts b/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts index d8313a31..ef88ce15 100644 --- a/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts +++ b/BuildLogic/src/main/kotlin/build-logic.java-common-conventions.gradle.kts @@ -12,8 +12,10 @@ // //===----------------------------------------------------------------------===// -import org.swift.swiftjava.gradle.JavaLibraryPathUtils.javaLibraryPaths +import java.util.* import java.io.* +import kotlin.system.exitProcess +import kotlinx.serialization.json.* plugins { java @@ -43,6 +45,62 @@ tasks.withType(JavaCompile::class).forEach { it.options.compilerArgs.add("-Xlint:preview") } + +fun getSwiftRuntimeLibraryPaths(): List { + val process = ProcessBuilder("swiftc", "-print-target-info") + .redirectError(ProcessBuilder.Redirect.INHERIT) + .start() + + val output = process.inputStream.bufferedReader().use { it.readText() } + val exitCode = process.waitFor() + if (exitCode != 0) { + System.err.println("Error executing swiftc -print-target-info") + exitProcess(exitCode) + } + + val json = Json.parseToJsonElement(output) + val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray + return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList() +} + +/** + * Find library paths for 'java.library.path' when running or testing projects inside this build. + */ +// TODO: can't figure out how to share this code between BuildLogic/ and buildSrc/ +fun javaLibraryPaths(rootDir: File): List { + val osName = System.getProperty("os.name").lowercase(Locale.getDefault()) + val osArch = System.getProperty("os.arch") + val isLinux = osName.contains("linux") + val base = rootDir.path.let { "$it/" } + + val projectBuildOutputPath = + if (isLinux) { + if (osArch == "amd64" || osArch == "x86_64") + "$base.build/x86_64-unknown-linux-gnu" + else + "$base.build/${osArch}-unknown-linux-gnu" + } else { + if (osArch == "aarch64") + "$base.build/arm64-apple-macosx" + else + "$base.build/${osArch}-apple-macosx" + } + val parentParentBuildOutputPath = + "../../$projectBuildOutputPath" + + + val swiftBuildOutputPaths = listOf( + projectBuildOutputPath, + parentParentBuildOutputPath + ) + + val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" } + val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" } + val swiftRuntimePaths = getSwiftRuntimeLibraryPaths() + + return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths +} + // Configure paths for native (Swift) libraries tasks.test { jvmArgs( diff --git a/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt b/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt deleted file mode 100644 index 64e78850..00000000 --- a/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/DO_NOT_EDIT.txt +++ /dev/null @@ -1,6 +0,0 @@ -DO NOT EDIT THIS PACKAGE - -It is copied from buildSrc/ during build. - -We cannot easily share the sources between BuildLogic and buildSrc so we automated the copy. -If this could be shared in some nicer way, please improve the build to do so :-) diff --git a/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt b/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt deleted file mode 100644 index d9faff2a..00000000 --- a/BuildLogic/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt +++ /dev/null @@ -1,81 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -package org.swift.swiftjava.gradle - -import java.io.File -import java.util.* -import kotlin.system.exitProcess -import kotlinx.serialization.json.* - -object JavaLibraryPathUtils { - - private fun getSwiftRuntimeLibraryPaths(): List { - val process = ProcessBuilder("swiftc", "-print-target-info") - .redirectError(ProcessBuilder.Redirect.INHERIT) - .start() - - val output = process.inputStream.bufferedReader().use { it.readText() } - val exitCode = process.waitFor() - if (exitCode != 0) { - System.err.println("Error executing swiftc -print-target-info") - exitProcess(exitCode) - } - - val json = Json.parseToJsonElement(output) - val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray - return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList() - } - - /** - * Find library paths for 'java.library.path' when running or testing projects inside this build. - */ - @JvmStatic - fun javaLibraryPaths(rootDir: File): List { - val osName = System.getProperty("os.name").lowercase(Locale.getDefault()) - val osArch = System.getProperty("os.arch") - val isLinux = osName.contains("linux") - val base = rootDir.path.let { "$it/" } - - val projectBuildOutputPath = - if (isLinux) { - if (osArch == "amd64" || osArch == "x86_64") - "$base.build/x86_64-unknown-linux-gnu" - else - "$base.build/${osArch}-unknown-linux-gnu" - } else { - if (osArch == "aarch64") - "$base.build/arm64-apple-macosx" - else - "$base.build/${osArch}-apple-macosx" - } - val parentParentBuildOutputPath = - "../../$projectBuildOutputPath" - - - val swiftBuildOutputPaths = listOf( - projectBuildOutputPath, - parentParentBuildOutputPath - ) - - val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" } - val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" } - val swiftRuntimePaths = getSwiftRuntimeLibraryPaths() - - return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths - } -} - - -// DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE diff --git a/Samples/SwiftKitSampleApp/build.gradle b/Samples/SwiftKitSampleApp/build.gradle index 4215b1b4..ff9e1837 100644 --- a/Samples/SwiftKitSampleApp/build.gradle +++ b/Samples/SwiftKitSampleApp/build.gradle @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -import org.swift.swiftjava.gradle.JavaLibraryPathUtils +import org.swift.swiftkit.gradle.BuildUtils import java.nio.file.* @@ -136,8 +136,8 @@ application { // Include the library paths where our dylibs are that we want to load and call "-Djava.library.path=" + - (JavaLibraryPathUtils.javaLibraryPaths(rootDir) + - JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"), + (BuildUtils.javaLibraryPaths(rootDir) + + BuildUtils.javaLibraryPaths(project.projectDir)).join(":"), // Enable tracing downcalls (to Swift) @@ -148,7 +148,7 @@ application { jmh { jvmArgsAppend = [ "-Djava.library.path=" + - (JavaLibraryPathUtils.javaLibraryPaths(rootDir) + - JavaLibraryPathUtils.javaLibraryPaths(project.projectDir)).join(":"), + (BuildUtils.javaLibraryPaths(rootDir) + + BuildUtils.javaLibraryPaths(project.projectDir)).join(":"), ] } diff --git a/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java b/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java index 36d7be77..e727f5db 100644 --- a/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java +++ b/SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java @@ -36,8 +36,7 @@ public ConfinedSwiftMemorySession(Thread owner) { public void checkValid() throws RuntimeException { if (this.owner != null && this.owner != Thread.currentThread()) { - throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!" - .formatted(this.owner, Thread.currentThread())); + throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!".formatted(this.owner, Thread.currentThread())); } else if (this.state.get() < ACTIVE) { throw new RuntimeException("SwiftArena is already closed!"); } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle similarity index 74% rename from buildSrc/build.gradle.kts rename to buildSrc/build.gradle index 0595b3ad..925db83f 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle @@ -12,19 +12,19 @@ // //===----------------------------------------------------------------------===// -plugins { - kotlin("jvm") version "1.9.10" -} - repositories { mavenCentral() } dependencies { - implementation("org.jetbrains.kotlin:kotlin-stdlib") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.7.3") } -kotlin { - jvmToolchain(17) +def cleanSwift = tasks.register("cleanSwift", Exec) { + workingDir = layout.projectDirectory + commandLine "swift" + args("package", "clean") +} +tasks.clean { + dependsOn("cleanSwift") } diff --git a/buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy b/buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy new file mode 100644 index 00000000..9512307f --- /dev/null +++ b/buildSrc/src/main/groovy/org/swift/swiftkit/gradle/BuildUtils.groovy @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +package org.swift.swiftkit.gradle + +import groovy.json.JsonSlurper + +final class BuildUtils { + + static List getSwiftRuntimeLibraryPaths() { + def process = ['swiftc', '-print-target-info'].execute() + def output = new StringWriter() + process.consumeProcessOutput(output, System.err) + process.waitFor() + + def json = new JsonSlurper().parseText(output.toString()) + def runtimeLibraryPaths = json.paths.runtimeLibraryPaths + return runtimeLibraryPaths + } + + /// Find library paths for 'java.library.path' when running or testing projects inside this build. + static def javaLibraryPaths(File rootDir) { + def osName = System.getProperty("os.name") + def osArch = System.getProperty("os.arch") + def isLinux = osName.toLowerCase(Locale.getDefault()).contains("linux") + def base = rootDir == null ? "" : "${rootDir}/" + + def debugPaths = [ + isLinux ? + /* Linux */ (osArch == "amd64" || osArch == "x86_64" ? + "${base}.build/x86_64-unknown-linux-gnu/debug/" : + "${base}.build/${osArch}-unknown-linux-gnu/debug/") : + /* macOS */ (osArch == "aarch64" ? + "${base}.build/arm64-apple-macosx/debug/" : + "${base}.build/${osArch}-apple-macosx/debug/"), + isLinux ? + /* Linux */ (osArch == "amd64" || osArch == "x86_64" ? + "${base}../../.build/x86_64-unknown-linux-gnu/debug/" : + "${base}../../.build/${osArch}-unknown-linux-gnu/debug/") : + /* macOS */ (osArch == "aarch64" ? + "${base}../../.build/arm64-apple-macosx/debug/" : + "${base}../../.build/${osArch}-apple-macosx/debug/"), + ] + def releasePaths = debugPaths.collect { it.replaceAll("debug", "release") } + def swiftRuntimePaths = getSwiftRuntimeLibraryPaths() + + return releasePaths + debugPaths + swiftRuntimePaths + } +} diff --git a/buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt b/buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt deleted file mode 100644 index d9faff2a..00000000 --- a/buildSrc/src/main/kotlin/org/swift/swiftjava/gradle/JavaLibraryPathUtils.kt +++ /dev/null @@ -1,81 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -package org.swift.swiftjava.gradle - -import java.io.File -import java.util.* -import kotlin.system.exitProcess -import kotlinx.serialization.json.* - -object JavaLibraryPathUtils { - - private fun getSwiftRuntimeLibraryPaths(): List { - val process = ProcessBuilder("swiftc", "-print-target-info") - .redirectError(ProcessBuilder.Redirect.INHERIT) - .start() - - val output = process.inputStream.bufferedReader().use { it.readText() } - val exitCode = process.waitFor() - if (exitCode != 0) { - System.err.println("Error executing swiftc -print-target-info") - exitProcess(exitCode) - } - - val json = Json.parseToJsonElement(output) - val runtimeLibraryPaths = json.jsonObject["paths"]?.jsonObject?.get("runtimeLibraryPaths")?.jsonArray - return runtimeLibraryPaths?.map { it.jsonPrimitive.content } ?: emptyList() - } - - /** - * Find library paths for 'java.library.path' when running or testing projects inside this build. - */ - @JvmStatic - fun javaLibraryPaths(rootDir: File): List { - val osName = System.getProperty("os.name").lowercase(Locale.getDefault()) - val osArch = System.getProperty("os.arch") - val isLinux = osName.contains("linux") - val base = rootDir.path.let { "$it/" } - - val projectBuildOutputPath = - if (isLinux) { - if (osArch == "amd64" || osArch == "x86_64") - "$base.build/x86_64-unknown-linux-gnu" - else - "$base.build/${osArch}-unknown-linux-gnu" - } else { - if (osArch == "aarch64") - "$base.build/arm64-apple-macosx" - else - "$base.build/${osArch}-apple-macosx" - } - val parentParentBuildOutputPath = - "../../$projectBuildOutputPath" - - - val swiftBuildOutputPaths = listOf( - projectBuildOutputPath, - parentParentBuildOutputPath - ) - - val debugBuildOutputPaths = swiftBuildOutputPaths.map { "$it/debug" } - val releaseBuildOutputPaths = swiftBuildOutputPaths.map { "$it/release" } - val swiftRuntimePaths = getSwiftRuntimeLibraryPaths() - - return debugBuildOutputPaths + releaseBuildOutputPaths + swiftRuntimePaths - } -} - - -// DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE DONE