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
This commit is contained in:
Ggicci 2021-07-04 07:57:17 +08:00 committed by GitHub
parent 652d45d08e
commit 8c5d3f1cf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 7 deletions

View file

@ -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.

View file

@ -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")
}

View file

@ -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 {