From b7a0b29848d02d74f39090924b5db310fa644d32 Mon Sep 17 00:00:00 2001 From: Parth Patel <661497+parthpatel@users.noreply.github.com> Date: Wed, 13 Nov 2024 05:50:09 +0000 Subject: [PATCH] Fix link-time optimization to work correctly for unit tests (i.e. -flto flag) (#1290) * We compile various c files into object and package them into library (.a file) using ar to feed to unit tests. With new GCC versions, the objects inside such library don't participate in LTO process without additional flags. * Here is a direct quote from gcc documentation explaining this issue: "If you are not using a linker with plugin support and/or do not enable the linker plugin, then the objects inside libfoo.a are extracted and linked as usual, but they do not participate in the LTO optimization process. In order to make a static library suitable for both LTO optimization and usual linkage, compile its object files with -flto-ffat-lto-objects." * Read full documentation about -flto at https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html * Without this additional flag, I get following errors while executing "make test-unit". With this change, those errors go away. ``` ARCHIVE libvalkey.a ar: threads_mngr.o: plugin needed to handle lto object ... .. . /tmp/ccDYbMXL.ltrans0.ltrans.o: In function `dictClear': /local/workplace/elasticache/valkey/src/unit/../dict.c:776: undefined reference to `valkey_free' /local/workplace/elasticache/valkey/src/unit/../dict.c:770: undefined reference to `valkey_free' /tmp/ccDYbMXL.ltrans0.ltrans.o: In function `dictGetVal': ``` Signed-off-by: Parth Patel <661497+parthpatel@users.noreply.github.com> --- src/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Makefile b/src/Makefile index ae2de1c626..94c14237a1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -27,6 +27,7 @@ ifeq ($(OPTIMIZATION),-O3) else OPTIMIZATION+=-flto=auto endif + OPTIMIZATION += -ffat-lto-objects endif ifneq ($(OPTIMIZATION),-O0) OPTIMIZATION+=-fno-omit-frame-pointer