Skip to content

Commit

Permalink
Start sccache server when ccache is enabled
Browse files Browse the repository at this point in the history
Start the sccache server so it is ready to be used by tools like Cargo.
This is a workaround for an issue where builds hang until the sccache server exits.
  • Loading branch information
silkeh committed Mar 30, 2024
1 parent 3dd024b commit efcff7b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager
cmd += fmt.Sprintf(" -t %v", h.GetLastVersionTimestamp())
}

if p.CanCCache {
// Start an sccache server to work around #87
StartSccache(overlay.MountPoint)
}

slog.Info("Now starting build", "package", p.Name)

if err := ChrootExec(notif, overlay.MountPoint, cmd); err != nil {
Expand Down
1 change: 0 additions & 1 deletion builder/eopkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ func readURIFile(path string) (string, error) {
if err != nil {
return "", err
}

defer fi.Close()

contents, err := io.ReadAll(fi)
Expand Down
7 changes: 6 additions & 1 deletion builder/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Package struct {
Path string // Path to the build spec
Sources []source.Source // Each package has 0 or more sources that we fetch
CanNetwork bool // Only applicable to ypkg builds
CanCCache bool // Flag to enable (s)ccache
}

// YmlPackage is a parsed ypkg build file.
Expand All @@ -71,6 +72,9 @@ type YmlPackage struct {
Release int `yaml:"release"`
Networking bool `yaml:"networking"` // If set to false (default) we disable networking in the build
Source []map[string]string `yaml:"source"`

// Disable (s)ccache for this build.
CCache bool `yaml:"ccache"`
}

// XMLUpdate represents an update in the package history.
Expand Down Expand Up @@ -209,7 +213,7 @@ func NewYmlPackage(path string) (*Package, error) {
func NewYmlPackageFromBytes(by []byte) (*Package, error) {
var err error

ypkg := &YmlPackage{Networking: false}
ypkg := &YmlPackage{Networking: false, CCache: true}
if err = yaml.Unmarshal(by, ypkg); err != nil {
return nil, err
}
Expand All @@ -220,6 +224,7 @@ func NewYmlPackageFromBytes(by []byte) (*Package, error) {
Release: ypkg.Release,
Type: PackageTypeYpkg,
CanNetwork: ypkg.Networking,
CanCCache: ypkg.CCache,
}

for _, row := range ypkg.Source {
Expand Down
19 changes: 19 additions & 0 deletions builder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package builder

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -225,6 +227,23 @@ func ChrootExecStdin(notif PidNotifier, dir, command string) error {
return c.Wait()
}

func StartSccache(dir string) {
var buf bytes.Buffer

c := exec.Command("chroot", dir, "/bin/su", BuildUser, "-c", "sccache --start-server")
c.Stdout = &buf
c.Stderr = &buf
c.Env = slices.Clone(ChrootEnvironment)
c.Env = append(c.Env, "SCCACHE_IDLE_TIMEOUT=0")
c.SysProcAttr = &syscall.SysProcAttr{Setsid: true}

slog.Debug("Starting sccache server")

if err := c.Run(); err != nil {
slog.Warn("Unable to start sccache server", "err", err, "output", buf.String())
}
}

// AddBuildUser will attempt to add the solbuild user & group if they've not
// previously been added
// Note this should be changed when Solus goes fully stateless for /etc/passwd.
Expand Down

0 comments on commit efcff7b

Please sign in to comment.