Skip to content

Commit

Permalink
Remove native image and rely on the JVM at runtime
Browse files Browse the repository at this point in the history
The complexity of setting a build pipeline for GraalVM native image
and having access to both macOs/linux for both x64 and ARM architecture
is too much of an overhead for a small opensource project.

This might be reconsidered in the future when availability on those
OS/arch will be more widely spread or that GraalVM will offer
cross compilation support.

See oracle/graal#407
  • Loading branch information
loicrouchon committed Nov 17, 2021
1 parent 2cd2eb5 commit b24066d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 128 deletions.
159 changes: 55 additions & 104 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

plugins {
application
`jvm-test-suite`
idea
jacoco
id("org.graalvm.buildtools.native") version "0.9.7.1"
id("org.asciidoctor.jvm.convert") version "3.3.2"
id("nebula.ospackage") version "9.0.0"
}
Expand Down Expand Up @@ -99,54 +96,6 @@ tasks.jacocoTestReport {
}
}

graalvmNative {
binaries {
named("main") {
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(17))
vendor.set(JvmVendorSpec.GRAAL_VM)
})
// mainClass.set(null)
// classpath.from()
buildArgs.addAll(
"-Dpicocli.converters.excludes=java.sql.*,java.time.*",
// "--module-path",
// "${buildDir}/install/${project.name}/lib/",
// "--module",
// "${appModuleName}/${appMainClassName}",
// Size optimization
"-H:-AddAllCharsets",
"-H:-UseServiceLoaderFeature",
"-J-Drx.unsafe-disable=true",
"-H:+RemoveUnusedSymbols",
"-DremoveUnusedAutoconfig=true",
"--initialize-at-build-time=${appModuleName},picocli",
// Native image size debug flags
// "-H:DashboardDump=img-dump",
// "-H:+DashboardAll",
// "-H:+DashboardHeap",
// "-H:+DashboardCode",
// "-H:+DashboardPointsTo",
// "-H:+DashboardPretty",
// Native image stdout debug
// "-H:+PrintImageElementSizes",
// "-H:+PrintUniverse",
// "-H:+PrintHeapHistogram",
// "-H:+PrintAnalysisCallTree",
// "-H:+PrintImageObjectTree",
// "-H:+PrintHeapHistogram",
// "-H:+PrintMethodHistogram",
// "-H:+PrintImageHeapPartitionSizes",
)
}
}
}

tasks.nativeCompile {
shouldRunAfter(tasks.named("integrationTest"))
}
tasks.assemble.get().dependsOn(tasks.nativeCompile)

tasks.register<JavaExec>("generateManpageAsciiDoc") {
dependsOn(tasks.installDist)
inputs.dir("${buildDir}/install/${project.name}/")
Expand Down Expand Up @@ -203,61 +152,63 @@ tasks.register<JavaExec>("generateShellCompletions") {
}
}


