Skip to content

Commit 373a35a

Browse files
committed
[ci] x-compile macos with cgo using zig
1 parent 3841e39 commit 373a35a

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

ci/main.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import (
2727
"dagger/apoxy-cli/internal/dagger"
2828
)
2929

30+
// Note that 0.12.0 and later fail to cross compile for Darwin.
31+
// See https://github.com/ziglang/zig/issues/20689
32+
const ZigVersion = "0.11.0"
33+
3034
type ApoxyCli struct{}
3135

3236
func canonArchFromGoArch(goarch string) string {
@@ -75,18 +79,18 @@ func (m *ApoxyCli) BuilderContainer(ctx context.Context, src *dagger.Directory)
7579
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build")).
7680
WithEnvVariable("GOCACHE", "/go/build-cache").
7781
// Install Zig toolchain.
78-
WithExec([]string{"apt-get", "update", "-yq"}).
82+
WithExec([]string{"apt-get", "update"}).
7983
WithExec([]string{
8084
"apt-get", "install", "-yq", "xz-utils", "clang",
8185
}).
8286
WithExec([]string{
83-
"wget", fmt.Sprintf("https://ziglang.org/download/0.13.0/zig-linux-%s-0.13.0.tar.xz", hostArch()),
87+
"wget", fmt.Sprintf("https://ziglang.org/download/%s/zig-linux-%s-%s.tar.xz", ZigVersion, hostArch(), ZigVersion),
8488
}).
8589
WithExec([]string{
86-
"tar", "-xf", fmt.Sprintf("zig-linux-%s-0.13.0.tar.xz", hostArch()),
90+
"tar", "-xf", fmt.Sprintf("zig-linux-%s-%s.tar.xz", hostArch(), ZigVersion),
8791
}).
8892
WithExec([]string{
89-
"ln", "-s", fmt.Sprintf("/zig-linux-%s-0.13.0/zig", hostArch()), "/bin/zig",
93+
"ln", "-s", fmt.Sprintf("/zig-linux-%s-%s/zig", hostArch(), ZigVersion), "/bin/zig",
9094
}).
9195
WithNewFile("/bin/zig-wrapper", zigWrapperScript, dagger.ContainerWithNewFileOpts{
9296
Permissions: 0755,
@@ -114,19 +118,44 @@ func (m *ApoxyCli) BuildCLI(
114118
fmt.Sprintf("-X '%s/build.BuildVersion=%s'", pkg, tag),
115119
fmt.Sprintf("-X '%s/build.BuildDate=%s'", pkg, date),
116120
fmt.Sprintf("-X '%s/build.CommitHash=%s'", pkg, sha),
117-
"-s", // strip symbols
118121
"-w", // disable DWARF
122+
// Before you think about adding -s here, see https://github.com/ziglang/zig/issues/22844
123+
}
124+
125+
targetArch := canonArchFromGoArch(goarch)
126+
zigTarget := fmt.Sprintf("%s-linux-musl", targetArch)
127+
if os == "darwin" {
128+
zigTarget = fmt.Sprintf("%s-macos", targetArch)
119129
}
120130

121131
builder := m.BuilderContainer(ctx, src)
132+
133+
if os == "darwin" {
134+
builder = builder.
135+
WithExec([]string{ "apt-get", "update" }).
136+
WithExec([]string{ "apt-get", "install", "-yq", "gcc", "g++", "zlib1g-dev", "libmpc-dev", "libmpfr-dev", "libgmp-dev"}).
137+
WithExec([]string{
138+
"wget", fmt.Sprintf("https://apoxy-public-build-tools.s3.us-west-2.amazonaws.com/MacOSX14.sdk.tar.xz"),
139+
}).
140+
WithExec([]string{
141+
"tar", "-xf", "MacOSX14.sdk.tar.xz",
142+
}).
143+
WithExec([]string{
144+
"mv", "MacOSX14.sdk", "/macsdk",
145+
})
146+
}
147+
122148
return builder.
123149
WithEnvVariable("GOARCH", goarch).
124150
WithEnvVariable("GOOS", os).
151+
WithEnvVariable("CGO_ENABLED", "1").
152+
WithEnvVariable("CC", fmt.Sprintf("zig-wrapper cc --target=%s --sysroot=/macsdk -I/macsdk/usr/include -L/macsdk/usr/lib -F/macsdk/System/Library/Frameworks -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness -DZIG_STATIC_ZLIB=on", zigTarget)).
153+
WithEnvVariable("CXX", fmt.Sprintf("zig-wrapper c++ --target=%s --sysroot=/macsdk -I/macsdk/usr/include -L/macsdk/usr/lib -F/macsdk/System/Library/Frameworks -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness -DZIG_STATIC_ZLIB=on", zigTarget)).
125154
WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-"+goarch)).
126155
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
127156
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-"+goarch)).
128157
WithEnvVariable("GOCACHE", "/go/build-cache").
129-
WithExec([]string{"go", "build", "-o", "/apoxy", "-ldflags", strings.Join(ldFlags, " "), "."})
158+
WithExec([]string{"go", "build", "-o", "/apoxy", "-ldflags", strings.Join(ldFlags, " "), "-tags", "netgo", "."})
130159
}
131160

132161
// PublishGithubRelease publishes a CLI binary to GitHub releases.

0 commit comments

Comments
 (0)