Skip to content

Commit a539c71

Browse files
authored
ci: Add CGO cross compilation support to release pipeline (#1903)
- Support CGO cross compilation for multiple architectures using Zig. - Download and link MacOSX SDK as needed by the MacOSX cross compilation. There is no official release for MacOSC SDK so I had to download from a third party repo. - Update dockerfile from using `gcr.io/distroless/static:nonroot` to `gcr.io/distroless/cc-debian12:nonroot` for C libraries that is needed for dynamic linking.
1 parent 975d02e commit a539c71

File tree

3 files changed

+317
-80
lines changed

3 files changed

+317
-80
lines changed

.ci/continuous.release.cloudbuild.yaml

Lines changed: 142 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,56 @@ steps:
3333
script: |
3434
go get -d ./...
3535
36+
- id: "install-zig"
37+
name: golang:1
38+
waitFor: ['-']
39+
volumes:
40+
- name: 'zig'
41+
path: '/zig-tools'
42+
script: |
43+
#!/usr/bin/env bash
44+
set -e
45+
apt-get update && apt-get install -y xz-utils
46+
curl -fL "https://ziglang.org/download/0.15.2/zig-x86_64-linux-0.15.2.tar.xz" -o zig.tar.xz
47+
tar -xf zig.tar.xz -C /zig-tools --strip-components=1
48+
49+
- id: "install-macos-sdk"
50+
name: golang:1
51+
waitFor: ['-']
52+
volumes:
53+
- name: 'macos-sdk'
54+
path: '/macos-sdk'
55+
script: |
56+
#!/usr/bin/env bash
57+
set -e
58+
apt-get update && apt-get install -y xz-utils
59+
echo "Downloading macOS 14.5 SDK..."
60+
curl -fL -o sdk.tar.xz https://github.com/alexey-lysiuk/macos-sdk/releases/download/14.5/MacOSX14.5.tar.xz
61+
62+
mkdir -p /macos-sdk/MacOSX14.5.sdk
63+
echo "Unpacking macOS 14.5 SDK..."
64+
tar -xf sdk.tar.xz -C /macos-sdk/MacOSX14.5.sdk --strip-components=1
65+
3666
- id: "build-linux-amd64"
3767
name: golang:1
38-
waitFor:
68+
waitFor:
3969
- "install-dependencies"
70+
- "install-zig"
4071
env:
4172
- 'GOPATH=/gopath'
73+
- 'CGO_ENABLED=1'
74+
- 'GOOS=linux'
75+
- 'GOARCH=amd64'
76+
- 'CC=/zig-tools/zig cc -target x86_64-linux-gnu'
77+
- 'CXX=/zig-tools/zig c++ -target x86_64-linux-gnu'
4278
volumes:
4379
- name: 'go'
4480
path: '/gopath'
81+
- name: 'zig'
82+
path: '/zig-tools'
4583
script: |
4684
#!/usr/bin/env bash
47-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
48-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.linux.amd64
85+
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.linux.amd64
4986
5087
- id: "store-linux-amd64"
5188
name: "gcr.io/cloud-builders/gcloud:latest"
@@ -57,33 +94,53 @@ steps:
5794
5895
- id: "build-linux-amd64-geminicli"
5996
name: golang:1
60-
waitFor:
97+
waitFor:
6198
- "install-dependencies"
62-
env:
99+
- "install-zig"
100+
env:
63101
- 'GOPATH=/gopath'
102+
- 'CGO_ENABLED=1'
103+
- 'GOOS=linux'
104+
- 'GOARCH=amd64'
105+
- 'CC=/zig-tools/zig cc -target x86_64-linux-gnu'
106+
- 'CXX=/zig-tools/zig c++ -target x86_64-linux-gnu'
64107
volumes:
65108
- name: 'go'
66109
path: '/gopath'
110+
- name: 'zig'
111+
path: '/zig-tools'
67112
script: |
68113
#!/usr/bin/env bash
69114
export VERSION=$(cat ./cmd/version.txt)
70-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
71-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.linux.amd64
72-
115+
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.linux.amd64
73116
74117
- id: "build-darwin-arm64"
75118
name: golang:1
76-
waitFor:
119+
waitFor:
77120
- "install-dependencies"
121+
- "install-zig"
122+
- "install-macos-sdk"
78123
env:
79124
- 'GOPATH=/gopath'
125+
- 'CGO_ENABLED=1'
126+
- 'GOOS=darwin'
127+
- 'GOARCH=arm64'
128+
- 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
129+
- 'MACOS_MIN_VER=10.14'
130+
- 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
131+
- 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
132+
- 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
133+
- 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
80134
volumes:
81135
- name: 'go'
82136
path: '/gopath'
137+
- name: 'zig'
138+
path: '/zig-tools'
139+
- name: 'macos-sdk'
140+
path: '/macos-sdk'
83141
script: |
84142
#!/usr/bin/env bash
85-
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 \
86-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.darwin.arm64
143+
go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.darwin.arm64
87144
88145
- id: "store-darwin-arm64"
89146
name: "gcr.io/cloud-builders/gcloud:latest"
@@ -95,32 +152,59 @@ steps:
95152
96153
- id: "build-darwin-arm64-geminicli"
97154
name: golang:1
98-
waitFor:
155+
waitFor:
99156
- "install-dependencies"
100-
env:
157+
- "install-zig"
158+
- "install-macos-sdk"
159+
env:
101160
- 'GOPATH=/gopath'
161+
- 'CGO_ENABLED=1'
162+
- 'GOOS=darwin'
163+
- 'GOARCH=arm64'
164+
- 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
165+
- 'MACOS_MIN_VER=10.14'
166+
- 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
167+
- 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
168+
- 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
169+
- 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target aarch64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
102170
volumes:
103171
- name: 'go'
104172
path: '/gopath'
173+
- name: 'zig'
174+
path: '/zig-tools'
175+
- name: 'macos-sdk'
176+
path: '/macos-sdk'
105177
script: |
106178
#!/usr/bin/env bash
107-
export VERSION=$(cat ./cmd/version.txt)
108-
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 \
109-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.darwin.arm64
179+
go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.darwin.arm64
110180
111181
- id: "build-darwin-amd64"
112182
name: golang:1
113-
waitFor:
183+
waitFor:
114184
- "install-dependencies"
185+
- "install-zig"
186+
- "install-macos-sdk"
115187
env:
116188
- 'GOPATH=/gopath'
189+
- 'CGO_ENABLED=1'
190+
- 'GOOS=darwin'
191+
- 'GOARCH=amd64'
192+
- 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
193+
- 'MACOS_MIN_VER=10.14'
194+
- 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
195+
- 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
196+
- 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
197+
- 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
117198
volumes:
118199
- name: 'go'
119200
path: '/gopath'
201+
- name: 'zig'
202+
path: '/zig-tools'
203+
- name: 'macos-sdk'
204+
path: '/macos-sdk'
120205
script: |
121206
#!/usr/bin/env bash
122-
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 \
123-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.darwin.amd64
207+
go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.darwin.amd64
124208
125209
- id: "store-darwin-amd64"
126210
name: "gcr.io/cloud-builders/gcloud:latest"
@@ -132,32 +216,52 @@ steps:
132216
133217
- id: "build-darwin-amd64-geminicli"
134218
name: golang:1
135-
waitFor:
219+
waitFor:
136220
- "install-dependencies"
137-
env:
221+
- "install-zig"
222+
- "install-macos-sdk"
223+
env:
138224
- 'GOPATH=/gopath'
225+
- 'CGO_ENABLED=1'
226+
- 'GOOS=darwin'
227+
- 'GOARCH=amd64'
228+
- 'SDK_PATH=/macos-sdk/MacOSX14.5.sdk'
229+
- 'MACOS_MIN_VER=10.14'
230+
- 'CGO_LDFLAGS=-mmacosx-version-min=10.14 --sysroot /macos-sdk/MacOSX14.5.sdk -F/macos-sdk/MacOSX14.5.sdk/System/Library/Frameworks -L/usr/lib'
231+
- 'COMMON_FLAGS=-mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
232+
- 'CC=/zig-tools/zig cc -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
233+
- 'CXX=/zig-tools/zig c++ -mmacosx-version-min=10.14 -target x86_64-macos.11.0.0-none -isysroot /macos-sdk/MacOSX14.5.sdk -iwithsysroot /usr/include -iframeworkwithsysroot /System/Library/Frameworks'
139234
volumes:
140235
- name: 'go'
141236
path: '/gopath'
237+
- name: 'zig'
238+
path: '/zig-tools'
239+
- name: 'macos-sdk'
240+
path: '/macos-sdk'
142241
script: |
143242
#!/usr/bin/env bash
144-
export VERSION=$(cat ./cmd/version.txt)
145-
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 \
146-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.darwin.amd64
243+
go build -trimpath -buildmode=pie -ldflags "-s -w -X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.darwin.amd64
147244
148245
- id: "build-windows-amd64"
149246
name: golang:1
150-
waitFor:
247+
waitFor:
151248
- "install-dependencies"
249+
- "install-zig"
152250
env:
153251
- 'GOPATH=/gopath'
252+
- 'CGO_ENABLED=1'
253+
- 'GOOS=windows'
254+
- 'GOARCH=amd64'
255+
- 'CC=/zig-tools/zig cc -target x86_64-windows-gnu'
256+
- 'CXX=/zig-tools/zig c++ -target x86_64-windows-gnu'
154257
volumes:
155258
- name: 'go'
156259
path: '/gopath'
260+
- name: 'zig'
261+
path: '/zig-tools'
157262
script: |
158263
#!/usr/bin/env bash
159-
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
160-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.windows.amd64
264+
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.windows.amd64
161265
162266
- id: "store-windows-amd64"
163267
name: "gcr.io/cloud-builders/gcloud:latest"
@@ -169,18 +273,25 @@ steps:
169273
170274
- id: "build-windows-amd64-geminicli"
171275
name: golang:1
172-
waitFor:
276+
waitFor:
173277
- "install-dependencies"
174-
env:
278+
- "install-zig"
279+
env:
175280
- 'GOPATH=/gopath'
281+
- 'CGO_ENABLED=1'
282+
- 'GOOS=windows'
283+
- 'GOARCH=amd64'
284+
- 'CC=/zig-tools/zig cc -target x86_64-windows-gnu'
285+
- 'CXX=/zig-tools/zig c++ -target x86_64-windows-gnu'
176286
volumes:
177287
- name: 'go'
178288
path: '/gopath'
289+
- name: 'zig'
290+
path: '/zig-tools'
179291
script: |
180292
#!/usr/bin/env bash
181293
export VERSION=$(cat ./cmd/version.txt)
182-
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
183-
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.windows.amd64
294+
go build -ldflags "-X github.com/googleapis/genai-toolbox/cmd.commitSha=$(git rev-parse --short HEAD)" -o toolbox.geminicli.windows.amd64
184295
185296
options:
186297
automapSubstitutions: true

0 commit comments

Comments
 (0)