-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
71 lines (63 loc) · 2.13 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package build_go
import (
"fmt"
"path"
xgolib "github.com/cardinalby/xgo-as-library"
"github.com/cardinalby/xgo-pack/pkg/build_go/config"
"github.com/cardinalby/xgo-pack/pkg/consts"
"github.com/cardinalby/xgo-pack/pkg/pipeline/buildctx"
fsutil "github.com/cardinalby/xgo-pack/pkg/util/fs"
"github.com/cardinalby/xgo-pack/pkg/util/logging"
typeutil "github.com/cardinalby/xgo-pack/pkg/util/type"
)
func Start(
ctx buildctx.Context,
config config.Config,
) error {
outBinPrefix := ctx.Cfg.Targets.Common.BinName
for target, tConfig := range config.Targets {
buildArgs := xgolib.BuildArgs{
Verbose: typeutil.PtrValueOrDefault(config.XGoConfig.Verbose),
Race: typeutil.PtrValueOrDefault(tConfig.TargetBuildConfig.Race),
Tags: tConfig.TargetBuildConfig.Tags,
LdFlags: tConfig.TargetBuildConfig.LdFlags,
Mode: tConfig.TargetBuildConfig.Mode,
VCS: tConfig.TargetBuildConfig.VCS,
TrimPath: typeutil.PtrValueOrDefault(tConfig.TargetBuildConfig.TrimPath),
}
args := xgolib.Args{
Repository: config.RootPath,
SrcPackage: config.MainPkgRelPath,
OutFolder: config.BinTempDir,
OutPrefix: outBinPrefix,
Build: buildArgs,
Targets: []string{target.String()},
}
//ctx.Artifacts.AddAnonymous(&resource.TempDir{Path: binTmpDir})
logger := logging.NewBufferedLogger(ctx.Logger.WithPrefix(fmt.Sprintf("xgo-%s", target.String())))
err := xgolib.StartBuild(args, logger)
logger.Flush()
if err != nil {
return fmt.Errorf("error building '%s' target: %w", target.String(), err)
}
outBinPath := getXgoOutBinPath(config.BinTempDir, outBinPrefix, target)
if err := fsutil.RenameOrCopyFile(outBinPath, tConfig.OutBinPath, ctx.Logger); err != nil {
return fmt.Errorf(
"error moving xgo build result bin '%s' to '%s': %w. It may be a result of unsuccessful build",
outBinPath, tConfig.OutBinPath, err,
)
}
}
return nil
}
func getXgoOutBinPath(
binTmpDir string,
outPrefix string,
target config.Target,
) string {
res := path.Join(binTmpDir, fmt.Sprintf("%s-%s-%s", outPrefix, target.Os, target.Arch))
if target.Os == consts.OsWindows {
res += ".exe"
}
return res
}