Only pass direct targets to compile (#591)

* Only pass direct targets to compile

They have all the transitive data they need inside them.
This should make complex builds considerably faster, although we have no useful
benchmark to prove it.
Also cleaned up the compile argument handling while I was in there.

* Add back --

just in a different place
diff --git a/go/private/library.bzl b/go/private/library.bzl
index 4024022..0b58de1 100644
--- a/go/private/library.bzl
+++ b/go/private/library.bzl
@@ -53,22 +53,26 @@
   lib_name = importpath + ".a"
   out_lib = ctx.new_file(lib_name)
   out_object = ctx.new_file(ctx.label.name + ".o")
-  search_path = out_lib.path[:-len(lib_name)]
+  searchpath = out_lib.path[:-len(lib_name)]
   gc_goopts = get_gc_goopts(ctx)
-  direct_go_library_paths = []
+  direct_go_library_deps = []
+  direct_search_paths = []
+  direct_import_paths = []
   transitive_go_library_deps = depset()
-  transitive_go_library_paths = depset([search_path])
+  transitive_go_library_paths = depset([searchpath])
   for dep in deps:
-    direct_go_library_paths += [dep.importpath]
+    direct_go_library_deps += [dep.library]
+    direct_search_paths += [dep.searchpath]
+    direct_import_paths += [dep.importpath]
     transitive_go_library_deps += dep.transitive_go_libraries
     transitive_cgo_deps += dep.transitive_cgo_deps
     transitive_go_library_paths += dep.transitive_go_library_paths
 
   go_srcs = emit_go_compile_action(ctx,
       sources = go_srcs,
-      libs = transitive_go_library_deps,
-      lib_paths = transitive_go_library_paths,
-      direct_paths = direct_go_library_paths,                                   
+      libs = direct_go_library_deps,
+      lib_paths = direct_search_paths,
+      direct_paths = direct_import_paths,
       out_object = out_object,
       gc_goopts = gc_goopts,
   )
@@ -85,6 +89,8 @@
   return struct(
     label = ctx.label,
     files = depset([out_lib]),
+    library = out_lib,
+    searchpath = searchpath,
     runfiles = runfiles,
     go_sources = go_srcs,
     asm_sources = asm_srcs,
@@ -113,6 +119,8 @@
   return struct(
     label = ctx.label,
     files = lib_result.files,
+    library = lib_result.library,
+    searchpath = lib_result.searchpath,
     runfiles = lib_result.runfiles,
     go_sources = lib_result.go_sources,
     asm_sources = lib_result.asm_sources,
@@ -218,10 +226,10 @@
     args += ["-src", src]
   for dep in direct_paths:
     args += ["-dep", dep]
-  args += ["--", "-o", out_object.path, "-trimpath", ".", "-I", "."]
+  args += ["-o", out_object.path, "-trimpath", ".", "-I", "."]
   for path in lib_paths:
     args += ["-I", path]
-  args += gc_goopts + cgo_sources
+  args += ["--"] + gc_goopts + cgo_sources
   ctx.action(
       inputs = list(inputs),
       outputs = [out_object],
diff --git a/go/private/test.bzl b/go/private/test.bzl
index 30e12e8..7cd4a91 100644
--- a/go/private/test.bzl
+++ b/go/private/test.bzl
@@ -32,7 +32,6 @@
   main_go = ctx.new_file(ctx.label.name + "_main_test.go")
   main_object = ctx.new_file(ctx.label.name + "_main_test.o")
   main_lib = ctx.new_file(ctx.label.name + "_main_test.a")
-  go_import = go_importpath(ctx)
   run_dir = pkg_dir(ctx.label.workspace_root, ctx.label.package)
 
   ctx.action(
@@ -42,7 +41,7 @@
       executable = go_toolchain.test_generator,
       arguments = [
           '--package',
-          go_import,
+          lib_result.importpath,
           '--rundir',
           run_dir,
           '--output',
@@ -54,9 +53,9 @@
   emit_go_compile_action(
     ctx,
     sources=depset([main_go]),
-    libs=lib_result.transitive_go_libraries,
-    lib_paths=lib_result.transitive_go_library_paths,
-    direct_paths=[go_import],
+    libs=[lib_result.library],
+    lib_paths=[lib_result.searchpath],
+    direct_paths=[lib_result.importpath],
     out_object=main_object,
     gc_goopts=get_gc_goopts(ctx),
   )
diff --git a/go/tools/builders/compile.go b/go/tools/builders/compile.go
index 4e0ceec..0650da3 100644
--- a/go/tools/builders/compile.go
+++ b/go/tools/builders/compile.go
@@ -30,23 +30,33 @@
 	"strings"
 )
 
-func run(args []string) error {
-	// process the args
-	if len(args) < 2 {
-		return fmt.Errorf("Usage: compile gotool [-src source ...] [-dep importpath ...] -- <extra options>")
+func abs(path string) string {
+	if abs, err := filepath.Abs(path); err != nil {
+		return path
+	} else {
+		return abs
 	}
-	gotool := args[0]
-	args = args[1:]
+}
 
+func run(args []string) error {
 	sources := multiFlag{}
 	deps := multiFlag{}
+	search := multiFlag{}
 	flags := flag.NewFlagSet("compile", flag.ContinueOnError)
 	flags.Var(&sources, "src", "A source file to be filtered and compiled")
 	flags.Var(&deps, "dep", "Import path of a direct dependency")
-	if err := flags.Parse(args); err != nil {
+	flags.Var(&search, "I", "Search paths of a direct dependency")
+	trimpath := flags.String("trimpath", "", "The base of the paths to trim")
+	output := flags.String("o", "", "The output object file to write")
+	// process the args
+	if len(args) < 2 {
+		flags.Usage()
+		return fmt.Errorf("The go tool must be specified")
+	}
+	gotool := args[0]
+	if err := flags.Parse(args[1:]); err != nil {
 		return err
 	}
-	goopts := flags.Args()
 
 	// apply build constraints to the source list
 	bctx := build.Default
@@ -64,26 +74,13 @@
 		return err
 	}
 
-	// Now we need to abs include and trim paths
-	needAbs := false
-	for i, arg := range goopts {
-		switch {
-		case needAbs:
-			needAbs = false
-			abs, err := filepath.Abs(arg)
-			if err == nil {
-				goopts[i] = abs
-			}
-		case arg == "-I":
-			needAbs = true
-		case arg == "-trimpath":
-			needAbs = true
-		default:
-			needAbs = false
-		}
+	goargs := []string{"tool", "compile"}
+	goargs = append(goargs, "-trimpath", abs(*trimpath))
+	for _, path := range search {
+		goargs = append(goargs, "-I", abs(path))
 	}
-
-	goargs := append([]string{"tool", "compile"}, goopts...)
+	goargs = append(goargs, "-o", *output)
+	goargs = append(goargs, flags.Args()...)
 	goargs = append(goargs, sources...)
 	cmd := exec.Command(gotool, goargs...)
 	cmd.Stdout = os.Stdout