Skip to content

Commit 9f96867

Browse files
committed
Name the native image without palantir's platform library
open-java-format-native put com.palantir.gradle.utils:platform on its buildscript classpath for two things: the <os>_<arch> part of the image name, and whether a collected binary is a Windows .exe. The new buildSrc plugin open-java-format.native-platform works out the first with the same names (macos, windows, linux-glibc or linux-musl from ldd --version; x86-64, aarch64, x86) and sets it as nativePlatform. The extension now comes from the .exe suffix the file name pattern already matched. The image here is still open-java-format-macos_aarch64, and six dummy binaries passed in with -PnativeImages get the classifiers and extensions already published on Maven Central.
1 parent faa74ef commit 9f96867

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// The platform this build runs on, as `nativePlatform`: <os>_<arch>, the names open-java-format-native's
2+
// classifiers carry (nativeImage-<os>_<arch>) and the Gradle plugin resolves (NativeImageFormatProviderPlugin).
3+
def osName = providers.systemProperty('os.name').get().toLowerCase(Locale.ROOT)
4+
def osArch = providers.systemProperty('os.arch').get().toLowerCase(Locale.ROOT)
5+
6+
String os
7+
if (osName.startsWith('mac')) {
8+
os = 'macos'
9+
} else if (osName.startsWith('windows')) {
10+
os = 'windows'
11+
} else if (osName.startsWith('linux')) {
12+
// A glibc image does not run on musl. glibc's ldd prints its version to stdout; musl's prints it
13+
// to stderr and exits with 1.
14+
def ldd = providers.exec {
15+
commandLine 'ldd', '--version'
16+
ignoreExitValue = true
17+
}
18+
def libc = (ldd.standardOutput.asText.get() + ldd.standardError.asText.get()).toLowerCase(Locale.ROOT)
19+
if (libc.contains('glibc') || libc.contains('gnu libc')) {
20+
os = 'linux-glibc'
21+
} else if (libc.contains('musl')) {
22+
os = 'linux-musl'
23+
} else {
24+
throw new GradleException("Cannot tell glibc from musl: ldd --version printed ${libc}")
25+
}
26+
} else {
27+
throw new GradleException("No native image name for the operating system ${osName}")
28+
}
29+
30+
String arch
31+
switch (osArch) {
32+
case ['x86_64', 'x64', 'amd64']:
33+
arch = 'x86-64'
34+
break
35+
case ['arm', 'arm64', 'aarch64']:
36+
arch = 'aarch64'
37+
break
38+
case ['x86', 'i686']:
39+
arch = 'x86'
40+
break
41+
default:
42+
throw new GradleException("No native image name for the architecture ${osArch}")
43+
}
44+
45+
ext.nativePlatform = "${os}_${arch}".toString()

‎open-java-format-native/build.gradle‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,22 @@ buildscript {
1010
}
1111
dependencies {
1212
classpath 'org.graalvm.buildtools:native-gradle-plugin:1.1.14'
13-
classpath 'com.palantir.gradle.utils:platform:0.29.0'
1413
}
1514
}
1615

1716

1817
apply plugin: 'java-library'
1918
apply plugin: 'org.graalvm.buildtools.native'
2019
apply plugin: 'open-java-format.publishing-conventions'
20+
// nativePlatform: the <os>_<arch> of this machine, as the image name and its classifier spell it.
21+
apply plugin: 'open-java-format.native-platform'
2122

2223
description = 'open-java-format compiled to a native executable with GraalVM, one classifier per platform'
2324

2425
dependencies {
2526
implementation project(':open-java-format')
2627
}
2728

28-
import com.palantir.platform.OperatingSystem
29-
import com.palantir.platform.Architecture
30-
31-
def operatingSystem = OperatingSystem.get()
32-
def architecture = Architecture.get()
33-
3429
def exports = [
3530
'jdk.compiler/com.sun.tools.javac.file',
3631
'jdk.compiler/com.sun.tools.javac.main',
@@ -59,7 +54,7 @@ graalvmNative {
5954
it.languageVersion.set(JavaLanguageVersion.of(25))
6055
it.vendor.set(JvmVendorSpec.GRAAL_VM)
6156
})
62-
it.imageName.set(String.format('open-java-format-%s_%s', operatingSystem.uiName(), architecture.uiName()))
57+
it.imageName.set("open-java-format-${nativePlatform}".toString())
6358
it.mainClass.set('com.palantir.javaformat.java.Main')
6459
it.fallback.set(false)
6560
it.sharedLibrary.set(false)
@@ -126,8 +121,7 @@ private static String getExtension(File file) {
126121
Pattern pattern = Pattern.compile("open-java-format-(.*)_(.*?)(\\.exe)?\$");
127122
Matcher matcher = pattern.matcher(file.name)
128123
if (matcher.find()) {
129-
return OperatingSystem.fromStringThrowing(matcher.group(1))
130-
== OperatingSystem.WINDOWS ? "exe" : "bin"
124+
return matcher.group(3) == null ? "bin" : "exe"
131125
}
132126
throw new GradleException("Could not determine OS name from file name: ${file.name}")
133127
}

0 commit comments

Comments
 (0)