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"` TimeoutGet time.Duration `json:"timeout_get,omitempty"`
TimeoutBuild time.Duration `json:"timeout_build,omitempty"` TimeoutBuild time.Duration `json:"timeout_build,omitempty"`
RaceDetector bool `json:"race_detector,omitempty"` RaceDetector bool `json:"race_detector,omitempty"`
SkipCleanup bool `json:"skip_cleanup,omitempty"`
} }
// Build builds Caddy at the configured version with the // 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, "GOOS="+b.OS)
env = setEnv(env, "GOARCH="+b.Arch) env = setEnv(env, "GOARCH="+b.Arch)
env = setEnv(env, "GOARM="+b.ARM) 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())) env = setEnv(env, fmt.Sprintf("CGO_ENABLED=%s", b.Compile.CgoEnabled()))
log.Println("[INFO] Building Caddy") log.Println("[INFO] Building Caddy")

View file

@ -32,6 +32,7 @@ import (
var ( var (
caddyVersion = os.Getenv("CADDY_VERSION") caddyVersion = os.Getenv("CADDY_VERSION")
raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1" raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1"
skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1"
) )
func main() { func main() {
@ -117,6 +118,7 @@ func runBuild(ctx context.Context, args []string) error {
Plugins: plugins, Plugins: plugins,
Replacements: replacements, Replacements: replacements,
RaceDetector: raceDetector, RaceDetector: raceDetector,
SkipCleanup: skipCleanup,
} }
err := builder.Build(ctx, output) err := builder.Build(ctx, output)
if err != nil { if err != nil {
@ -204,12 +206,16 @@ func runDev(ctx context.Context, args []string) error {
// build caddy with this module plugged in // build caddy with this module plugged in
builder := xcaddy.Builder{ builder := xcaddy.Builder{
Compile: xcaddy.Compile{
Cgo: os.Getenv("CGO_ENABLED") == "1",
},
CaddyVersion: caddyVersion, CaddyVersion: caddyVersion,
Plugins: []xcaddy.Dependency{ Plugins: []xcaddy.Dependency{
{ModulePath: importPath}, {ModulePath: importPath},
}, },
Replacements: replacements, Replacements: replacements,
RaceDetector: raceDetector, RaceDetector: raceDetector,
SkipCleanup: skipCleanup,
} }
err = builder.Build(ctx, binOutput) err = builder.Build(ctx, binOutput)
if err != nil { if err != nil {
@ -227,6 +233,10 @@ func runDev(ctx context.Context, args []string) error {
return err return err
} }
defer func() { defer func() {
if skipCleanup {
log.Printf("[INFO] Skipping cleanup as requested; leaving artifact: %s", binOutput)
return
}
err = os.Remove(binOutput) err = os.Remove(binOutput)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
log.Printf("[ERROR] Deleting temporary binary %s: %v", binOutput, 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, caddyModulePath: caddyModulePath,
tempFolder: tempFolder, tempFolder: tempFolder,
timeoutGoGet: b.TimeoutGet, timeoutGoGet: b.TimeoutGet,
skipCleanup: b.SkipCleanup,
} }
// initialize the go module // initialize the go module
@ -165,11 +166,16 @@ type environment struct {
caddyModulePath string caddyModulePath string
tempFolder string tempFolder string
timeoutGoGet time.Duration timeoutGoGet time.Duration
skipCleanup bool
} }
// Close cleans up the build environment, including deleting // Close cleans up the build environment, including deleting
// the temporary folder from the disk. // the temporary folder from the disk.
func (env environment) Close() error { 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) log.Printf("[INFO] Cleaning up temporary folder: %s", env.tempFolder)
return os.RemoveAll(env.tempFolder) return os.RemoveAll(env.tempFolder)
} }