Optionally skip cleanup; fix cgo when race detector enabled

This commit is contained in:
Matthew Holt 2020-07-15 17:44:06 -06:00
parent 8266ddc40e
commit 3742b72df8
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
3 changed files with 21 additions and 0 deletions

View file

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

View file

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

View file

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