Skip to content

Commit 6e6312d

Browse files
authored
feat: added support for specifying native options (quarkusio#1494)
Added both `//NATIVE_OPTIONS` and `--native-option`. Also enabled `//COMPILE_OPTIONS` for both Kotlin and Groovy sources. Updated docs to prefer the newer tag and option names. Fixes quarkusio#1493
1 parent 876a540 commit 6e6312d

26 files changed

+181
-65
lines changed

docs/modules/ROOT/pages/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ toc::[]
6161
* Installation of scripts to user `PATH`
6262
* Include multiple files and sources
6363
* Dependency declarations using `//DEPS <gav>` for automatic dependency resolution
64-
* Control compile and runtime options with `//JAVAC_OPTIONS <flags>` and `//JAVA_OPTIONS <flags>`
64+
* Control compile and runtime options with `//COMPILE_OPTIONS <flags>`, `//NATIVE_OPTIONS <flags>` and `//RUNTIME_OPTIONS <flags>`
6565
* Compiled jar and Dependency resolution caching
6666
* native-image generation (`--native`)
6767
* Launch with debug enabled for instant debugging from your favorite IDE

docs/modules/ROOT/pages/running.adoc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ to your script/class to see it.
2626

2727
Flight recorder is a feature of the Java VM that lets you gather diagnostic and profiling data about your script.
2828

29-
You can use `//JAVA_OPTIONS` to have full control over it; but for the easiest setup `jbang` lets you just run with `--jfr`, e.g.,
29+
You can use `//RUNTIME_OPTIONS` to have full control over it; but for the easiest setup `jbang` lets you just run with `--jfr`, e.g.,
3030

3131
jbang --jfr myapp.java
3232

@@ -37,18 +37,23 @@ Then you can use tools like `jvisualvm` or `jmc` to explore the data.
3737
If you want to tweak the configuration you can pass flight recorder options, like `jbang --jfr=filename=\{basename}.jfr,maxage=24h` where `\{basename}` will be replaced
3838
by the filename and then added `maxage=24h` to flight recording options.
3939

40-
If you want further control use `//JAVAC_OPTIONS -XX:StartFlightRecording=<your options>` instead.
40+
If you want further control use `//COMPILE_OPTIONS -XX:StartFlightRecording=<your options>` instead.
4141

42-
== `java` and `javac` Options
42+
== `java`, `javac` and `nativeimage` Options
4343

44-
If you want to tweak memory settings or enable preview features you can setup the necessary options using
45-
`//JAVA_OPTIONS` and `//JAVAC_OPTIONS` as in the following example using Java 14 experimental `record` feature:
44+
You can pass options directly from JBang to the compiler and runtime tools that it uses to build and run your code.
45+
46+
The following source tags are available for setting JVM (`java`) options, compiler options (`javac`, `kotlinc`, etc) and native image builders (`nativeimage`) respectively: `//RUNTIME_OPTIONS`, `//COMPILE_OPTIONS` and `//NATIVE_OPTIONS`.
47+
48+
You can also set the same options from the command line using: `--runtime-option`, `--compile-option` and `--native-option`.
49+
50+
As an example, if you want to enable preview features you can set up the necessary options as in the following example that uses Java 14 experimental `record` feature:
4651

4752
[source, java]
4853
----
4954
///usr/bin/env jbang "$0" "$@" ; exit $?
50-
//JAVAC_OPTIONS --enable-preview -source 14 <.>
51-
//JAVA_OPTIONS --enable-preview // <.>
55+
//COMPILE_OPTIONS --enable-preview -source 14
56+
//RUNTIME_OPTIONS --enable-preview
5257
5358
import static java.lang.System.*;
5459

itests/gh_fetch_release_assets.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///usr/bin/env jbang "$0" "$@" ; exit $?
2-
///JAVA_OPTIONS --add-opens java.base/java.net=ALL-UNNAMED
3-
///JAVA_OPTIONS --add-opens java.base/sun.net.www.protocol.https=ALL-UNNAMED
2+
///RUNTIME_OPTIONS --add-opens java.base/java.net=ALL-UNNAMED
3+
///RUNTIME_OPTIONS --add-opens java.base/sun.net.www.protocol.https=ALL-UNNAMED
44
//DEPS info.picocli:picocli:4.6.3
55
//DEPS org.kohsuke:github-api:1.116
66

itests/helloworld.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///usr/bin/env jbang "$0" "$@" ; exit $?
22
// //DEPS <dependency1> <dependency2>
3-
//JAVA_OPTIONS -Dfoo=bar "-Dbar=aap noot mies"
3+
//RUNTIME_OPTIONS -Dfoo=bar "-Dbar=aap noot mies"
44
//MANIFEST foo bar=baz baz=${bazprop:nada}
55

66
import static java.lang.System.*;

itests/with space/helloworld.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///usr/bin/env jbang "$0" "$@" ; exit $?
22
// //DEPS <dependency1> <dependency2>
3-
//JAVA_OPTIONS -Dfoo=bar "-Dbar=aap noot mies"
3+
//RUNTIME_OPTIONS -Dfoo=bar "-Dbar=aap noot mies"
44
//MANIFEST foo bar=baz baz=${bazprop:nada}
55

66
import static java.lang.System.*;

src/main/java/dev/jbang/catalog/Alias.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ public class Alias extends CatalogItem {
3333
public final String mainClass;
3434
@SerializedName(value = "compile-options")
3535
public final List<String> compileOptions;
36+
@SerializedName(value = "native-options")
37+
public final List<String> nativeOptions;
3638

3739
private Alias() {
38-
this(null, null, null, null, null, null, null, null, null, null, null, null, null, null);
40+
this(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
3941
}
4042

4143
public Alias(String scriptRef,
@@ -51,6 +53,7 @@ public Alias(String scriptRef,
5153
String javaVersion,
5254
String mainClass,
5355
List<String> compileOptions,
56+
List<String> nativeOptions,
5457
Catalog catalog) {
5558
super(catalog);
5659
this.scriptRef = scriptRef;
@@ -66,6 +69,7 @@ public Alias(String scriptRef,
6669
this.javaVersion = javaVersion;
6770
this.mainClass = mainClass;
6871
this.compileOptions = compileOptions;
72+
this.nativeOptions = nativeOptions;
6973
}
7074

7175
/**
@@ -145,9 +149,11 @@ private static Alias merge(Alias a1, String name, Function<String, Alias> findUn
145149
String mainClass = a1.mainClass != null ? a1.mainClass : a2.mainClass;
146150
List<String> copts = a1.compileOptions != null && !a1.compileOptions.isEmpty() ? a1.compileOptions
147151
: a2.compileOptions;
152+
List<String> nopts = a1.nativeOptions != null && !a1.nativeOptions.isEmpty() ? a1.nativeOptions
153+
: a2.nativeOptions;
148154
Catalog catalog = a2.catalog != null ? a2.catalog : a1.catalog;
149155
return new Alias(a2.scriptRef, desc, args, jopts, srcs, ress, deps, repos, cpaths, props, javaVersion,
150-
mainClass, copts, catalog);
156+
mainClass, copts, nopts, catalog);
151157
} else {
152158
return a1;
153159
}

src/main/java/dev/jbang/catalog/Catalog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Catalog(String baseRef, String description, ResourceRef catalogRef, Map<S
5858
aliases.forEach((key, a) -> this.aliases.put(key,
5959
new Alias(a.scriptRef, a.description, a.arguments, a.runtimeOptions, a.sources, a.resources,
6060
a.dependencies, a.repositories, a.classpaths, a.properties, a.javaVersion, a.mainClass,
61-
a.compileOptions, this)));
61+
a.compileOptions, a.nativeOptions, this)));
6262
templates.forEach((key, t) -> this.templates.put(key,
6363
new Template(t.fileRefs, t.description, t.properties, this)));
6464
}

src/main/java/dev/jbang/catalog/CatalogUtil.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ public static Path addNearestAlias(String name,
4343
Map<String, String> properties,
4444
String javaVersion,
4545
String mainClass,
46-
List<String> compileOptions) {
46+
List<String> compileOptions,
47+
List<String> nativeOptions) {
4748
Path catalogFile = Catalog.getCatalogFile(null);
4849
addAlias(catalogFile, name, scriptRef, description, arguments, javaRuntimeOptions, sources, resources,
49-
dependencies, repositories, classPaths, properties, javaVersion, mainClass, compileOptions);
50+
dependencies, repositories, classPaths, properties, javaVersion, mainClass, compileOptions,
51+
nativeOptions);
5052
return catalogFile;
5153
}
5254

@@ -70,13 +72,15 @@ public static Alias addAlias(Path catalogFile,
7072
Map<String, String> properties,
7173
String javaVersion,
7274
String mainClass,
73-
List<String> compileOptions) {
75+
List<String> compileOptions,
76+
List<String> nativeOptions) {
7477
Path cwd = Util.getCwd();
7578
catalogFile = cwd.resolve(catalogFile);
7679
Catalog catalog = Catalog.get(catalogFile);
7780
scriptRef = catalog.relativize(scriptRef);
7881
Alias alias = new Alias(scriptRef, description, arguments, javaRuntimeOptions, sources, resources,
79-
dependencies, repositories, classPaths, properties, javaVersion, mainClass, compileOptions, catalog);
82+
dependencies, repositories, classPaths, properties, javaVersion, mainClass, compileOptions,
83+
nativeOptions, catalog);
8084
catalog.aliases.put(name, alias);
8185
try {
8286
catalog.write();

src/main/java/dev/jbang/cli/Alias.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ public Integer doCall() {
106106
scriptMixin.sources, scriptMixin.resources, dependencyInfoMixin.getDependencies(),
107107
dependencyInfoMixin.getRepositories(), dependencyInfoMixin.getClasspaths(),
108108
dependencyInfoMixin.getProperties(), buildMixin.javaVersion, buildMixin.main,
109-
buildMixin.compileOptions);
109+
buildMixin.compileOptions, buildMixin.nativeOptions);
110110
} else {
111111
catFile = CatalogUtil.addNearestAlias(name, scriptMixin.scriptOrFile, desc, userParams, javaRuntimeOptions,
112112
scriptMixin.sources, scriptMixin.resources, dependencyInfoMixin.getDependencies(),
113113
dependencyInfoMixin.getRepositories(), dependencyInfoMixin.getClasspaths(),
114114
dependencyInfoMixin.getProperties(), buildMixin.javaVersion, buildMixin.main,
115-
buildMixin.compileOptions);
115+
buildMixin.compileOptions, buildMixin.nativeOptions);
116116
}
117117
info(String.format("Alias '%s' added to '%s'", name, catFile));
118118
return EXIT_OK;
@@ -131,7 +131,7 @@ ProjectBuilder createProjectBuilder() {
131131
.javaVersion(buildMixin.javaVersion)
132132
.mainClass(buildMixin.main)
133133
.setArguments(userParams)
134-
.javaOptions(javaRuntimeOptions);
134+
.runtimeOptions(javaRuntimeOptions);
135135
Path cat = getCatalog(false);
136136
if (cat != null) {
137137
pb.catalog(cat.toFile());

src/main/java/dev/jbang/cli/BuildMixin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ void setJavaVersion(String javaVersion) {
2323

2424
@CommandLine.Option(names = { "-C", "--compile-option" }, description = "Options to pass to the compiler")
2525
public List<String> compileOptions;
26+
27+
@CommandLine.Option(names = { "-N", "--native-option" }, description = "Options to pass to the native image tool")
28+
public List<String> nativeOptions;
2629
}

0 commit comments

Comments
 (0)