Skip to content

Commit ca5afc1

Browse files
authored
fix: Better build overrides (#1492)
Instead of passing `static` option we should allow for env var values overrides in the package call.
1 parent d177320 commit ca5afc1

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

plugin/options.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ func WithJSONSchema(schema string) Option {
2929
}
3030
}
3131

32-
func WithStaticLinking() Option {
33-
return func(p *Plugin) {
34-
p.staticLinking = true
35-
}
36-
}
37-
3832
func WithKind(kind string) Option {
3933
k := Kind(kind)
4034
err := k.Validate()

plugin/plugin.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ type Plugin struct {
6363
targets []BuildTarget
6464
// Called upon init call to validate and init configuration
6565
newClient NewClientFunc
66-
// staticLinking true if C libraries should be added to compiled executable
67-
staticLinking bool
6866
// Logger to call, this logger is passed to the serve.Serve Client, if not defined Serve will create one instead.
6967
logger zerolog.Logger
7068
// mu is a mutex that limits the number of concurrent init/syncs (can only be one at a time)
@@ -172,11 +170,6 @@ func (p *Plugin) OnSyncFinish(ctx context.Context) error {
172170
return nil
173171
}
174172

175-
// IsStaticLinkingEnabled whether static linking is to be enabled
176-
func (p *Plugin) IsStaticLinkingEnabled() bool {
177-
return p.staticLinking
178-
}
179-
180173
func (p *Plugin) Targets() []BuildTarget {
181174
return p.targets
182175
}

plugin/plugin_package.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,30 @@ const (
3434
)
3535

3636
type BuildTarget struct {
37-
OS string `json:"os"`
38-
Arch string `json:"arch"`
37+
OS string `json:"os"`
38+
Arch string `json:"arch"`
39+
CGO bool `json:"cgo"`
40+
Env []string `json:"env"`
41+
}
42+
43+
func (t BuildTarget) EnvVariables() []string {
44+
cgo := "CGO_ENABLED="
45+
if t.CGO {
46+
cgo += "1"
47+
} else {
48+
cgo += "0"
49+
}
50+
return append([]string{
51+
"GOOS=" + t.OS,
52+
"GOARCH=" + t.Arch,
53+
cgo, // default is to tool at the param. Can be overridden by adding `CGO_ENABLED=1` to BuildTarget.Env
54+
}, t.Env...)
3955
}
4056

4157
var DefaultBuildTargets = []BuildTarget{
42-
{GoOSLinux, GoArchAmd64},
43-
{GoOSLinux, GoArchArm64},
44-
{GoOSWindows, GoArchAmd64},
45-
{GoOSDarwin, GoArchAmd64},
46-
{GoOSDarwin, GoArchArm64},
58+
{OS: GoOSLinux, Arch: GoArchAmd64},
59+
{OS: GoOSLinux, Arch: GoArchArm64},
60+
{OS: GoOSWindows, Arch: GoArchAmd64},
61+
{OS: GoOSDarwin, Arch: GoArchAmd64},
62+
{OS: GoOSDarwin, Arch: GoArchArm64},
4763
}

serve/package.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,22 @@ func (s *PluginServe) writeTablesJSON(ctx context.Context, dir string) error {
107107
return os.WriteFile(outputPath, buffer.Bytes(), 0644)
108108
}
109109

110-
func (s *PluginServe) build(pluginDirectory, goos, goarch, distPath, pluginVersion string) (*TargetBuild, error) {
111-
pluginFileName := fmt.Sprintf("plugin-%s-%s-%s-%s", s.plugin.Name(), pluginVersion, goos, goarch)
110+
func (s *PluginServe) build(pluginDirectory string, target plugin.BuildTarget, distPath, pluginVersion string) (*TargetBuild, error) {
111+
pluginFileName := fmt.Sprintf("plugin-%s-%s-%s-%s", s.plugin.Name(), pluginVersion, target.OS, target.Arch)
112112
pluginPath := path.Join(distPath, pluginFileName)
113113
importPath, err := s.getModuleName(pluginDirectory)
114114
if err != nil {
115115
return nil, err
116116
}
117117
ldFlags := fmt.Sprintf("-s -w -X %[1]s/plugin.Version=%[2]s -X %[1]s/resources/plugin.Version=%[2]s", importPath, pluginVersion)
118-
if s.plugin.IsStaticLinkingEnabled() && strings.EqualFold(goos, plugin.GoOSLinux) {
119-
ldFlags += " -linkmode external -extldflags=-static"
120-
}
121118
args := []string{"build", "-o", pluginPath}
122119
args = append(args, "-buildmode=exe")
123120
args = append(args, "-ldflags", ldFlags)
124121
cmd := exec.Command("go", args...)
125122
cmd.Dir = pluginDirectory
126123
cmd.Stdout = os.Stdout
127124
cmd.Stderr = os.Stderr
128-
cmd.Env = os.Environ()
129-
cmd.Env = append(cmd.Env, fmt.Sprintf("GOOS=%s", goos))
130-
cmd.Env = append(cmd.Env, fmt.Sprintf("GOARCH=%s", goarch))
131-
cmd.Env = append(cmd.Env, fmt.Sprintf("CGO_ENABLED=%v", getEnvOrDefault("CGO_ENABLED", "0"))) // default to CGO_ENABLED=0
125+
cmd.Env = append(os.Environ(), target.EnvVariables()...)
132126
if err := cmd.Run(); err != nil {
133127
return nil, fmt.Errorf("failed to build plugin with `go %v`: %w", args, err)
134128
}
@@ -178,8 +172,8 @@ func (s *PluginServe) build(pluginDirectory, goos, goarch, distPath, pluginVersi
178172
}
179173

180174
return &TargetBuild{
181-
OS: goos,
182-
Arch: goarch,
175+
OS: target.OS,
176+
Arch: target.Arch,
183177
Path: targetZip,
184178
Checksum: "sha256:" + checksum,
185179
}, nil
@@ -421,7 +415,7 @@ func (s *PluginServe) newCmdPluginPackage() *cobra.Command {
421415
targets := []TargetBuild{}
422416
for _, target := range s.plugin.Targets() {
423417
fmt.Println("Building for OS: " + target.OS + ", ARCH: " + target.Arch)
424-
targetBuild, err := s.build(pluginDirectory, target.OS, target.Arch, distPath, pluginVersion)
418+
targetBuild, err := s.build(pluginDirectory, target, distPath, pluginVersion)
425419
if err != nil {
426420
return fmt.Errorf("failed to build plugin for %s/%s: %w", target.OS, target.Arch, err)
427421
}

0 commit comments

Comments
 (0)