insert buildinfo at compile time

This commit is contained in:
Timon Ringwald 2022-08-29 10:42:18 +02:00
parent 402b269bf4
commit f91780d6ce
6 changed files with 167 additions and 8 deletions

View File

@ -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

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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)
}

11
main.go
View File

@ -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)

153
metadata.go Normal file
View File

@ -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()
}