-
Notifications
You must be signed in to change notification settings - Fork 77
Migrate build scripts from Groovy to Kotlin #525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ktoso
merged 5 commits into
swiftlang:main
from
abdulowork:timofey.solonin/migrate-from-groovy-to-kts
Jan 22, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca20af0
Migrate build scripts from Groovy to Kotlin
abdulowork 20f0c5d
Add missing licenses
abdulowork c5e3f47
Declare the exact path of the jextract generated sources
abdulowork 28de286
Add license to the version catalog toml file
abdulowork 047c04e
Use the target instead of the product to infer the jextract output path
abdulowork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../gradle/libs.versions.toml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 org.gradle.api.Project | ||
| import org.gradle.api.tasks.Exec | ||
| import org.gradle.kotlin.dsl.register | ||
| import org.gradle.kotlin.dsl.support.serviceOf | ||
| import org.gradle.process.ExecOperations | ||
| import utilities.SwiftcTargetInfo | ||
| import utilities.json | ||
| import utilities.swiftPMPackage | ||
| import java.io.ByteArrayOutputStream | ||
| import java.io.File | ||
|
|
||
| fun Project.swiftProductDylibPaths(swiftBuildConfiguration: String): List<String> { | ||
| // TODO: require that we depend on swift-java | ||
| // TODO: all the products where the targets depend on swift-java plugin | ||
| return swiftPMPackage().targets.map { | ||
| it.productMemberships | ||
| }.flatten().map { | ||
| logger.info("[swift-java] Include Swift product: '${it}' in product resource paths.") | ||
| "${layout.projectDirectory}/.build/${swiftBuildConfiguration}/lib${it}.dylib" | ||
| } | ||
| } | ||
|
|
||
| fun Project.registerCleanSwift(workingDir: File = layout.projectDirectory.asFile) { | ||
| val cleanSwift = tasks.register<Exec>("cleanSwift") { | ||
| this.workingDir = workingDir | ||
| commandLine("swift") | ||
| args("package", "clean") | ||
| } | ||
| tasks.named("clean").configure { | ||
| dependsOn(cleanSwift) | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 utilities | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
| import org.gradle.api.Project | ||
| import org.gradle.kotlin.dsl.support.serviceOf | ||
| import org.gradle.process.ExecOperations | ||
| import java.io.ByteArrayOutputStream | ||
|
|
||
| @Serializable | ||
| internal data class SwiftPMPackage( | ||
| val targets: List<SwiftPMTarget>, | ||
| ) | ||
|
|
||
| internal fun Project.swiftPMPackage(): SwiftPMPackage { | ||
| val stdout = ByteArrayOutputStream() | ||
| serviceOf<ExecOperations>().exec { | ||
| workingDir(projectDir) | ||
| commandLine("swift", "package", "describe", "--type", "json") | ||
| standardOutput = stdout | ||
| } | ||
| return json.decodeFromString<SwiftPMPackage>(stdout.toString()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 utilities | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| internal data class SwiftPMTarget( | ||
| val name: String, | ||
| @SerialName("product_dependencies") | ||
| val productDependencies: List<String> = emptyList(), | ||
| @SerialName("product_memberships") | ||
| val productMemberships: List<String> = emptyList(), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
BuildLogic/src/main/kotlin/utilities/SwiftcTargetInfoPaths.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 utilities | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| internal data class SwiftcTargetInfoPaths( | ||
| val runtimeLibraryPaths: List<String>, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 utilities | ||
|
|
||
| import org.gradle.api.Project | ||
| import org.gradle.kotlin.dsl.support.serviceOf | ||
| import org.gradle.process.ExecOperations | ||
| import java.io.ByteArrayOutputStream | ||
| import java.io.File | ||
|
|
||
| private fun Project.swiftRuntimeLibraryPaths(): List<String> { | ||
| val stdout = ByteArrayOutputStream() | ||
| serviceOf<ExecOperations>().exec { | ||
| workingDir(projectDir) | ||
| commandLine("swiftc", "-print-target-info") | ||
| standardOutput = stdout | ||
| } | ||
| return json.decodeFromString<SwiftcTargetInfo>(stdout.toString()).paths.runtimeLibraryPaths | ||
| } | ||
|
|
||
| fun Project.javaLibraryPaths(rootDir: File?): List<String> { | ||
| val osName = System.getProperty("os.name") | ||
| val osArch = System.getProperty("os.arch") | ||
| val isLinux = osName.lowercase().contains("linux") | ||
| val base = if (rootDir == null) "" else "${rootDir}/" | ||
|
|
||
| val triple = if (isLinux) { | ||
| val arch = if (osArch == "amd64" || osArch == "x86_64") "x86_64" else osArch | ||
| "${arch}-unknown-linux-gnu" | ||
| } else { | ||
| val arch = if (osArch == "aarch64") "arm64" else osArch | ||
| "${arch}-apple-macosx" | ||
| } | ||
|
|
||
| val paths: List<String> = listOf("release", "debug").flatMap { configuration -> | ||
| listOf( | ||
| "${base}.build/${triple}/$configuration/", | ||
| "${base}../../.build/${triple}/$configuration/", | ||
| ) | ||
| } | ||
| val swiftRuntimePaths = swiftRuntimeLibraryPaths() | ||
|
|
||
| return paths + swiftRuntimePaths | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 utilities | ||
|
|
||
| import kotlinx.serialization.json.Json | ||
|
|
||
| internal val json = Json { ignoreUnknownKeys = true } |
76 changes: 76 additions & 0 deletions
76
BuildLogic/src/main/kotlin/utilities/registerJextractTask.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 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 utilities | ||
|
|
||
| import org.gradle.api.Project | ||
| import org.gradle.api.tasks.Exec | ||
| import org.gradle.api.tasks.TaskProvider | ||
| import org.gradle.kotlin.dsl.register | ||
| import java.io.File | ||
|
|
||
| private fun Project.swiftTargetsWithJExtractPlugin(): List<String> = swiftPMPackage().targets.filter { | ||
| it.productDependencies.contains("JExtractSwiftPlugin") | ||
| }.map { | ||
| it.name | ||
| } | ||
|
|
||
| private fun Project.registerSwiftCheckValidTask(): TaskProvider<*> = tasks.register<Exec>("swift-check-valid") { | ||
| commandLine("swift") | ||
| args("-version") | ||
| } | ||
|
|
||
| fun Project.registerJextractTask( | ||
| arguments: () -> List<String> = { | ||
| listOf("build", "--disable-experimental-prebuilts") | ||
| } | ||
| ): TaskProvider<*> { | ||
| val swiftCheckValid = registerSwiftCheckValidTask() | ||
| return tasks.register<Exec>("jextract") { | ||
| 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(File(rootDir, "Package.swift")) | ||
| inputs.dir(File(rootDir, "Sources")) | ||
|
|
||
| // If the package description changes, we should execute jextract again, maybe we added jextract to new targets | ||
| inputs.file(File(projectDir, "Package.swift")) | ||
|
|
||
| // monitor all targets/products which depend on the JExtract plugin | ||
| swiftTargetsWithJExtractPlugin().forEach { targetName -> | ||
| logger.info("[swift-java:jextract (Gradle)] Swift input target: ${targetName}") | ||
| inputs.dir(File(layout.projectDirectory.asFile, "Sources/${targetName}")) | ||
| outputs.dir( | ||
| layout.buildDirectory.dir( | ||
| "../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().lowercase()}/${targetName}/destination/JExtractSwiftPlugin/src/generated/java" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| workingDir = layout.projectDirectory.asFile | ||
| commandLine("swift") | ||
| // FIXME: disable prebuilts until swift-syntax isn't broken on 6.2 anymore: https://github.com/swiftlang/swift-java/issues/418 | ||
| args(arguments()) // 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") | ||
| // ) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.