Move runpaths out of global ldflags

The make macros add these to the linker lines even if the default
compiler flags are being skipped.

This also allows us to add another runpath for test binaries, which are
currently being installed in:

  <out>/host/linux-x86/nativetest[64]/<test>/<binary>

So they need to use ../../lib[64] as a rpath.

Change-Id: Ia7e954cdf63bc627a8f71d01c953530355c248ab
diff --git a/cc/cc.go b/cc/cc.go
index 56655b9..3b3128f 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1086,9 +1086,17 @@
 		VariantIsStatic       bool `blueprint:"mutated"`
 		VariantIsStaticBinary bool `blueprint:"mutated"`
 	}
+
+	runPaths []string
 }
 
-func (linker *baseLinker) begin(ctx BaseModuleContext) {}
+func (linker *baseLinker) begin(ctx BaseModuleContext) {
+	if ctx.toolchain().Is64Bit() {
+		linker.runPaths = []string{"../lib64", "lib64"}
+	} else {
+		linker.runPaths = []string{"../lib", "lib"}
+	}
+}
 
 func (linker *baseLinker) props() []interface{} {
 	return []interface{}{&linker.Properties, &linker.dynamicProperties}
@@ -1152,6 +1160,17 @@
 		}
 	}
 
+	if ctx.Host() && !linker.static() {
+		rpath_prefix := `\$$ORIGIN/`
+		if ctx.Darwin() {
+			rpath_prefix = "@loader_path/"
+		}
+
+		for _, rpath := range linker.runPaths {
+			flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
+		}
+	}
+
 	if flags.Clang {
 		flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
 	} else {
@@ -1781,6 +1800,16 @@
 	Properties TestLinkerProperties
 }
 
+func (test *testLinker) begin(ctx BaseModuleContext) {
+	test.binaryLinker.begin(ctx)
+
+	runpath := "../../lib"
+	if ctx.toolchain().Is64Bit() {
+		runpath += "64"
+	}
+	test.runPaths = append([]string{runpath}, test.runPaths...)
+}
+
 func (test *testLinker) props() []interface{} {
 	return append(test.binaryLinker.props(), &test.Properties)
 }