Skip to content

Commit fb97f09

Browse files
committed
Enable thin lto
1 parent 92d4602 commit fb97f09

File tree

6 files changed

+155
-41
lines changed

6 files changed

+155
-41
lines changed

.github/workflows/naive-build.yml

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ concurrency:
1616
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
1717
cancel-in-progress: true
1818

19+
env:
20+
CACHE_EPOCH: 1
21+
1922
jobs:
2023
linux:
2124
runs-on: ubuntu-22.04
@@ -36,19 +39,26 @@ jobs:
3639
- name: Get Chromium version
3740
id: chromium
3841
run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
42+
- name: Get clang toolchain version
43+
id: clang
44+
shell: bash
45+
run: |
46+
clang_revision=$(sed -n "s/^CLANG_REVISION = '\\(.*\\)'$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
47+
clang_subrevision=$(sed -n "s/^CLANG_SUB_REVISION = \\([0-9]*\\)$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
48+
echo "version=${clang_revision}-${clang_subrevision}" >> $GITHUB_OUTPUT
3949
- name: Cache build artifacts
4050
id: build-cache
4151
uses: actions/cache@v4
4252
with:
4353
path: naiveproxy/src/out
44-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-${{ matrix.arch }}
54+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-${{ matrix.arch }}
4555
- name: Cache ccache files
4656
if: steps.build-cache.outputs.cache-hit != 'true'
4757
uses: actions/cache@v4
4858
with:
4959
path: ~/.cache/ccache
50-
key: ccache-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
51-
restore-keys: ccache-linux-${{ matrix.arch }}-
60+
key: ccache-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
61+
restore-keys: ccache-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
5262
- name: Install packages
5363
if: steps.build-cache.outputs.cache-hit != 'true'
5464
run: |
@@ -74,8 +84,8 @@ jobs:
7484
naiveproxy/src/third_party/llvm-build
7585
naiveproxy/src/gn/out
7686
naiveproxy/src/chrome/build/pgo_profiles
77-
key: toolchain-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
78-
restore-keys: toolchain-linux-${{ matrix.arch }}-
87+
key: toolchain-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
88+
restore-keys: toolchain-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
7989
- name: Download clang and sysroot
8090
run: go run ./cmd/build-naive --target=linux/${{ matrix.arch }} download-toolchain
8191
- name: Install QEMU for cross-platform testing
@@ -152,19 +162,26 @@ jobs:
152162
- name: Get Chromium version
153163
id: chromium
154164
run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
165+
- name: Get clang toolchain version
166+
id: clang
167+
shell: bash
168+
run: |
169+
clang_revision=$(sed -n "s/^CLANG_REVISION = '\\(.*\\)'$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
170+
clang_subrevision=$(sed -n "s/^CLANG_SUB_REVISION = \\([0-9]*\\)$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
171+
echo "version=${clang_revision}-${clang_subrevision}" >> $GITHUB_OUTPUT
155172
- name: Cache build artifacts
156173
id: build-cache
157174
uses: actions/cache@v4
158175
with:
159176
path: naiveproxy/src/out
160-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-${{ matrix.name }}
177+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-${{ matrix.name }}
161178
- name: Cache ccache files
162179
if: steps.build-cache.outputs.cache-hit != 'true'
163180
uses: actions/cache@v4
164181
with:
165182
path: ~/Library/Caches/ccache
166-
key: ccache-darwin-${{ matrix.name }}-${{ steps.chromium.outputs.version }}
167-
restore-keys: ccache-darwin-${{ matrix.name }}-
183+
key: ccache-darwin-${{ matrix.name }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
184+
restore-keys: ccache-darwin-${{ matrix.name }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
168185
- name: Install tools
169186
if: steps.build-cache.outputs.cache-hit != 'true'
170187
run: brew install ninja ccache
@@ -184,6 +201,7 @@ jobs:
184201
- name: Build and run test binary
185202
if: matrix.target == 'darwin/arm64'
186203
run: |
204+
eval $(go run ./cmd/build-naive --target=darwin/arm64 env --export)
187205
CGO_ENABLED=1 go test -c -o cronet.test .
188206
./cronet.test -test.v -test.run=TestEngineVersion
189207
- uses: actions/upload-artifact@v4
@@ -215,19 +233,33 @@ jobs:
215233
id: chromium
216234
shell: bash
217235
run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
236+
- name: Get clang toolchain version
237+
id: clang
238+
shell: bash
239+
run: |
240+
clang_revision=$(sed -n "s/^CLANG_REVISION = '\\(.*\\)'$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
241+
clang_subrevision=$(sed -n "s/^CLANG_SUB_REVISION = \\([0-9]*\\)$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
242+
echo "version=${clang_revision}-${clang_subrevision}" >> $GITHUB_OUTPUT
243+
- name: Get clang toolchain version
244+
id: clang
245+
shell: bash
246+
run: |
247+
clang_revision=$(sed -n "s/^CLANG_REVISION = '\\(.*\\)'$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
248+
clang_subrevision=$(sed -n "s/^CLANG_SUB_REVISION = \\([0-9]*\\)$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
249+
echo "version=${clang_revision}-${clang_subrevision}" >> $GITHUB_OUTPUT
218250
- name: Cache build artifacts
219251
id: build-cache
220252
uses: actions/cache@v4
221253
with:
222254
path: naiveproxy/src/out
223-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-windows-${{ matrix.arch }}
255+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-windows-${{ matrix.arch }}
224256
- name: Cache sccache files
225257
if: steps.build-cache.outputs.cache-hit != 'true'
226258
uses: actions/cache@v4
227259
with:
228260
path: ~/AppData/Local/Mozilla/sccache
229-
key: sccache-windows-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
230-
restore-keys: sccache-windows-${{ matrix.arch }}-
261+
key: sccache-windows-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
262+
restore-keys: sccache-windows-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
231263
- name: Install tools
232264
if: steps.build-cache.outputs.cache-hit != 'true'
233265
run: |
@@ -289,19 +321,26 @@ jobs:
289321
- name: Get Chromium version
290322
id: chromium
291323
run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
324+
- name: Get clang toolchain version
325+
id: clang
326+
shell: bash
327+
run: |
328+
clang_revision=$(sed -n "s/^CLANG_REVISION = '\\(.*\\)'$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
329+
clang_subrevision=$(sed -n "s/^CLANG_SUB_REVISION = \\([0-9]*\\)$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
330+
echo "version=${clang_revision}-${clang_subrevision}" >> $GITHUB_OUTPUT
292331
- name: Cache build artifacts
293332
id: build-cache
294333
uses: actions/cache@v4
295334
with:
296335
path: naiveproxy/src/out
297-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-android-${{ matrix.arch }}
336+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-android-${{ matrix.arch }}
298337
- name: Cache ccache files
299338
if: steps.build-cache.outputs.cache-hit != 'true'
300339
uses: actions/cache@v4
301340
with:
302341
path: ~/.cache/ccache
303-
key: ccache-android-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
304-
restore-keys: ccache-android-${{ matrix.arch }}-
342+
key: ccache-android-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
343+
restore-keys: ccache-android-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
305344
- name: Install packages
306345
if: steps.build-cache.outputs.cache-hit != 'true'
307346
run: |
@@ -327,6 +366,7 @@ jobs:
327366
ndk-version: r28
328367
- name: Build test binary
329368
run: |
369+
eval $(go run ./cmd/build-naive --target=android/${{ matrix.arch }} env --export)
330370
CC=${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin/${{ matrix.cc }} \
331371
CGO_ENABLED=1 GOOS=android GOARCH=${{ matrix.arch }} go test -c -o cronet.test .
332372
- uses: actions/upload-artifact@v4
@@ -356,19 +396,26 @@ jobs:
356396
- name: Get Chromium version
357397
id: chromium
358398
run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
399+
- name: Get clang toolchain version
400+
id: clang
401+
shell: bash
402+
run: |
403+
clang_revision=$(sed -n "s/^CLANG_REVISION = '\\(.*\\)'$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
404+
clang_subrevision=$(sed -n "s/^CLANG_SUB_REVISION = \\([0-9]*\\)$/\\1/p" naiveproxy/src/tools/clang/scripts/update.py)
405+
echo "version=${clang_revision}-${clang_subrevision}" >> $GITHUB_OUTPUT
359406
- name: Cache build artifacts
360407
id: build-cache
361408
uses: actions/cache@v4
362409
with:
363410
path: naiveproxy/src/out
364-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-musl-${{ matrix.arch }}
411+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-musl-${{ matrix.arch }}
365412
- name: Cache ccache files
366413
if: steps.build-cache.outputs.cache-hit != 'true'
367414
uses: actions/cache@v4
368415
with:
369416
path: ~/.cache/ccache
370-
key: ccache-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
371-
restore-keys: ccache-linux-musl-${{ matrix.arch }}-
417+
key: ccache-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
418+
restore-keys: ccache-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
372419
- name: Install packages
373420
if: steps.build-cache.outputs.cache-hit != 'true'
374421
run: |
@@ -394,8 +441,8 @@ jobs:
394441
naiveproxy/src/third_party/llvm-build
395442
naiveproxy/src/gn/out
396443
naiveproxy/src/chrome/build/pgo_profiles
397-
key: toolchain-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
398-
restore-keys: toolchain-linux-musl-${{ matrix.arch }}-
444+
key: toolchain-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
445+
restore-keys: toolchain-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
399446
- name: Download toolchain
400447
run: go run ./cmd/build-naive --target=linux/${{ matrix.arch }} --libc=musl download-toolchain
401448
- name: Install QEMU for cross-platform testing
@@ -514,7 +561,7 @@ jobs:
514561
uses: actions/cache/restore@v4
515562
with:
516563
path: naiveproxy/src/out
517-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}
564+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}
518565

519566
- name: Restore toolchain cache (Linux)
520567
if: runner.os == 'Linux'
@@ -524,8 +571,8 @@ jobs:
524571
naiveproxy/src/third_party/llvm-build
525572
naiveproxy/src/gn/out
526573
naiveproxy/src/chrome/build/pgo_profiles
527-
key: toolchain-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}-${{ steps.chromium.outputs.version }}
528-
restore-keys: toolchain-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}-
574+
key: toolchain-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}
575+
restore-keys: toolchain-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}-${{ steps.chromium.outputs.version }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-
529576

530577
- name: Download toolchain (Linux)
531578
if: runner.os == 'Linux'
@@ -537,7 +584,7 @@ jobs:
537584
uses: actions/cache/restore@v4
538585
with:
539586
path: naiveproxy/src/out
540-
key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-${{ contains(matrix.target, 'amd64') && 'darwin-amd64' || 'darwin-arm64' }}
587+
key: naive-build-${{ steps.naive.outputs.commit }}-${{ steps.clang.outputs.version }}-${{ env.CACHE_EPOCH }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-${{ contains(matrix.target, 'amd64') && 'darwin-amd64' || 'darwin-arm64' }}
541588

542589
# Install iperf3
543590
- name: Install iperf3 (Linux)

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,42 @@ Go bindings for [naiveproxy](https://github.com/klzgrad/naiveproxy).
4343
| macOS / iOS | macOS Xcode | - |
4444
| iOS simulator/ tvOS / tvOS simulator | macOS Xcode + SagerNet/gomobile | - |
4545
| Windows | - | `CGO_ENABLED=0 -tags with_purego` |
46-
| Android | Android NDK | - |
46+
| Android | Chromium toolchain + Android NDK | - |
4747

48-
## Linux Build instructions
48+
## Repository setup
4949

5050
```bash
5151
git clone --recursive --depth=1 https://github.com/sagernet/cronet-go.git
5252
cd cronet-go
53+
```
54+
55+
## Linux Build instructions
56+
57+
```bash
5358
go run ./cmd/build-naive --target=linux/amd64 download-toolchain
5459
#go run ./cmd/build-naive --target=linux/amd64 --libc=musl download-toolchain
5560

56-
# Outputs CC, CXX, and CGO_LDFLAGS=-fuse-ld=lld
57-
export $(go run ./cmd/build-naive --target=linux/amd64 env)
58-
#export $(go run ./cmd/build-naive --target=linux/amd64 --libc=musl env)
61+
# Outputs CC, CXX, and CGO_LDFLAGS for the Chromium toolchain
62+
eval $(go run ./cmd/build-naive --target=linux/amd64 env --export)
63+
#eval $(go run ./cmd/build-naive --target=linux/amd64 --libc=musl env --export)
5964

6065
cd /path/to/your/project
6166
go build
6267
# go build -tags with_musl
6368
```
6469

70+
## Android Build Instructions
71+
72+
```bash
73+
go run ./cmd/build-naive --target=android/arm64 download-toolchain
74+
75+
# Outputs CGO_LDFLAGS with Chromium ld.lld
76+
eval $(go run ./cmd/build-naive --target=android/arm64 env --export)
77+
78+
cd /path/to/your/project
79+
CGO_ENABLED=1 GOOS=android GOARCH=arm64 go build
80+
```
81+
6582
### Directories to cache
6683

6784
```yaml

cmd/build-naive/cmd_build.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func buildTarget(t Target) {
192192
"is_debug=false",
193193
"is_clang=true",
194194
"use_clang_modules=false",
195-
"use_thin_lto=false", // Disable ThinLTO so static lib can be linked with system clang
195+
// "use_thin_lto=false", // Disable ThinLTO so static lib can be linked with system clang
196196
"fatal_linker_warnings=false",
197197
"treat_warnings_as_errors=false",
198198
"is_cronet_build=true",
@@ -217,7 +217,6 @@ func buildTarget(t Target) {
217217
fmt.Sprintf("target_os=\"%s\"", t.OS),
218218
fmt.Sprintf("target_cpu=\"%s\"", t.CPU),
219219
}
220-
221220
switch t.OS {
222221
case "mac":
223222
args = append(args, "use_sysroot=false")
@@ -240,9 +239,9 @@ func buildTarget(t Target) {
240239
args = append(args,
241240
"use_sysroot=true",
242241
fmt.Sprintf("target_sysroot=\"//%s\"", sysrootDirectory),
243-
"build_static=true", // Static linking for musl
244-
"use_allocator_shim=false", // Disable allocator shim for musl compatibility
245-
"use_partition_alloc=false", // Disable PartitionAlloc to avoid GetStackTop() crash on musl
242+
"build_static=true", // Static linking for musl
243+
"use_allocator_shim=false", // Disable allocator shim for musl compatibility
244+
"use_partition_alloc=false", // Disable PartitionAlloc to avoid GetStackTop() crash on musl
246245
)
247246
if t.CPU == "x64" {
248247
args = append(args, "use_cfi_icall=false", "is_cfi=false")
@@ -252,7 +251,7 @@ func buildTarget(t Target) {
252251
case "android":
253252
args = append(args,
254253
"use_sysroot=false",
255-
"default_min_sdk_version=23",
254+
fmt.Sprintf("default_min_sdk_version=%d", androidMinimumApiLevel),
256255
"is_high_end_android=true",
257256
"android_ndk_major_version=28",
258257
)

0 commit comments

Comments
 (0)