From 688a6d7ac3ef850ffd2d0443c938c29f9923b722 Mon Sep 17 00:00:00 2001 From: nojaf Date: Thu, 3 Oct 2024 08:31:03 +0000 Subject: [PATCH] Add zig target Signed-off-by: nojaf --- magefiles/build.go | 56 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/magefiles/build.go b/magefiles/build.go index 5a2ac90..e27ff2e 100644 --- a/magefiles/build.go +++ b/magefiles/build.go @@ -3,11 +3,47 @@ //nolint:wrapcheck package main -import "github.com/magefile/mage/sh" +import ( + "errors" + "fmt" + "runtime" + + "github.com/magefile/mage/sh" +) + +const ( + amd64 = "amd64" +) + +var errUnknownTarget = errors.New("could not determine zig target") + +// Helper function to determine the Zig target triple based on OS and architecture. +func getTargetTriple(goos, goarch string) (string, error) { + switch goos { + case "linux": + if goarch == amd64 { + return "x86_64-linux-gnu", nil + } else if goarch == "arm64" { + return "aarch64-linux-gnu", nil + } + case "darwin": + if goarch == amd64 { + return "x86_64-macos", nil + } else if goarch == "arm64" { + return "aarch64-macos", nil + } + case "windows": + if goarch == amd64 { + return "x86_64-windows-gnu", nil + } + } + + return "", fmt.Errorf("%w: %s/%s", errUnknownTarget, goos, goarch) +} // Build a Python wheel. func Build() error { - if err := sh.RunV("python3", "-mvenv", "foo"); err != nil { + if err := sh.RunV("python3", "-mvenv", "env"); err != nil { return err } @@ -19,7 +55,21 @@ func Build() error { return err } - if err := sh.RunV("build"); err != nil { + // Set Zig as the C compiler for cross-compilation + targetTriple, err := getTargetTriple(runtime.GOOS, runtime.GOARCH) + if err != nil { + return err + } + + zigCC := "zig cc -target " + targetTriple + + if err := sh.RunWithV( + map[string]string{ + "CC": zigCC, + "GOOS": runtime.GOOS, + "GOARCH": runtime.GOARCH, + }, + "build"); err != nil { return err }