2023-07-01 11:18:12 +00:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "code.icb4dc0.de/buildr/wasi-module-sdk-go"
|
|
|
|
"code.icb4dc0.de/buildr/wasi-module-sdk-go/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-15 19:46:36 +00:00
|
|
|
"log/slog"
|
2023-07-01 11:18:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultArgsLength = 6
|
|
|
|
goArchAndGoOS = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ sdk.Module = (*GoBuild)(nil)
|
|
|
|
// _ sdk.Helper = (*GoBuild)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type GoBuild struct {
|
|
|
|
Env map[string]string `hcl:"environment,optional"`
|
|
|
|
Binary string `hcl:"binary"`
|
|
|
|
Main string `hcl:"main"`
|
|
|
|
GoOS string `hcl:"goos"`
|
|
|
|
GoArch string `hcl:"goarch"`
|
|
|
|
Flags []string `hcl:"flags,optional"`
|
|
|
|
LdFlags []string `hcl:"ldflags,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g GoBuild) Type() string {
|
|
|
|
return "go_build"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g GoBuild) Category() sdk.Category {
|
|
|
|
return sdk.CategoryBuild
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g GoBuild) Execute(ctx sdk.ExecutionContext) (err error) {
|
|
|
|
logger := ctx.Logger()
|
|
|
|
|
|
|
|
buildArgs := make([]string, 1, defaultArgsLength+len(g.Flags))
|
|
|
|
buildArgs[0] = "build"
|
|
|
|
|
|
|
|
if ldFlags := strings.Join(g.LdFlags, " "); ldFlags != "" {
|
|
|
|
buildArgs = append(buildArgs, "-ldflags", ldFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildArgs = append(buildArgs, g.Flags...)
|
|
|
|
|
|
|
|
buildArgs = append(
|
|
|
|
buildArgs,
|
|
|
|
"-o",
|
|
|
|
filepath.Join(ctx.OutDir(), g.Binary),
|
|
|
|
g.Main,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.Info(
|
|
|
|
"Executing go build",
|
|
|
|
slog.String("args", strings.Join(buildArgs, " ")),
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd := exec.NewCommand(
|
|
|
|
"go",
|
|
|
|
buildArgs...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Env == nil {
|
|
|
|
g.Env = make(map[string]string, goArchAndGoOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Env["GOARCH"] = g.GoArch
|
|
|
|
g.Env["GOOS"] = g.GoOS
|
|
|
|
|
|
|
|
cmd.AddEnv(g.Env)
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|