Skip to content

Add native image builds #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
saturnism opened this issue Mar 28, 2019 · 6 comments
Closed

Add native image builds #358

saturnism opened this issue Mar 28, 2019 · 6 comments

Comments

@saturnism
Copy link

It would be awesome to produce a native binary build using graalvm's native-image as part of the release.

native-image seems to work well w/ the current all-deps jar.

native-image -jar google-java-format-1.7-all-deps.jar
@natros
Copy link

natros commented Mar 28, 2019

wow! realy fast formting.

in my case I had to pass -H:IncludeResourceBundles=org.openjdk.tools.javac.resources.compiler

@natros
Copy link

natros commented Oct 30, 2019

graalvm 19.2.1

#!/usr/bin/env bash

die() {
  echo "${@}" 1>&2
  exit 1
}

version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)

jar="core/target/google-java-format-${version}-all-deps.jar"

if [[ ! -f "${jar}" ]]; then
  echo "building ${jar}..."
  mvn clean verify -DskipTests -pl core -am || die "cannot build ${jar}"
fi

echo "building native image from ${jar}..."
native-image \
    -H:+ReportExceptionStackTraces \
    -H:+ReportUnsupportedElementsAtRuntime \
    -H:+TraceClassInitialization \
    -H:IncludeResourceBundles=org.openjdk.tools.javac.resources.compiler \
    -H:Class=com.google.googlejavaformat.java.Main  \
    -H:Name="google-java-format" \
    -cp "${jar}" \
    --initialize-at-build-time\
    --no-fallback \
    --no-server
echo "done"

@joprice
Copy link

joprice commented May 29, 2020

This is great. Any reason it couldn't be the main distribution format?

@perezd
Copy link
Contributor

perezd commented Sep 27, 2020

Trying this with Graal CE Java11/20.2.0 + GJF 1.9 I get the following error:

java.lang.IllegalStateException: Unknown Java version: 11
	at com.google.googlejavaformat.java.Formatter.getMajor(Formatter.java:193)
	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:157)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:291)
	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.lang.Thread.run(Thread.java:834)
	at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:517)
	at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192)

This was the command I used:

native-image \
-H:+ReportExceptionStackTraces \
-H:+ReportUnsupportedElementsAtRuntime \
-H:+TraceClassInitialization \
-H:Class=com.google.googlejavaformat.java.Main \
-H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler \
-H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
-H:Name="google-java-format" \
-jar google-java-format-1.9-all-deps.jar \
--initialize-at-build-time \
--no-fallback \
--no-server 

@natros
Copy link

natros commented Sep 28, 2020

This is what I did (I'm using GraalVM Enterprise Edition 11-20.2.0 on macOS, should work with Comunity Edition)

java --version
java 11.0.8.0.2 2020-07-09 LTS
Java(TM) SE Runtime Environment GraalVM EE 20.2.0 (build 11.0.8.0.2+1-LTS-jvmci-20.2-b03)
Java HotSpot(TM) 64-Bit Server VM GraalVM EE 20.2.0 (build 11.0.8.0.2+1-LTS-jvmci-20.2-b03, mixed mode, sharing)

First, I build the google-java-format-1.10-SNAPSHOT-all-deps.jar with java provided by graalvm.

mvn clean verify -DskipTests -Dmaven.javadoc.skip=true -pl core -am 

Then I selected a few projects and created a file all.txt with all java files:

echo -i > all.txt
find ${HOME}/Projects/jetty.project -type f -name \*.java >> all.txt

and did the same with few more projects.

Then inside google-java-format I did

mkdir -p core/src/main/resources/META-INF/native-image
java -agentlib:native-image-agent=config-output-dir=core/src/main/resources/META-INF/native-image -jar core/target/google-java-format-1.10-SNAPSHOT-all-deps.jar @all.txt

This will create the json files with information for graalvm

And finally

native-image \
    -H:+ReportExceptionStackTraces \
    -H:+ReportUnsupportedElementsAtRuntime \
    -H:+TraceClassInitialization \
    -H:ConfigurationFileDirectories=/Users/fsousa/Projects/code/google-java-format/core/src/main/resources/META-INF/native-image \
    -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
    -H:Class=com.google.googlejavaformat.java.Main  \
    -H:Name="google-java-format" \
    -Dgraal.EnterprisePartialUnroll=true \
    -cp "${jar}" \
    --initialize-at-build-time\
    --no-fallback \
    --no-server

Note: remove -Dgraal.EnterprisePartialUnroll=true for comunity edition

$ time google-java-format --version
google-java-format: Version 1.10-SNAPSHOT

real	0m0.006s
user	0m0.002s
sys	0m0.002s

I have a script that resumes what I did.

#!/usr/bin/env bash
#
# echo -i > all.txt
# find /Users/fsousa/Projects/ipb/gdoc2 -type f -name \*.java >> all.txt
# find /Users/fsousa/Projects/ipb/candidaturas -type f -name \*.java >> all.txt
# find /Users/fsousa/Projects/egoi/te-proxy -type f -name \*.java >> all.txt
# find /Users/fsousa/Projects/code/jetty.project -type f -name \*.java >> all.txt
# mkdir -p core/src/main/resources/META-INF/native-image
# java -agentlib:native-image-agent=config-output-dir=core/src/main/resources/META-INF/native-image -jar core/target/google-java-format-1.10-SNAPSHOT-all-deps.jar @all.txt

die() {
  echo "${@}" 1>&2
  exit 1
}

version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)

jar="core/target/google-java-format-${version}-all-deps.jar"

if [[ ! -f "${jar}" ]]; then
  echo "building ${jar}..."
  mvn clean verify -DskipTests -Dmaven.javadoc.skip=true -pl core -am || die "cannot build ${jar}"
fi


#-H:ConfigurationResourceRoots=META-INF/native-image \

echo "building native image from ${jar}..."
native-image \
    -H:+ReportExceptionStackTraces \
    -H:+ReportUnsupportedElementsAtRuntime \
    -H:+TraceClassInitialization \
    -H:ConfigurationFileDirectories=/Users/fsousa/Projects/code/google-java-format/core/src/main/resources/META-INF/native-image \
    -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
    -H:Class=com.google.googlejavaformat.java.Main  \
    -H:Name="google-java-format" \
    -Dgraal.EnterprisePartialUnroll=true \
    -cp "${jar}" \
    --initialize-at-build-time\
    --no-fallback \
    --no-server
echo "done"

for the command line, I have 2 scripts:

java-format

#!/usr/bin/env bash

TMPFILE=$(mktemp)
echo "-i" > ${TMPFILE}
find . -type f -name \*.java >> ${TMPFILE}
google-java-format @${TMPFILE}

and java-format-changes

#!/usr/bin/env bash

files=$(git diff --name-only|grep \.java$)
[[ -z ${files} ]] || google-java-format -i ${files}

I'm using File Watchers plugin for IntelliJ, which formats the current file when the file is saved or format all unsaved files when compiling. Anyway, formating a file is faster than formating with the google plugin, especially when dealing with big files.

image

@perezd
Copy link
Contributor

perezd commented Oct 6, 2020

So I followed your and it compiled but I get this error:

java.lang.IllegalStateException: Unknown Java version: 11
	at com.google.googlejavaformat.java.Formatter.getMajor(Formatter.java:193)
	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:157)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:291)
	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.lang.Thread.run(Thread.java:834)
	at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:517)
	at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192)

This is with the released jar google-java-format-1.9-all-deps.jar Also tried verbatim your steps w/ a copy of master repo same error. I am building w/ Java 11 if that matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants