From a1e17ca9b98a2b0915a55091b97a8ad1b942279b Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 22 May 2020 10:31:34 -0600 Subject: [PATCH] Honor CGO_ENABLED environment variable (fix #17) --- builder.go | 2 +- cmd/xcaddy/main.go | 3 +++ platforms.go | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/builder.go b/builder.go index ed21912..24553ac 100644 --- a/builder.go +++ b/builder.go @@ -83,7 +83,7 @@ func (b Builder) Build(ctx context.Context, outputFile string) error { env = setEnv(env, "GOOS="+b.OS) env = setEnv(env, "GOARCH="+b.Arch) env = setEnv(env, "GOARM="+b.ARM) - env = setEnv(env, fmt.Sprintf("CGO_ENABLED=%t", b.Cgo)) + env = setEnv(env, fmt.Sprintf("CGO_ENABLED=%s", b.Compile.CgoEnabled())) log.Println("[INFO] Building Caddy") diff --git a/cmd/xcaddy/main.go b/cmd/xcaddy/main.go index 81ae3ed..d1fe9f0 100644 --- a/cmd/xcaddy/main.go +++ b/cmd/xcaddy/main.go @@ -109,6 +109,9 @@ func runBuild(ctx context.Context, args []string) error { // perform the build builder := xcaddy.Builder{ + Compile: xcaddy.Compile{ + Cgo: os.Getenv("CGO_ENABLED") == "1", + }, CaddyVersion: caddyVersion, Plugins: plugins, Replacements: replacements, diff --git a/platforms.go b/platforms.go index 94b3227..b336fa8 100644 --- a/platforms.go +++ b/platforms.go @@ -11,6 +11,15 @@ type Compile struct { Cgo bool `json:"cgo,omitempty"` } +// CgoEnabled returns "1" if c.Cgo is true, "0" otherwise. +// This is used for setting the CGO_ENABLED env variable. +func (c Compile) CgoEnabled() string { + if c.Cgo { + return "1" + } + return "0" +} + // Platform represents a build target. type Platform struct { OS string `json:"os,omitempty"`