Skip to content

Commit 9d4fb49

Browse files
melixfniephaus
andauthored
Disable toolchain detection by default (#420)
* Disable toolchain detection by default With the changes coming in GraalVM regarding versioning, in particular the fact that the GraalVM version becomes internal and that the vendor string is changing, toolchain detection will clearly be broken for all users. This commit therefore disables toolchain detection by default, and makes sure to document properly the new behavior. * Apply suggestions from code review Co-authored-by: Fabio Niephaus <code@fniephaus.com> --------- Co-authored-by: Fabio Niephaus <code@fniephaus.com>
1 parent 0ead262 commit 9d4fb49

File tree

7 files changed

+47
-56
lines changed

7 files changed

+47
-56
lines changed

docs/src/docs/asciidoc/gradle-plugin.adoc

+30-42
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,13 @@ include::../snippets/gradle/kotlin/settings.gradle.kts[tags=pre-release, indent=
9191

9292
=== Installing GraalVM Native Image tool
9393

94-
The plugin relies on Gradle's https://docs.gradle.org/7.1.1/userguide/toolchains.html[JVM toolchain support], allowing to decorrelate the tool used to run Gradle, the compiler used to build your application, and eventually the SDK used to generate a native image.
95-
96-
In practice, it means that this plugin will try to locate a suitable installation of GraalVM for you, even if you don't run Gradle itself with GraalVM.
97-
For this, it will look into conventional places on your machine, including from installations done by popular tools like https://sdkman.io/[SDKMAN!] or https://github.com/shyiko/jabba[Jabba].
98-
99-
WARNING: Even if you have a GraalVM SDK installed, Gradle will _not_ automatically detect if `native-image` is also installed.
100-
Therefore, you will need to make sure that you have executed `gu install native-image` as indicated in the <<graalvm-setup.adoc#,setup instructions>>.
101-
102-
If Gradle cannot find a GraalVM installation on the machine, it will fail with an error like this:
103-
104-
----
105-
> No compatible toolchains found for request filter: {languageVersion=11, vendor=matching('GraalVM'), implementation=vendor-specific} (auto-detect true, auto-download true)
106-
----
107-
108-
This happens because there's no automatic provisioning of the GraalVM toolchain available yet, so you will have to install it first.
109-
Follow the <<graalvm-setup.adoc#,following instructions>> to install it properly.
94+
By default, the plugin will try to use the `native-image` tool that is bundled with the JDK that is used to run Gradle.
95+
This means you must make sure that you run Gradle with a GraalVM JDK.
11096

11197
Alternatively, you may choose to:
11298

113-
1. <<configuration-toolchains-disabling, Disable toolchain support>>
114-
2. Run Gradle itself with a GraalVM SDK
115-
3. Set up a `GRAALVM_HOME` environment variable pointing to your GraalVM installation
116-
117-
Note that none of the above options is recommended as they are more fragile.
99+
1. Set up a `GRAALVM_HOME` environment variable pointing to your GraalVM installation, in which case the JDK pointed at this location will be used for Native Image builds instead
100+
2. <<configuration-toolchains-enabling, Enable toolchain support to automatically>>
118101

119102
[[configuration]]
120103
== Configuration
@@ -139,44 +122,49 @@ The main executable is configured by the image named `main`, while the test exec
139122
The link:javadocs/native-gradle-plugin/org/graalvm/buildtools/gradle/dsl/NativeImageOptions.html[NativeImageOptions] allows you to tweak how the native image is going to be built.
140123

141124
[[configuration-toolchains]]
142-
==== Selecting the GraalVM toolchain
125+
=== Using Gradle toolchains
143126

144-
By default, the plugin will select a Java 11 GraalVM toolchain.
145-
If you want to use a different toolchain, for example a GraalVM Community Edition for Java 8, you can configure the toolchain like this:
127+
[[configuration-toolchains-enabling]]
128+
==== Enabling toolchain detection
146129

147-
.Selecting the GraalVM toolchain
148-
[source, groovy, role="multi-language-sample"]
130+
Instead of relying on the JDK which is used to run Gradle, you can use the https://docs.gradle.org/current/userguide/toolchains.html[Gradle toolchain support] to select a specific GraalVM installation.
131+
132+
However, because of limitations in Gradle, the plugin may not be able to properly detect the toolchain.
133+
In particular, this will only work properly if you _only_ have GraalVM JDKs installed on the machine: **Otherwise, Gradle will not be able to reliably detect GraalVM JDKs**, nor detect GraalVM distributions from different vendors.
134+
135+
Should you still want to enable toolchain support, you do it via the `graalvmNative` extension:
136+
137+
.Enabling toolchain detection
138+
[source,groovy,role="multi-language-sample"]
149139
----
150-
include::../snippets/gradle/groovy/build.gradle[tags=select-toolchain]
140+
include::../snippets/gradle/groovy/build.gradle[tags=enabling-toolchain, indent=0]
151141
----
152142

153-
[source,kotlin,role="multi-language-sample"]
143+
[source, kotlin, role="multi-language-sample"]
154144
----
155-
include::../snippets/gradle/kotlin/build.gradle.kts[tags=select-toolchain]
145+
include::../snippets/gradle/kotlin/build.gradle.kts[tags=enabling-toolchain, indent=0]
156146
----
157147

158-
[[configuration-toolchains-disabling]]
159-
===== Disabling toolchain detection
148+
==== Selecting the GraalVM toolchain
160149

161-
Because of limitations in Gradle, the plugin may not be able to properly detect the toolchain.
162-
This is the case if, for example, you want to use GraalVM Enterprise or you want to be able to select a particular version of GraalVM.
150+
By default, the plugin will select a Java 11 GraalVM toolchain using the vendor string `GraalVM`,
151+
which works properly for GraalVM up to version 22.3 included.
152+
More recent versions of GraalVM do not have a specific version and are aligned with the language version they support.
163153

164-
To work around this problem, you can disable toolchain detection:
154+
If you want to use a different toolchain, for example a distribution compatible with Java 20 from Oracle, you can configure the toolchain like this:
165155

166-
.Disabling toolchain detection
167-
[source,groovy,role="multi-language-sample"]
156+
.Selecting the GraalVM toolchain
157+
[source, groovy, role="multi-language-sample"]
168158
----
169-
include::../snippets/gradle/groovy/build.gradle[tags=disabling-toolchain, indent=0]
159+
include::../snippets/gradle/groovy/build.gradle[tags=select-toolchain]
170160
----
171161

172-
[source, kotlin, role="multi-language-sample"]
162+
[source,kotlin,role="multi-language-sample"]
173163
----
174-
include::../snippets/gradle/kotlin/build.gradle.kts[tags=disabling-toolchain, indent=0]
164+
include::../snippets/gradle/kotlin/build.gradle.kts[tags=select-toolchain]
175165
----
176166

177-
If you do this, the plugin will search for 2 environment variables: `GRAALVM_HOME` and `JAVA_HOME` _in that order_.
178-
If one of them is set, it will assume that it points to a valid GraalVM installation and completely bypass toolchain selection.
179-
Therefore, it becomes your responsibility to make sure that the environment variable points to a JDK that is compatible with your build script requirements (in particular, the language version).
167+
Again, be aware that the toolchain detection _cannot_ distinguish between GraalVM JDKs and standard JDKs without Native Image support: if you have both installed on the machine, Gradle may randomly pick one or the other.
180168

181169
[[configuration-options]]
182170
==== Configuration options

docs/src/docs/asciidoc/index.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ If you are using alternative build systems, see <<alternative-build-systems.adoc
2626
- Bump minimal version of Gradle to 7.4
2727
- Fix compatibility with Gradle's https://docs.gradle.org/8.0.2/userguide/configuration_cache.html#header[configuration cache] (requires Gradle 7.5+)
2828
- Remove use of deprecated Gradle APIs
29+
- [Behavior change] Toolchain detection is now disabled by default
2930

3031
==== Maven plugin
3132

docs/src/docs/snippets/gradle/groovy/build.gradle

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ graalvmNative {
4949
binaries {
5050
main {
5151
javaLauncher = javaToolchains.launcherFor {
52-
languageVersion = JavaLanguageVersion.of(8)
53-
vendor = JvmVendorSpec.matching("GraalVM Community")
52+
languageVersion = JavaLanguageVersion.of(20)
53+
vendor = JvmVendorSpec.matching("Oracle Corporation")
5454
}
5555
}
5656
}
5757
}
5858
// end::select-toolchain[]
5959

60-
if (providers.environmentVariable("DISABLE_TOOLCHAIN").isPresent()) {
61-
// tag::disabling-toolchain[]
60+
if (providers.environmentVariable("ENABLE_TOOLCHAIN").isPresent()) {
61+
// tag::enabling-toolchain[]
6262
graalvmNative {
63-
toolchainDetection = false
63+
toolchainDetection = true
6464
}
65-
// end::disabling-toolchain[]
65+
// end::enabling-toolchain[]
6666
}
6767

6868
// tag::all-config-options[]

docs/src/docs/snippets/gradle/kotlin/build.gradle.kts

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ graalvmNative {
5050
binaries {
5151
named("main") {
5252
javaLauncher.set(javaToolchains.launcherFor {
53-
languageVersion.set(JavaLanguageVersion.of(8))
54-
vendor.set(JvmVendorSpec.matching("GraalVM Community"))
53+
languageVersion.set(JavaLanguageVersion.of(20))
54+
vendor.set(JvmVendorSpec.matching("Oracle Corporation"))
5555
})
5656
}
5757
}
5858
}
5959
// end::select-toolchain[]
6060

61-
if (providers.environmentVariable("DISABLE_TOOLCHAIN").isPresent()) {
62-
// tag::disabling-toolchain[]
61+
if (providers.environmentVariable("ENABLE_TOOLCHAIN").isPresent()) {
62+
// tag::enabling-toolchain[]
6363
graalvmNative {
64-
toolchainDetection.set(false)
64+
toolchainDetection.set(true)
6565
}
66-
// end::disabling-toolchain[]
66+
// end::enabling-toolchain[]
6767
}
6868

6969
// tag::all-config-options[]

native-gradle-plugin/src/functionalTest/groovy/org/graalvm/buildtools/gradle/NativeImageOptionsTest.groovy

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class NativeImageOptionsTest extends Specification {
2929
id 'org.graalvm.buildtools.native'
3030
}
3131
32+
graalvmNative.toolchainDetection = true
33+
3234
assert graalvmNative.binaries.main.javaLauncher
3335
.get()
3436
.metadata

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/dsl/GraalVMExtension.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public interface GraalVMExtension {
100100

101101
/**
102102
* Property driving the detection of toolchains which support building native images.
103-
* The default is true.
103+
* The default is false.
104104
*
105105
* @return is toolchain detection on
106106
*/

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/DefaultGraalVmExtension.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public DefaultGraalVmExtension(NamedDomainObjectContainer<NativeImageOptions> na
7272
this.plugin = plugin;
7373
this.project = project;
7474
this.defaultJavaLauncher = project.getObjects().property(JavaLauncher.class);
75-
getToolchainDetection().convention(true);
75+
getToolchainDetection().convention(false);
7676
nativeImages.configureEach(options -> options.getJavaLauncher().convention(defaultJavaLauncher));
7777
getTestSupport().convention(true);
7878
AgentOptions agentOpts = getAgent();

0 commit comments

Comments
 (0)