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 # If debugging is desired, then GNU make can be invoked with
# DEBUG=1 on the command line. # 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 # The default build will run per simulator tests if they are
# available. If building without running tests is desired, # available. If building without running tests is desired,
# then GNU make should be invoked with TESTS=0 on the command # then GNU make should be invoked with TESTS=0 on the command
@ -485,7 +492,7 @@ ifeq (${WIN32},) #*nix Environments (&& cygwin)
endif endif
OS_CCDEFS += -D_HPUX_SOURCE -D_LARGEFILE64_SOURCE OS_CCDEFS += -D_HPUX_SOURCE -D_LARGEFILE64_SOURCE
OS_LDFLAGS += -Wl,+b: OS_LDFLAGS += -Wl,+b:
NO_LTO = 1 LTO =
else else
LIBEXT = a LIBEXT = a
endif endif
@ -509,14 +516,6 @@ ifeq (${WIN32},) #*nix Environments (&& cygwin)
endif endif
export CPATH = $(subst $() $(),:,$(INCPATH)) export CPATH = $(subst $() $(),:,$(INCPATH))
export LIBRARY_PATH = $(subst $() $(),:,$(LIBPATH)) 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 endif
$(info lib paths are: ${LIBPATH}) $(info lib paths are: ${LIBPATH})
$(info include paths are: ${INCPATH}) $(info include paths are: ${INCPATH})
@ -1268,18 +1267,13 @@ ifneq ($(DEBUG),)
CFLAGS_G = -g -ggdb -g3 CFLAGS_G = -g -ggdb -g3
CFLAGS_O = -O0 CFLAGS_O = -O0
BUILD_FEATURES = - debugging support BUILD_FEATURES = - debugging support
LTO =
else else
ifneq (,$(findstring clang,$(COMPILER_NAME))$(findstring LLVM,$(COMPILER_NAME))) ifneq (,$(findstring clang,$(COMPILER_NAME))$(findstring LLVM,$(COMPILER_NAME)))
CFLAGS_O = -O2 -fno-strict-overflow CFLAGS_O = -O2 -fno-strict-overflow
GCC_OPTIMIZERS_CMD = ${GCC} --help GCC_OPTIMIZERS_CMD = ${GCC} --help 2>&1
NO_LTO = 1
else else
NO_LTO = 1 CFLAGS_O := -O2
ifeq (Darwin,$(OSTYPE))
CFLAGS_O += -O4 -flto -fwhole-program
else
CFLAGS_O := -O2
endif
endif endif
LDFLAGS_O = LDFLAGS_O =
GCC_MAJOR_VERSION = $(firstword $(subst ., ,$(GCC_VERSION))) GCC_MAJOR_VERSION = $(firstword $(subst ., ,$(GCC_VERSION)))
@ -1295,9 +1289,6 @@ else
ifneq (,$(GCC_COMMON_CMD)) ifneq (,$(GCC_COMMON_CMD))
GCC_OPTIMIZERS += $(shell $(GCC_COMMON_CMD)) GCC_OPTIMIZERS += $(shell $(GCC_COMMON_CMD))
endif endif
ifneq (,$(findstring $(GCC_VERSION),$(LTO_EXCLUDE_VERSIONS)))
NO_LTO = 1
endif
ifneq (,$(findstring -finline-functions,$(GCC_OPTIMIZERS))) ifneq (,$(findstring -finline-functions,$(GCC_OPTIMIZERS)))
CFLAGS_O += -finline-functions CFLAGS_O += -finline-functions
endif endif
@ -1316,13 +1307,19 @@ else
ifneq (,$(findstring -fstrict-overflow,$(GCC_OPTIMIZERS))) ifneq (,$(findstring -fstrict-overflow,$(GCC_OPTIMIZERS)))
CFLAGS_O += -fno-strict-overflow CFLAGS_O += -fno-strict-overflow
endif endif
ifeq (,$(NO_LTO)) ifneq (,$(findstring $(GCC_VERSION),$(LTO_EXCLUDE_VERSIONS)))
LTO =
endif
ifneq (,$(LTO))
ifneq (,$(findstring -flto,$(GCC_OPTIMIZERS))) ifneq (,$(findstring -flto,$(GCC_OPTIMIZERS)))
CFLAGS_O += -flto -fwhole-program CFLAGS_O += -flto
LDFLAGS_O += -flto -fwhole-program ifneq (,$(and $(findstring gcc,$(COMPILER_NAME)),$(findstring -fwhole-program,$(GCC_OPTIMIZERS))))
CFLAGS_O += -fwhole-program
endif
LTO_FEATURE = , with Link Time Optimization,
endif endif
endif endif
BUILD_FEATURES = - compiler optimizations and no debugging support BUILD_FEATURES = - compiler optimizations$(LTO_FEATURE) and no debugging support
endif endif
ifneq (3,$(GCC_MAJOR_VERSION)) ifneq (3,$(GCC_MAJOR_VERSION))
ifeq (,$(GCC_WARNINGS_CMD)) ifeq (,$(GCC_WARNINGS_CMD))