Skip to content

Commit

Permalink
Merge pull request #695 from jonjohnsonjr/skip-wrong-arch
Browse files Browse the repository at this point in the history
Skip packages for different arch
  • Loading branch information
imjasonh committed Mar 12, 2024
2 parents a895ac4 + 9e755a4 commit b8c5277
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 23 deletions.
61 changes: 38 additions & 23 deletions pkg/cli/build.go
Expand Up @@ -126,16 +126,6 @@ func buildArch(ctx context.Context, cfg *global, arch string, args []string) err
return fmt.Errorf("creating buildlogs directory: %w", err)
}

newTask := func(pkg string) *task {
return &task{
cfg: cfg,
pkg: pkg,
arch: arch,
cond: sync.NewCond(&sync.Mutex{}),
deps: map[string]*task{},
}
}

// We want to ignore info level here during setup, but further down below we pull whatever was passed to use via ctx.
log := clog.New(charmlog.NewWithOptions(os.Stderr, charmlog.Options{ReportTimestamp: true, Level: charmlog.WarnLevel}))
setupCtx := clog.WithLogger(ctx, log)
Expand All @@ -145,6 +135,12 @@ func buildArch(ctx context.Context, cfg *global, arch string, args []string) err
if err != nil {
return err
}

pkgs, err = pkgs.WithArch(arch)
if err != nil {
return fmt.Errorf("arch: %w", err)
}

g, err := dag.NewGraph(setupCtx, pkgs, dag.WithKeys(cfg.extraKeys...), dag.WithRepos(cfg.extraRepos...), dag.WithArch(arch))
if err != nil {
return err
Expand All @@ -168,6 +164,27 @@ func buildArch(ctx context.Context, cfg *global, arch string, args []string) err
return err
}

newTask := func(pkg string) *task {
// We should only hit these errors if dag.NewPackages is wrong.
loadedCfg := pkgs.Config(pkg, true)
if len(loadedCfg) == 0 {
panic(fmt.Sprintf("package does not seem to exist: %s", pkg))
}
c := loadedCfg[0]
if pkg != c.Package.Name {
panic(fmt.Sprintf("mismatched package, got %q, want %q", c.Package.Name, pkg))
}

return &task{
cfg: cfg,
pkg: pkg,
path: c.Path,
arch: arch,
cond: sync.NewCond(&sync.Mutex{}),
deps: map[string]*task{},
}
}

tasks := map[string]*task{}
for _, pkg := range g.Packages() {
if tasks[pkg] == nil {
Expand Down Expand Up @@ -282,6 +299,7 @@ type task struct {

pkg string
arch string
path string

err error
deps map[string]*task
Expand All @@ -292,10 +310,8 @@ type task struct {
skipped bool
}

func (t *task) gitSDE(ctx context.Context, origin string) (string, error) {
// TODO: Support nested yaml files.
yamlfile := filepath.Join(t.cfg.dir, origin) + ".yaml"
cmd := exec.CommandContext(ctx, "git", "log", "-1", "--pretty=%ct", "--follow", yamlfile)
func (t *task) gitSDE(ctx context.Context) (string, error) {
cmd := exec.CommandContext(ctx, "git", "log", "-1", "--pretty=%ct", "--follow", t.path) // #nosec G204
b, err := cmd.Output()
if err != nil {
return "", err
Expand Down Expand Up @@ -348,7 +364,7 @@ func (t *task) build(ctx context.Context) error {
}

log := clog.FromContext(ctx)
cfg, err := config.ParseConfiguration(ctx, fmt.Sprintf("%s.yaml", t.pkg), config.WithFS(os.DirFS(t.cfg.dir)))
cfg, err := config.ParseConfiguration(ctx, t.path, config.WithFS(os.DirFS(t.cfg.dir)))
if err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
Expand All @@ -369,6 +385,11 @@ func (t *task) build(ctx context.Context) error {
return nil
}

if t.cfg.dryrun {
log.Infof("DRYRUN: would have built %s", apkPath)
return nil
}

f, err := os.Create(logfile)
if err != nil {
return fmt.Errorf("creating logfile: :%w", err)
Expand All @@ -392,26 +413,20 @@ func (t *task) build(ctx context.Context) error {
return fmt.Errorf("creating source directory: %v", err)
}

fn := fmt.Sprintf("%s.yaml", t.pkg)
if t.cfg.dryrun {
log.Infof("DRYRUN: would have built %s", apkPath)
return nil
}

runner, err := newRunner(fctx, t.cfg.runner)
if err != nil {
return fmt.Errorf("creating runner: %w", err)
}

sde, err := t.gitSDE(ctx, cfg.Package.Name)
sde, err := t.gitSDE(ctx)
if err != nil {
return fmt.Errorf("finding source date epoch: %w", err)
}

log.Infof("Building %s", t.pkg)
bc, err := build.New(fctx,
build.WithArch(types.ParseArchitecture(arch)),
build.WithConfig(filepath.Join(t.cfg.dir, fn)),
build.WithConfig(t.path),
build.WithPipelineDir(t.cfg.pipelineDir),
build.WithExtraKeys(t.cfg.extraKeys),
build.WithExtraRepos(t.cfg.extraRepos),
Expand Down
46 changes: 46 additions & 0 deletions pkg/dag/packages.go
Expand Up @@ -357,6 +357,52 @@ func (p Packages) Sub(names ...string) (*Packages, error) {
return pkgs, nil
}

func wantArch(have string, want []string) bool {
if len(want) == 0 {
return true
}

for _, a := range want {
if a == have {
return true
}
}

return false
}

// WithArch returns a new Packages whose members are valid for the given arch.
func (p Packages) WithArch(arch string) (*Packages, error) {
pkgs := &Packages{
configs: make(map[string][]*Configuration),
index: p.index,
packages: make(map[string][]*Configuration),
}

for name, c := range p.configs {
for _, config := range c {
if !wantArch(arch, config.Package.TargetArchitecture) {
continue
}
if err := pkgs.addConfiguration(name, config); err != nil {
return nil, err
}
}
}

for name, c := range p.packages {
for _, config := range c {
if !wantArch(arch, config.Package.TargetArchitecture) {
continue
}
if err := pkgs.addPackage(name, config); err != nil {
return nil, err
}
}
}
return pkgs, nil
}

// Repository provide the Packages as a apk.RepositoryWithIndex. To be used in other places that require
// using alpine/go structs instead of ours.
func (p Packages) Repository(arch string) apk.NamedIndex {
Expand Down

0 comments on commit b8c5277

Please sign in to comment.