Skip to content

Commit

Permalink
Add zig target
Browse files Browse the repository at this point in the history
Signed-off-by: nojaf <[email protected]>
  • Loading branch information
nojaf committed Oct 3, 2024
1 parent aedfa61 commit 688a6d7
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions magefiles/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 688a6d7

Please sign in to comment.