Skip to content

Commit

Permalink
Optionally skip cleanup; fix cgo when race detector enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Jul 15, 2020
1 parent 8266ddc commit 3742b72
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Builder struct {
TimeoutGet time.Duration `json:"timeout_get,omitempty"`
TimeoutBuild time.Duration `json:"timeout_build,omitempty"`
RaceDetector bool `json:"race_detector,omitempty"`
SkipCleanup bool `json:"skip_cleanup,omitempty"`
}

// Build builds Caddy at the configured version with the
Expand Down Expand Up @@ -83,6 +84,10 @@ 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)
if b.RaceDetector && !b.Compile.Cgo {
log.Println("[WARNING] Enabling cgo because it is required by the race detector")
b.Compile.Cgo = true
}
env = setEnv(env, fmt.Sprintf("CGO_ENABLED=%s", b.Compile.CgoEnabled()))

log.Println("[INFO] Building Caddy")
Expand Down
10 changes: 10 additions & 0 deletions cmd/xcaddy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
var (
caddyVersion = os.Getenv("CADDY_VERSION")
raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1"
skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1"
)

func main() {
Expand Down Expand Up @@ -117,6 +118,7 @@ func runBuild(ctx context.Context, args []string) error {
Plugins: plugins,
Replacements: replacements,
RaceDetector: raceDetector,
SkipCleanup: skipCleanup,
}
err := builder.Build(ctx, output)
if err != nil {
Expand Down Expand Up @@ -204,12 +206,16 @@ func runDev(ctx context.Context, args []string) error {

// build caddy with this module plugged in
builder := xcaddy.Builder{
Compile: xcaddy.Compile{
Cgo: os.Getenv("CGO_ENABLED") == "1",
},
CaddyVersion: caddyVersion,
Plugins: []xcaddy.Dependency{
{ModulePath: importPath},
},
Replacements: replacements,
RaceDetector: raceDetector,
SkipCleanup: skipCleanup,
}
err = builder.Build(ctx, binOutput)
if err != nil {
Expand All @@ -227,6 +233,10 @@ func runDev(ctx context.Context, args []string) error {
return err
}
defer func() {
if skipCleanup {
log.Printf("[INFO] Skipping cleanup as requested; leaving artifact: %s", binOutput)
return
}
err = os.Remove(binOutput)
if err != nil && !os.IsNotExist(err) {
log.Printf("[ERROR] Deleting temporary binary %s: %v", binOutput, err)
Expand Down
6 changes: 6 additions & 0 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
caddyModulePath: caddyModulePath,
tempFolder: tempFolder,
timeoutGoGet: b.TimeoutGet,
skipCleanup: b.SkipCleanup,
}

// initialize the go module
Expand Down Expand Up @@ -165,11 +166,16 @@ type environment struct {
caddyModulePath string
tempFolder string
timeoutGoGet time.Duration
skipCleanup bool
}

// Close cleans up the build environment, including deleting
// the temporary folder from the disk.
func (env environment) Close() error {
if env.skipCleanup {
log.Printf("[INFO] Skipping cleanup as requested; leaving folder intact: %s", env.tempFolder)
return nil
}
log.Printf("[INFO] Cleaning up temporary folder: %s", env.tempFolder)
return os.RemoveAll(env.tempFolder)
}
Expand Down

0 comments on commit 3742b72

Please sign in to comment.