Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds --target-arch flag #331

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions carton/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"github.com/paketo-buildpacks/libpak/internal"
)

const DefaultTargetArch = "all"

// Package is an object that contains the configuration for building a package.
type Package struct {

Expand All @@ -58,6 +60,9 @@ type Package struct {

// Version is a version to substitute into an existing buildpack.toml.
Version string

// TargetArch is the target architecture to package. Default is "all".
TargetArch string
}

// Create creates a package.
Expand Down Expand Up @@ -113,7 +118,8 @@ func (p Package) Create(options ...Option) {
}
}

if len(supportedTargets) == 0 {
oldOutputFormat := len(supportedTargets) == 0
if oldOutputFormat {
logger.Info("No supported targets found, defaulting to old format")
}

Expand All @@ -122,7 +128,7 @@ func (p Package) Create(options ...Option) {
entries := map[string]string{}

for _, i := range metadata.IncludeFiles {
if len(supportedTargets) == 0 || strings.HasPrefix(i, "linux/") || i == "buildpack.toml" {
if oldOutputFormat || strings.HasPrefix(i, "linux/") || i == "buildpack.toml" {
entries[i] = filepath.Join(p.Source, i)
} else {
for _, target := range supportedTargets {
Expand Down Expand Up @@ -233,8 +239,18 @@ func (p Package) Create(options ...Option) {
}
sort.Strings(files)
for _, d := range files {
logger.Bodyf("Adding %s", d)
file = filepath.Join(p.Destination, d)
if p.TargetArch != DefaultTargetArch && !oldOutputFormat && strings.HasPrefix(d, "linux/") && !strings.HasPrefix(d, fmt.Sprintf("linux/%s", p.TargetArch)) {
logger.Debugf("Skipping %s because target arch is %s", d, p.TargetArch)
continue
}

targetLocation := d
if p.TargetArch != DefaultTargetArch {
targetLocation = strings.Replace(d, fmt.Sprintf("linux/%s/", p.TargetArch), "", 1)
}

logger.Bodyf("Adding %s", targetLocation)
file = filepath.Join(p.Destination, targetLocation)
if err = config.entryWriter.Write(entries[d], file); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write file %s to %s\n%w", entries[d], file, err))
return
Expand Down
1 change: 1 addition & 0 deletions cmd/create-package/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func main() {
flagSet.BoolVar(&p.StrictDependencyFilters, "strict-filters", false, "require filter to match all data or just some data (default: false)")
flagSet.StringVar(&p.Source, "source", defaultSource(), "path to build package source directory (default: $PWD)")
flagSet.StringVar(&p.Version, "version", "", "version to substitute into buildpack.toml")
flagSet.StringVar(&p.TargetArch, "target-arch", carton.DefaultTargetArch, "target architecture for the package (default: all)")

if err := flagSet.Parse(os.Args[1:]); err != nil {
log.Fatal(fmt.Errorf("unable to parse flags\n%w", err))
Expand Down