1
1
@file:Suppress(" RedundantUnitExpression" )
2
2
3
- import com.android.tools.r8.internal.os
4
-
5
3
plugins {
6
4
id(" module-conventions" )
7
5
id(" java-library" )
8
6
id(" maven-publish" )
9
7
id(libs.plugins.mavenPublish.get().pluginId)
10
8
}
11
9
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() =
37
20
when (os) {
38
21
" macos" -> " dylib"
39
22
" windows" -> " dll"
40
23
else -> " so"
41
24
}
42
25
43
- val sharedLibraryName =
26
+ val DesktopVariant .sharedLibraryName: String
27
+ get() =
44
28
when (os) {
45
29
" windows" -> " maplibre-jni.${sharedLibraryExtension} "
46
30
else -> " libmaplibre-jni.${sharedLibraryExtension} "
47
31
}
48
32
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" )
80
35
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 " )
82
41
83
42
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)) }
88
45
}
89
46
}
90
47
91
48
java {
92
49
toolchain {
93
50
languageVersion.set(JavaLanguageVersion .of(properties[" jvmToolchain" ]!! .toString().toInt()))
94
51
}
95
- for (variant in Variant .values( )) {
52
+ for (variant in DesktopVariant .currentValues(project )) {
96
53
registerFeature(variant.name) { usingSourceSet(sourceSets[variant.sourceSetName]) }
97
54
}
98
55
}
@@ -118,7 +75,7 @@ publishing {
118
75
}
119
76
}
120
77
121
- if (configureForPublishing ) {
78
+ if (config.shouldConfigureForPublishing ) {
122
79
// when publishing, we build all variants in CI and copy them to the resources directory
123
80
// so in gradle, we just need to validate that they're present
124
81
@@ -127,7 +84,7 @@ if (configureForPublishing) {
127
84
128
85
doLast {
129
86
val missing = mutableListOf<String >()
130
- for (variant in Variant .values().filter { it.publish } ) {
87
+ for (variant in DesktopVariant .currentValues(project) ) {
131
88
val file =
132
89
variant.resourcesTargetDirectory(layout).get().asFile.resolve(variant.sharedLibraryName)
133
90
if (! file.exists()) {
@@ -160,7 +117,7 @@ if (configureForPublishing) {
160
117
inputs.dir(layout.buildDirectory.dir(" generated/simplejni-headers" ))
161
118
162
119
// 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
164
121
val buildDir = layout.buildDirectory.dir(" cmake/${preset} " )
165
122
outputs.dir(buildDir)
166
123
@@ -185,7 +142,7 @@ if (configureForPublishing) {
185
142
dependsOn(" configureCMake" )
186
143
dependsOn(" :lib:maplibre-native-bindings:kspKotlinDesktop" )
187
144
188
- val variant = Variant .current (project)
145
+ val variant = DesktopVariant .currentValues (project).first( )
189
146
val preset = variant.cmakePreset
190
147
val buildDir = layout.buildDirectory.dir(" cmake/${preset} " ).get().asFile
191
148
workingDir = buildDir
@@ -212,7 +169,7 @@ if (configureForPublishing) {
212
169
group = " build"
213
170
dependsOn(" buildNative" )
214
171
215
- val variant = Variant .current (project)
172
+ val variant = DesktopVariant .currentValues (project).first( )
216
173
val fromDirectory = variant.cmakeOutputDirectory(layout)
217
174
val intoDirectory = variant.resourcesTargetDirectory(layout)
218
175
@@ -226,7 +183,7 @@ if (configureForPublishing) {
226
183
}
227
184
}
228
185
229
- Variant .values( ).forEach { variant ->
186
+ DesktopVariant .currentValues(project ).forEach { variant ->
230
187
tasks.named(" process${variant.sourceSetName} Resources" ) { dependsOn(" copyNativeToResources" ) }
231
188
}
232
189
0 commit comments