Repository navigation
Expand file tree
/
Copy pathbuild.gradle
More file actions
151 lines (137 loc) · 6.39 KB
/
Copy pathbuild.gradle
File metadata and controls
151 lines (137 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.Optional
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask
buildscript {
repositories {
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath 'org.graalvm.buildtools:native-gradle-plugin:1.1.17'
}
}
apply plugin: 'java-library'
apply plugin: 'org.graalvm.buildtools.native'
apply plugin: 'open-java-format.publishing-conventions'
// nativePlatform: the <os>_<arch> of this machine, as the image name and its classifier spell it.
apply plugin: 'open-java-format.native-platform'
description = 'open-java-format compiled to a native executable with GraalVM, one classifier per platform'
dependencies {
implementation project(':open-java-format')
}
def exports = [
'jdk.compiler/com.sun.tools.javac.file',
'jdk.compiler/com.sun.tools.javac.main',
'jdk.compiler/com.sun.tools.javac.parser',
'jdk.compiler/com.sun.tools.javac.tree',
'jdk.compiler/com.sun.tools.javac.util',
'jdk.compiler/com.sun.tools.javac.code',
'jdk.compiler/com.sun.tools.javac.api'
]
def jvmArgList = exports.collect { value -> "--add-exports=${value}=ALL-UNNAMED".toString() }
graalvmNative {
// 1.x turns the shared GraalVM reachability-metadata repository on by default; 0.10.x had
// no such thing, and it refuses to run unless the JDK is newer than the one Gradle resolves
// here. This project ships its own metadata (configurationFileDirectories below), so keep
// the previous behaviour rather than couple the build to a GraalVM patch level.
metadataRepository {
enabled = false
}
// native-gradle-plugin 1.x exposes these as lazy Property/FileCollection. Groovy's `=`
// shorthand does not reach them through the container's proxy, so set them explicitly.
binaries.named('main') {
it.javaLauncher.set(javaToolchains.launcherFor {
it.languageVersion.set(JavaLanguageVersion.of(25))
it.vendor.set(JvmVendorSpec.GRAAL_VM)
})
it.imageName.set("open-java-format-${nativePlatform}".toString())
it.mainClass.set('com.palantir.javaformat.java.Main')
it.fallback.set(false)
it.sharedLibrary.set(false)
it.classpath.from(configurations.named('runtimeClasspath'))
it.configurationFileDirectories.from(file('src/main/resources/META-INF/native-image'))
it.buildArgs.addAll(
'-H:+UnlockExperimentalVMOptions',
'-H:+IncludeAllLocales',
'--no-fallback',
// Missing reachability metadata must fail loudly instead of looking like an
// absent class: without this an unregistered Class.forName throws
// ClassNotFoundException, and any optional-dependency fallback swallows it.
// GraalVM 23+ only — affordable because the image is the one artifact that
// owes nobody backwards compatibility.
'--exact-reachability-metadata',
'--initialize-at-build-time=com.sun.tools.javac.file.Locations',
'-H:+ReportExceptionStackTraces',
'-H:-UseContainerSupport',
"-march=compatibility")
it.jvmArgs.addAll(jvmArgList)
}
}
// One publication either way: external-publish's CI-only variant is gone, and JReleaser
// stages whatever maven-publish produces.
//
// -PnativeImages=<dir> publishes every binary in that directory rather than the one compiled here.
// A released version can never gain files afterwards, so the per-platform CI jobs hand their
// binaries to a single deploy job (.github/workflows/release.yml) and all the classifiers go up
// together. Each classifier is read back out of the file name, exactly as for a local build.
def collectedNativeImages = providers.gradleProperty('nativeImages').getOrNull()
publishing {
publications {
nativeImage(MavenPublication) {
if (collectedNativeImages == null) {
artifact(tasks.named('nativeCompile', BuildNativeImageTask.class).map { it.getOutputFile() }) {
classifier = String.format("nativeImage-%s", getOsArchName(it.getFile()))
extension = getExtension(it.getFile())
}
} else {
def imagesDir = rootProject.file(collectedNativeImages)
def binaries = fileTree(imagesDir) {
include 'open-java-format-*'
exclude '**/*.txt'
}.files.sort { it.name }
if (binaries.isEmpty()) {
throw new GradleException("No native images to publish in ${imagesDir}")
}
binaries.each { binary ->
artifact(binary) {
classifier = String.format("nativeImage-%s", getOsArchName(binary))
extension = getExtension(binary)
}
}
}
}
}
}
// A binary is named open-java-format-<os>_<arch>, and native-image adds .exe on Windows. The .exe stays
// out of the classifier: it is the extension.
private static String getExtension(File file) {
Pattern pattern = Pattern.compile("open-java-format-(.*)_(.*?)(\\.exe)?\$");
Matcher matcher = pattern.matcher(file.name)
if (matcher.find()) {
return matcher.group(3) == null ? "bin" : "exe"
}
throw new GradleException("Could not determine OS name from file name: ${file.name}")
}
private static String getOsArchName(File file) {
Pattern pattern = Pattern.compile("open-java-format-(.*)_(.*?)(\\.exe)?\$");
Matcher matcher = pattern.matcher(file.name)
if (matcher.find()) {
return matcher.group(1) + "_" + matcher.group(2)
}
throw new GradleException("Could not determine OS name from file name: ${file.name}")
}
configurations {
nativeImage {
canBeConsumed = true
canBeResolved = false
// Publishing the binary here is precisely what makes `assemble` (and therefore
// `build`) depend on `nativeCompile`, so it is opt-in. See rootProject.build.gradle.
if (rootProject.ext.nativeImageRequested) {
outgoing.artifact(tasks.named('nativeCompile').map(task -> task.getOutputFile()))
}
attributes {
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'nativeImage'))
}
}
}