Skip to content

Commit

Permalink
azlinux: add go mod cache when building rpms
Browse files Browse the repository at this point in the history
This offers a tremendous speedup for local development.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Aug 30, 2024
1 parent 39222f7 commit 387ca5c
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions frontend/azlinux/handle_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ func hasGolangBuildDep(spec *dalec.Spec, targetKey string) bool {
return false
}

func platformOrDefault(p *ocispecs.Platform) ocispecs.Platform {
if p == nil {
return platforms.DefaultSpec()
}
return *p
}

func createRPM(w worker, sOpt dalec.SourceOpts, spec *dalec.Spec, targetKey string, platform *ocispecs.Platform, opts ...llb.ConstraintsOpt) (llb.State, error) {
br, err := createBuildroot(w, sOpt, spec, targetKey, opts...)
if err != nil {
Expand All @@ -219,13 +226,23 @@ func createRPM(w worker, sOpt dalec.SourceOpts, spec *dalec.Spec, targetKey stri
}

var runOpts []llb.RunOption
if !platformFuzzyMatches(platform) && hasGolangBuildDep(spec, targetKey) {
native, err := rpmWorker(w, sOpt, spec, targetKey, nil, opts...)
if err != nil {
return llb.Scratch(), err
if hasGolangBuildDep(spec, targetKey) {
if !platformFuzzyMatches(platform) {
native, err := rpmWorker(w, sOpt, spec, targetKey, nil, opts...)
if err != nil {
return llb.Scratch(), err
}

runOpts = append(runOpts, nativeGoMount(native, platform))
}

runOpts = append(runOpts, nativeGoMount(native, platform))
const goCacheDir = "/tmp/dalec/internal/gocache"
runOpts = append(runOpts, llb.AddEnv("GOCACHE", goCacheDir))

// Unfortunately, go cannot invalidate caches for cgo (rather, cgo with 'include' directives).
// As such we need to include the platform in our cache key.
cacheKey := targetKey + "-golang-" + platforms.Format(platformOrDefault(platform))
runOpts = append(runOpts, llb.AddMount(goCacheDir, llb.Scratch(), llb.AsPersistentCacheDir(cacheKey, llb.CacheMountShared)))
}

specPath := filepath.Join("SPECS", spec.Name, spec.Name+".spec")
Expand Down

0 comments on commit 387ca5c

Please sign in to comment.