Skip to content

Commit

Permalink
Improve the handling of optimization flags, add --enable-lto.
Browse files Browse the repository at this point in the history
* Added --enable-lto for enabling link-time optimization.
* The default debug optimization level is now -Og.
* Sanitizer flags are now applied independently of release/debug.
* Removed redundant optimization flags from console ports.
  • Loading branch information
AliceLR committed Jan 14, 2024
1 parent 289b935 commit c07f17e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 46 deletions.
37 changes: 22 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -225,49 +225,56 @@ PTHREAD_LDFLAGS ?= -lpthread
#
# Set up general CFLAGS/LDFLAGS
#
OPTIMIZE_CFLAGS ?= -O3
DEBUG_CFLAGS ?= -O0

ifeq (${DEBUG},1)
#
# Usually, just disable the optimizer for "true" debug builds and
# define "DEBUG" to enable optional code at compile time.
# Optimized builds have assert() compiled out.
#
# Sanitizer builds turn on some optimizations for the sake of being usable.
ifneq (${DEBUG},1)
OPTIMIZE_FLAGS := -O3 ${OPTIMIZE_CFLAGS} ${OPTIMIZE_FLAGS}
OPTIMIZE_DEF ?= -DNDEBUG
else
OPTIMIZE_FLAGS := -Og ${DEBUG_CFLAGS} ${OPTIMIZE_FLAGS}
OPTIMIZE_DEF ?= -DDEBUG
endif

#
# Enable a sanitizer and its corresponding extra options, if selected.
#
ifeq (${SANITIZER},address)
DEBUG_CFLAGS := -fsanitize=address -O1 -fno-omit-frame-pointer
OPTIMIZE_FLAGS += -fsanitize=address -fno-omit-frame-pointer
endif
ifeq (${SANITIZER},undefined)
# Signed integer overflows (shift-base, signed-integer-overflow)
# are pretty much inevitable in Robotic, so ignore them.
DEBUG_CFLAGS := -fsanitize=undefined -O0 -fno-omit-frame-pointer \
OPTIMIZE_FLAGS += -fsanitize=undefined -fno-omit-frame-pointer \
-fno-sanitize-recover=all -fno-sanitize=shift-base,signed-integer-overflow
endif
ifeq (${SANITIZER},thread)
DEBUG_CFLAGS := -fsanitize=thread -O2 -fno-omit-frame-pointer -fPIE
OPTIMIZE_FLAGS += -fsanitize=thread -fno-omit-frame-pointer -fPIE
ARCH_EXE_LDFLAGS += -pie
endif
ifeq (${SANITIZER},memory)
# Note: to be useful, this requires a fairly special build with most
# external libraries turned off or re-built with instrumentation.
# This sanitizer is only implemented by clang.
DEBUG_CFLAGS := -fsanitize=memory -O1 -fno-omit-frame-pointer -fPIC \
OPTIMIZE_FLAGS += -fsanitize=memory -fno-omit-frame-pointer -fPIC \
-fsanitize-recover=memory -fsanitize-memory-track-origins=2
ARCH_EXE_LDFLAGS += -pie
endif

CFLAGS = ${DEBUG_CFLAGS} -DDEBUG
CXXFLAGS = ${DEBUG_CFLAGS} -DDEBUG
LDFLAGS += ${DEBUG_CFLAGS}
else
#
# Optimized builds have assert() compiled out
# Enable link-time optimization.
#
CFLAGS += ${OPTIMIZE_CFLAGS} -DNDEBUG
CXXFLAGS += ${OPTIMIZE_CFLAGS} -DNDEBUG
ifeq (${LTO},1)
OPTIMIZE_FLAGS += -flto
endif

CFLAGS += ${OPTIMIZE_FLAGS} ${OPTIMIZE_DEF}
CXXFLAGS += ${OPTIMIZE_FLAGS} ${OPTIMIZE_DEF}
LDFLAGS += ${OPTIMIZE_FLAGS}

