Skip to content

Commit ad8b4c6

Browse files
committed
feat: Add support for build tags
Signed-off-by: Achal Shah <[email protected]>
1 parent 1daf6fb commit ad8b4c6

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

cmd_build.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ex:
4545
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
4646
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")
4747
cmd.Flag.Bool("dynamic-link", false, "whether to link output shared library dynamically to Python")
48+
cmd.Flag.String("build-tags", "", "build tags to be passed to `go build`")
4849
return cmd
4950
}
5051

@@ -66,6 +67,7 @@ func gopyRunCmdBuild(cmdr *commander.Command, args []string) error {
6667
cfg.NoWarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
6768
cfg.NoMake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
6869
cfg.DynamicLinking = cmdr.Flag.Lookup("dynamic-link").Value.Get().(bool)
70+
cfg.BuildTags = cmdr.Flag.Lookup("build-tags").Value.Get().(string)
6971

7072
bind.NoWarn = cfg.NoWarn
7173
bind.NoMake = cfg.NoMake
@@ -129,7 +131,12 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error {
129131
cmd = exec.Command(cfg.VM, "build.py")
130132
cmd.Run() // will fail, we don't care about errors
131133

132-
args := []string{"build", "-mod=mod", "-buildmode=c-shared", "-o", buildname + libExt, "."}
134+
args := []string{"build", "-mod=mod", "-buildmode=c-shared"}
135+
if cfg.BuildTags != "" {
136+
args = append(args, "-tags", cfg.BuildTags)
137+
}
138+
args = append(args, "-o", buildname+libExt, ".")
139+
133140
fmt.Printf("go %v\n", strings.Join(args, " "))
134141
cmd = exec.Command("go", args...)
135142
cmdout, err = cmd.CombinedOutput()
@@ -149,7 +156,11 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error {
149156
err = os.Remove(cfg.Name + "_go" + libExt)
150157

151158
fmt.Printf("go build -o py%s\n", cfg.Name)
152-
cmd = exec.Command("go", "build", "-mod=mod", "-o", "py"+cfg.Name)
159+
cmd = exec.Command("go", "build", "-mod=mod")
160+
if cfg.BuildTags != "" {
161+
args = append(args, "-tags", cfg.BuildTags)
162+
}
163+
args = append(args, "-o", "py"+cfg.Name)
153164
cmdout, err = cmd.CombinedOutput()
154165
if err != nil {
155166
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
@@ -170,6 +181,9 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error {
170181
// build the go shared library upfront to generate the header
171182
// needed by our generated cpython code
172183
args := []string{"build", "-mod=mod", "-buildmode=c-shared"}
184+
if cfg.BuildTags != "" {
185+
args = append(args, "-tags", cfg.BuildTags)
186+
}
173187
if !cfg.Symbols {
174188
// These flags will omit the various symbol tables, thereby
175189
// reducing the final size of the binary. From https://golang.org/cmd/link/

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type BuildCfg struct {
3030
NoMake bool
3131
// link resulting library dynamically
3232
DynamicLinking bool
33+
// BuildTags to be passed into `go build`.
34+
BuildTags string
3335
}
3436

3537
// NewBuildCfg returns a newly constructed build config

0 commit comments

Comments
 (0)