Skip to content

Commit 8f6ef94

Browse files
authored
Initial build target (#9)
Signed-off-by: nojaf <florian.verdonck@outlook.com>
1 parent 7bb4ec8 commit 8f6ef94

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

.github/workflows/build.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Build
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
wheel:
11+
name: Build Python wheel
12+
strategy:
13+
matrix:
14+
os: ["darwin","linux", "windows"]
15+
arch: ["amd64", "arm64"]
16+
runs-on: macos-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: "1.22"
24+
check-latest: true
25+
cache: false
26+
27+
- name: Setup Python
28+
uses: actions/setup-python@v5
29+
30+
- name: Install mage
31+
run: go install github.com/magefile/mage@v1.15.0
32+
33+
- name: Run build target
34+
run: mage build ${{ matrix.os }} ${{ matrix.arch }}
35+
36+
- name: Upload wheels artifact
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: mlflow-go-wheels-${{ matrix.os }}-${{ matrix.arch }}
40+
path: dist/*.whl

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ jobs:
1616

1717
test:
1818
uses: ./.github/workflows/test.yml
19+
20+
build:
21+
uses: ./.github/workflows/build.yml

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ __pycache__/
1313
.mlflow.repo/
1414

1515
# JetBrains
16-
.idea
16+
.idea
17+
18+
# Wheel
19+
build/
20+
21+
# virtual environment
22+
env/

magefiles/build.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//go:build mage
2+
3+
//nolint:wrapcheck
4+
package main
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"os"
10+
"path/filepath"
11+
"runtime"
12+
13+
"github.com/magefile/mage/sh"
14+
)
15+
16+
var errUnknownTarget = errors.New("could not determine zig target")
17+
18+
// Helper function to determine the Zig target triple based on OS and architecture.
19+
func getTargetTriple(goos, goarch string) (string, error) {
20+
switch goos {
21+
case "linux":
22+
if goarch == "amd64" {
23+
return "x86_64-linux-gnu", nil
24+
} else if goarch == "arm64" {
25+
return "aarch64-linux-gnu", nil
26+
}
27+
case "windows":
28+
if goarch == "amd64" {
29+
return "x86_64-windows-gnu", nil
30+
} else if goarch == "arm64" {
31+
return "aarch64-windows-gnu", nil
32+
}
33+
}
34+
35+
return "", fmt.Errorf("%w: %s/%s", errUnknownTarget, goos, goarch)
36+
}
37+
38+
var errUnsupportedDarwin = errors.New(`it is unsupported to build a Python wheel on Mac on a non-Mac platform`)
39+
40+
// Build a Python wheel.
41+
func Build(goos, goarch string) error {
42+
tmp, err := os.MkdirTemp("", "")
43+
if err != nil {
44+
return err
45+
}
46+
47+
defer os.RemoveAll(tmp)
48+
49+
env, err := filepath.Abs(filepath.Join(tmp, ".venv"))
50+
if err != nil {
51+
return err
52+
}
53+
54+
if err := sh.RunV("python3", "-mvenv", env); err != nil {
55+
return err
56+
}
57+
58+
pip := filepath.Join(env, "bin", "pip")
59+
python := filepath.Join(env, "bin", "python")
60+
61+
if err := sh.RunV(pip, "install", "build", "ziglang"); err != nil {
62+
return err
63+
}
64+
65+
environmentVariables := map[string]string{
66+
"GOOS": goos,
67+
"GOARCH": goarch,
68+
}
69+
70+
// Set Zig as the C compiler for cross-compilation
71+
// If we are on Mac and targeting Mac we don't need Zig.
72+
if goos == "darwin" {
73+
if runtime.GOOS != "darwin" {
74+
return errUnsupportedDarwin
75+
}
76+
} else {
77+
target, err := getTargetTriple(goos, goarch)
78+
if err != nil {
79+
return err
80+
}
81+
82+
zigCC := python + " -mziglang cc -target " + target
83+
environmentVariables["CC"] = zigCC
84+
}
85+
86+
if err := sh.RunWithV(
87+
environmentVariables,
88+
python, "-mbuild"); err != nil {
89+
return err
90+
}
91+
92+
return nil
93+
}

0 commit comments

Comments
 (0)