From f91780d6ce685137c3cfb1d1f9d146bcf9dc7751 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 29 Aug 2022 10:42:18 +0200 Subject: [PATCH] insert buildinfo at compile time --- compile.go | 5 +- go.mod | 1 + go.sum | 2 + init.go | 3 +- main.go | 11 ++-- metadata.go | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 metadata.go diff --git a/compile.go b/compile.go index 4e1edcb..4500071 100644 --- a/compile.go +++ b/compile.go @@ -35,10 +35,7 @@ func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) { ch <- &CompileReport{Config: cfg, State: StateCompiling} - args := []string{"build", "-o", filePath} - if !*KeepDebugFlags { - args = append(args, "-ldflags=-s -w") - } + args := []string{"build", "-o", filePath, BuildLdFlags(cfg.OS, cfg.Arch)} compileCmd := exec.Command("go", args...) compileCmd.Dir = ModulePath diff --git a/go.mod b/go.mod index 7d77de6..a332055 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.milar.in/milarin/gocc go 1.19 require ( + git.milar.in/milarin/buildinfo v1.0.0 git.milar.in/milarin/channel v0.0.7 git.milar.in/milarin/configfile v1.0.2 git.milar.in/milarin/gmath v0.0.1 diff --git a/go.sum b/go.sum index 942bcd7..36a7f97 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.milar.in/milarin/buildinfo v1.0.0 h1:tw98GupUYl/0a/3aPGuezhE4wseycOSsbcLp70hy60U= +git.milar.in/milarin/buildinfo v1.0.0/go.mod h1:arI9ZoENOgcZcanv25k9y4dKDUhPp0buJrlVerGruas= git.milar.in/milarin/channel v0.0.7 h1:cVKtwgH/EE7U+XTHcoFCClJ4LR349KanzjX9xKwRcNg= git.milar.in/milarin/channel v0.0.7/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM= git.milar.in/milarin/configfile v1.0.2 h1:QgZVSVDsFm3HK7PEg6a2ANeZxqo0JlIooyyJv8VaCNI= diff --git a/init.go b/init.go index 8742664..5c904da 100644 --- a/init.go +++ b/init.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" + "git.milar.in/milarin/buildinfo" "git.milar.in/milarin/configfile" ) @@ -16,7 +17,7 @@ func Init() { var err error if *ShowVersion { - fmt.Printf("gocc %s\n", VERSION) + buildinfo.Print(buildinfo.Options{}) os.Exit(0) } diff --git a/main.go b/main.go index a0685b1..98d1d74 100644 --- a/main.go +++ b/main.go @@ -16,15 +16,19 @@ import ( "git.milar.in/milarin/channel" ) -var VERSION = "v1.0.3" - // globals var ( OutputFileTmpl = template.New("output-file") ModulePath string ProjectName string - Runner channel.Runner + + // meta data for executables + VersionTag string + CommitHash string + BuildTime string + + Runner channel.Runner MaxConfigStringLength int ) @@ -63,6 +67,7 @@ var ( func main() { Init() + GatherMetaData() ch := make(chan *CompileReport, len(CompileConfigs)) wg := new(sync.WaitGroup) diff --git a/metadata.go b/metadata.go new file mode 100644 index 0000000..7b1945b --- /dev/null +++ b/metadata.go @@ -0,0 +1,153 @@ +package main + +import ( + "errors" + "fmt" + "io" + "os/exec" + "strings" + "time" +) + +func GetCommitHash(hashfmt string) (string, error) { + gitCmd := exec.Command("git", "log", "-n", "1", "--pretty=format:"+hashfmt) + gitCmd.Dir = ModulePath + + stdout, err := gitCmd.StdoutPipe() + if err != nil { + return "", err + } + + if err := gitCmd.Start(); err != nil { + return "", err + } + + hashData, err := io.ReadAll(stdout) + if err != nil { + return "", err + } + + if err := gitCmd.Wait(); err != nil { + return "", err + } + + trimmedHash := strings.TrimSpace(string(hashData)) + if trimmedHash == "" { + return "", errors.New("no valid hash found") + } + + return trimmedHash, nil +} + +func GetVersionTag() (string, error) { + gitCmd := exec.Command("git", "log", "-n", "1", "--pretty=format:%(describe:tags)") + gitCmd.Dir = ModulePath + + stdout, err := gitCmd.StdoutPipe() + if err != nil { + return "", err + } + + if err := gitCmd.Start(); err != nil { + return "", err + } + + tagData, err := io.ReadAll(stdout) + if err != nil { + return "", err + } + + if err := gitCmd.Wait(); err != nil { + return "", err + } + + trimmedTag := strings.TrimSpace(string(tagData)) + if trimmedTag == "" { + return "", errors.New("no valid version tag found") + } + + return trimmedTag, nil +} + +func WorkTreeChanged() bool { + gitCmd := exec.Command("git", "status", "--porcelain") + gitCmd.Dir = ModulePath + + stdout, err := gitCmd.StdoutPipe() + if err != nil { + return false + } + + if err := gitCmd.Start(); err != nil { + return false + } + + data, err := io.ReadAll(stdout) + if err != nil { + return false + } + + if err := gitCmd.Wait(); err != nil { + return false + } + + return len(data) != 0 +} + +func GatherMetaData() { + if !WorkTreeChanged() { + VersionTag, _ = GetVersionTag() + CommitHash, _ = GetCommitHash("%H") + } else { + hash, _ := GetCommitHash("%h") + VersionTag = fmt.Sprintf("(devel-%s)", hash) + } + + BuildTime = time.Now().Format(time.RFC3339) +} + +func BuildLdFlags(os, arch string) string { + b := &strings.Builder{} + + b.WriteString("-ldflags=") + + if !*KeepDebugFlags { + b.WriteString("-s -w") + } + + if CommitHash != "" { + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.Commit") + b.WriteString("=") + b.WriteString(CommitHash) + } + + if VersionTag != "" { + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.Version") + b.WriteString("=") + b.WriteString(VersionTag) + } + + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.Name") + b.WriteString("=") + b.WriteString(ProjectName) + + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.BuildTime") + b.WriteString("=") + b.WriteString(BuildTime) + + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.OS") + b.WriteString("=") + b.WriteString(os) + + b.WriteString(" -X ") + b.WriteString("git.milar.in/milarin/buildinfo.Arch") + b.WriteString("=") + b.WriteString(arch) + + return b.String() +}