Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial build target #9

Merged
merged 22 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]

- 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
}
Loading