From c9bab2fcadac0498d5071c7bd69d52920c2c29a9 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 4 Apr 2023 09:38:40 -0700 Subject: [PATCH] WIP: Universal binary compilation This doesn't work because scard can't be cross compiled --- magefile.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/magefile.go b/magefile.go index 8a0a739..02685ae 100644 --- a/magefile.go +++ b/magefile.go @@ -42,12 +42,60 @@ func init() { } } +func CompileBinaryForArch(goos, goarch, outputPath string) error { + env := info.Env() + if goos != "" { + env["GOOS"] = goos + } + if goarch != "" { + env["GOARCH"] = goarch + } + + err := sh.RunWith(env, "go", "build", "-o", outputPath, ".") + if err != nil { + return fmt.Errorf("error building %s %s binary %w", goos, goarch, err) + } + + return nil +} + +func CompileUniversalBinary() error { + armPath := filepath.Join(buildDir, "alfred-yubico-auth_arm64") + // amdPath := filepath.Join(buildDir, "alfred-yubico-auth_amd64") + + // NOTE: Universal binaries can't be compiled because we can't cross compile against the scard + // libraries due to missing headers + if err := CompileBinaryForArch("darwin", "arm64", armPath); err != nil { + return fmt.Errorf("error compiling universal binary %w", err) + } + + /* + * if err := CompileBinaryForArch("darwin", "amd64", amdPath); err != nil { + * return fmt.Errorf("error compiling universal binary %w", err) + * } + */ + + if err := sh.RunWith( + info.Env(), + "lipo", + "-create", + "-output", + binPath, + armPath, + // amdPath, + ); err != nil { + return fmt.Errorf("failed combining binaries to universal %w", err) + } + + return nil +} + // Build workflow. func Build() error { mg.Deps(cleanBuild) fmt.Println("Building...") - if err := sh.RunWith(info.Env(), "go", "build", "-o", binPath, "."); err != nil { + if err := CompileUniversalBinary(); err != nil { return fmt.Errorf("error building binary %w", err) }