Skip to content

Commit 0fc1095

Browse files
committed
Make -H:Backend= a real option
Originally, it was parsed out manually because the svm.platform property was read before parsing option. This no longer seems the case and we can use the NativeImageClassLoaderPostProcessing SPI to set the property after the hosted options are parsed and before the property is read to create the platform.
1 parent de720e2 commit 0fc1095

File tree

5 files changed

+83
-56
lines changed

5 files changed

+83
-56
lines changed

web-image/mx.web-image/mx_web_image.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def get_or_compute_lines(self) -> List[str]:
647647
lines: List[str] = [
648648
"# This file is auto-generated",
649649
"ExcludeFromAll = true",
650-
"ProvidedHostedOptions = " + " ".join(self.subject.provided_hosted_options + ["Backend="]),
650+
"ProvidedHostedOptions = " + " ".join(self.subject.provided_hosted_options),
651651
]
652652

653653
if builder_jars:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.oracle.svm.hosted.webimage.WebImagePlatformInjector

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/NativeImageWasmGeneratorRunner.java

+11-55
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@
2626

2727
import java.lang.reflect.Method;
2828
import java.util.ArrayList;
29-
import java.util.Arrays;
3029
import java.util.Comparator;
31-
import java.util.Iterator;
3230
import java.util.List;
33-
import java.util.Locale;
3431

3532
import org.graalvm.collections.EconomicMap;
3633
import org.graalvm.collections.Pair;
37-
import org.graalvm.nativeimage.Platform;
3834
import org.graalvm.nativeimage.c.type.CIntPointer;
3935
import org.graalvm.nativeimage.c.type.CShortPointer;
4036

@@ -74,60 +70,11 @@
7470
*/
7571
public class NativeImageWasmGeneratorRunner extends NativeImageGeneratorRunner {
7672

77-
public static final String BACKEND_ARG = "-H:Backend(@[^=]*)?=.*";
78-
7973
/**
8074
* @param args Hosted and runtime options
8175
*/
8276
public static void main(String[] args) {
83-
List<String> arguments = Arrays.asList(args);
84-
arguments = extractDriverArguments(arguments);
85-
86-
CompilerBackend backend;
87-
if (WebImageOptions.isNativeImageBackend()) {
88-
// Under native-image, selection of the Web Image backend is not allowed
89-
backend = CompilerBackend.WASMGC;
90-
} else {
91-
backend = extractBackend(arguments);
92-
}
93-
94-
// installNativeImageClassLoader uses this property to create the platform instance.
95-
System.setProperty(Platform.PLATFORM_PROPERTY_NAME, backend.platform.getName());
96-
new NativeImageWasmGeneratorRunner().start(arguments.toArray(String[]::new));
97-
}
98-
99-
/**
100-
* Extracts the {@link NativeImageWasmGeneratorRunner#BACKEND_ARG} argument as a
101-
* {@link CompilerBackend} from the list of arguments.
102-
*
103-
* @param arguments The list of arguments. The backend argument will be removed from the list if
104-
* specified.
105-
* @return The appropriate compiler backend for the given arguments or
106-
* {@link CompilerBackend#JS} if none was specified
107-
*/
108-
public static CompilerBackend extractBackend(List<String> arguments) {
109-
Iterator<String> it = arguments.iterator();
110-
111-
while (it.hasNext()) {
112-
String param = it.next();
113-
114-
if (param.matches(BACKEND_ARG)) {
115-
it.remove();
116-
String[] parts = param.split("=", 2);
117-
if (parts.length != 2) {
118-
throw new IllegalArgumentException(BACKEND_ARG + " needs an argument");
119-
}
120-
String backendName = parts[1].toUpperCase(Locale.ROOT);
121-
try {
122-
return CompilerBackend.valueOf(backendName);
123-
} catch (IllegalArgumentException e) {
124-
throw new IllegalArgumentException("No backend with name " + backendName + " exists", e);
125-
}
126-
}
127-
}
128-
129-
// Use JS backend by default
130-
return CompilerBackend.JS;
77+
new NativeImageWasmGeneratorRunner().start(args);
13178
}
13279

13380
/**
@@ -142,7 +89,16 @@ private static void dumpProvidedHostedOptions(HostedOptionParser optionParser) {
14289
List<String> names = new ArrayList<>();
14390

14491
for (OptionDescriptor value : allHostedOptions.getValues()) {
145-
if (!value.getDeclaringClass().getPackageName().contains("webimage") || WebImageOptions.DebugOptions.DumpProvidedHostedOptionsAndExit == value.getOptionKey()) {
92+
if (!value.getDeclaringClass().getPackageName().contains("webimage")) {
93+
continue;
94+
}
95+
96+
if (WebImageOptions.DebugOptions.DumpProvidedHostedOptionsAndExit == value.getOptionKey()) {
97+
continue;
98+
}
99+
100+
// Do not print the Backend option, it's not available in the svm-wasm macro
101+
if (WebImageOptions.Backend == value.getOptionKey()) {
146102
continue;
147103
}
148104

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.hosted.webimage;
27+
28+
import static com.oracle.svm.hosted.webimage.options.WebImageOptions.CompilerBackend;
29+
import static com.oracle.svm.hosted.webimage.options.WebImageOptions.isNativeImageBackend;
30+
31+
import org.graalvm.nativeimage.Platform;
32+
33+
import com.oracle.svm.hosted.NativeImageClassLoaderPostProcessing;
34+
import com.oracle.svm.hosted.NativeImageClassLoaderSupport;
35+
import com.oracle.svm.hosted.webimage.options.WebImageOptions;
36+
37+
/**
38+
* Sets the {@link Platform#PLATFORM_PROPERTY_NAME} property based on which code-generation backend
39+
* is selected.
40+
* <p>
41+
* In the {@code native-image} launcher this is always the WasmGC backend. Under {@code web-image},
42+
* it is determined by the {@link WebImageOptions#Backend} option.
43+
*/
44+
public class WebImagePlatformInjector implements NativeImageClassLoaderPostProcessing {
45+
@Override
46+
public void apply(NativeImageClassLoaderSupport support) {
47+
CompilerBackend backend;
48+
if (isNativeImageBackend()) {
49+
// Under native-image, selection of the Web Image backend is not allowed
50+
backend = CompilerBackend.WASMGC;
51+
} else {
52+
backend = WebImageOptions.Backend.getValue(support.getParsedHostedOptions());
53+
}
54+
55+
/*
56+
* installNativeImageClassLoader uses this property at the end to create the platform
57+
* instance. This method is called before that.
58+
*/
59+
System.setProperty(Platform.PLATFORM_PROPERTY_NAME, backend.platform.getName());
60+
}
61+
}

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/options/WebImageOptions.java

+9
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ public static final class DebugOptions {
135135
public static final HostedOptionKey<Boolean> DumpProvidedHostedOptionsAndExit = new HostedOptionKey<>(false);
136136
}
137137

138+
/**
139+
* Web Image only.
140+
* <p>
141+
* Do not read this value directly. Instead, look it up based on the selected {@link Platform}
142+
* using {@link #getBackend}.
143+
*/
144+
@Option(help = "The Web Image Backend to use.") //
145+
public static final EnumOptionKey<CompilerBackend> Backend = new EnumOptionKey<>(CompilerBackend.JS);
146+
138147
@Option(help = "Report the code sizes of different parts of the generated JavaScript image. If the closure compiler is applied, this instruments the generated javascript code by injecting labels.")//
139148
public static final HostedOptionKey<Boolean> ReportImageSizeBreakdown = new HostedOptionKey<>(false);
140149

0 commit comments

Comments
 (0)