From aca1030d29f627314d13884ebc7b2c313d718df7 Mon Sep 17 00:00:00 2001
From: Ovidiu Panait <ovidiu.panait@windriver.com>
Date: Wed, 13 Apr 2022 17:17:54 +0300
Subject: [PATCH] sys/targets/targets.go: allow users to override hardcoded
 cross-compilers

Currently, cross compiler names are hardcoded for each os/arch combo. However,
toolchain tuples differ, especially when using vendor provided toolchains.
Allow users to specify the cross compiler for an os/arch combo using
SYZ_CC_<os>_<arch> environment variables.

Also, remove hardcoded "-march=armv6" flag to fix compilation on arm.

Upstream-Status: Inappropriate [embedded specific]

Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
---
 sys/targets/targets.go | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

--- a/sys/targets/targets.go
+++ b/sys/targets/targets.go
@@ -262,7 +262,6 @@ var List = map[string]map[string]*Target
 			PtrSize:          4,
 			PageSize:         4 << 10,
 			LittleEndian:     true,
-			CFlags:           []string{"-D__LINUX_ARM_ARCH__=6", "-march=armv6"},
 			Triple:           "arm-linux-gnueabi",
 			KernelArch:       "arm",
 			KernelHeaderArch: "arm",
@@ -700,12 +699,16 @@ func initTarget(target *Target, OS, arch
 	for i := range target.CFlags {
 		target.replaceSourceDir(&target.CFlags[i], sourceDir)
 	}
-	if OS == Linux && arch == runtime.GOARCH {
-		// Don't use cross-compiler for native compilation, there are cases when this does not work:
-		// https://github.com/google/syzkaller/pull/619
-		// https://github.com/google/syzkaller/issues/387
-		// https://github.com/google/syzkaller/commit/06db3cec94c54e1cf720cdd5db72761514569d56
-		target.Triple = ""
+	if OS == Linux {
+		if cc := os.Getenv("SYZ_CC_" + OS + "_" + arch); cc != "" {
+			target.CCompiler = cc
+		} else if arch == runtime.GOARCH {
+			// Don't use cross-compiler for native compilation, there are cases when this does not work:
+			// https://github.com/google/syzkaller/pull/619
+			// https://github.com/google/syzkaller/issues/387
+			// https://github.com/google/syzkaller/commit/06db3cec94c54e1cf720cdd5db72761514569d56
+			target.Triple = ""
+		}
 	}
 	if target.CCompiler == "" {
 		target.setCompiler(useClang)
@@ -839,7 +842,7 @@ func (target *Target) lazyInit() {
 	// On CI we want to fail loudly if cross-compilation breaks.
 	// Also fail if SOURCEDIR_GOOS is set b/c in that case user probably assumes it will work.
 	if (target.OS != runtime.GOOS || !runningOnCI) && getSourceDir(target) == "" {
-		if _, err := exec.LookPath(target.CCompiler); err != nil {
+		if _, err := exec.LookPath(strings.Fields(target.CCompiler)[0]); err != nil {
 			target.BrokenCompiler = fmt.Sprintf("%v is missing (%v)", target.CCompiler, err)
 			return
 		}
