|
| 1 | +import java.util.regex.Matcher |
| 2 | +import java.util.regex.Pattern |
| 3 | +import java.util.Optional |
| 4 | +import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask |
| 5 | + |
| 6 | +buildscript { |
| 7 | + repositories { |
| 8 | + mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } } |
| 9 | + gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } } |
| 10 | + } |
| 11 | + dependencies { |
| 12 | + classpath 'org.graalvm.buildtools:native-gradle-plugin:0.10.4' |
| 13 | + classpath 'com.palantir.gradle.utils:platform:0.10.0' |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +apply plugin: 'java-library' |
| 19 | +apply plugin: 'org.graalvm.buildtools.native' |
| 20 | +apply plugin: 'com.palantir.external-publish-custom' |
| 21 | + |
| 22 | +dependencies { |
| 23 | + implementation project(':palantir-java-format') |
| 24 | +} |
| 25 | + |
| 26 | +import com.palantir.platform.OperatingSystem |
| 27 | +import com.palantir.platform.Architecture |
| 28 | + |
| 29 | +def operatingSystem = OperatingSystem.get() |
| 30 | +def architecture = Architecture.get() |
| 31 | + |
| 32 | +def exports = [ |
| 33 | + 'jdk.compiler/com.sun.tools.javac.file', |
| 34 | + 'jdk.compiler/com.sun.tools.javac.main', |
| 35 | + 'jdk.compiler/com.sun.tools.javac.parser', |
| 36 | + 'jdk.compiler/com.sun.tools.javac.tree', |
| 37 | + 'jdk.compiler/com.sun.tools.javac.util', |
| 38 | + 'jdk.compiler/com.sun.tools.javac.code', |
| 39 | + 'jdk.compiler/com.sun.tools.javac.api' |
| 40 | +] |
| 41 | + |
| 42 | +def jvmArgList = exports.collect { value -> "--add-exports=${value}=ALL-UNNAMED".toString() } |
| 43 | + |
| 44 | +graalvmNative { |
| 45 | + binaries { |
| 46 | + main { |
| 47 | + javaLauncher = javaToolchains.launcherFor { |
| 48 | + languageVersion = JavaLanguageVersion.of(23) |
| 49 | + vendor = JvmVendorSpec.GRAAL_VM |
| 50 | + } |
| 51 | + imageName = String.format('palantir-java-format-%s_%s', operatingSystem.uiName(), architecture.uiName()) |
| 52 | + mainClass = 'com.palantir.javaformat.java.Main' |
| 53 | + fallback = false |
| 54 | + sharedLibrary = false |
| 55 | + classpath.setFrom(configurations.named('runtimeClasspath')) |
| 56 | + configurationFileDirectories.from(file('src/main/resources/META-INF/native-image')) |
| 57 | + buildArgs.addAll("-H:ConfigurationFileDirectories=${projectDir}/src/main/resources/META-INF/native-image", |
| 58 | + '-H:+UnlockExperimentalVMOptions', |
| 59 | + '-H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler', |
| 60 | + '-H:IncludeResourceBundles=com.sun.tools.javac.resources.javac', |
| 61 | + '--no-fallback', |
| 62 | + '--initialize-at-build-time=com.sun.tools.javac.file.Locations', |
| 63 | + '-H:+ReportExceptionStackTraces', |
| 64 | + '-H:-UseContainerSupport', |
| 65 | + "-march=compatibility") |
| 66 | + jvmArgs.addAll(jvmArgList) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +// The image requires parameters to be passed to; eg. gw nativeRun -Pargs=--palantir,-i,<file> |
| 73 | +tasks.named("nativeRun", org.graalvm.buildtools.gradle.tasks.NativeRunTask) { |
| 74 | + runtimeArgs.addAll(project.hasProperty('appArgs') ? project.appArgs.split(',') : []) |
| 75 | +} |
| 76 | + |
| 77 | +if (Optional.ofNullable(System.getenv("CI")).isPresent()) { |
| 78 | + externalPublishing { |
| 79 | + publication('nativeImage') { |
| 80 | + fileTree("build/native/nativeCompile", { it.include("**/*") }).forEach { file -> |
| 81 | + artifact(file) { |
| 82 | + classifier = String.format("nativeImage-%s", getOsArchName(file)) |
| 83 | + extension = getExtension(file) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} else { |
| 89 | + logger.info("Configuring local publishing, skipping the external publishing setup because CI was not detected") |
| 90 | + publishing { |
| 91 | + publications { |
| 92 | + nativeImage(MavenPublication) { |
| 93 | + artifact(tasks.named('nativeCompile', BuildNativeImageTask.class).map { it.getOutputFile() }) { |
| 94 | + classifier = String.format("nativeImage-%s", getOsArchName(it.getFile())) |
| 95 | + extension = getExtension(it.getFile()) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +private static String getExtension(File file) { |
| 103 | + Pattern pattern = Pattern.compile("palantir-java-format-(.*)_(.*)\$"); |
| 104 | + Matcher matcher = pattern.matcher(file.name) |
| 105 | + if (matcher.find()) { |
| 106 | + return OperatingSystem.fromStringThrowing(matcher.group(1)) |
| 107 | + == OperatingSystem.WINDOWS ? "exe" : "bin" |
| 108 | + } |
| 109 | + throw new GradleException("Could not determine OS name from file name: ${file.name}") |
| 110 | +} |
| 111 | + |
| 112 | +private static String getOsArchName(File file) { |
| 113 | + Pattern pattern = Pattern.compile("palantir-java-format-(.*)_(.*)\$"); |
| 114 | + Matcher matcher = pattern.matcher(file.name) |
| 115 | + if (matcher.find()) { |
| 116 | + return matcher.group(1) + "_" + matcher.group(2) |
| 117 | + } |
| 118 | + throw new GradleException("Could not determine OS name from file name: ${file.name}") |
| 119 | +} |
| 120 | + |
| 121 | +configurations { |
| 122 | + nativeImage { |
| 123 | + canBeConsumed = true |
| 124 | + canBeResolved = false |
| 125 | + outgoing.artifact(tasks.named('nativeCompile').map(task -> task.getOutputFile())) |
| 126 | + attributes { |
| 127 | + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'nativeImage')) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments