-
Notifications
You must be signed in to change notification settings - Fork 122
introduce zig cc cross-compilation setup for GitHub Actions and local development
#2535
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
base: main
Are you sure you want to change the base?
Changes from all commits
66af41c
86902e3
ef3c850
8325e63
e68d85d
40a29b0
21fc66d
dec825a
6ef1168
5f07afd
56ef767
ca7bcb2
bf837dc
c6c9441
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| .PHONY: build test clean | ||
|
|
||
| build: bin/zigcc | ||
|
|
||
| # always build for linux and the machine's native architecture | ||
| bin/zigcc: *.go go.mod | ||
| GOOS=linux go build -o $@ -ldflags="-s -w" -v ./... | ||
|
|
||
| test: | ||
| go test -v ./... | ||
|
|
||
| fmt: | ||
| go fmt ./... | ||
|
|
||
| vet: | ||
| go vet ./... | ||
|
|
||
| clean: | ||
| go clean | ||
| rm -f bin/* | ||
| rmdir bin |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,3 @@ | ||||||||||||||
| module zigcc | ||||||||||||||
|
|
||||||||||||||
| go 1.24 | ||||||||||||||
|
Comment on lines
+1
to
+3
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Target a released Go toolchain
-go 1.24
+go 1.22📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func processArg0(arg0 string) (string, error) { | ||
| switch arg0 { | ||
| case "zig-cc": | ||
| return "cc", nil | ||
| case "zig-c++": | ||
| return "c++", nil | ||
| default: | ||
| return "", fmt.Errorf("unknown wrapper name: %s", arg0) | ||
| } | ||
| } | ||
|
|
||
| func processArgs(args []string) []string { | ||
| newArgs := make([]string, 0, len(args)) | ||
| for _, arg := range args { | ||
| if strings.HasPrefix(arg, "-Wp,") { | ||
| newArgs = append(newArgs, strings.Split(arg, ",")[1:]...) | ||
| } else { | ||
| newArgs = append(newArgs, arg) | ||
| } | ||
| } | ||
| return newArgs | ||
| } | ||
|
|
||
| func main() { | ||
| arg0 := filepath.Base(os.Args[0]) | ||
| subcommand, err := processArg0(arg0) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| newArgs := make([]string, 0, len(os.Args)+4) | ||
| newArgs = append(newArgs, | ||
| "/mnt/zig", // Path to the real Zig executable. | ||
| subcommand, | ||
| "-target", | ||
| "s390x-linux-gnu.2.34", | ||
| ) | ||
| newArgs = append(newArgs, processArgs(os.Args[1:])...) | ||
|
|
||
| env := os.Environ() | ||
| if err := syscall.Exec(newArgs[0], newArgs, env); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error executing zig: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestProcessWp(t *testing.T) { | ||
| args := []string{"-Wp,-D_FORTIFY_SOURCE=2"} | ||
| newArgs := processArgs(args) | ||
| if !reflect.DeepEqual(newArgs, []string{"-D_FORTIFY_SOURCE=2"}) { | ||
| t.Fatalf("expected -DFOO=bar, got %v", newArgs) | ||
| } | ||
| for _, tc := range []struct { | ||
| args []string | ||
| expected []string | ||
| }{ | ||
| { | ||
| args: []string{"-Wp,-D_FORTIFY_SOURCE=2"}, | ||
| expected: []string{"-D_FORTIFY_SOURCE=2"}, | ||
| }, | ||
| { | ||
| args: []string{"-Wp,-DNDEBUG,-D_FORTIFY_SOURCE=2"}, | ||
| expected: []string{"-DNDEBUG", "-D_FORTIFY_SOURCE=2"}, | ||
| }, | ||
| } { | ||
| t.Run(fmt.Sprint(tc.args), func(t *testing.T) { | ||
| newArgs := processArgs(tc.args) | ||
| if !reflect.DeepEqual(newArgs, tc.expected) { | ||
| t.Fatalf("expected %#v, got %#v", tc.expected, newArgs) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #! /usr/bin/env python3 | ||
| import os | ||
| import pathlib | ||
| import sys | ||
|
|
||
| def main(): | ||
| arg0 = pathlib.Path(sys.argv[0]).name | ||
| args = [] | ||
| for arg in sys.argv[1:]: | ||
| if arg.startswith("-Wp,-D"): | ||
| args.append(arg.replace("-Wp,-D", "-D", 1)) | ||
| else: | ||
| args.append(arg) | ||
|
|
||
| if arg0 == "zig-cc": | ||
| args = ["/mnt/zig", "cc", "-target", "s390x-linux-gnu.2.34"] + args | ||
| elif arg0 == "zig-c++": | ||
| args = ["/mnt/zig", "c++", "-target", "s390x-linux-gnu.2.34"] + args | ||
|
Comment on lines
+9
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix Replacing - for arg in sys.argv[1:]:
- if arg.startswith("-Wp,-D"):
- args.append(arg.replace("-Wp,-D", "-D", 1))
- else:
- args.append(arg)
+ for arg in sys.argv[1:]:
+ if arg.startswith("-Wp,"):
+ args.extend(part for part in arg.split(",")[1:] if part)
+ else:
+ args.append(arg)🧰 Tools🪛 Ruff (0.14.4)16-16: Consider iterable unpacking instead of concatenation Replace with iterable unpacking (RUF005) 18-18: Consider iterable unpacking instead of concatenation Replace with iterable unpacking (RUF005) |
||
| else: | ||
| raise ValueError(f"Unknown argument {arg0}") | ||
|
|
||
| os.execve(args[0], args, os.environ) | ||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore
makefor non-ninja builds on s390x/ppc64leDropping
makehere leaves those arches without the classic build tool that many Python/C extensions still expect, sopip installof projects that callmakewill now fail. Please keepmakewhile addingninja-build.🤖 Prompt for AI Agents