Skip to content

Commit 7c27065

Browse files
authored
feat: Add support for conditional static linking of C lib to builds (#1292)
1 parent 7c634dc commit 7c27065

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

plugin/options.go

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

32+
func WithStaticLinking() Option {
33+
return func(p *Plugin) {
34+
p.staticLinking = true
35+
}
36+
}
37+
3238
type TableOptions struct {
3339
Tables []string
3440
SkipTables []string

plugin/plugin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type Plugin struct {
5757
targets []BuildTarget
5858
// Called upon init call to validate and init configuration
5959
newClient NewClientFunc
60+
// staticLinking true if C libraries should be added to compiled executable
61+
staticLinking bool
6062
// Logger to call, this logger is passed to the serve.Serve Client, if not defined Serve will create one instead.
6163
logger zerolog.Logger
6264
// 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 {
108110
return p.version
109111
}
110112

113+
// IsStaticLinkingEnabled whether static linking is to be enabled
114+
func (p *Plugin) IsStaticLinkingEnabled() bool {
115+
return p.staticLinking
116+
}
117+
111118
func (p *Plugin) Targets() []BuildTarget {
112119
return p.targets
113120
}

serve/package.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,17 @@ func (s *PluginServe) writeTablesJSON(ctx context.Context, dir string) error {
105105
func (s *PluginServe) build(pluginDirectory, goos, goarch, distPath, pluginVersion string) (*TargetBuild, error) {
106106
pluginName := fmt.Sprintf("plugin-%s-%s-%s-%s", s.plugin.Name(), pluginVersion, goos, goarch)
107107
pluginPath := path.Join(distPath, pluginName)
108-
args := []string{"build", "-o", pluginPath}
109108
importPath, err := s.getModuleName(pluginDirectory)
110109
if err != nil {
111110
return nil, err
112111
}
112+
ldFlags := fmt.Sprintf("-s -w -X %s/plugin.Version=%s", importPath, pluginVersion)
113+
if s.plugin.IsStaticLinkingEnabled() {
114+
ldFlags += " -linkmode external -extldflags=-static"
115+
}
116+
args := []string{"build", "-o", pluginPath}
113117
args = append(args, "-buildmode=exe")
114-
args = append(args, "-ldflags", fmt.Sprintf("-s -w -X %s/plugin.Version=%s", importPath, pluginVersion))
118+
args = append(args, "-ldflags", ldFlags)
115119
cmd := exec.Command("go", args...)
116120
cmd.Dir = pluginDirectory
117121
cmd.Stdout = os.Stdout

0 commit comments

Comments
 (0)