Skip to content

Commit 56ef767

Browse files
committed
Add zigcc wrapper for s390x cross-compilation, including Makefile, tests, and Go module setup.
1 parent 5f07afd commit 56ef767

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

scripts/zigcc/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: build test clean
2+
3+
build: bin/zigcc
4+
5+
# always build for linux and the machine's native architecture
6+
bin/zigcc: *.go go.mod
7+
GOOS=linux go build -o $@ -ldflags="-s -w" -v ./...
8+
9+
test:
10+
go test -v ./...
11+
12+
fmt:
13+
go fmt ./...
14+
15+
vet:
16+
go vet ./...
17+
18+
clean:
19+
go clean
20+
rm -f bin/*
21+
rmdir bin

scripts/zigcc/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module zigcc
2+
3+
go 1.24

scripts/zigcc/zigcc.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"syscall"
9+
)
10+
11+
func processArg0(arg0 string) (string, error) {
12+
switch arg0 {
13+
case "zig-cc":
14+
return "cc", nil
15+
case "zig-c++":
16+
return "c++", nil
17+
default:
18+
return "", fmt.Errorf("unknown wrapper name: %s", arg0)
19+
}
20+
}
21+
22+
func processArgs(args []string) []string {
23+
newArgs := make([]string, 0, len(args))
24+
for _, arg := range args {
25+
if strings.HasPrefix(arg, "-Wp,") {
26+
newArgs = append(newArgs, strings.Split(arg, ",")[1:]...)
27+
} else {
28+
newArgs = append(newArgs, arg)
29+
}
30+
}
31+
return newArgs
32+
}
33+
34+
func main() {
35+
arg0 := filepath.Base(os.Args[0])
36+
subcommand, err := processArg0(arg0)
37+
if err != nil {
38+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
39+
os.Exit(1)
40+
}
41+
42+
newArgs := make([]string, 0, len(os.Args)+4)
43+
newArgs = append(newArgs,
44+
"/mnt/zig", // Path to the real Zig executable.
45+
subcommand,
46+
"-target",
47+
"s390x-linux-gnu.2.34",
48+
)
49+
newArgs = append(newArgs, processArgs(os.Args[1:])...)
50+
51+
env := os.Environ()
52+
if err := syscall.Exec(newArgs[0], newArgs, env); err != nil {
53+
fmt.Fprintf(os.Stderr, "Error executing zig: %v\n", err)
54+
os.Exit(1)
55+
}
56+
}

scripts/zigcc/zigcc_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestProcessWp(t *testing.T) {
10+
args := []string{"-Wp,-D_FORTIFY_SOURCE=2"}
11+
newArgs := processArgs(args)
12+
if !reflect.DeepEqual(newArgs, []string{"-D_FORTIFY_SOURCE=2"}) {
13+
t.Fatalf("expected -DFOO=bar, got %v", newArgs)
14+
}
15+
for _, tc := range []struct {
16+
args []string
17+
expected []string
18+
}{
19+
{
20+
args: []string{"-Wp,-D_FORTIFY_SOURCE=2"},
21+
expected: []string{"-D_FORTIFY_SOURCE=2"},
22+
},
23+
{
24+
args: []string{"-Wp,-DNDEBUG,-D_FORTIFY_SOURCE=2"},
25+
expected: []string{"-DNDEBUG", "-D_FORTIFY_SOURCE=2"},
26+
},
27+
} {
28+
t.Run(fmt.Sprint(tc.args), func(t *testing.T) {
29+
newArgs := processArgs(tc.args)
30+
if !reflect.DeepEqual(newArgs, tc.expected) {
31+
t.Fatalf("expected %#v, got %#v", tc.expected, newArgs)
32+
}
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)