diff --git a/plugin/options.go b/plugin/options.go index a96e64887f..a0c6ae9f2e 100644 --- a/plugin/options.go +++ b/plugin/options.go @@ -29,6 +29,12 @@ func WithJSONSchema(schema string) Option { } } +func WithStaticLinking() Option { + return func(p *Plugin) { + p.staticLinking = true + } +} + type TableOptions struct { Tables []string SkipTables []string diff --git a/plugin/plugin.go b/plugin/plugin.go index 3b96f8d026..366e1e2fc5 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -57,6 +57,8 @@ type Plugin struct { targets []BuildTarget // Called upon init call to validate and init configuration newClient NewClientFunc + // staticLinking true if C libraries should be added to compiled executable + staticLinking bool // Logger to call, this logger is passed to the serve.Serve Client, if not defined Serve will create one instead. logger zerolog.Logger // mu is a mutex that limits the number of concurrent init/syncs (can only be one at a time) @@ -108,6 +110,11 @@ func (p *Plugin) Version() string { return p.version } +// IsStaticLinkingEnabled whether static linking is to be enabled +func (p *Plugin) IsStaticLinkingEnabled() bool { + return p.staticLinking +} + func (p *Plugin) Targets() []BuildTarget { return p.targets } diff --git a/serve/package.go b/serve/package.go index f377808c76..848e3ffed4 100644 --- a/serve/package.go +++ b/serve/package.go @@ -105,13 +105,17 @@ func (s *PluginServe) writeTablesJSON(ctx context.Context, dir string) error { func (s *PluginServe) build(pluginDirectory, goos, goarch, distPath, pluginVersion string) (*TargetBuild, error) { pluginName := fmt.Sprintf("plugin-%s-%s-%s-%s", s.plugin.Name(), pluginVersion, goos, goarch) pluginPath := path.Join(distPath, pluginName) - args := []string{"build", "-o", pluginPath} importPath, err := s.getModuleName(pluginDirectory) if err != nil { return nil, err } + ldFlags := fmt.Sprintf("-s -w -X %s/plugin.Version=%s", importPath, pluginVersion) + if s.plugin.IsStaticLinkingEnabled() { + ldFlags += " -linkmode external -extldflags=-static" + } + args := []string{"build", "-o", pluginPath} args = append(args, "-buildmode=exe") - args = append(args, "-ldflags", fmt.Sprintf("-s -w -X %s/plugin.Version=%s", importPath, pluginVersion)) + args = append(args, "-ldflags", ldFlags) cmd := exec.Command("go", args...) cmd.Dir = pluginDirectory cmd.Stdout = os.Stdout