#
# Enable C++11 for compilers that support it.
# Anything actually using C++11 should be optional or platform-specific,
Expand Down
8 changes: 3 additions & 5 deletions arch/3ds/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ PORTLIBS_LIBS := $(foreach dir, $(PORTLIBS), -L$(dir)/lib)
EXTRA_INCLUDES := -isystem $(CTRULIB)/include ${PORTLIBS_INCLUDES}
EXTRA_LIBS := -L$(CTRULIB)/lib ${PORTLIBS_LIBS}
ifeq (${DEBUG},1)
ARCH_CFLAGS += -Og
EXTRA_LIBS += -lcitro3dd -lctrud -lpng16 -lz
EXTRA_LIBS += -lcitro3dd -lctrud -lpng16 -lz
else
OPTIMIZE_CFLAGS = -O3 -fomit-frame-pointer -ffunction-sections
# OPTIMIZE_CFLAGS += -flto
EXTRA_LIBS += -lcitro3d -lctru -lpng16 -lz
OPTIMIZE_FLAGS += -fomit-frame-pointer -ffunction-sections
EXTRA_LIBS += -lcitro3d -lctru -lpng16 -lz
endif

MACHDEP = -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -mword-relocations
Expand Down
9 changes: 3 additions & 6 deletions arch/emscripten/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,10 @@ ARCH_LDFLAGS += -g4 --source-map-base ${SOURCE_MAP_BASE}
endif

ifeq (${DEBUG},1)
ARCH_CFLAGS += -s ASSERTIONS=1 --profiling-funcs
ARCH_LDFLAGS += -s ASSERTIONS=1 --profiling-funcs
EMSCRIPTEN_FRONTEND_BUILD_PROFILE = build
OPTIMIZE_FLAGS += -s ASSERTIONS=1 --profiling-funcs
EMSCRIPTEN_FRONTEND_BUILD_PROFILE = build
else
ARCH_LDFLAGS += -O3
OPTIMIZE_CFLAGS = -O3
EMSCRIPTEN_FRONTEND_BUILD_PROFILE = build-release
EMSCRIPTEN_FRONTEND_BUILD_PROFILE = build-release
endif

clean:
Expand Down
10 changes: 5 additions & 5 deletions arch/switch/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ SUPPRESS_HOST_TARGETS ?= 1
MACHDEP := -D__SWITCH__ -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE

ifeq (${DEBUG},1)
#DRM_NOUVEAU := drm_nouveaud
NX := nxd
#DRM_NOUVEAU := drm_nouveaud
NX := nxd
else
OPTIMIZE_CFLAGS := -O3 -ffunction-sections
#DRM_NOUVEAU := drm_nouveau
NX := nx
OPTIMIZE_FLAGS += -ffunction-sections
#DRM_NOUVEAU := drm_nouveau
NX := nx
endif

MESA_LIBS :=
Expand Down
6 changes: 3 additions & 3 deletions arch/wiiu/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ SUPPRESS_HOST_TARGETS ?= 1
MACHDEP := -DESPRESSO -mcpu=750 -meabi -mhard-float

ifeq (${DEBUG},1)
WUT := wutd
WUT := wutd
else
OPTIMIZE_CFLAGS := -O3 -ffunction-sections
WUT := wut
OPTIMIZE_FLAGS += -ffunction-sections
WUT := wut
endif

