diff --git a/build.sbt b/build.sbt index e9bcb8d1ed40..34aba998cd12 100644 --- a/build.sbt +++ b/build.sbt @@ -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 = @@ -2517,6 +2517,7 @@ lazy val `engine-runner` = project commands += WithDebugCommand.withDebug, inConfig(Compile)(truffleRunOptionsSettings), libraryDependencies ++= Seq( + "org.graalvm.sdk" % "launcher-common" % graalMavenPackagesVersion, "org.graalvm.polyglot" % "polyglot" % graalMavenPackagesVersion, "org.graalvm.sdk" % "polyglot-tck" % graalMavenPackagesVersion % Provided, "commons-cli" % "commons-cli" % commonsCliVersion, diff --git a/engine/runner/src/main/java/org/enso/runner/ContextFactory.java b/engine/runner/src/main/java/org/enso/runner/ContextFactory.java index e2581be369fc..e68a1fa28a09 100644 --- a/engine/runner/src/main/java/org/enso/runner/ContextFactory.java +++ b/engine/runner/src/main/java/org/enso/runner/ContextFactory.java @@ -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; @@ -51,10 +52,12 @@ final class ContextFactory { private int warningsLimit = 100; private java.util.Map 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) { @@ -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()) diff --git a/engine/runner/src/main/java/org/enso/runner/Main.java b/engine/runner/src/main/java/org/enso/runner/Main.java index 7e2dd587a1f7..57a2e283c4f9 100644 --- a/engine/runner/src/main/java/org/enso/runner/Main.java +++ b/engine/runner/src/main/java/org/enso/runner/Main.java @@ -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; @@ -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; @@ -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"; @@ -612,7 +616,7 @@ private void compile( } var context = - ContextFactory.create() + ContextFactory.create(Context.newBuilder()) .projectRoot(packagePath) .in(System.in) .out(System.out) @@ -657,6 +661,7 @@ private void compile( * null} */ private void run( + Context.Builder initialBuilder, String path, java.util.List additionalArgs, String projectPath, @@ -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) @@ -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) @@ -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, @@ -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) @@ -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); } @@ -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); @@ -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), @@ -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, @@ -1290,28 +1304,18 @@ private static final scala.collection.immutable.List 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) @@ -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) { @@ -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 preprocessArguments( + List arguments, Map 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); diff --git a/engine/runtime-fat-jar/src/main/java/module-info.java b/engine/runtime-fat-jar/src/main/java/module-info.java index 1f5b2eee274d..85a1fcd6a9b0 100644 --- a/engine/runtime-fat-jar/src/main/java/module-info.java +++ b/engine/runtime-fat-jar/src/main/java/module-info.java @@ -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; diff --git a/project/GraalVM.scala b/project/GraalVM.scala index b6e1957f48a7..85bf3bd35884 100644 --- a/project/GraalVM.scala +++ b/project/GraalVM.scala @@ -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,