forgepb/build.go

209 lines
5.6 KiB
Go
Raw Normal View History

2024-12-01 10:44:29 -06:00
package forgepb
// for golang repos, this is an attempt to build the package
2024-12-11 13:55:21 -06:00
//
2024-12-01 10:44:29 -06:00
// Additions to 'go build' that are attempted here:
//
// * detect packages that are plugins
2024-12-11 13:55:21 -06:00
// * run autogenpb packages that have .proto protobuf files
// * use some 'standard' ldflags (VERISON, BUILDTIME)
2024-12-01 10:44:29 -06:00
//
import (
"errors"
"fmt"
"os"
2024-12-01 22:23:02 -06:00
"path/filepath"
"strings"
2024-12-01 10:44:29 -06:00
"time"
2024-12-10 01:48:29 -06:00
"go.wit.com/lib/gui/shell"
2024-12-01 10:44:29 -06:00
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
2024-12-01 22:23:02 -06:00
return f.doBuild(repo, userFlags, "build")
}
func (f *Forge) Install(repo *gitpb.Repo, userFlags []string) error {
return f.doBuild(repo, userFlags, "install")
}
func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) error {
2024-12-07 16:50:04 -06:00
if repo == nil {
log.Warn("forge.doBuild repo == nil")
return errors.New("forge.doBuild repo == nil")
}
2024-12-10 01:48:29 -06:00
if f.IsGoWork() {
// when building using a go.work file, never use GO111MODULE=off
os.Unsetenv("GO111MODULE")
} else {
// when building from ~/go/src, always use GO111MODULE=off
os.Setenv("GO111MODULE", "off")
defer os.Unsetenv("GO111MODULE")
}
2024-12-01 10:44:29 -06:00
2024-12-14 14:30:45 -06:00
if f.IsGoWork() {
// there must be a valid go.mod file if compiling with go.work
if err := repo.ValidGoSum(); err != nil {
2024-12-15 12:14:36 -06:00
log.Warn("forge.doBuild() failed. run go-mod-clean here?")
2024-12-14 14:30:45 -06:00
return err
}
2024-12-01 10:44:29 -06:00
}
2024-12-11 13:55:21 -06:00
if repo.Exists(".forge") {
log.Info("custom build instructions")
data, _ := repo.ReadFile(".forge")
log.Info(".forge =", string(data))
log.Info("todo: do the custom build instructions")
return nil
}
2024-12-13 12:56:23 -06:00
// if not GoPrimitive, autogen each dependent git repo
if repo.GoDepsLen() != 0 {
// build the protobuf files in all protobuf repos
all := repo.GoDeps.SortByGoPath()
for all.Scan() {
t := all.Next()
2024-12-17 06:37:00 -06:00
found := f.FindByGoPath(t.GetGoPath())
if found.GetRepoType() == "protobuf" {
2024-12-13 12:56:23 -06:00
if err := f.runAutogenpb(found); err != nil {
return err
}
2024-12-01 22:23:02 -06:00
}
}
2024-12-01 10:44:29 -06:00
}
// get the version
version := repo.GetCurrentBranchVersion()
2024-12-01 10:44:29 -06:00
if repo.CheckDirty() {
version = version + "-dirty"
}
2024-12-14 14:30:45 -06:00
cmd := []string{"go"}
2024-12-17 06:37:00 -06:00
if repo.GetRepoType() == "plugin" {
2024-12-13 12:56:23 -06:00
if goWhat == "install" {
2024-12-14 14:30:45 -06:00
log.Info("Can not go install plugins yet. just building to ~/go/lib/")
2024-12-13 12:56:23 -06:00
}
2024-12-14 14:30:45 -06:00
cmd = append(cmd, "build")
} else {
cmd = append(cmd, goWhat)
2024-12-13 12:56:23 -06:00
}
2024-12-11 13:55:21 -06:00
2024-12-14 22:32:11 -06:00
_, fname := filepath.Split(repo.FullPath)
homeDir, _ := os.UserHomeDir()
soname := fname + "." + version + ".so"
linkname := fname + ".so"
sopath := filepath.Join(homeDir, "go/lib/go-gui")
2024-12-11 13:55:21 -06:00
// if this is a plugin, use buildmode=plugin
2024-12-17 06:37:00 -06:00
if repo.GetRepoType() == "plugin" {
2024-12-14 14:30:45 -06:00
if goWhat == "install" {
2024-12-14 22:32:11 -06:00
fullname := filepath.Join(sopath, soname)
2024-12-14 14:30:45 -06:00
cmd = append(cmd, "-buildmode=plugin", "-o", fullname)
} else {
cmd = append(cmd, "-buildmode=plugin", "-o", soname)
}
2024-12-11 13:55:21 -06:00
}
cmd = append(cmd, "-v")
2024-12-01 10:44:29 -06:00
// set standard ldflag options
now := time.Now()
datestamp := now.UTC().Format("2006/01/02_1504_UTC")
2024-12-01 22:23:02 -06:00
// log.Info("datestamp =", datestamp)
2024-12-01 10:44:29 -06:00
// add some standard golang flags
ldflags := "-X main.VERSION=" + version + " "
ldflags += "-X main.BUILDTIME=" + datestamp + " "
ldflags += "-X main.GUIVERSION=" + version + "" // todo: git this from the filesystem
cmd = append(cmd, "-ldflags", ldflags)
// add any flags from the command line
// this might not actually work
// todo: test this
for _, flag := range userFlags {
cmd = append(cmd, "-ldflags", "-X "+flag)
}
2024-12-10 01:48:29 -06:00
testenv := os.Getenv("GO111MODULE")
if testenv == "off" {
log.Info("GO111MODULE=off", "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
} else {
log.Info("GO111MODULE=", testenv, "f.goWork =", f.IsGoWork(), "f.gosrc =", f.GetGoSrc())
}
2024-12-11 13:55:21 -06:00
log.Info("running:", repo.FullPath)
2024-12-01 22:23:02 -06:00
log.Info("running:", cmd)
2024-12-03 13:24:41 -06:00
result := repo.RunRealtime(cmd)
2024-12-14 22:32:11 -06:00
if result.Exit != 0 {
// build failed
2024-12-03 13:24:41 -06:00
log.DaemonMode(true)
log.Info(strings.Join(result.Stdout, "\n"))
log.Info(strings.Join(result.Stderr, "\n"))
2024-12-14 22:32:11 -06:00
log.Info("result.Error =", result.Error)
log.Info("result.Exit =", result.Exit)
2024-12-03 13:24:41 -06:00
log.DaemonMode(false)
2024-12-01 22:23:02 -06:00
log.Warn("go build failed", cmd)
2024-12-14 22:32:11 -06:00
/*
pwd, _ := os.Getwd()
log.Warn("go build pwd", pwd)
res2 := shell.RunEcho(cmd)
if res2.Exit == 0 {
log.Info("again failed", res2.Exit)
log.Info("again failed cmd", strings.Join(cmd, "a"))
log.Info("again failed", strings.Join(res2.Stdout, "\n"))
}
*/
2024-12-13 12:56:23 -06:00
return errors.New("go " + goWhat + " failed: " + fmt.Sprint(result.Error))
2024-12-01 10:44:29 -06:00
}
2024-12-14 22:32:11 -06:00
// make symlinks
2024-12-17 06:37:00 -06:00
if repo.GetRepoType() == "plugin" {
2024-12-14 22:32:11 -06:00
cmd := []string{"ln", "-sf", soname, linkname}
if goWhat == "install" {
shell.PathRun(sopath, cmd)
} else {
repo.Run(cmd)
}
}
log.Info(strings.Join(result.Stdout, "\n"))
return nil
2024-12-01 10:44:29 -06:00
}
2024-12-01 22:23:02 -06:00
func (f *Forge) runAutogenpb(repo *gitpb.Repo) error {
// log.Info("run autogenpb here:", repo.GetGoPath())
files, err := repo.GetProtoFiles()
if err != nil {
log.Info("gitpb.GetProtoFiles()", err)
return err
}
for _, s := range files {
_, filename := filepath.Split(s)
pbfile := strings.TrimSuffix(filename, ".proto") + ".pb.go"
cmd := []string{"autogenpb", "--proto", filename}
if repo.Exists(pbfile) {
// log.Info("skip running:", cmd)
continue
}
log.Info("running:", cmd)
r := repo.RunRealtime(cmd)
if r.Error != nil {
log.Warn("")
log.Warn("autogenpb error", r.Error)
log.Warn("")
log.Warn("You might be missing autogenpb?")
log.Warn("")
log.Warn("To install it run: go install go.wit.com/apps/autogenpb@latest")
log.Warn("")
log.Sleep(2)
return r.Error
}
}
return nil
}
2024-12-16 00:19:23 -06:00
// sortcut to find
func (f *Forge) FindWorkingDirRepo() *gitpb.Repo {
pwd, _ := os.Getwd()
basedir := strings.TrimPrefix(pwd, f.GetGoSrc())
basedir = strings.Trim(basedir, "/")
2024-12-17 06:37:00 -06:00
return f.FindByGoPath(basedir)
2024-12-16 00:19:23 -06:00
}