From 1b201b48c59d619af21de7fcc5face22824c0285 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 16 Jul 2021 15:32:28 +0000 Subject: pkg/build/linux: add compiler detection Detect compiler that was used to build the Linux kernel from auto-generated header files. This would be the most precise information. --- pkg/build/linux.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'pkg/build/linux.go') diff --git a/pkg/build/linux.go b/pkg/build/linux.go index 7632712f2..85575e621 100644 --- a/pkg/build/linux.go +++ b/pkg/build/linux.go @@ -15,6 +15,7 @@ import ( "os" "path" "path/filepath" + "regexp" "runtime" "time" @@ -28,6 +29,11 @@ func (linux linux) build(params Params) (ImageDetails, error) { if err := linux.buildKernel(params); err != nil { return ImageDetails{}, err } + compilerID, err := queryLinuxCompiler(params.KernelDir) + if err != nil { + return ImageDetails{}, err + } + kernelPath := filepath.Join(params.KernelDir, filepath.FromSlash(kernelBin(params.TargetArch))) if fileInfo, err := os.Stat(params.UserspaceDir); err == nil && fileInfo.IsDir() { // The old way of assembling the image from userspace dir. @@ -52,7 +58,8 @@ func (linux linux) build(params Params) (ImageDetails, error) { return ImageDetails{}, err } return ImageDetails{ - Signature: signature, + Signature: signature, + CompilerID: compilerID, }, nil } @@ -221,6 +228,20 @@ func kernelBin(arch string) string { } } +var linuxCompilerRegexp = regexp.MustCompile(`#define\s+LINUX_COMPILER\s+"(.*)"`) + +func queryLinuxCompiler(kernelDir string) (string, error) { + bytes, err := ioutil.ReadFile(filepath.Join(kernelDir, "include", "generated", "compile.h")) + if err != nil { + return "", err + } + result := linuxCompilerRegexp.FindSubmatch(bytes) + if result == nil { + return "", fmt.Errorf("include/generated/compile.h does not contain build information") + } + return string(result[1]), nil +} + // elfBinarySignature calculates signature of an elf binary aiming at runtime behavior // (text/data, debug info is ignored). func elfBinarySignature(bin string) (string, error) { -- cgit mrf-deployment