@@ -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+
3034type ApoxyCli struct {}
3135
3236func canonArchFromGoArch (goarch string ) string {
@@ -75,18 +79,18 @@ func (m *ApoxyCli) BuilderContainer(ctx context.Context, src *dagger.Directory)
7579WithMountedCache ("/go/build-cache" , dag .CacheVolume ("go-build" )).
7680WithEnvVariable ("GOCACHE" , "/go/build-cache" ).
7781// Install Zig toolchain.
78- WithExec ([]string {"apt-get" , "update" , "-yq" }).
82+ WithExec ([]string {"apt-get" , "update" }).
7983WithExec ([]string {
8084"apt-get" , "install" , "-yq" , "xz-utils" , "clang" ,
8185}).
8286WithExec ([]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}).
8589WithExec ([]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}).
8892WithExec ([]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}).
9195WithNewFile ("/bin/zig-wrapper" , zigWrapperScript , dagger.ContainerWithNewFileOpts {
9296Permissions : 0755 ,
@@ -114,19 +118,44 @@ func (m *ApoxyCli) BuildCLI(
114118fmt .Sprintf ("-X '%s/build.BuildVersion=%s'" , pkg , tag ),
115119fmt .Sprintf ("-X '%s/build.BuildDate=%s'" , pkg , date ),
116120fmt .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
121131builder := 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+
122148return builder .
123149WithEnvVariable ("GOARCH" , goarch ).
124150WithEnvVariable ("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 )).
125154WithMountedCache ("/go/pkg/mod" , dag .CacheVolume ("go-mod-" + goarch )).
126155WithEnvVariable ("GOMODCACHE" , "/go/pkg/mod" ).
127156WithMountedCache ("/go/build-cache" , dag .CacheVolume ("go-build-" + goarch )).
128157WithEnvVariable ("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