Skip to content

Commit

Permalink
fix edge case for from file
Browse files Browse the repository at this point in the history
  • Loading branch information
konoui committed Jul 4, 2023
1 parent 769efc9 commit c63cfcf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
36 changes: 20 additions & 16 deletions pkg/testlipo/bin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ func (bm *BinManager) singleAdd(t *testing.T, arch string) (path string) {
}

archBin := filepath.Join(bm.Dir, arch)
defer func() {
if path == "" {
path = "bad arch: " + arch
return
}
bm.archBins[arch] = path
if arch == "arm64" {
bm.arm64Bin = path
}
}()

// from file cache
m, err := macho.Open(archBin)
Expand All @@ -90,35 +100,29 @@ func (bm *BinManager) singleAdd(t *testing.T, arch string) (path string) {
if farch != arch {
panic(fmt.Sprintf("file %s does not match arch %s", arch, farch))
}
bm.archBins[arch] = archBin
return archBin
}

// generate a new binary
if arch == "arm64" {
switch {
case arch == "arm64":
bm.writeMainFile(t)
compile(t, bm.mainFile, archBin, "arm64")
bm.arm64Bin = archBin
bm.archBins[arch] = archBin
return archBin
}

if arch == "x86_64" {
case arch == "x86_64":
bm.writeMainFile(t)
compile(t, bm.mainFile, archBin, "amd64")
bm.archBins[arch] = archBin
return archBin
}

if strings.HasPrefix(arch, "obj_") {
case arch == "amd64":
t.Fatal("use x86_64 instead of amd64")
return ""
case strings.HasPrefix(arch, "obj_"):
copyAndManipulate(t, bm.arm64Bin, archBin, arch[4:], macho.TypeObj)
bm.archBins[arch] = archBin
return archBin
default:
copyAndManipulate(t, bm.arm64Bin, archBin, arch, macho.TypeExec)
return archBin
}

copyAndManipulate(t, bm.arm64Bin, archBin, arch, macho.TypeExec)
bm.archBins[arch] = archBin
return archBin
}

func (bm *BinManager) writeMainFile(t *testing.T) {
Expand Down
9 changes: 3 additions & 6 deletions pkg/testlipo/testlipo.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ func Setup(t *testing.T, bm *BinManager, arches []string, opts ...Opt) *TestLipo

lipoBin := NewLipoBin(t, opts...)
fatBin := filepath.Join(dir, "fat-"+strings.Join(arches, "-"))
tmp := lipoBin.ignoreErr
lipoBin.ignoreErr = false
lipoBin.Create(t, fatBin, bm.getBinPaths(t, arches)...)
lipoBin.ignoreErr = true
lipoBin.ignoreErr = tmp

return &TestLipo{
arches: arches,
Expand Down Expand Up @@ -261,7 +262,6 @@ func appendCmd(cmd string, args []string) []string {
}

func PatchFat64Reserved(t *testing.T, p string) {
t.Helper()

ff, err := lmacho.NewFatFile(p)
if err != nil {
Expand Down Expand Up @@ -346,8 +346,6 @@ func printStat(t *testing.T, bin string) {
}

func compile(t *testing.T, mainfile, binPath, arch string) {
t.Helper()

args := []string{"build", "-o"}
args = append(args, binPath, mainfile)
cmd := exec.Command("go", args...)
Expand All @@ -357,7 +355,6 @@ func compile(t *testing.T, mainfile, binPath, arch string) {
}

func calcSha256(t *testing.T, p string) string {
t.Helper()
f, err := os.Open(p)
fatalIf(t, err)
defer f.Close()
Expand All @@ -370,7 +367,6 @@ func calcSha256(t *testing.T, p string) string {
}

func copyAndManipulate(t *testing.T, src, dst string, arch string, typ macho.Type) {
t.Helper()
cpu, sub, ok := lmacho.ToCpu(arch)
if !ok {
t.Fatalf("copyAndManipulate: unsupported arch: %s\n", arch)
Expand Down Expand Up @@ -419,6 +415,7 @@ func copyAndManipulate(t *testing.T, src, dst string, arch string, typ macho.Typ
}

func fatalIf(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c63cfcf

Please sign in to comment.