Skip to content

Commit

Permalink
feat(unikraft): Expose output directory variable (#2043)
Browse files Browse the repository at this point in the history
Reviewed-by: Alexander Jung <alex@unikraft.io>
Approved-by: Alexander Jung <alex@unikraft.io>
  • Loading branch information
nderjung authored Jan 9, 2025
2 parents 349f410 + 29937bc commit aeda0f8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/cli/kraft/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type BuildOptions struct {
NoFetch bool `long:"no-fetch" usage:"Do not run Unikraft's fetch step before building"`
NoRootfs bool `long:"no-rootfs" usage:"Do not build the root file system (initramfs)"`
NoUpdate bool `long:"no-update" usage:"Do not update package index before running the build"`
Output string `long:"output" short:"o" usage:"Set the output directory for the build artifacts"`
Platform string `long:"plat" short:"p" usage:"Filter the creation of the build by platform of known targets (fc/qemu/xen)"`
PrintStats bool `long:"print-stats" usage:"Print build statistics"`
Project app.Application `noattribute:"true"`
Expand Down
4 changes: 4 additions & 0 deletions internal/cli/kraft/build/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (opts *BuildOptions) initProject(ctx context.Context) error {
popts = append(popts, app.WithProjectDefaultKraftfiles())
}

if opts.Output != "" {
popts = append(popts, app.WithProjectOutDir(opts.Output))
}

// Interpret the project directory
opts.Project, err = app.NewProjectFromOptions(ctx, popts...)
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion unikraft/app/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package app
import (
"context"
"fmt"
"path/filepath"
"strings"

interp "github.com/compose-spec/compose-go/interpolation"
Expand Down Expand Up @@ -57,6 +58,9 @@ func NewApplicationFromInterface(ctx context.Context, iface map[string]interface
return nil, errors.New("output directory must be a string")
}
}
if popts.outDir != "" {
outdir = popts.outDir
}

if n, ok := iface["rootfs"]; ok {
app.rootfs, ok = n.(string)
Expand All @@ -76,8 +80,15 @@ func NewApplicationFromInterface(ctx context.Context, iface map[string]interface
}
}

if popts.resolvePaths {
if popts.resolvePaths && popts.outDir == "" {
app.outDir = popts.RelativePath(outdir)
} else if popts.outDir != "" {
abs, err := filepath.Abs(outdir)
if err != nil {
return nil, err
}

app.outDir = abs
}

if err := Transform(ctx, getSection(iface, "unikraft"), &app.unikraft); err != nil {
Expand Down
10 changes: 8 additions & 2 deletions unikraft/app/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ func NewProjectFromOptions(ctx context.Context, opts ...ProjectOption) (Applicat
}

name, _ := popts.GetProjectName()
outdir := unikraft.BuildDir

var outdir string
if popts.outDir == "" {
outdir = popts.RelativePath(unikraft.BuildDir)
} else {
outdir = popts.outDir
}

iface := popts.kraftfile.config
if iface == nil {
Expand Down Expand Up @@ -110,7 +116,7 @@ func NewProjectFromOptions(ctx context.Context, opts ...ProjectOption) (Applicat
uk := &unikraft.Context{
UK_NAME: name,
UK_BASE: popts.RelativePath(workdir),
BUILD_DIR: popts.RelativePath(outdir),
BUILD_DIR: outdir,
}

if _, err := os.Stat(uk.BUILD_DIR); err != nil && os.IsNotExist(err) {
Expand Down
9 changes: 9 additions & 0 deletions unikraft/app/project_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ProjectOptions struct {
workdir string
kraftfile *Kraftfile
kconfig kconfig.KeyValueMap
outDir string
skipValidation bool
skipInterpolation bool
skipNormalization bool
Expand Down Expand Up @@ -251,6 +252,14 @@ func WithProjectNormalization(normalization bool) ProjectOption {
}
}

// WithProjectOutDir defines ProjectOptions' output directory
func WithProjectOutDir(outDir string) ProjectOption {
return func(popts *ProjectOptions) error {
popts.outDir = outDir
return nil
}
}

// WithProjectResolvedPaths set ProjectOptions to enable paths resolution
func WithProjectResolvedPaths(resolve bool) ProjectOption {
return func(popts *ProjectOptions) error {
Expand Down

0 comments on commit aeda0f8

Please sign in to comment.