From de72f848b4e32645a84ab6631f635011dc2df68f Mon Sep 17 00:00:00 2001 From: Marcel Tyszkiewicz Date: Mon, 16 Oct 2023 10:24:32 +0200 Subject: [PATCH 1/2] Add support for conditional static linking of C lib to builds --- serve/package.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/serve/package.go b/serve/package.go index f377808c76..c56a1d6d02 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 getEnvOrDefault("STATIC_LINKING_ENABLED", "0") == "1" { + 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 From 50caf3a590e010875e45c0f14e4109eab691e017 Mon Sep 17 00:00:00 2001 From: Marcel Tyszkiewicz Date: Mon, 16 Oct 2023 11:07:24 +0200 Subject: [PATCH 2/2] Move ENV flag to Option --- plugin/options.go | 6 ++++++ plugin/plugin.go | 7 +++++++ serve/package.go | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) 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 c56a1d6d02..848e3ffed4 100644 --- a/serve/package.go +++ b/serve/package.go @@ -110,7 +110,7 @@ func (s *PluginServe) build(pluginDirectory, goos, goarch, distPath, pluginVersi return nil, err } ldFlags := fmt.Sprintf("-s -w -X %s/plugin.Version=%s", importPath, pluginVersion) - if getEnvOrDefault("STATIC_LINKING_ENABLED", "0") == "1" { + if s.plugin.IsStaticLinkingEnabled() { ldFlags += " -linkmode external -extldflags=-static" } args := []string{"build", "-o", pluginPath}