From d709cba2c1014cac6fefeab17f02daef60ead761 Mon Sep 17 00:00:00 2001 From: Alex Palaistras Date: Thu, 5 Dec 2024 19:08:11 +0000 Subject: [PATCH] build: Append existing CFLAGS and LDFLAGS for CGO Underlying `go build` commands can pass custom `CFLAGS` and `LDFLAGS` to CGO invocations, which GoPy uses in order to pass some of its own command-line arguments to builds. However, there are cases where additional, build-specific parameters may need to be used (e.g. in the case where we're linking against a static library which itself links to other dynamic libraries, which aren't set as options in the packages themselves); this commit respects any existing uses of the `CGO_CFLAGS` and `CGO_LDFLAGS` environment variables, and appends their values to ones used internally by GoPy if needed. --- cmd_build.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd_build.go b/cmd_build.go index 7b57a7e6..d8fb85f6 100644 --- a/cmd_build.go +++ b/cmd_build.go @@ -235,6 +235,9 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error { if include, exists := os.LookupEnv("GOPY_INCLUDE"); exists { cflags = append(cflags, "-I"+filepath.ToSlash(include)) } + if oldcflags, exists := os.LookupEnv("CGO_CFLAGS"); exists { + cflags = append(cflags, oldcflags) + } var ldflags []string if cfg.DynamicLinking { ldflags = strings.Fields(strings.TrimSpace(pycfg.LdDynamicFlags)) @@ -250,6 +253,9 @@ func runBuild(mode bind.BuildMode, cfg *BuildCfg) error { if libname, exists := os.LookupEnv("GOPY_PYLIB"); exists { ldflags = append(ldflags, "-l"+filepath.ToSlash(libname)) } + if oldldflags, exists := os.LookupEnv("CGO_LDFLAGS"); exists { + ldflags = append(ldflags, oldldflags) + } removeEmpty := func(src []string) []string { o := make([]string, 0, len(src))