aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/build/linux.go
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2021-07-16 15:32:28 +0000
committerAleksandr Nogikh <wp32pw@gmail.com>2021-07-20 13:39:45 +0200
commit1b201b48c59d619af21de7fcc5face22824c0285 (patch)
treee0e7dec8e2b626a61ce6ef0a9aebf47dad108190 /pkg/build/linux.go
parent5a171d724fb4a90890e868348e85c03646cdedab (diff)
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.
Diffstat (limited to 'pkg/build/linux.go')
-rw-r--r--pkg/build/linux.go23
1 files changed, 22 insertions, 1 deletions
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) {