Skip to content

Commit

Permalink
Passing args to launcher (#933)
Browse files Browse the repository at this point in the history
Pull request: #933
  • Loading branch information
LMnet authored Aug 6, 2020
1 parent d81662f commit 5e164d3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 15 deletions.
66 changes: 52 additions & 14 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ object Deps {
val jettyServer = ivy"org.eclipse.jetty:jetty-server:8.1.16.v20140903"
val jettyWebsocket = ivy"org.eclipse.jetty:jetty-websocket:8.1.16.v20140903"
val jgraphtCore = ivy"org.jgrapht:jgrapht-core:1.3.0"

val jna = ivy"net.java.dev.jna:jna:5.0.0"
val jnaPlatform = ivy"net.java.dev.jna:jna-platform:5.0.0"

val junitInterface = ivy"com.novocode:junit-interface:0.11"
val lambdaTest = ivy"de.tototec:de.tobiasroeser.lambdatest:0.7.0"
val osLib = ivy"com.lihaoyi::os-lib:0.7.1"
Expand Down Expand Up @@ -585,47 +585,85 @@ def launcherScript(shellJvmArgs: Seq[String],
mill.modules.Jvm.universalScript(
shellCommands = {
val jvmArgsStr = shellJvmArgs.mkString(" ")
def java(mainClass: String) =
s"""exec $$JAVACMD $jvmArgsStr $$JAVA_OPTS -cp "${shellClassPath.mkString(":")}" $mainClass "$$@""""
def java(mainClass: String, passMillJvmOpts: Boolean) = {
val millJvmOpts = if (passMillJvmOpts) "$mill_jvm_opts" else ""
s"""exec $$JAVACMD $jvmArgsStr $$JAVA_OPTS $millJvmOpts -cp "${shellClassPath.mkString(":")}" $mainClass "$$@""""
}

s"""if [ -z "$$JAVA_HOME" ] ; then
| JAVACMD="java"
|else
| JAVACMD="$$JAVA_HOME/bin/java"
|fi
|
|mill_jvm_opts=""
|init_mill_jvm_opts () {
| if [ -z $$MILL_JVM_OPTS_PATH ] ; then
| mill_jvm_opts_file=".mill-jvm-opts"
| else
| mill_jvm_opts_file=$$MILL_JVM_OPTS_PATH
| fi
|
| if [ -f "$$mill_jvm_opts_file" ] ; then
| while IFS= read line
| do
| case $$line in
| "-X"*) mill_jvm_opts="$${mill_jvm_opts} $$line"
| esac
| done <"$$mill_jvm_opts_file"
| fi
|}
|
|# Client-server mode doesn't seem to work on WSL, just disable it for now
|# https://stackoverflow.com/a/43618657/871202
|if grep -qEi "(Microsoft|WSL)" /proc/version > /dev/null 2> /dev/null ; then
| COURSIER_CACHE=.coursier ${java("mill.MillMain")}
| init_mill_jvm_opts
| COURSIER_CACHE=.coursier ${java("mill.MillMain", true)}
|else
| case "$$1" in
| -i | --interactive | --repl | --no-server )
| ${java("mill.MillMain")}
| init_mill_jvm_opts
| ${java("mill.MillMain", true)}
| ;;
| *)
| ${java("mill.main.client.MillClientMain")}
| ${java("mill.main.client.MillClientMain", false)}
| ;;
|esac
|fi
|""".stripMargin
},
cmdCommands = {
val jvmArgsStr = cmdJvmArgs.mkString(" ")
def java(mainClass: String) =
s""""%JAVACMD%" $jvmArgsStr %JAVA_OPTS% -cp "${cmdClassPath.mkString(";")}" $mainClass %*"""
def java(mainClass: String, passMillJvmOpts: Boolean) = {
val millJvmOpts = if (passMillJvmOpts) "!mill_jvm_opts!" else ""
s""""%JAVACMD%" $jvmArgsStr %JAVA_OPTS% $millJvmOpts -cp "${cmdClassPath.mkString(";")}" $mainClass %*"""
}

s"""set "JAVACMD=java.exe"
s"""setlocal EnableDelayedExpansion
|set "JAVACMD=java.exe"
|if not "%JAVA_HOME%"=="" set "JAVACMD=%JAVA_HOME%\\bin\\java.exe"
|if "%1" == "-i" set _I_=true
|if "%1" == "--interactive" set _I_=true
|if "%1" == "--repl" set _I_=true
|if "%1" == "--no-server" set _I_=true
|
|set "mill_jvm_opts="
|set "mill_jvm_opts_file=.mill-jvm-opts"
|if not "%MILL_JVM_OPTS_PATH%"=="" set "mill_jvm_opts_file=%MILL_JVM_OPTS_PATH%"
|
|if defined _I_ (
| ${java("mill.MillMain")}
| if exist %mill_jvm_opts_file% (
| for /f "delims=" %%G in (%mill_jvm_opts_file%) do (
| set line=%%G
| if "!line:~0,2!"=="-X" set "mill_jvm_opts=!mill_jvm_opts! !line!"
| )
| )
| ${java("mill.MillMain", true)}
|) else (
| ${java("mill.main.client.MillClientMain")}
|)""".stripMargin
| ${java("mill.main.client.MillClientMain", false)}
|)
|endlocal
|""".stripMargin
}
)
}
Expand Down Expand Up @@ -694,7 +732,7 @@ object dev extends MillModule{
// for more detailed explanation
val isWin = scala.util.Properties.isWin
val classpath = runClasspath().map{ pathRef =>
val path = if (isWin) "/" + pathRef.path.toString.replace("\\", "/")
val path = if (isWin) "/" + pathRef.path.toString.replace("\\", "/")
else pathRef.path.toString
if (path.endsWith(".jar")) path
else path + "/"
Expand Down
12 changes: 12 additions & 0 deletions docs/pages/1 - Intro to Mill.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,3 +934,15 @@ variable or `.mill-version` file.
Come by our [Gitter Channel](https://gitter.im/lihaoyi/mill) if you want to ask
questions or say hi!
## Running Mill with custom JVM options
It's possible to pass JVM options to the Mill launcher. To do this you need to create a `.mill-jvm-opts` file in your project's root. This file should contain JVM options (strings, starting with `-X`), one per line. All other lines will be ignored.
For example, if your build requires a lot of memory and bigger stack size, your `.mill-jvm-opts` could look like this:
```
-Xss10m
-Xmx10G
```
The file name for passing JVM options to the Mill launcher is configurable. If for some reason you don't want to use `.mill-jvm-opts` file name, add `MILL_JVM_OPTS_PATH` environment variable with any other file name.
19 changes: 18 additions & 1 deletion main/client/src/mill/main/client/MillClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MillClientMain {
public static final int ExitServerCodeWhenVersionMismatch() { return 101; }

static void initServer(String lockBase, boolean setJnaNoSys) throws IOException,URISyntaxException{

String selfJars = "";
List<String> vmOptions = new ArrayList<>();
String millOptionsPath = System.getProperty("MILL_OPTIONS_PATH");
Expand Down Expand Up @@ -51,6 +51,23 @@ static void initServer(String lockBase, boolean setJnaNoSys) throws IOException,
vmOptions.add("-Djna.nosys=true");
}

String millJvmOptsPath = System.getProperty("MILL_JVM_OPTS_PATH");
if (millJvmOptsPath == null) {
millJvmOptsPath = ".mill-jvm-opts";
}

File millJvmOptsFile = new File(millJvmOptsPath);
if (millJvmOptsFile.exists()) {
try (Scanner sc = new Scanner(millJvmOptsFile)) {
while (sc.hasNextLine()) {
String arg = sc.nextLine();
if (arg.startsWith("-X")) {
vmOptions.add(arg);
}
}
}
}

List<String> l = new ArrayList<>();
l.add(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java");
l.addAll(vmOptions);
Expand Down

0 comments on commit 5e164d3

Please sign in to comment.