Skip to content

Commit

Permalink
super fast crossbuilds
Browse files Browse the repository at this point in the history
  • Loading branch information
shmsr committed Sep 27, 2024
1 parent 307e95c commit e1836ef
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 140 deletions.
8 changes: 3 additions & 5 deletions dev-tools/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"go/build"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -124,8 +123,7 @@ func DefaultGolangCrossBuildArgs() BuildArgs {
// environment.
func GolangCrossBuild(params BuildArgs) error {
if os.Getenv("GOLANG_CROSSBUILD") != "1" {
return errors.New("Use the crossBuild target. golangCrossBuild can " +
"only be executed within the golang-crossbuild docker environment.")
return errors.New("use the crossBuild target. golangCrossBuild can only be executed within the golang-crossbuild docker environment")
}

defer DockerChown(filepath.Join(params.OutputDir, params.Name+binaryExtension(GOOS)))
Expand Down Expand Up @@ -206,15 +204,15 @@ func Build(params BuildArgs) error {
}

if GOOS == "windows" && params.WinMetadata {
log.Println("Generating a .syso containing Windows file metadata.")
fmt.Println("Generating a .syso containing Windows file metadata.")
syso, err := MakeWindowsSysoFile()
if err != nil {
return fmt.Errorf("failed generating Windows .syso metadata file: %w", err)
}
defer os.Remove(syso)
}

log.Println("Adding build environment vars:", env)
fmt.Println("Adding build environment vars:", env)
return sh.RunWith(env, "go", args...)
}

Expand Down
2 changes: 1 addition & 1 deletion dev-tools/mage/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func checkDashboardForErrors(file string, d []byte) bool {
var dashboard DashboardObject
err := json.Unmarshal(d, &dashboard)
if err != nil {
fmt.Println(fmt.Sprintf("failed to parse dashboard from %s: %s", file, err))
fmt.Printf("failed to parse dashboard from %s: %s\n", file, err)
return true
}

Expand Down
3 changes: 1 addition & 2 deletions dev-tools/mage/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func Clean(pathLists ...[]string) error {
if err := sh.Rm(f); err != nil {
if errors.Is(err, os.ErrPermission) ||
strings.Contains(err.Error(), "permission denied") {
fmt.Printf("warn: cannot delete %q: %v, proceeding anyway\n",
f, err)
fmt.Printf("warn: cannot delete %q: %v, proceeding anyway\n", f, err)
continue
}
return err
Expand Down
69 changes: 27 additions & 42 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,15 @@ func Tar(src string, targetFile string) error {
func untar(sourceFile, destinationDir string) error {
file, err := os.Open(sourceFile)
if err != nil {
return err
return fmt.Errorf("failed to open source file: %w", err)
}
defer file.Close()

var fileReader io.ReadCloser = file

if strings.HasSuffix(sourceFile, ".gz") {
if fileReader, err = gzip.NewReader(file); err != nil {
return err
return fmt.Errorf("failed to create gzip reader: %w", err)
}
defer fileReader.Close()
}
Expand All @@ -480,38 +480,31 @@ func untar(sourceFile, destinationDir string) error {
if err == io.EOF {
break
}
return err
return fmt.Errorf("error reading tar: %w", err)
}

path := filepath.Join(destinationDir, header.Name)
if !strings.HasPrefix(path, destinationDir) {
if !strings.HasPrefix(path, filepath.Clean(destinationDir)+string(os.PathSeparator)) {
return fmt.Errorf("illegal file path in tar: %v", header.Name)
}

switch header.Typeflag {
case tar.TypeDir:
if err = os.MkdirAll(path, os.FileMode(header.Mode)); err != nil {
return err
return fmt.Errorf("failed to create directory: %w", err)
}
case tar.TypeReg:
writer, err := os.Create(path)
writer, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(header.Mode))
if err != nil {
return err
}

if _, err = io.Copy(writer, tarReader); err != nil {
return err
}

if err = os.Chmod(path, os.FileMode(header.Mode)); err != nil {
return err
return fmt.Errorf("failed to create file: %w", err)
}

if err = writer.Close(); err != nil {
return err
_, err = io.Copy(writer, tarReader)
writer.Close()
if err != nil {
return fmt.Errorf("failed to write file contents: %w", err)
}
default:
return fmt.Errorf("unable to untar type=%c in file=%s", header.Typeflag, path)
return fmt.Errorf("unsupported tar entry type: %c for file: %s", header.Typeflag, path)
}
}

Expand Down Expand Up @@ -585,7 +578,7 @@ func ParallelCtx(ctx context.Context, fns ...interface{}) {
}

var mu sync.Mutex
var errs []string
var errs []error
var wg sync.WaitGroup

for _, fw := range fnWrappers {
Expand All @@ -594,26 +587,26 @@ func ParallelCtx(ctx context.Context, fns ...interface{}) {
defer func() {
if v := recover(); v != nil {
mu.Lock()
errs = append(errs, fmt.Sprint(v))
errs = append(errs, fmt.Errorf("%s", v))
mu.Unlock()
}
wg.Done()
<-parallelJobs()
}()
waitStart := time.Now()
parallelJobs() <- 1
log.Println("Parallel job waited", time.Since(waitStart), "before starting.")
fmt.Printf("Parallel job waited %v before starting.\n", time.Since(waitStart))
if err := fw(ctx); err != nil {
mu.Lock()
errs = append(errs, fmt.Sprint(err))
errs = append(errs, err)
mu.Unlock()
}
}(fw)
}

wg.Wait()
if len(errs) > 0 {
panic(fmt.Errorf(strings.Join(errs, "\n")))
panic(errors.Join(errs...))
}
}

Expand Down Expand Up @@ -675,7 +668,6 @@ func FindFilesRecursive(match func(path string, info os.FileInfo) bool) ([]strin
}

if !info.Mode().IsRegular() {
// continue
return nil
}

Expand All @@ -696,31 +688,25 @@ func FileConcat(out string, perm os.FileMode, files ...string) error {
defer f.Close()

w := bufio.NewWriter(f)
defer w.Flush()

append := func(file string) error {
for _, file := range files {
in, err := os.Open(file)
if err != nil {
return err
return fmt.Errorf("failed to open input file %s: %w", file, err)
}
defer in.Close()

if _, err := io.Copy(w, in); err != nil {
return err
return fmt.Errorf("failed to copy from %s: %w", file, err)
}

return nil
}

for _, in := range files {
if err := append(in); err != nil {
return err
}
if err := w.Flush(); err != nil {
return fmt.Errorf("failed to flush writer: %w", err)
}

if err = w.Flush(); err != nil {
return err
}
return f.Close()
return nil
}

// MustFileConcat invokes FileConcat and panics if an error occurs.
Expand Down Expand Up @@ -748,11 +734,10 @@ func VerifySHA256(file string, hash string) error {
expectedHash := strings.TrimSpace(hash)

if computedHash != expectedHash {
return fmt.Errorf("SHA256 verification of %v failed. Expected=%v, "+
"but computed=%v", f.Name(), expectedHash, computedHash)
return fmt.Errorf("SHA256 verification of %v failed. Expected=%v, computed=%v", f.Name(), expectedHash, computedHash)
}
log.Println("SHA256 OK:", f.Name())

fmt.Printf("SHA256 OK: %s\n", f.Name())
return nil
}

Expand All @@ -773,7 +758,7 @@ func CreateSHA512File(file string) error {
computedHash := hex.EncodeToString(sum.Sum(nil))
out := fmt.Sprintf("%v %v", computedHash, filepath.Base(file))

return ioutil.WriteFile(file+".sha512", []byte(out), 0644)
return os.WriteFile(file+".sha512", []byte(out), 0644)
}

// Mage executes mage targets in the specified directory.
Expand Down
13 changes: 6 additions & 7 deletions dev-tools/mage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -136,7 +135,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
confFile = confParams.Docker
tmplParams = map[string]interface{}{"Docker": true}
default:
panic(fmt.Errorf("Invalid config file type: %v", typ))
panic(fmt.Errorf("invalid config file type: %v", typ))
}

// Build the dependencies.
Expand Down Expand Up @@ -196,7 +195,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
}
}

data, err := ioutil.ReadFile(confFile.Template)
data, err := os.ReadFile(confFile.Template)
if err != nil {
return fmt.Errorf("failed to read config template %q: %w", confFile.Template, err)
}
Expand Down Expand Up @@ -265,7 +264,7 @@ type moduleFieldsYmlData []struct {
}

func readModuleFieldsYml(path string) (title string, useShort bool, err error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return "", false, err
}
Expand Down Expand Up @@ -302,7 +301,7 @@ func moduleDashes(name string) string {
func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {
var moduleConfigs []moduleConfigTemplateData
for _, dir := range moduleDirs {
modules, err := ioutil.ReadDir(dir)
modules, err := os.ReadDir(dir)
if err != nil {
return err
}
Expand All @@ -327,7 +326,7 @@ func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {

var data []byte
for _, f := range files {
data, err = ioutil.ReadFile(f)
data, err = os.ReadFile(f)
if err != nil {
if os.IsNotExist(err) {
continue
Expand Down Expand Up @@ -365,5 +364,5 @@ func GenerateModuleReferenceConfig(out string, moduleDirs ...string) error {
"Modules": moduleConfigs,
})

return ioutil.WriteFile(createDir(out), []byte(config), 0644)
return os.WriteFile(createDir(out), []byte(config), 0644)
}
Loading

0 comments on commit e1836ef

Please sign in to comment.