Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable overriding build flags from environment #104

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
mholt marked this conversation as resolved.
Show resolved Hide resolved
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=