Skip to content
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

Align Enso launcher with Truffle's AbstractLanguageLauncher #10335

Closed
Closed
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
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2350,7 +2350,7 @@ lazy val `runtime-fat-jar` =
.settings(
libraryDependencies ++= {
val graalMods =
GraalVM.modules.map(_.withConfigurations(Some(Runtime.name)))
GraalVM.modules.map(_.withConfigurations(Some(Compile.name)))
val langMods =
GraalVM.langsPkgs.map(_.withConfigurations(Some(Runtime.name)))
val logbackMods =
Expand Down Expand Up @@ -2517,6 +2517,7 @@ lazy val `engine-runner` = project
commands += WithDebugCommand.withDebug,
inConfig(Compile)(truffleRunOptionsSettings),
libraryDependencies ++= Seq(
"org.graalvm.sdk" % "launcher-common" % graalMavenPackagesVersion,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using launcher-common is not easy. It is not a nicely designed API. Rather an abstraction of common functionality found among various Truffle language launchers. Some things aren't even possible/easy - for example get list of unprocessed arguments.

"org.graalvm.polyglot" % "polyglot" % graalMavenPackagesVersion,
"org.graalvm.sdk" % "polyglot-tck" % graalMavenPackagesVersion % Provided,
"commons-cli" % "commons-cli" % commonsCliVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @param warningsLimit maximal number of warnings reported to the user
*/
final class ContextFactory {
private final Context.Builder initialBuilder;
private String projectRoot;
private InputStream in;
private OutputStream out;
Expand All @@ -51,10 +52,12 @@ final class ContextFactory {
private int warningsLimit = 100;
private java.util.Map<String, String> options = java.util.Collections.emptyMap();

private ContextFactory() {}
private ContextFactory(Context.Builder b) {
this.initialBuilder = b;
}

public static ContextFactory create() {
return new ContextFactory();
public static ContextFactory create(Context.Builder b) {
return new ContextFactory(b);
}

public ContextFactory projectRoot(String projectRoot) {
Expand Down Expand Up @@ -139,7 +142,7 @@ PolyglotContext build() {
var julLogLevel = Converter.toJavaLevel(logLevel);
var logLevelName = julLogLevel.getName();
var builder =
Context.newBuilder()
initialBuilder
.allowExperimentalOptions(true)
.allowAllAccess(true)
.allowHostAccess(new HostAccessFactory().allWithTypeMapping())
Expand Down
68 changes: 46 additions & 22 deletions engine/runner/src/main/java/org/enso/runner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand Down Expand Up @@ -40,6 +42,8 @@
import org.enso.profiling.sampler.NoopSampler;
import org.enso.profiling.sampler.OutputStreamSampler;
import org.enso.version.VersionDescription;
import org.graalvm.options.OptionCategory;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.PolyglotException.StackFrame;
import org.graalvm.polyglot.SourceSection;
Expand All @@ -52,7 +56,7 @@
import scala.runtime.BoxedUnit;

/** The main CLI entry point class. */
public final class Main {
public final class Main extends org.graalvm.launcher.AbstractLanguageLauncher {
private static final String RUN_OPTION = "run";
private static final String INSPECT_OPTION = "inspect";
private static final String DUMP_GRAPHS_OPTION = "dump-graphs";
Expand Down Expand Up @@ -612,7 +616,7 @@ private void compile(
}

var context =
ContextFactory.create()
ContextFactory.create(Context.newBuilder())
.projectRoot(packagePath)
.in(System.in)
.out(System.out)
Expand Down Expand Up @@ -657,6 +661,7 @@ private void compile(
* null}
*/
private void run(
Context.Builder initialBuilder,
String path,
java.util.List<String> additionalArgs,
String projectPath,
Expand Down Expand Up @@ -687,7 +692,7 @@ private void run(
options.put("inspect", "");
}
var context =
ContextFactory.create()
ContextFactory.create(initialBuilder)
.projectRoot(projectRoot)
.in(System.in)
.out(System.out)
Expand Down Expand Up @@ -756,7 +761,7 @@ private void genDocs(
private void generateDocsFrom(
String path, Level logLevel, boolean logMasking, boolean enableIrCaches) {
var executionContext =
ContextFactory.create()
ContextFactory.create(Context.newBuilder())
.projectRoot(path)
.in(System.in)
.out(System.out)
Expand Down Expand Up @@ -883,6 +888,7 @@ private void runMain(
* @param enableStaticAnalysis whether or not static type checking should be enabled
*/
private void runRepl(
Context.Builder initialBuilder,
String projectPath,
Level logLevel,
boolean logMasking,
Expand All @@ -900,7 +906,7 @@ private void runRepl(
var replModuleName = "Internal_Repl_Module___";
var projectRoot = projectPath != null ? projectPath : "";
var context =
ContextFactory.create()
ContextFactory.create(initialBuilder)
.projectRoot(projectRoot)
.in(System.in)
.out(System.out)
Expand Down Expand Up @@ -966,7 +972,8 @@ private URI parseUri(String string) {
*
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
Thread.sleep(1000);
new Main().launch(args);
}

Expand All @@ -978,7 +985,12 @@ public static void main(String[] args) throws IOException {
* @param logLevel the provided log level
* @param logMasking the flag indicating if the log masking is enabled
*/
private void runMain(Options options, CommandLine line, Level logLevel, boolean logMasking)
private void runMain(
Context.Builder initialBuilder,
Options options,
CommandLine line,
Level logLevel,
boolean logMasking)
throws IOException {
if (line.hasOption(HELP_OPTION)) {
printHelp(options);
Expand Down Expand Up @@ -1062,6 +1074,7 @@ private void runMain(Options options, CommandLine line, Level logLevel, boolean

if (line.hasOption(RUN_OPTION)) {
run(
initialBuilder,
line.getOptionValue(RUN_OPTION),
Arrays.asList(line.getArgs()),
line.getOptionValue(IN_PROJECT_OPTION),
Expand All @@ -1080,6 +1093,7 @@ private void runMain(Options options, CommandLine line, Level logLevel, boolean
}
if (line.hasOption(REPL_OPTION)) {
runRepl(
initialBuilder,
line.getOptionValue(IN_PROJECT_OPTION),
logLevel,
logMasking,
Expand Down Expand Up @@ -1290,28 +1304,18 @@ private static final <T> scala.collection.immutable.List<T> join(
return scala.collection.immutable.$colon$colon$.MODULE$.apply(head, tail);
}

private void println(String msg) {
System.out.println(msg);
}

private void launch(String[] args) {
var options = buildOptions();
var line = preprocessArguments(options, args);
launch(options, line);
}

protected CommandLine preprocessArguments(Options options, String[] args) {
private CommandLine preprocessCommandLine(Options options, String[] args) {
var parser = new DefaultParser();
try {
var line = parser.parse(options, args);
return line;
return parser.parse(options, args, true);
} catch (Exception e) {
printHelp(options);
throw exitFail();
}
}

protected void launch(Options options, CommandLine line) {
@Override
protected void launch(Context.Builder contextBuilder) {
var logLevel =
scala.Option.apply(line.getOptionValue(LOG_LEVEL))
.map(this::parseLogLevel)
Expand All @@ -1330,7 +1334,7 @@ protected void launch(Options options, CommandLine line) {
conf,
ExecutionContext.global(),
() -> {
runMain(options, line, logLevel, logMasking);
runMain(contextBuilder, options, line, logLevel, logMasking);
return BoxedUnit.UNIT;
});
} catch (IOException ex) {
Expand All @@ -1344,10 +1348,30 @@ protected void launch(Options options, CommandLine line) {
}
}

@Override
protected String getLanguageId() {
return LanguageInfo.ID;
}

private final Options options = buildOptions();
private CommandLine line;

@Override
protected List<String> preprocessArguments(
List<String> arguments, Map<String, String> polyglotOptions) {
// if (arguments.stream().filter(a -> a.startsWith("--help")).findFirst().isPresent()) {
// return arguments;
// }
var args = arguments.toArray(new String[0]);
line = preprocessCommandLine(options, args);
return line.getArgList();
}

@Override
protected void printHelp(OptionCategory maxCategory) {
printHelp(options);
}

private static final class WrongOption extends Exception {
WrongOption(String msg) {
super(msg);
Expand Down
1 change: 1 addition & 0 deletions engine/runtime-fat-jar/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// works.
requires org.enso.profiling;
requires org.enso.ydoc;
requires org.graalvm.launcher;
requires org.graalvm.polyglot;
requires org.graalvm.truffle;
requires static org.slf4j;
Expand Down
2 changes: 2 additions & 0 deletions project/GraalVM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ object GraalVM {
* When invoking the `java` command, these modules need to be put on the module-path.
*/
val modules: Seq[ModuleID] = Seq(
"org.graalvm.sdk" % "launcher-common" % version,
"org.graalvm.shadowed" % "jline" % version,
"org.graalvm.sdk" % "nativeimage" % version,
"org.graalvm.sdk" % "word" % version,
"org.graalvm.sdk" % "jniutils" % version,
Expand Down
Loading