val os: OperatingSystem = DefaultNativePlatform.getCurrentOperatingSystem()
if (os.isLinux()) {
ospackage {
packageName = "symly"
packageDescription = "Manages symbolic links."
url = "https://github.com/loicrouchon/symly"

release = "1"
os = org.redline_rpm.header.Os.LINUX
user = "root"

license = "ASL 2.0"
from("LICENSE", closureOf<CopySpec> {
into("usr/share/doc/${project.name}")
rename("LICENSE", "copyright")
fileType = org.redline_rpm.payload.Directive.LICENSE
})

from("${buildDir}/native/nativeCompile/${project.name}", closureOf<CopySpec> {
into("usr/bin/")
fileMode = 755
})
from("${buildDir}/docs/manpage/gz", closureOf<CopySpec> {
into("usr/man/man1")
fileType = org.redline_rpm.payload.Directive.DOC
})
// requires https://github.com/remkop/picocli/issues/1346
ospackage {
packageName = "symly"
packageDescription = "Manages symbolic links."
url = "https://github.com/loicrouchon/symly"

release = "1"
os = org.redline_rpm.header.Os.LINUX
user = "root"

license = "ASL 2.0"
from("LICENSE", closureOf<CopySpec> {
into("usr/share/doc/${project.name}")
rename("LICENSE", "copyright")
fileType = org.redline_rpm.payload.Directive.LICENSE
})
from("src/main/packaging/linux/${project.name}", closureOf<CopySpec> {
into("usr/bin")
})
from("${buildDir}/install/${project.name}/bin/symly", closureOf<CopySpec> {
into("usr/share/${project.name}/bin")
})
from("${buildDir}/install/${project.name}/lib", closureOf<CopySpec> {
into("usr/share/${project.name}/lib")
})
from("${buildDir}/docs/manpage/gz", closureOf<CopySpec> {
into("usr/man/man1")
fileType = org.redline_rpm.payload.Directive.DOC
})
// requires https ://github.com/remkop/picocli/issues/1346
// from("${buildDir}/shell/completions") {
// into "/usr/share/bash-completion/completions"
// }
}
}

val buildDebPackage = tasks.register<com.netflix.gradle.plugins.deb.Deb>("buildDebPackage") {
dependsOn(
tasks.named("nativeCompile"),
tasks.named("generateManpage"),
tasks.named("generateShellCompletions")
)
shouldRunAfter(tasks.named("integrationTest"))
maintainer = "Loic Rouchon"
setArch("amd64")
license = "ASL 2.0"
}
val buildDebPackage = tasks.register<com.netflix.gradle.plugins.deb.Deb>("buildDebPackage") {
dependsOn(
tasks.named("installDist"),
tasks.named("generateManpage"),
tasks.named("generateShellCompletions")
)
shouldRunAfter(tasks.named("integrationTest"))
maintainer = "Loic Rouchon"
setArch("amd64")
license = "ASL 2.0"
requires("openjdk-17-jre-headless", "").or("java17-runtime-headless", null)
}

val buildRpmPackage = tasks.register<com.netflix.gradle.plugins.rpm.Rpm>("buildRpmPackage") {
dependsOn(
tasks.named("nativeCompile"),
tasks.named("generateManpage"),
tasks.named("generateShellCompletions")
)
shouldRunAfter(tasks.named("integrationTest"))
packager = "Loïc Rouchon"
setArch("x86_64")
addParentDirs = false
}
tasks.assemble.get().dependsOn(buildDebPackage, buildRpmPackage)
}
val buildRpmPackage = tasks.register<com.netflix.gradle.plugins.rpm.Rpm>("buildRpmPackage") {
dependsOn(
tasks.named("installDist"),
tasks.named("generateManpage"),
tasks.named("generateShellCompletions")
)
shouldRunAfter(tasks.named("integrationTest"))
packager = "Loïc Rouchon"
setArch("x86_64")
requires("java-latest-openjdk-headless", "")
addParentDirs = false
}
tasks.assemble.get().dependsOn(buildDebPackage, buildRpmPackage)
4 changes: 0 additions & 4 deletions src/main/java/org/symly/cli/BeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class BeanFactory implements CommandLine.IFactory {

private final CommandLine.IFactory parentFactory = CommandLine.defaultFactory();

public void preInit() {
constructors.keySet().forEach(this::create);
}

@Override
@SuppressWarnings("unchecked")
public <K> K create(Class<K> cls) {
Expand Down
25 changes: 5 additions & 20 deletions src/main/java/org/symly/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,20 @@

public class Main {

private static final BeanFactory BEAN_FACTORY = initializeBeanFactory();

private static final CliConsole CONSOLE = BEAN_FACTORY.create(CliConsole.class);

/**
* Done as part of the Main class static initializer to benefit from GraalVM native-image static initialization
* optimization.
*/
private static final CommandLine COMMAND_LINE = initializeCommandLine(BEAN_FACTORY, CONSOLE);

public static void main(String... args) {
int exitCode = runCommand(args);
System.exit(exitCode);
}

private static int runCommand(String... args) {
int exitCode = COMMAND_LINE.execute(args);
CONSOLE.flush();
BeanFactory beanFactory = new BeanFactory();
CliConsole console = beanFactory.create(CliConsole.class);
CommandLine commandLine = initializeCommandLine(beanFactory, console);
int exitCode = commandLine.execute(args);
console.flush();
return exitCode;
}

private static BeanFactory initializeBeanFactory() {
BeanFactory factory = new BeanFactory();
// Instantiate at compile time thanks to GraalVM native-image optimization
// Doing so also avoids having to include resources in the native-image.
factory.preInit();
return factory;
}

private static CommandLine initializeCommandLine(BeanFactory factory, CliConsole console) {
CommandLine commandLine = new CommandLine(factory.create(MainCommand.class), factory);
commandLine.setOut(console.writer());
Expand Down
3 changes: 3 additions & 0 deletions src/main/packaging/homebrew/symly
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
export JAVA_HOME="/usr/local/opt/openjdk"
/usr/local/Cellar/symly/bin/symly "$@"
3 changes: 3 additions & 0 deletions src/main/packaging/linux/symly
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
export JAVA_HOME="/usr/lib/jvm/$(ls /usr/lib/jvm | grep -E "java-[0-9]+" | sort -V -r | head -n 1)"
/usr/share/symly/bin/symly "$@"

0 comments on commit b24066d

Please sign in to comment.