Skip to content

Commit

Permalink
Initial build target (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: nojaf <florian.verdonck@outlook.com>
  • Loading branch information
nojaf authored Oct 11, 2024
1 parent 7bb4ec8 commit 8f6ef94
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build

on:
workflow_call:

permissions:
contents: read

jobs:
wheel:
name: Build Python wheel
strategy:
matrix:
os: ["darwin","linux", "windows"]
arch: ["amd64", "arm64"]
runs-on: macos-latest
steps:
- uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "1.22"
check-latest: true
cache: false

- name: Setup Python
uses: actions/setup-python@v5

- name: Install mage
run: go install github.com/magefile/mage@v1.15.0

- name: Run build target
run: mage build ${{ matrix.os }} ${{ matrix.arch }}

- name: Upload wheels artifact
uses: actions/upload-artifact@v4
with:
name: mlflow-go-wheels-${{ matrix.os }}-${{ matrix.arch }}
path: dist/*.whl
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ jobs:

test:
uses: ./.github/workflows/test.yml

build:
uses: ./.github/workflows/build.yml
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ __pycache__/
.mlflow.repo/

# JetBrains
.idea
.idea

# Wheel
build/

# virtual environment
env/
93 changes: 93 additions & 0 deletions magefiles/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//go:build mage

//nolint:wrapcheck
package main

import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/magefile/mage/sh"
)

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 "windows":
if goarch == "amd64" {
return "x86_64-windows-gnu", nil
} else if goarch == "arm64" {
return "aarch64-windows-gnu", nil
}
}

return "", fmt.Errorf("%w: %s/%s", errUnknownTarget, goos, goarch)
}

var errUnsupportedDarwin = errors.New(`it is unsupported to build a Python wheel on Mac on a non-Mac platform`)

// Build a Python wheel.
func Build(goos, goarch string) error {
tmp, err := os.MkdirTemp("", "")
if err != nil {
return err
}

defer os.RemoveAll(tmp)

env, err := filepath.Abs(filepath.Join(tmp, ".venv"))
if err != nil {
return err
}

if err := sh.RunV("python3", "-mvenv", env); err != nil {
return err
}

pip := filepath.Join(env, "bin", "pip")
python := filepath.Join(env, "bin", "python")

if err := sh.RunV(pip, "install", "build", "ziglang"); err != nil {
return err
}

environmentVariables := map[string]string{
"GOOS": goos,
"GOARCH": goarch,
}

// Set Zig as the C compiler for cross-compilation
// If we are on Mac and targeting Mac we don't need Zig.
if goos == "darwin" {
if runtime.GOOS != "darwin" {
return errUnsupportedDarwin
}
} else {
target, err := getTargetTriple(goos, goarch)
if err != nil {
return err
}

zigCC := python + " -mziglang cc -target " + target
environmentVariables["CC"] = zigCC
}

if err := sh.RunWithV(
environmentVariables,
python, "-mbuild"); err != nil {
return err
}

return nil
}

0 comments on commit 8f6ef94

Please sign in to comment.