Skip to content

[GR-61288] Make -H:Backend= a real option #11094

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web-image/mx.web-image/mx_web_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ def get_or_compute_lines(self) -> List[str]:
lines: List[str] = [
"# This file is auto-generated",
"ExcludeFromAll = true",
"ProvidedHostedOptions = " + " ".join(self.subject.provided_hosted_options + ["Backend="]),
"ProvidedHostedOptions = " + " ".join(self.subject.provided_hosted_options),
]

if builder_jars:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.oracle.svm.hosted.webimage.WebImagePlatformInjector
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.Pair;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.c.type.CIntPointer;
import org.graalvm.nativeimage.c.type.CShortPointer;

Expand Down Expand Up @@ -74,60 +70,11 @@
*/
public class NativeImageWasmGeneratorRunner extends NativeImageGeneratorRunner {

public static final String BACKEND_ARG = "-H:Backend(@[^=]*)?=.*";

/**
* @param args Hosted and runtime options
*/
public static void main(String[] args) {
List<String> arguments = Arrays.asList(args);
arguments = extractDriverArguments(arguments);

CompilerBackend backend;
if (WebImageOptions.isNativeImageBackend()) {
// Under native-image, selection of the Web Image backend is not allowed
backend = CompilerBackend.WASMGC;
} else {
backend = extractBackend(arguments);
}

// installNativeImageClassLoader uses this property to create the platform instance.
System.setProperty(Platform.PLATFORM_PROPERTY_NAME, backend.platform.getName());
new NativeImageWasmGeneratorRunner().start(arguments.toArray(String[]::new));
}

/**
* Extracts the {@link NativeImageWasmGeneratorRunner#BACKEND_ARG} argument as a
* {@link CompilerBackend} from the list of arguments.
*
* @param arguments The list of arguments. The backend argument will be removed from the list if
* specified.
* @return The appropriate compiler backend for the given arguments or
* {@link CompilerBackend#JS} if none was specified
*/
public static CompilerBackend extractBackend(List<String> arguments) {
Iterator<String> it = arguments.iterator();

while (it.hasNext()) {
String param = it.next();

if (param.matches(BACKEND_ARG)) {
it.remove();
String[] parts = param.split("=", 2);
if (parts.length != 2) {
throw new IllegalArgumentException(BACKEND_ARG + " needs an argument");
}
String backendName = parts[1].toUpperCase(Locale.ROOT);
try {
return CompilerBackend.valueOf(backendName);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("No backend with name " + backendName + " exists", e);
}
}
}

// Use JS backend by default
return CompilerBackend.JS;
new NativeImageWasmGeneratorRunner().start(args);
}

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

for (OptionDescriptor value : allHostedOptions.getValues()) {
if (!value.getDeclaringClass().getPackageName().contains("webimage") || WebImageOptions.DebugOptions.DumpProvidedHostedOptionsAndExit == value.getOptionKey()) {
if (!value.getDeclaringClass().getPackageName().contains("webimage")) {
continue;
}

if (WebImageOptions.DebugOptions.DumpProvidedHostedOptionsAndExit == value.getOptionKey()) {
continue;
}

// Do not print the Backend option, it's not available in the svm-wasm macro
if (WebImageOptions.Backend == value.getOptionKey()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.svm.hosted.webimage;

import static com.oracle.svm.hosted.webimage.options.WebImageOptions.CompilerBackend;
import static com.oracle.svm.hosted.webimage.options.WebImageOptions.isNativeImageBackend;

import org.graalvm.nativeimage.Platform;

import com.oracle.svm.hosted.NativeImageClassLoaderPostProcessing;
import com.oracle.svm.hosted.NativeImageClassLoaderSupport;
import com.oracle.svm.hosted.webimage.options.WebImageOptions;

/**
* Sets the {@link Platform#PLATFORM_PROPERTY_NAME} property based on which code-generation backend
* is selected.
* <p>
* In the {@code native-image} launcher this is always the WasmGC backend. Under {@code web-image},
* it is determined by the {@link WebImageOptions#Backend} option.
*/
public class WebImagePlatformInjector implements NativeImageClassLoaderPostProcessing {
@Override
public void apply(NativeImageClassLoaderSupport support) {
CompilerBackend backend;
if (isNativeImageBackend()) {
// Under native-image, selection of the Web Image backend is not allowed
backend = CompilerBackend.WASMGC;
} else {
backend = WebImageOptions.Backend.getValue(support.getParsedHostedOptions());
}

/*
* installNativeImageClassLoader uses this property at the end to create the platform
* instance. This method is called before that.
*/
System.setProperty(Platform.PLATFORM_PROPERTY_NAME, backend.platform.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ public static final class DebugOptions {
public static final HostedOptionKey<Boolean> DumpProvidedHostedOptionsAndExit = new HostedOptionKey<>(false);
}

/**
* Web Image only.
* <p>
* Do not read this value directly. Instead, look it up based on the selected {@link Platform}
* using {@link #getBackend}.
*/
@Option(help = "The Web Image Backend to use.") //
public static final EnumOptionKey<CompilerBackend> Backend = new EnumOptionKey<>(CompilerBackend.JS);

@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.")//
public static final HostedOptionKey<Boolean> ReportImageSizeBreakdown = new HostedOptionKey<>(false);

Expand Down