Mojo: Parse command line flags from a variable set at build-time.

We were parsing flags from an environment variable, but that won't work
on Android.

This approach is very flexible.  Any time we build a new binary, we can
determine what flags to run with.

I considered parsing flags from the url parameters, but that doesn't
seem right because the client should not dictate what settings the
service runs with.  It also had implications for caching.

I also considered fetching a config file via HTTP and parsing the flags
from that, but that didn't work because there's no way to tell where the
http server will be running at this point in the code.

Change-Id: I7c438cdc2d134bf3a509d18b8b152c5064e2329c
diff --git a/runtime/internal/mojo_util.go b/runtime/internal/mojo_util.go
index e434d68..308ba01 100644
--- a/runtime/internal/mojo_util.go
+++ b/runtime/internal/mojo_util.go
@@ -8,22 +8,32 @@
 
 import (
 	"flag"
-	"os"
+	"log"
 	"strings"
 
 	"v.io/x/ref/lib/flags"
 )
 
+// NOTE(nlacasse): This variable must be set at build time by passing the
+// "-ldflags" flag to "go build" like so:
+// go build -ldflags "-X v.io/x/ref/runtime/internal.commandLineFlags '--flag1=foo --flag2=bar'"
+var commandLineFlags string
+
 // TODO(sadovsky): Terrible, terrible hack.
 func parseFlagsInternal(f *flags.Flags, config map[string]string) error {
-	// We expect that command-line flags have not been parsed. v23_util performs
-	// command-line parsing at this point. For Mojo, we instead parse command-line
-	// flags from the V23_MOJO_FLAGS env var.
+	// We expect that command-line flags have not been parsed. v23_util
+	// performs command-line parsing at this point. For Mojo, we instead parse
+	// command-line flags from the commandLineFlags variable set at build time.
 	// TODO(sadovsky): Maybe move this check to util.go, or drop it?
 	if flag.CommandLine.Parsed() {
 		panic("flag.CommandLine.Parse() has been called")
 	}
+
+	// NOTE(nlacasse): Don't use vlog here, since vlog output depends on
+	// command line flags which have not been parsed yet.
+	log.Printf("Parsing flags: %v\n", commandLineFlags)
+
 	// TODO(sadovsky): Support argument quoting. More generally, parse this env
 	// var similar to how bash parses arguments.
-	return f.Parse(strings.Split(os.Getenv("V23_MOJO_FLAGS"), " "), config)
+	return f.Parse(strings.Split(commandLineFlags, " "), config)
 }