Skip to content

Commit

Permalink
Refactor build cache and drop build cache support for legacy XML builds
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Zhao <[email protected]>
  • Loading branch information
GZGavinZhao committed Aug 25, 2023
1 parent fd62ab2 commit 6e4c203
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 102 deletions.
109 changes: 26 additions & 83 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,22 @@ func (p *Package) CreateDirs(o *Overlay) error {
dirs := []string{
p.GetWorkDir(o),
p.GetSourceDir(o),
p.GetCcacheDir(o),
p.GetSccacheDir(o),
}
for _, p := range dirs {
if err := os.MkdirAll(p, 0o0755); err != nil {
return fmt.Errorf("Failed to create required directory %s. Reason: %w\n", p, err)
}
}

// Fix up the ccache directories
if p.Type == PackageTypeXML {
// Ensure we have root owned ccache/sccache
if err := os.MkdirAll(LegacyCcacheDirectory, 0o0755); err != nil {
return fmt.Errorf("Failed to create ccache directory %+v, reason: %w\n", p, err)
}

if err := os.MkdirAll(LegacySccacheDirectory, 0o0755); err != nil {
return fmt.Errorf("Failed to create sccache directory %+v, reason: %w\n", p, err)
}
} else {
// Ensure we have root owned ccache/sccache
if err := os.MkdirAll(CcacheDirectory, 0o0755); err != nil {
return fmt.Errorf("Failed to create ccache directory %+v, reason: %w\n", p, err)
}

if err := os.Chown(CcacheDirectory, BuildUserID, BuildUserGID); err != nil {
return fmt.Errorf("Failed to chown ccache directory %+v, reason: %w\n", p, err)
}

if err := os.MkdirAll(SccacheDirectory, 0o0755); err != nil {
return fmt.Errorf("Failed to create sccache directory %+v, reason: %w\n", p, err)
}

if err := os.Chown(SccacheDirectory, BuildUserID, BuildUserGID); err != nil {
return fmt.Errorf("Failed to chown sccache directory %+v, reason: %w\n", p, err)
// Ensure we have root owned cache directories.
//
// Currently we are only using build caches for YPKG builds to reduce
// maintenance burden.
if p.Type == PackageTypeYpkg {
for _, cache := range Caches {
if err := os.MkdirAll(cache.CacheDir, 0o0755); err != nil {
return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w\n", cache.CacheDir, cache.Name, err)
}
}
}

Expand Down Expand Up @@ -134,51 +115,28 @@ func (p *Package) BindSources(o *Overlay) error {
return nil
}

// BindCcache will make the ccache directory available to the build.
func (p *Package) BindCcache(o *Overlay) error {
// BindCache will make all cache defined in [caches] available to the build
func (p *Package) BindCaches(o *Overlay) error {
mountMan := disk.GetMountManager()
ccacheDir := p.GetCcacheDir(o)

var ccacheSource string
if p.Type == PackageTypeXML {
ccacheSource = LegacyCcacheDirectory
} else {
ccacheSource = CcacheDirectory
}

log.Debugf("Exposing ccache to build %s\n", ccacheDir)

// Bind mount local ccache into chroot
if err := mountMan.BindMount(ccacheSource, ccacheDir); err != nil {
return fmt.Errorf("Failed to bind mount ccache %s, reason: %w\n", ccacheDir, err)
}

o.ExtraMounts = append(o.ExtraMounts, ccacheDir)
for _, c := range Caches {
var cacheSource string
var cacheDir string

return nil
}

// BindSccache will make the sccache directory available to the build.
func (p *Package) BindSccache(o *Overlay) error {
mountMan := disk.GetMountManager()
sccacheDir := p.GetSccacheDir(o)

var sccacheSource string
if p.Type == PackageTypeXML {
sccacheSource = LegacySccacheDirectory
} else {
sccacheSource = SccacheDirectory
}
if p.Type == PackageTypeYpkg {
cacheSource = filepath.Join(CacheDirectory, c.Name, "ypkg")
cacheDir = filepath.Join(o.MountPoint, c.CacheDir[1:])
}

log.Debugf("Exposing sccache to build %s\n", sccacheDir)
log.Debugf("Exposing %s to build %s\n", c.Name, cacheDir)

// Bind mount local sccache into chroot
if err := mountMan.BindMount(sccacheSource, sccacheDir); err != nil {
return fmt.Errorf("Failed to bind mount sccache %s, reason: %w\n", sccacheDir, err)
// Bind mount local ccache into chroot
if err := mountMan.BindMount(cacheSource, cacheDir); err != nil {
return fmt.Errorf("Failed to bind mount %s %s, reason: %s\n", c.Name, cacheDir, err)
}
o.ExtraMounts = append(o.ExtraMounts, cacheDir)
}

o.ExtraMounts = append(o.ExtraMounts, sccacheDir)

return nil
}

Expand Down Expand Up @@ -371,13 +329,8 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager
return err
}

// Ensure we have ccache available
if err := p.BindCcache(overlay); err != nil {
return err
}

// Ensure we have sccache available
if err := p.BindSccache(overlay); err != nil {
// Ensure we have build caches available
if err := p.BindCaches(overlay); err != nil {
return err
}

Expand Down Expand Up @@ -434,16 +387,6 @@ func (p *Package) BuildXML(notif PidNotifier, pman *EopkgManager, overlay *Overl
return fmt.Errorf("Cannot continue without sources.\n")
}

// Ensure we have ccache available
if err := p.BindCcache(overlay); err != nil {
return err
}

// Ensure we have ccache available
if err := p.BindSccache(overlay); err != nil {
return err
}

// Now recopy the assets prior to build
if err := pman.CopyAssets(); err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions builder/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package builder

import (
"path"
)

var (
Ccache = Cache{
Name: "ccache",
CacheDir: path.Join(BuildUserHome, ".ccache"),
}

Sccache = Cache{
Name: "sccache",
CacheDir: path.Join(BuildUserHome, ".cache", "sccache"),
}

Caches = []Cache{Ccache, Sccache}
)

type Cache struct {
Name string
CacheDir string // CacheDir is the chroot-internal cache directory.
}
21 changes: 10 additions & 11 deletions builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ const (
// PackageCacheDirectory is where we share packages between all builders.
PackageCacheDirectory = "/var/lib/solbuild/packages"

// CcacheDirectory is the system wide ccache directory.
CcacheDirectory = "/var/lib/solbuild/ccache/ypkg"

// LegacyCcacheDirectory is the root owned ccache directory for pspec.xml.
LegacyCcacheDirectory = "/var/lib/solbuild/ccache/legacy"

// SccacheDirectory is the root owned sccache directory.
SccacheDirectory = "/var/lib/solbuild/sccache/ypkg"

// LegacySccacheDirectory is the root owned ccache directory for pspec.xml.
LegacySccacheDirectory = "/var/lib/solbuild/sccache/legacy"
// CacheDirectory is where packages' build cache are stored
CacheDirectory = "/var/lib/solbuild/cache"

// Obsolete cache directories. These are only still specified so that the
// `delete-cache -a` subcommand will remove them. In the future they will
// be removed.
ObsoleteCcacheDirectory = "/var/lib/solbuild/ccache/ypkg"
ObsoleteLegacyCcacheDirectory = "/var/lib/solbuild/ccache/legacy"
ObsoleteSccacheDirectory = "/var/lib/solbuild/sccache/ypkg"
ObsoleteLegacySccacheDirectory = "/var/lib/solbuild/sccache/legacy"
)

const (
Expand Down
18 changes: 10 additions & 8 deletions cli/delete_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) {
if sFlags.Sizes {
sizeDirs := []string{
manager.Config.OverlayRootDir,
builder.CcacheDirectory,
builder.LegacyCcacheDirectory,
builder.SccacheDirectory,
builder.LegacySccacheDirectory,
builder.CacheDirectory,
builder.ObsoleteCcacheDirectory,
builder.ObsoleteSccacheDirectory,
builder.ObsoleteLegacyCcacheDirectory,
builder.ObsoleteLegacySccacheDirectory,
builder.PackageCacheDirectory,
source.SourceDir,
}
Expand Down Expand Up @@ -109,10 +110,11 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) {
}
if sFlags.All {
nukeDirs = append(nukeDirs, []string{
builder.CcacheDirectory,
builder.LegacyCcacheDirectory,
builder.SccacheDirectory,
builder.LegacySccacheDirectory,
builder.CacheDirectory,
builder.ObsoleteCcacheDirectory,
builder.ObsoleteSccacheDirectory,
builder.ObsoleteLegacyCcacheDirectory,
builder.ObsoleteLegacySccacheDirectory,
builder.PackageCacheDirectory,
source.SourceDir,
}...)
Expand Down

0 comments on commit 6e4c203

Please sign in to comment.