Skip to content

Commit be33de1

Browse files
aykevldeadprogram
authored andcommitted
GNUmakefile: shrink TinyGo binaries on Linux
With these flags, the TinyGo binary gets 18.8MB (11.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. This also switches the linker to prefer mold or lld over the default linker, since the system linker is usually ld.bfd which is very slow. (Also, for some reason mold produces smaller binaries than lld).
1 parent e5bdfb0 commit be33de1

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

.github/workflows/linux.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
# tar: needed for actions/cache@v4
2727
# git+openssh: needed for checkout (I think?)
2828
# ruby: needed to install fpm
29-
run: apk add tar git openssh make g++ ruby-dev
29+
run: apk add tar git openssh make g++ ruby-dev mold
3030
- name: Work around CVE-2022-24765
3131
# We're not on a multi-user machine, so this is safe.
3232
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
@@ -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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ 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+
HAS_MOLD := $(shell command -v ld.mold 2> /dev/null)
86+
HAS_LLD := $(shell command -v ld.lld 2> /dev/null)
87+
LLVM_CFLAGS := -ffunction-sections -fdata-sections -fvisibility=hidden
88+
LLVM_LDFLAGS := -Wl,--gc-sections
89+
ifneq ($(HAS_MOLD),)
90+
# Mold might be slightly faster.
91+
LLVM_LDFLAGS += -fuse-ld=mold -Wl,--icf=all
92+
else ifneq ($(HAS_LLD),)
93+
# LLD is more commonly available.
94+
LLVM_LDFLAGS += -fuse-ld=lld -Wl,--icf=all
95+
endif
96+
LLVM_OPTION += \
97+
-DCMAKE_C_FLAGS="$(LLVM_CFLAGS)" \
98+
-DCMAKE_CXX_FLAGS="$(LLVM_CFLAGS)"
99+
CGO_LDFLAGS += $(LLVM_LDFLAGS)
100+
endif
101+
82102
# Cross compiling support.
83103
ifneq ($(CROSS),)
84104
CC = $(CROSS)-gcc

0 commit comments

Comments
 (0)