Skip to content

Commit

Permalink
Add XCADDY_GO_BUILD_FLAGS env var (caddyserver#104)
Browse files Browse the repository at this point in the history
Add environment variable

 XCADDY_GO_BUILD_FLAGS

to override default build arguments from environment (caddyserver#102).

To support flags with variable arguments Unix-style quoting is supported.
  • Loading branch information
aabelix authored Jun 20, 2022
1 parent a83fac1 commit 47f9ded
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Because the subcommands and flags are constrained to benefit rapid plugin protot
- `XCADDY_SKIP_BUILD=1` causes xcaddy to not compile the program, it is used in conjunction with build tools such as [GoReleaser](https://goreleaser.com). Implies `XCADDY_SKIP_CLEANUP=1`.
- `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting.
- `XCADDY_WHICH_GO` sets the go command to use when for example more then 1 version of go is installed.
- `XCADDY_GO_BUILD_FLAGS` overrides default build arguments. Supports Unix-style shell quoting, for example: XCADDY_GO_BUILD_FLAGS="-ldflags '-w s'".
---

© 2020 Matthew Holt
19 changes: 15 additions & 4 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/caddyserver/xcaddy/internal/utils"
"github.com/google/shlex"
)

// Builder can produce a custom Caddy build with the
Expand All @@ -45,6 +46,7 @@ type Builder struct {
SkipCleanup bool `json:"skip_cleanup,omitempty"`
SkipBuild bool `json:"skip_build,omitempty"`
Debug bool `json:"debug,omitempty"`
BuildFlags string `json:"build_flags,omitempty"`
}

// Build builds Caddy at the configured version with the
Expand Down Expand Up @@ -115,10 +117,19 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {
// support dlv
cmd.Args = append(cmd.Args, "-gcflags", "all=-N -l")
} else {
cmd.Args = append(cmd.Args,
"-ldflags", "-w -s", // trim debug symbols
"-trimpath",
)
if b.BuildFlags != "" {
// override build flags from environment if given
flags, err := shlex.Split(b.BuildFlags)
if err != nil {
log.Fatalf("[FATAL] Splitting arguments failed: %s", b.BuildFlags)
}
cmd.Args = append(cmd.Args, flags...)
} else {
cmd.Args = append(cmd.Args,
"-ldflags", "-w -s", // trim debug symbols
"-trimpath",
)
}
}

if b.RaceDetector {
Expand Down
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
skipBuild = os.Getenv("XCADDY_SKIP_BUILD") == "1"
skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1" || skipBuild
buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1"
buildFlags = os.Getenv("XCADDY_GO_BUILD_FLAGS")
)

func Main() {
Expand Down Expand Up @@ -134,6 +135,7 @@ func runBuild(ctx context.Context, args []string) error {
SkipBuild: skipBuild,
SkipCleanup: skipCleanup,
Debug: buildDebugOutput,
BuildFlags: buildFlags,
}
err := builder.Build(ctx, output)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
tempFolder: tempFolder,
timeoutGoGet: b.TimeoutGet,
skipCleanup: b.SkipCleanup,
buildFlags: b.BuildFlags,
}

// initialize the go module
Expand Down Expand Up @@ -177,6 +178,7 @@ type environment struct {
tempFolder string
timeoutGoGet time.Duration
skipCleanup bool
buildFlags string
}

// Close cleans up the build environment, including deleting
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/caddyserver/xcaddy

go 1.14

require github.com/Masterminds/semver/v3 v3.1.1
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=

0 comments on commit 47f9ded

Please sign in to comment.