From 8c5d3f1cf635d27e9c1c6f9e62c2937dad8e34f4 Mon Sep 17 00:00:00 2001 From: Ggicci Date: Sun, 4 Jul 2021 07:57:17 +0800 Subject: [PATCH] Keep DWARF info by setting XCADDY_DEBUG=1 (#62) * feat: add --debug option to build a debug output * chore: renaming * chore: update README * fix: typo * feat: introduce env XCADDY_DEBUG * fix: remove unnecessary change --- README.md | 3 ++- builder.go | 10 ++++++++-- cmd/xcaddy/main.go | 11 +++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2ef6f8a..c4ec61a 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ $ xcaddy run $ xcaddy run --config caddy.json ``` -The race detector can be enabled by setting `XCADDY_RACE_DETECTOR=1`. +The race detector can be enabled by setting `XCADDY_RACE_DETECTOR=1`. The DWARF debug info can be enabled by setting `XCADDY_DEBUG=1`. ## Library usage @@ -135,6 +135,7 @@ Because the subcommands and flags are constrained to benefit rapid plugin protot - `CADDY_VERSION` sets the version of Caddy to build. - `XCADDY_RACE_DETECTOR=1` enables the Go race detector in the build. +- `XCADDY_DEBUG=1` enables the DWARF debug information in the build. - `XCADDY_SETCAP=1` will run `sudo setcap cap_net_bind_service=+ep` on the temporary binary before running it when in dev mode. - `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. diff --git a/builder.go b/builder.go index 351a136..8bfff23 100644 --- a/builder.go +++ b/builder.go @@ -43,6 +43,7 @@ type Builder struct { RaceDetector bool `json:"race_detector,omitempty"` SkipCleanup bool `json:"skip_cleanup,omitempty"` SkipBuild bool `json:"skip_build,omitempty"` + Debug bool `json:"debug,omitempty"` } // Build builds Caddy at the configured version with the @@ -108,9 +109,14 @@ func (b Builder) Build(ctx context.Context, outputFile string) error { // compile cmd := buildEnv.newCommand("go", "build", "-o", absOutputFile, - "-ldflags", "-w -s", // trim debug symbols - "-trimpath", ) + if !b.Debug { + cmd.Args = append(cmd.Args, + "-ldflags", "-w -s", // trim debug symbols + "-trimpath", + ) + } + if b.RaceDetector { cmd.Args = append(cmd.Args, "-race") } diff --git a/cmd/xcaddy/main.go b/cmd/xcaddy/main.go index 0be38b5..86550ad 100644 --- a/cmd/xcaddy/main.go +++ b/cmd/xcaddy/main.go @@ -30,10 +30,11 @@ import ( ) var ( - caddyVersion = os.Getenv("CADDY_VERSION") - raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1" - skipBuild = os.Getenv("XCADDY_SKIP_BUILD") == "1" - skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1" + caddyVersion = os.Getenv("CADDY_VERSION") + raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1" + skipBuild = os.Getenv("XCADDY_SKIP_BUILD") == "1" + skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1" + buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1" ) func main() { @@ -122,6 +123,7 @@ func runBuild(ctx context.Context, args []string) error { RaceDetector: raceDetector, SkipBuild: skipBuild, SkipCleanup: skipCleanup, + Debug: buildDebugOutput, } err := builder.Build(ctx, output) if err != nil { @@ -228,6 +230,7 @@ func runDev(ctx context.Context, args []string) error { RaceDetector: raceDetector, SkipBuild: skipBuild, SkipCleanup: skipCleanup, + Debug: buildDebugOutput, } err = builder.Build(ctx, binOutput) if err != nil {