Skip to content

Commit

Permalink
feat: allow use of remote file args in compiler arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
maxandersen committed Sep 20, 2024
1 parent 7af0ad5 commit 76819ce
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
12 changes: 12 additions & 0 deletions itests/flags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
match: ["*.*"],
inline: [
"+com.github.tjake.jlama.tensor.operations.PanamaTensorOperations::dotProduct*",
"+com.github.tjake.jlama.tensor.operations.PanamaTensorOperations::quantize*",
"+com.github.tjake.jlama.tensor.operations.PanamaTensorOperations*::mpack*",
"+com.github.tjake.jlama.tensor.AbstractTensor::getOffset*",
"+com.github.tjake.jlama.tensor.*::getVector*"
]
}
]
4 changes: 3 additions & 1 deletion src/main/java/dev/jbang/cli/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public Integer doCall() throws IOException {

buildAgents(ctx);

// swizzle remote files after agents been added.
runMixin.javaRuntimeOptions = Util.handleRemoteFiles(runMixin.javaRuntimeOptions);

String cmdline = updateGeneratorForRun(genb).build().generate();

Util.verboseMsg("run: " + cmdline);
Expand All @@ -113,7 +116,6 @@ void buildAgents(BuildContext ctx) throws IOException {
if (runMixin.javaRuntimeOptions == null) {
runMixin.javaRuntimeOptions = new ArrayList<>();
}
agents = handleRemoteFiles(agents);
for (Map.Entry<String, String> agentOption : agents.entrySet()) {
String javaAgent = agentOption.getKey();
String javaAgentOptions = agentOption.getValue();
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/dev/jbang/source/CmdGeneratorBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.jbang.source;

import java.util.*;
import java.util.stream.Collectors;

import dev.jbang.catalog.Alias;
import dev.jbang.source.generators.JarCmdGenerator;
Expand Down Expand Up @@ -145,9 +144,9 @@ private NativeCmdGenerator createNativeCmdGenerator() {

private void updateFromAlias(Alias alias) {
if (arguments.isEmpty()) {
setArguments(handleRemoteFiles(alias.arguments));
setArguments(Util.handleRemoteFiles(alias.arguments));
} else if (alias.arguments != null && !alias.arguments.isEmpty()) {
List<String> args = new ArrayList<>(handleRemoteFiles(alias.arguments));
List<String> args = new ArrayList<>(Util.handleRemoteFiles(alias.arguments));
args.addAll(arguments);
setArguments(args);
}
Expand Down Expand Up @@ -184,11 +183,4 @@ private void updateFromAlias(Alias alias) {
}
}

private static List<String> handleRemoteFiles(List<String> args) {
if (args != null) {
return args.stream().map(Util::substituteRemote).collect(Collectors.toList());
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected Project compile() throws IOException {
optionList.add("-source");
optionList.add("" + JavaUtil.javaVersion(requestedJavaVersion));
}
optionList.addAll(project.getMainSourceSet().getCompileOptions());
optionList.addAll(Util.handleRemoteFiles(project.getMainSourceSet().getCompileOptions()));
String path = ctx.resolveClassPath().getClassPath();
if (!Util.isBlankString(path)) {
if (project.getModuleName().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ protected List<String> generateCommandLineList() throws IOException {

fullArgs.add(javacmd);

fullArgs.addAll(project.getRuntimeOptions());
fullArgs.addAll(runtimeOptions);
fullArgs.addAll(Util.handleRemoteFiles(project.getRuntimeOptions()));
fullArgs.addAll(Util.handleRemoteFiles(runtimeOptions));
fullArgs.addAll(ctx.resolveClassPath().getAutoDectectedModuleArguments(jdk));
fullArgs.addAll(optionalArgs);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/jbang/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ public static String unkebabify(String name) {
return name;
}

public static List<String> handleRemoteFiles(List<String> args) {
if (args != null) {
return args.stream().map(Util::substituteRemote).collect(Collectors.toList());
} else {
return null;
}
}

public enum OS {
linux, alpine_linux, mac, windows, aix, unknown
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/dev/jbang/cli/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,68 @@ void testRemoteFileJavaagentComplex() throws Exception {
assertThat(result.err, containsString("-javaagent:" + jar + "=test:" + file));
}

@Test
@SuppressWarnings("unchecked")
void testRemoteFileXXargs(@TempDir File output) throws Exception {

wms.stubFor(
WireMock.get(urlEqualTo("/flags.json"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBodyFile("flags.json")
.withBody(
Util.readString(
examplesTestFolder.resolve("flags.json")))));

wms.start();

String script = examplesTestFolder.resolve("helloworld.java").toString();
String arg = "http://localhost:" + wms.port() + "/flags.json";
CaptureResult<Integer> result = checkedRun(null, "run", "--verbose",
"-R=-XX:CompilerDirectivesFile=%{" + arg + "}",
script);
assertThat(result.err, containsString("Requesting HTTP GET " + arg));
Path file = Util.downloadAndCacheFile(arg);
assertThat(result.err, containsString("-XX:CompilerDirectivesFile=" + file));
}

@Test
@SuppressWarnings("unchecked")
void testRemoteFileEmbeddedXXargs(@TempDir File output) throws Exception {

wms.stubFor(
WireMock.get(urlEqualTo("/flags2.json"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBodyFile("flags2.json")
.withBody(
Util.readString(
examplesTestFolder.resolve("flags.json")))));

wms.start();
String arg = "http://localhost:" + wms.port() + "/flags2.json";

String directives = "//RUNTIME_OPTIONS -XX:CompilerDirectivesFile=%{" + arg + "}\n"
+ "class funky { static void main(String args[]) {} }";

Path f = output.toPath().resolve("funky.java");

Util.writeString(f, directives);

String script = f.toAbsolutePath().toString();

CaptureResult<Integer> result = checkedRun(null, "run", "--verbose",
script);
assertThat(result.err, containsString("Requesting HTTP GET " + arg));
Path file = Util.downloadAndCacheFile(arg);
assertThat(result.err, containsString("-XX:CompilerDirectivesFile=" + file));

}

void testRemoteFileEmbeddedInOptions() {

}

@Test
@SuppressWarnings("unchecked")
void testRemoteFileArgSimpleEscaped() throws Exception {
Expand Down

0 comments on commit 76819ce

Please sign in to comment.