From bf13a64d9e01e4ce97486edbfc88213746040f9f Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Fri, 28 Oct 2022 15:15:59 -1000 Subject: [PATCH] makefile: Add explicit option LTO=1 to enable Link Time Optimization builds When building compiler optimized binaries with the gcc or clang compilers, invoking GNU make with LTO=1 on the command line will cause the build to use Link Time Optimization to maximally optimize the results. Link Time Optimization can report errors which aren't otherwise detected and will also take significantly longer to complete. --- makefile | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/makefile b/makefile index 24786e4a..28d2a45e 100644 --- a/makefile +++ b/makefile @@ -44,6 +44,13 @@ # If debugging is desired, then GNU make can be invoked with # DEBUG=1 on the command line. # +# When building compiler optimized binaries with the gcc or clang +# compilers, invoking GNU make with LTO=1 on the command line will +# cause the build to use Link Time Optimization to maximally optimize +# the results. Link Time Optimization can report errors which aren't +# otherwise detected and will also take significantly longer to +# complete. +# # The default build will run per simulator tests if they are # available. If building without running tests is desired, # then GNU make should be invoked with TESTS=0 on the command @@ -485,7 +492,7 @@ ifeq (${WIN32},) #*nix Environments (&& cygwin) endif OS_CCDEFS += -D_HPUX_SOURCE -D_LARGEFILE64_SOURCE OS_LDFLAGS += -Wl,+b: - NO_LTO = 1 + LTO = else LIBEXT = a endif @@ -509,14 +516,6 @@ ifeq (${WIN32},) #*nix Environments (&& cygwin) endif export CPATH = $(subst $() $(),:,$(INCPATH)) export LIBRARY_PATH = $(subst $() $(),:,$(LIBPATH)) - # Some gcc versions don't support LTO, so only use LTO when the compiler is known to support it - ifeq (,$(NO_LTO)) - ifneq (,$(GCC_VERSION)) - ifeq (,$(shell ${GCC} -v /dev/null 2>&1 | grep '\-\-enable-lto')) - LTO_EXCLUDE_VERSIONS += $(GCC_VERSION) - endif - endif - endif endif $(info lib paths are: ${LIBPATH}) $(info include paths are: ${INCPATH}) @@ -1268,18 +1267,13 @@ ifneq ($(DEBUG),) CFLAGS_G = -g -ggdb -g3 CFLAGS_O = -O0 BUILD_FEATURES = - debugging support + LTO = else ifneq (,$(findstring clang,$(COMPILER_NAME))$(findstring LLVM,$(COMPILER_NAME))) CFLAGS_O = -O2 -fno-strict-overflow - GCC_OPTIMIZERS_CMD = ${GCC} --help - NO_LTO = 1 + GCC_OPTIMIZERS_CMD = ${GCC} --help 2>&1 else - NO_LTO = 1 - ifeq (Darwin,$(OSTYPE)) - CFLAGS_O += -O4 -flto -fwhole-program - else - CFLAGS_O := -O2 - endif + CFLAGS_O := -O2 endif LDFLAGS_O = GCC_MAJOR_VERSION = $(firstword $(subst ., ,$(GCC_VERSION))) @@ -1295,9 +1289,6 @@ else ifneq (,$(GCC_COMMON_CMD)) GCC_OPTIMIZERS += $(shell $(GCC_COMMON_CMD)) endif - ifneq (,$(findstring $(GCC_VERSION),$(LTO_EXCLUDE_VERSIONS))) - NO_LTO = 1 - endif ifneq (,$(findstring -finline-functions,$(GCC_OPTIMIZERS))) CFLAGS_O += -finline-functions endif @@ -1316,13 +1307,19 @@ else ifneq (,$(findstring -fstrict-overflow,$(GCC_OPTIMIZERS))) CFLAGS_O += -fno-strict-overflow endif - ifeq (,$(NO_LTO)) + ifneq (,$(findstring $(GCC_VERSION),$(LTO_EXCLUDE_VERSIONS))) + LTO = + endif + ifneq (,$(LTO)) ifneq (,$(findstring -flto,$(GCC_OPTIMIZERS))) - CFLAGS_O += -flto -fwhole-program - LDFLAGS_O += -flto -fwhole-program + CFLAGS_O += -flto + ifneq (,$(and $(findstring gcc,$(COMPILER_NAME)),$(findstring -fwhole-program,$(GCC_OPTIMIZERS)))) + CFLAGS_O += -fwhole-program + endif + LTO_FEATURE = , with Link Time Optimization, endif endif - BUILD_FEATURES = - compiler optimizations and no debugging support + BUILD_FEATURES = - compiler optimizations$(LTO_FEATURE) and no debugging support endif ifneq (3,$(GCC_MAJOR_VERSION)) ifeq (,$(GCC_WARNINGS_CMD))