Skip to content

Commit f1c37d4

Browse files
committed
GNUmakefile: shrink TinyGo binaries on Linux
With these flags, the TinyGo binary gets 17MB (10.6%) smaller. That seems like a quite useful win for such a small change! This is only for Linux for now. MacOS and Windows can be tested later, the flags for those probably need to be modified. Originally inspired by: https://discourse.llvm.org/t/state-of-the-art-for-reducing-executable-size-with-heavily-optimized-program/87952/18 There are some other flags like -Wl,--pack-dyn-relocs=relr that did not shrink binary size in my testing, so I've left them out.
1 parent 632e5f9 commit f1c37d4

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

.github/workflows/linux.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
uses: actions/cache/restore@v4
7474
id: cache-llvm-build
7575
with:
76-
key: llvm-build-19-linux-alpine-v1
76+
key: llvm-build-19-linux-alpine-v2
7777
path: llvm-build
7878
- name: Build LLVM
7979
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
@@ -222,7 +222,7 @@ jobs:
222222
uses: actions/cache/restore@v4
223223
id: cache-llvm-build
224224
with:
225-
key: llvm-build-19-linux-asserts-v1
225+
key: llvm-build-19-linux-asserts-v2
226226
path: llvm-build
227227
- name: Build LLVM
228228
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
@@ -329,7 +329,7 @@ jobs:
329329
uses: actions/cache/restore@v4
330330
id: cache-llvm-build
331331
with:
332-
key: llvm-build-19-linux-${{ matrix.goarch }}-v3
332+
key: llvm-build-19-linux-${{ matrix.goarch }}-v4
333333
path: llvm-build
334334
- name: Build LLVM
335335
if: steps.cache-llvm-build.outputs.cache-hit != 'true'

GNUmakefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ ifeq (1, $(STATIC))
7979
BINARYEN_OPTION += -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static"
8080
endif
8181

82+
# Optimize the binary size for Linux.
83+
# These flags may work on other platforms, but have only been tested on Linux.
84+
ifeq ($(uname),Linux)
85+
LLVM_CFLAGS := -ffunction-sections -fdata-sections -fvisibility=hidden
86+
LLVM_LDFLAGS := -Wl,--gc-sections -Wl,--icf=all
87+
LLVM_OPTION += \
88+
-DCMAKE_C_FLAGS="$(LLVM_CFLAGS)" \
89+
-DCMAKE_CXX_FLAGS="$(LLVM_CFLAGS)"
90+
CGO_LDFLAGS += $(LLVM_LDFLAGS)
91+
endif
92+
8293
# Cross compiling support.
8394
ifneq ($(CROSS),)
8495
CC = $(CROSS)-gcc

0 commit comments

Comments
 (0)