Skip to content

Commit 6167afb

Browse files
authored
Fix and clean up publish config (#582)
1 parent 9c90a4b commit 6167afb

File tree

3 files changed

+101
-84
lines changed

3 files changed

+101
-84
lines changed

buildSrc/src/main/kotlin/util.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,75 @@ fun KotlinNativeTarget.configureSpmMaplibre(project: Project) {
2121
binaries.all { linkerOpts("-F$rpath", "-rpath", rpath) }
2222
compilations.getByName("main") { cinterops { create("spmMaplibre") } }
2323
}
24+
25+
class Configuration(private val project: Project) {
26+
val hostOs =
27+
when (val os = System.getProperty("os.name").lowercase()) {
28+
"mac os x" -> "macos"
29+
else -> os.split(" ").first()
30+
}
31+
32+
val hostArch =
33+
when (val arch = System.getProperty("os.arch").lowercase()) {
34+
"x86_64" -> "amd64" // jdk returns x86_64 on macos but amd64 elsewhere
35+
else -> arch
36+
}
37+
38+
val desktopRenderer: String
39+
get() =
40+
project.findProperty("desktopRenderer")?.toString()
41+
?: when (hostOs) {
42+
"macos" -> "metal"
43+
else -> "opengl"
44+
}
45+
46+
val hostOsArchRendererTriplet: String
47+
get() = "${hostOs}-${hostArch}-${desktopRenderer}"
48+
49+
val shouldConfigureForPublishing
50+
get() = project.properties["configureForPublishing"]?.toString()?.toBoolean() ?: false
51+
}
52+
53+
enum class DesktopVariant(
54+
val os: String,
55+
val arch: String,
56+
val renderer: String,
57+
val publish: Boolean = false,
58+
) {
59+
MacosAmd64Metal("macos", "amd64", "metal"),
60+
MacosAarch64Metal("macos", "aarch64", "metal", true),
61+
MacosAmd64Vulkan("macos", "amd64", "vulkan"),
62+
MacosAarch64Vulkan("macos", "aarch64", "vulkan"),
63+
LinuxAmd64Opengl("linux", "amd64", "opengl", true),
64+
LinuxAarch64Opengl("linux", "aarch64", "opengl"),
65+
LinuxAmd64Vulkan("linux", "amd64", "vulkan"),
66+
LinuxAarch64Vulkan("linux", "aarch64", "vulkan"),
67+
WindowsAmd64Opengl("windows", "amd64", "opengl", true),
68+
WindowsAarch64Opengl("windows", "aarch64", "opengl"),
69+
WindowsAmd64Vulkan("windows", "amd64", "vulkan"),
70+
WindowsAarch64Vulkan("windows", "aarch64", "vulkan");
71+
72+
companion object {
73+
74+
private fun valueForHost(project: Project): DesktopVariant {
75+
val config = Configuration(project)
76+
return values().firstOrNull {
77+
it.os == config.hostOs &&
78+
it.arch == config.hostArch &&
79+
it.renderer == config.desktopRenderer
80+
}
81+
?: error(
82+
"Unsupported combination: ${config.hostOs}/${config.hostArch}/${config.desktopRenderer}"
83+
)
84+
}
85+
86+
private fun valuesForPublishing(): List<DesktopVariant> {
87+
return values().filter { it.publish }
88+
}
89+
90+
fun currentValues(project: Project): List<DesktopVariant> {
91+
return if (Configuration(project).shouldConfigureForPublishing) valuesForPublishing()
92+
else listOf(valueForHost(project))
93+
}
94+
}
95+
}

demo-app/build.gradle.kts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,8 @@ kotlin {
138138

139139
runtimeOnly(project(":lib:maplibre-native-bindings-jni")) {
140140
capabilities {
141-
val osPart =
142-
when (val os = System.getProperty("os.name").lowercase()) {
143-
"mac os x" -> "macos"
144-
else -> os.split(' ').first()
145-
}
146-
val archPart =
147-
when (val arch = System.getProperty("os.arch").lowercase()) {
148-
"x86_64" -> "amd64" // jdk returns x86_64 on macos but amd64 elsewhere
149-
else -> arch
150-
}
151-
val rendererPart =
152-
project.properties["desktopRenderer"] ?: if (osPart == "macos") "metal" else "opengl"
153141
requireCapability(
154-
"org.maplibre.compose:maplibre-native-bindings-jni-$osPart-$archPart-$rendererPart"
142+
"org.maplibre.compose:maplibre-native-bindings-jni-${Configuration(project).hostOsArchRendererTriplet}"
155143
)
156144
}
157145
}

lib/maplibre-native-bindings-jni/build.gradle.kts

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,55 @@
11
@file:Suppress("RedundantUnitExpression")
22

3-
import com.android.tools.r8.internal.os
4-
53
plugins {
64
id("module-conventions")
75
id("java-library")
86
id("maven-publish")
97
id(libs.plugins.mavenPublish.get().pluginId)
108
}
119

12-
enum class Variant(
13-
val os: String,
14-
val arch: String,
15-
val renderer: String,
16-
// TODO: default true when all/most are publishable
17-
val publish: Boolean = false,
18-
) {
19-
// TODO: enable alternate architectures and renderers
20-
MacosAmd64Metal("macos", "amd64", "metal"),
21-
MacosAarch64Metal("macos", "aarch64", "metal", true),
22-
MacosAmd64Vulkan("macos", "amd64", "vulkan"),
23-
MacosAarch64Vulkan("macos", "aarch64", "vulkan"),
24-
LinuxAmd64Opengl("linux", "amd64", "opengl", true),
25-
LinuxAarch64Opengl("linux", "aarch64", "opengl"),
26-
LinuxAmd64Vulkan("linux", "amd64", "vulkan"),
27-
LinuxAarch64Vulkan("linux", "aarch64", "vulkan"),
28-
WindowsAmd64Opengl("windows", "amd64", "opengl", true),
29-
WindowsAarch64Opengl("windows", "aarch64", "opengl"),
30-
WindowsAmd64Vulkan("windows", "amd64", "vulkan"),
31-
WindowsAarch64Vulkan("windows", "aarch64", "vulkan");
32-
33-
val sourceSetName = "${name}Main"
34-
val cmakePreset = "$os-$renderer"
35-
36-
val sharedLibraryExtension =
10+
val config = Configuration(project)
11+
12+
val DesktopVariant.sourceSetName: String
13+
get() = "${name}Main"
14+
15+
val DesktopVariant.cmakePreset: String
16+
get() = "${os}-${renderer}"
17+
18+
val DesktopVariant.sharedLibraryExtension: String
19+
get() =
3720
when (os) {
3821
"macos" -> "dylib"
3922
"windows" -> "dll"
4023
else -> "so"
4124
}
4225

43-
val sharedLibraryName =
26+
val DesktopVariant.sharedLibraryName: String
27+
get() =
4428
when (os) {
4529
"windows" -> "maplibre-jni.${sharedLibraryExtension}"
4630
else -> "libmaplibre-jni.${sharedLibraryExtension}"
4731
}
4832

49-
fun cmakeOutputDirectory(layout: ProjectLayout) =
50-
layout.buildDirectory.dir("lib/$cmakePreset/shared")
51-
52-
fun resourcesTargetDirectory(layout: ProjectLayout) =
53-
layout.buildDirectory.dir("copiedResources/$name/$os/$arch/$renderer")
54-
55-
fun resourcesSourceDir(layout: ProjectLayout) = layout.buildDirectory.dir("copiedResources/$name")
56-
57-
companion object {
58-
private fun find(os: String, arch: String, renderer: String? = null) =
59-
Variant.values().firstOrNull {
60-
it.os == os && it.arch == arch && (renderer == null || it.renderer == renderer)
61-
} ?: error("Unsupported combination: ${os}/${arch}/${renderer}")
62-
63-
fun current(project: Project): Variant {
64-
return find(
65-
os =
66-
when (val os = System.getProperty("os.name").lowercase()) {
67-
"mac os x" -> "macos"
68-
else -> os.split(" ").first()
69-
},
70-
arch =
71-
when (val arch = System.getProperty("os.arch").lowercase()) {
72-
"x86_64" -> "amd64" // jdk returns x86_64 on macos but amd64 elsewhere
73-
else -> arch
74-
},
75-
renderer = project.findProperty("desktopRenderer")?.toString(),
76-
)
77-
}
78-
}
79-
}
33+
fun DesktopVariant.cmakeOutputDirectory(layout: ProjectLayout) =
34+
layout.buildDirectory.dir("lib/$cmakePreset/shared")
8035

81-
val configureForPublishing = project.findProperty("configureForPublishing")?.toString() == "true"
36+
fun DesktopVariant.resourcesTargetDirectory(layout: ProjectLayout) =
37+
layout.buildDirectory.dir("copiedResources/$name/${os}/$arch/$renderer")
38+
39+
fun DesktopVariant.resourcesSourceDir(layout: ProjectLayout) =
40+
layout.buildDirectory.dir("copiedResources/$name")
8241

8342
sourceSets {
84-
for (variant in Variant.values()) {
85-
if (!configureForPublishing || variant.publish) {
86-
create(variant.sourceSetName) { resources.srcDir(variant.resourcesSourceDir(layout)) }
87-
}
43+
for (variant in DesktopVariant.currentValues(project)) {
44+
create(variant.sourceSetName) { resources.srcDir(variant.resourcesSourceDir(layout)) }
8845
}
8946
}
9047

9148
java {
9249
toolchain {
9350
languageVersion.set(JavaLanguageVersion.of(properties["jvmToolchain"]!!.toString().toInt()))
9451
}
95-
for (variant in Variant.values()) {
52+
for (variant in DesktopVariant.currentValues(project)) {
9653
registerFeature(variant.name) { usingSourceSet(sourceSets[variant.sourceSetName]) }
9754
}
9855
}
@@ -118,7 +75,7 @@ publishing {
11875
}
11976
}
12077

121-
if (configureForPublishing) {
78+
if (config.shouldConfigureForPublishing) {
12279
// when publishing, we build all variants in CI and copy them to the resources directory
12380
// so in gradle, we just need to validate that they're present
12481

@@ -127,7 +84,7 @@ if (configureForPublishing) {
12784

12885
doLast {
12986
val missing = mutableListOf<String>()
130-
for (variant in Variant.values().filter { it.publish }) {
87+
for (variant in DesktopVariant.currentValues(project)) {
13188
val file =
13289
variant.resourcesTargetDirectory(layout).get().asFile.resolve(variant.sharedLibraryName)
13390
if (!file.exists()) {
@@ -160,7 +117,7 @@ if (configureForPublishing) {
160117
inputs.dir(layout.buildDirectory.dir("generated/simplejni-headers"))
161118

162119
// Use preset-specific subdirectory to avoid rebuilding when switching presets
163-
val preset = Variant.current(project).cmakePreset
120+
val preset = DesktopVariant.currentValues(project).first().cmakePreset
164121
val buildDir = layout.buildDirectory.dir("cmake/${preset}")
165122
outputs.dir(buildDir)
166123

@@ -185,7 +142,7 @@ if (configureForPublishing) {
185142
dependsOn("configureCMake")
186143
dependsOn(":lib:maplibre-native-bindings:kspKotlinDesktop")
187144

188-
val variant = Variant.current(project)
145+
val variant = DesktopVariant.currentValues(project).first()
189146
val preset = variant.cmakePreset
190147
val buildDir = layout.buildDirectory.dir("cmake/${preset}").get().asFile
191148
workingDir = buildDir
@@ -212,7 +169,7 @@ if (configureForPublishing) {
212169
group = "build"
213170
dependsOn("buildNative")
214171

215-
val variant = Variant.current(project)
172+
val variant = DesktopVariant.currentValues(project).first()
216173
val fromDirectory = variant.cmakeOutputDirectory(layout)
217174
val intoDirectory = variant.resourcesTargetDirectory(layout)
218175

@@ -226,7 +183,7 @@ if (configureForPublishing) {
226183
}
227184
}
228185

229-
Variant.values().forEach { variant ->
186+
DesktopVariant.currentValues(project).forEach { variant ->
230187
tasks.named("process${variant.sourceSetName}Resources") { dependsOn("copyNativeToResources") }
231188
}
232189

0 commit comments

Comments
 (0)