treewide: replace hardcoded runfiles paths

We hardcoded some of the runfiles paths to find specific files. This replaces the hardcoded paths by a call to rlocationpath. This prevents running a target without the correct dependencies at build time instead of at runtime

Change-Id: I7ce56935ac80be6b28b824ccb0781ab401bd6521
Reviewed-on: https://review.monogon.dev/c/monogon/+/3301
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/metropolis/vm/smoketest/main.go b/metropolis/vm/smoketest/main.go
index 3f0b8ca..58f1a22 100644
--- a/metropolis/vm/smoketest/main.go
+++ b/metropolis/vm/smoketest/main.go
@@ -25,10 +25,30 @@
 	"net"
 	"os"
 	"os/exec"
+	"path/filepath"
 
 	"github.com/bazelbuild/rules_go/go/runfiles"
 )
 
+var (
+	// These are filled by bazel at linking time with the canonical path of
+	// their corresponding file. Inside the init function we resolve it
+	// with the rules_go runfiles package to the real path.
+	xQemuPath string
+)
+
+func init() {
+	var err error
+	for _, path := range []*string{
+		&xQemuPath,
+	} {
+		*path, err = runfiles.Rlocation(*path)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 func main() {
 	testSocket, err := net.Listen("unix", "@metropolis/vm/smoketest")
 	if err != nil {
@@ -49,17 +69,9 @@
 		}
 	}()
 
-	qemuPath, err := runfiles.Rlocation("qemu/qemu-x86_64-softmmu")
-	if err != nil {
-		panic(err)
-	}
-
 	// TODO(lorenz): This explicitly doesn't use our own qboot because it cannot be built in a musl configuration.
 	// This will be fixed once we have a proper multi-target toolchain.
-	biosPath, err := runfiles.Rlocation("qemu/pc-bios/qboot.rom")
-	if err != nil {
-		panic(err)
-	}
+	biosPath := filepath.Join(filepath.Dir(xQemuPath), "pc-bios/qboot.rom")
 
 	baseArgs := []string{"-nodefaults", "-no-user-config", "-nographic", "-no-reboot",
 		"-accel", "kvm", "-cpu", "host",
@@ -74,7 +86,7 @@
 		"-chardev", "socket,id=test,path=metropolis/vm/smoketest,abstract=on",
 		"-device", "virtserialport,chardev=test",
 	}
-	qemuCmd := exec.Command(qemuPath, baseArgs...)
+	qemuCmd := exec.Command(xQemuPath, baseArgs...)
 	qemuCmd.Stdout = os.Stdout
 	qemuCmd.Stderr = os.Stderr
 	if err := qemuCmd.Run(); err != nil {