mirror of
https://github.com/goreleaser/nfpm
synced 2026-11-29 08:54:49 +01:00
macho.OpenFat and macho.Open return io.EOF for a file of 0 to 3 bytes, which failed the build with a bare "EOF". Such a file cannot be a Mach-O program, so it is packaged as it is, like any other non-Mach-O main executable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
6.1 KiB
Go
190 lines
6.1 KiB
Go
package macapp
|
|
|
|
import (
|
|
"debug/macho"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/goreleaser/nfpm/v2"
|
|
"github.com/goreleaser/nfpm/v2/files"
|
|
"github.com/goreleaser/nfpm/v2/internal/macos"
|
|
)
|
|
|
|
// contentsRoot is the directory inside the bundle every destination lives
|
|
// under.
|
|
const contentsRoot = "/Contents"
|
|
|
|
// generatedPaths are the bundle paths nFPM writes itself.
|
|
// nolint: gochecknoglobals
|
|
var generatedPaths = []string{
|
|
contentsRoot + "/Info.plist",
|
|
contentsRoot + "/PkgInfo",
|
|
contentsRoot + "/CodeResources",
|
|
contentsRoot + "/_CodeSignature",
|
|
}
|
|
|
|
// entry is one contents entry placed in the bundle.
|
|
type entry struct {
|
|
content *files.Content
|
|
// path is the entry's path relative to the bundle's Contents directory.
|
|
path string
|
|
}
|
|
|
|
// bundleEntries maps the prepared contents onto paths inside the bundle and
|
|
// checks that they form a bundle macOS can launch.
|
|
func bundleEntries(packager string, info *nfpm.Info, arch string) ([]entry, error) {
|
|
executable := contentsRoot + "/MacOS/" + executableName(info)
|
|
index := macos.NewPathIndex()
|
|
// placed maps each bundle path to the contents entry that supplies it.
|
|
// The path index accepts the same spelling twice, because parent
|
|
// directories repeat; two entries for one path would leave only the
|
|
// later one in the bundle.
|
|
placed := map[string]string{}
|
|
var (
|
|
entries []entry
|
|
hasMain bool
|
|
warnedOwners bool
|
|
)
|
|
for _, content := range info.Contents {
|
|
if content.Type == files.TypeImplicitDir {
|
|
continue
|
|
}
|
|
destination := path.Clean("/" + strings.ReplaceAll(content.Destination, "\\", "/"))
|
|
if content.Type == files.TypeDir && destination == contentsRoot {
|
|
// A tree placed at /Contents lists the directory itself, which
|
|
// every bundle has.
|
|
continue
|
|
}
|
|
if err := validateDestination(content.Destination, destination); err != nil {
|
|
return nil, err
|
|
}
|
|
if previous, taken := placed[destination]; taken {
|
|
return nil, fmt.Errorf(
|
|
"package contents %q and %q are placed at the same path in the bundle, %s",
|
|
previous, content.Destination, destination)
|
|
}
|
|
placed[destination] = content.Destination
|
|
if err := index.Add(destination); err != nil {
|
|
return nil, err
|
|
}
|
|
if !warnedOwners && (content.FileInfo.Owner != "root" || content.FileInfo.Group != "root") {
|
|
log.Printf(
|
|
"warning: %s: an application bundle records no file ownership, the owner and group of %s are ignored",
|
|
packager, content.Destination)
|
|
warnedOwners = true
|
|
}
|
|
if destination == executable {
|
|
if content.Type == files.TypeDir || content.Type == files.TypeSymlink {
|
|
return nil, fmt.Errorf(
|
|
"package contents %q is the main executable and must be a regular file", content.Destination)
|
|
}
|
|
if content.Mode().Perm()&0o111 == 0 {
|
|
return nil, fmt.Errorf(
|
|
"package contents %q is the main executable but its mode %04o is not executable; set file_info.mode to 0755",
|
|
content.Destination, content.Mode().Perm())
|
|
}
|
|
if err := validateExecutable(content, arch); err != nil {
|
|
return nil, err
|
|
}
|
|
hasMain = true
|
|
}
|
|
entries = append(entries, entry{
|
|
content: content,
|
|
path: strings.TrimPrefix(destination, contentsRoot+"/"),
|
|
})
|
|
}
|
|
if !hasMain {
|
|
return nil, fmt.Errorf(
|
|
"package contents must include the main executable at %s; add it, or name another file in Contents/MacOS with app.executable",
|
|
executable)
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
// validateDestination requires a (clean) destination to be a path inside the
|
|
// bundle that nFPM does not generate itself.
|
|
func validateDestination(destination, clean string) error {
|
|
if !strings.HasPrefix(clean, contentsRoot+"/") {
|
|
return fmt.Errorf(
|
|
"package contents %q: destinations of an application bundle are paths inside it and start with %s/, "+
|
|
"for example %s/MacOS/myapp; keep other formats' paths out with the contents \"packager\" field",
|
|
destination, contentsRoot, contentsRoot)
|
|
}
|
|
for _, generated := range generatedPaths {
|
|
if strings.EqualFold(clean, generated) || strings.HasPrefix(strings.ToLower(clean), strings.ToLower(generated)+"/") {
|
|
return fmt.Errorf("package contents %q: %s is generated by nfpm and cannot be supplied", destination, generated)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateExecutable checks that a main executable which is a Mach-O binary
|
|
// was built for the architecture the package claims. Other executables, such
|
|
// as launcher scripts, run on every architecture and are accepted as they
|
|
// are.
|
|
func validateExecutable(content *files.Content, arch string) error {
|
|
built, err := machoArchitectures(content.Source)
|
|
if err != nil {
|
|
return fmt.Errorf("reading file %s: %w", content.Source, err)
|
|
}
|
|
if built == nil {
|
|
return nil
|
|
}
|
|
for _, want := range macos.HostArchitectures(arch) {
|
|
if !built[want] {
|
|
return fmt.Errorf(
|
|
"package contents %q: the main executable %s is not built for %s, but the package architecture is %s",
|
|
content.Destination, content.Source, want, arch)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// machoArchitectures returns the architectures a Mach-O file contains, or nil
|
|
// when the file is not a Mach-O binary at all.
|
|
func machoArchitectures(source string) (map[string]bool, error) {
|
|
if fat, err := macho.OpenFat(source); err == nil {
|
|
defer fat.Close() // nolint: errcheck
|
|
built := map[string]bool{}
|
|
for _, arch := range fat.Arches {
|
|
built[cpuName(arch.Cpu)] = true
|
|
}
|
|
return built, nil
|
|
} else if !isFormatError(err) {
|
|
return nil, err
|
|
}
|
|
thin, err := macho.Open(source)
|
|
if err != nil {
|
|
if isFormatError(err) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
defer thin.Close() // nolint: errcheck
|
|
return map[string]bool{cpuName(thin.Cpu): true}, nil
|
|
}
|
|
|
|
// isFormatError reports whether err says the file is not the kind of Mach-O
|
|
// that was asked for, as opposed to the file being unreadable. A file too
|
|
// short to hold a Mach-O header ends before the magic number can be read.
|
|
func isFormatError(err error) bool {
|
|
var format *macho.FormatError
|
|
return errors.Is(err, macho.ErrNotFat) || errors.As(err, &format) ||
|
|
errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF)
|
|
}
|
|
|
|
func cpuName(cpu macho.Cpu) string {
|
|
switch cpu {
|
|
case macho.CpuArm64:
|
|
return macos.ArchARM64
|
|
case macho.CpuAmd64:
|
|
return macos.ArchX8664
|
|
default:
|
|
return cpu.String()
|
|
}
|
|
}
|