From 903a8ded275932cf025895c70e27b6916903293c Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Thu, 9 Jan 2025 14:19:39 +0200 Subject: [PATCH 1/2] feat(unikraft): Expose output directory variable Signed-off-by: Cezar Craciunoiu --- unikraft/app/loader.go | 13 ++++++++++++- unikraft/app/project.go | 10 ++++++++-- unikraft/app/project_options.go | 9 +++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/unikraft/app/loader.go b/unikraft/app/loader.go index 5ed9b2836..9c4a503f0 100644 --- a/unikraft/app/loader.go +++ b/unikraft/app/loader.go @@ -20,6 +20,7 @@ package app import ( "context" "fmt" + "path/filepath" "strings" interp "github.com/compose-spec/compose-go/interpolation" @@ -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) @@ -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 { diff --git a/unikraft/app/project.go b/unikraft/app/project.go index 5dce74e31..fe611c7c0 100644 --- a/unikraft/app/project.go +++ b/unikraft/app/project.go @@ -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 { @@ -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) { diff --git a/unikraft/app/project_options.go b/unikraft/app/project_options.go index a44350f52..ae6bd6bf8 100644 --- a/unikraft/app/project_options.go +++ b/unikraft/app/project_options.go @@ -49,6 +49,7 @@ type ProjectOptions struct { workdir string kraftfile *Kraftfile kconfig kconfig.KeyValueMap + outDir string skipValidation bool skipInterpolation bool skipNormalization bool @@ -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 { From 29937bc95dc2456788ddce62416a888a31cc408f Mon Sep 17 00:00:00 2001 From: Cezar Craciunoiu Date: Thu, 9 Jan 2025 14:20:18 +0200 Subject: [PATCH 2/2] feat(internal): Add output directory for building GitHub-Fixes: #2036 Signed-off-by: Cezar Craciunoiu --- internal/cli/kraft/build/build.go | 1 + internal/cli/kraft/build/utils.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/internal/cli/kraft/build/build.go b/internal/cli/kraft/build/build.go index b6b9e2e6e..db2b50529 100644 --- a/internal/cli/kraft/build/build.go +++ b/internal/cli/kraft/build/build.go @@ -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"` diff --git a/internal/cli/kraft/build/utils.go b/internal/cli/kraft/build/utils.go index a050ab7c9..04379b8dc 100644 --- a/internal/cli/kraft/build/utils.go +++ b/internal/cli/kraft/build/utils.go @@ -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 {