From debb3e9647ca98a3c276731a31b2dacfb0ff72a2 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Thu, 19 Nov 2020 13:06:51 -0500 Subject: [PATCH 1/2] Add debug and LLVM asan build options In addition, the default build now uses -O3 and has no debug symbols. -Wmissing-field-initializer was removed due to false positives on newer versions of clang. --- Makefile | 17 +++++++++++++++-- cfg/targets/help.mk | 2 ++ src/toxic.c | 10 +++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 27b9f28c6..ee4a6c822 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,7 @@ CFG_DIR = $(BASE_DIR)/cfg LIBS = toxcore ncursesw libconfig libcurl -CFLAGS ?= -g -CFLAGS += -std=c99 -pthread -Wall -Wpedantic -Wunused -fstack-protector-all -Wvla -Wmissing-field-initializers -Wno-missing-braces +CFLAGS = -std=c99 -pthread -Wall -Wpedantic -Wunused -fstack-protector-all -Wvla -Wno-missing-braces CFLAGS += '-DTOXICVER="$(VERSION)"' -DHAVE_WIDECHAR -D_XOPEN_SOURCE_EXTENDED -D_FILE_OFFSET_BITS=64 CFLAGS += '-DPACKAGE_DATADIR="$(abspath $(DATADIR))"' CFLAGS += ${USER_CFLAGS} @@ -18,6 +17,20 @@ OBJ += file_transfers.o friendlist.o global_commands.o conference_commands.o con OBJ += line_info.o log.o message_queue.o misc_tools.o name_lookup.o notify.o prompt.o qr_code.o settings.o OBJ += term_mplex.o toxic.o toxic_strings.o windows.o +# Check if debug build is enabled +DEBUG := $(shell if [ -z "$(DEBUG_ENABLED)" ] || [ "$(DEBUG_ENABLED)" = "0" ] ; then echo disabled ; else echo enabled ; fi) +ifneq ($(DEBUG), disabled) + CFLAGS += -O0 -g -DDEBUG +else + CFLAGS += -O3 +endif + +# Check if LLVM Address Sanitizer is enabled +ASAN_ENABLED := $(shell if [ -z "$(ENABLE_ASAN)" ] || [ "$(ENABLE_ASAN)" = "0" ] ; then echo disabled ; else echo enabled ; fi) +ifneq ($(ASAN_ENABLED), disabled) + CFLAGS += -fsanitize=address -fno-omit-frame-pointer +endif + # Check on wich system we are running UNAME_S = $(shell uname -s) ifeq ($(UNAME_S), Linux) diff --git a/cfg/targets/help.mk b/cfg/targets/help.mk index b7baf9afc..3f780cc91 100644 --- a/cfg/targets/help.mk +++ b/cfg/targets/help.mk @@ -17,6 +17,8 @@ help: @echo " DISABLE_QRCODE: Set to \"1\" to force building without QR export support" @echo " DISABLE_QRPNG: Set to \"1\" to force building without QR exported as PNG support" @echo " ENABLE_PYTHON: Set to \"1\" to enable building with Python scripting support" + @echo " DEBUG_ENABLED: Set to \"1\" to build with debug symbols, no compiler optimizations and stderr enabled" + @echo " ASAN_ENABLED: Set to \"1\" to build with LLVM address sanitizer enabled. @echo " USER_CFLAGS: Add custom flags to default CFLAGS" @echo " USER_LDFLAGS: Add custom flags to default LDFLAGS" @echo " PREFIX: Specify a prefix directory for binaries, data files,... (default is \"$(abspath $(PREFIX))\")" diff --git a/src/toxic.c b/src/toxic.c index fd0a5b26e..92005a2d7 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -1372,12 +1372,20 @@ int main(int argc, char **argv) parse_args(argc, argv); - /* Use the -b flag to enable stderr */ +#ifndef DEBUG + + /* Use the -b flag or build with DEBUG_ENABLED=1 to enable stderr */ if (!arg_opts.debug) { if (!freopen("/dev/null", "w", stderr)) { fprintf(stderr, "Warning: failed to enable stderr\n"); } } +#else + if (!arg_opts.debug) { + queue_init_message("stderr enabled (debug build)"); + } + +#endif // DEBUG if (arg_opts.encrypt_data && arg_opts.unencrypt_data) { arg_opts.encrypt_data = 0; From 53ffffa8180bc7cffcb8ed079f1063dac69d369e Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 19 Nov 2020 18:10:13 +0000 Subject: [PATCH 2/2] Restyled by astyle --- src/toxic.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/toxic.c b/src/toxic.c index 92005a2d7..468536b19 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -1380,7 +1380,9 @@ int main(int argc, char **argv) fprintf(stderr, "Warning: failed to enable stderr\n"); } } + #else + if (!arg_opts.debug) { queue_init_message("stderr enabled (debug build)"); }