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.
This commit is contained in:
Mark Pizzolato 2022-10-28 15:15:59 -10:00
parent 0cc7747202
commit bf13a64d9e

View file

@ -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,19 +1267,14 @@ 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
else
NO_LTO = 1
ifeq (Darwin,$(OSTYPE))
CFLAGS_O += -O4 -flto -fwhole-program
GCC_OPTIMIZERS_CMD = ${GCC} --help 2>&1
else
CFLAGS_O := -O2
endif
endif
LDFLAGS_O =
GCC_MAJOR_VERSION = $(firstword $(subst ., ,$(GCC_VERSION)))
ifneq (3,$(GCC_MAJOR_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))