SDL_PREFIX := ${PORTLIBS_PATH}/wiiu
Expand Down
31 changes: 24 additions & 7 deletions config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ usage() {
echo " --as-needed-hack Pass --as-needed through to GNU ld."
echo " --enable-release Optimize and remove debugging code."
echo " --enable-verbose Build system is always verbose (V=1)."
echo " --optimize-speed Perform speed optimizations (-O3, default)."
echo " --optimize-size Perform size optimizations (-Os)."
echo " --enable-lto Perform link-time optimizations (-flto)."
echo " --enable-asan Enable AddressSanitizer for debug builds."
echo " --enable-msan Enable MemorySanitizer for debug builds."
echo " --enable-tsan Enable ThreadSanitizer for debug builds."
Expand Down Expand Up @@ -155,6 +157,7 @@ DATE_STAMP="true"
AS_NEEDED="false"
RELEASE="false"
OPT_SIZE="false"
LTO="false"
SANITIZER="false"
ANALYZER="false"
STACK_PROTECTOR="true"
Expand Down Expand Up @@ -299,6 +302,9 @@ while [ "$1" != "" ]; do
[ "$1" = "--optimize-size" ] && OPT_SIZE="true"
[ "$1" = "--optimize-speed" ] && OPT_SIZE="false"

[ "$1" = "--enable-lto" ] && LTO="true"
[ "$1" = "--disable-lto" ] && LTO="false"

[ "$1" = "--enable-asan" ] && SANITIZER="address"
[ "$1" = "--disable-asan" ] && SANITIZER="false"

Expand Down Expand Up @@ -1300,19 +1306,22 @@ if [ "$RELEASE" = "true" ]; then
#
if [ "$OPT_SIZE" = "true" ]; then
echo "Optimizing for size."
echo "OPTIMIZE_CFLAGS=-Os" >> platform.inc
echo "OPTIMIZE_FLAGS=-Os" >> platform.inc
else
echo "Optimizing for speed."
fi
#
# Enable link-time optimizations.
#
if [ "$LTO" = "true" ]; then
echo "Link-time optimizations enabled."
echo "LTO=1" >> platform.inc
else
echo "Link-time optimizations disabled."
fi
else
echo "Disabling optimization, debug enabled."
echo "DEBUG=1" >> platform.inc

if [ "$SANITIZER" != "false" ]; then
echo "Enabling $SANITIZER sanitizer (may enable some optimizations)."
echo "SANITIZER=$SANITIZER" >> platform.inc
fi

#
# Trace logging for debug builds, if enabled.
#
Expand All @@ -1322,6 +1331,14 @@ else
fi
fi

#
# Enable sanitizers.
#
if [ "$SANITIZER" != "false" ]; then
echo "Enabling $SANITIZER sanitizer (may enable some optimizations)."
echo "SANITIZER=$SANITIZER" >> platform.inc
fi

#
# Enable stack protector.
#
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ USERS

DEVELOPERS

+ -Og is now the default debug optimization level (was -O0).
+ Added --enable-lto to enable link-time optimizations.
+ The sanitizer config.sh options can now be used with release
builds e.g. --enable-release --enable-asan.
+ Simplified the configuration of system directories for ports
in config.sh, which now prints more information about them.
+ Fixed config.sh output of "/usr/bin/git" and the Git HEAD.
Expand Down
11 changes: 6 additions & 5 deletions unit/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ unit_common_objs += \
endif

#
# This may already be set with sanitizer flags.
# Build unit tests with debug optimizations and without link-time optimzations.
# (Overrides these optimization flags from the global flags.)
#
DEBUG_CFLAGS ?= -O0
UNIT_OPTIMIZE_FLAGS += -Og -fno-lto

#
# Use DEBUG_CFLAGS to override any optimization flags in the global flags.
# Build without -DDEBUG to suppress debug messages,
# build without -DNDEBUG to allow assert().
# Turn on exceptions explicitly, since the global CXXFLAGS turn them off.
#
unit_cflags ?= ${CXXFLAGS} ${DEBUG_CFLAGS} -UDEBUG -UNDEBUG
unit_cflags ?= ${CXXFLAGS}
unit_cflags += ${UNIT_OPTIMIZE_FLAGS} -UDEBUG -UNDEBUG
unit_cflags += -fexceptions -funsigned-char -std=gnu++11
unit_ldflags += ${DEBUG_CFLAGS}
unit_ldflags += ${UNIT_OPTIMIZE_FLAGS}

unit_cflags += ${SDL_CFLAGS} -Umain
unit_ldflags += ${SDL_LDFLAGS} ${ZLIB_LDFLAGS}
Expand Down

0 comments on commit c07f17e

Please sign in to comment.