Skip to content

Commit

Permalink
Merge branch 'master' into IPECC
Browse files Browse the repository at this point in the history
  • Loading branch information
rben-dev committed Jun 12, 2023
2 parents bb994f8 + 7d5a366 commit 18be808
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 99 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.o
*.d
*~
*.su
112 changes: 73 additions & 39 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
MINGW := $(shell $(CROSS_COMPILE)$(CC) -dumpmachine 2>&1 | grep -v mingw)
# Detect Mac OS compilers: these usually don't like ELF pie related flags ...
APPLE := $(shell $(CROSS_COMPILE)$(CC) -dumpmachine 2>&1 | grep -v apple)
SYS_ROOT :=
ifneq ($(MINGW),)
FPIC_CFLAG=-fPIC
ifneq ($(APPLE),)
FPIE_CFLAG=-fPIE
FPIE_LDFLAGS=-pie -Wl,-z,relro,-z,now
FPIC_CFLAG=-fPIC
ifneq ($(APPLE),)
FPIE_CFLAG=-fPIE
FPIE_LDFLAGS=-pie -Wl,-z,relro,-z,now
endif
endif

ifeq ($(APPLE),)
SYS_ROOT_PATH := $(shell xcode-select --print-path)
ifneq ($(SYS_ROOT_PATH),)
SYS_ROOT_PATH := $(SYS_ROOT_PATH)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
SYS_ROOT := --sysroot=$(SYS_ROOT_PATH)
$(info Using MacOS SDK $(SYS_ROOT_PATH))
endif
endif

# NOTE: with mingw, FORTIFY_SOURCE=2 must be used
Expand All @@ -22,7 +32,43 @@ FORTIFY_FLAGS=-D_FORTIFY_SOURCE=2
# debug mode, expected word size (16, 32, 64), etc. Those are then used to
# define two differents kinds of CFLAGS we will use for building our library
# (LIB_CFLAGS) and binaries (BIN_CFLAGS) objects.

# Detect if we are using clang or gcc
CLANG := $(shell $(CROSS_COMPILE)$(CC) -v 2>&1 | grep clang)

ifneq ($(CLANG),)
# get clang version e.g. 14.1.3
CLANG_VERSION := $(shell $(CROSS_COMPILE)$(CC) -dumpversion)
# convert to single number e.g. 14 * 100 + 1
CLANG_VERSION := $(shell echo $(CLANG_VERSION) | cut -f1-2 -d. | sed -e 's/\./*100+/g')
# Calculate value - e.g. 1401
CLANG_VERSION := $(shell echo $$(($(CLANG_VERSION))))
# Comparison results (true if true, empty if false)
CLANG_VERSION_GTE_12 := $(shell [ $(CLANG_VERSION) -ge 1200 ] && echo true)
CLANG_VERSION_GTE_13 := $(shell [ $(CLANG_VERSION) -ge 1300 ] && echo true)
CLANG_VERSION_GTE_17 := $(shell [ $(CLANG_VERSION) -ge 1700 ] && echo true)
endif

# Default warning flags
# -Werror: treat warnings as errors
#
# Pedantic mode: enable more warnings
# -Wshadow: warn the user if a variable declaration shadows one from a parent context
# -Wdouble-promotion: warn about implicit conversion from float to double
# -Wformat=2: warn about format string vulnerabilities
# -fno-common: disallow global variables with same name and type
# -Wconversion: warn about implicit conversion
# -Wformat-security: warn about format string vulnerabilities
WARNING_CFLAGS = -Werror
ifeq ($(PEDANTIC),1)
WARNING_CFLAGS += -Wshadow -Wdouble-promotion -Wformat=2 -fno-common -Wconversion -Wformat-security
endif

# Disable certain warnings:
# -Wno-unused-parameter: commonly a false positive. Functions may be required to have a certain signature.
# -Wno-declaration-after-statement: our C standard supports declaration after statements
WARNING_CFLAGS += -Wno-unused-parameter -Wno-declaration-after-statement

# When compiler is *explicitly* set to clang, use its -Weverything option by
# default but disable the sepcific options we cannot support:
#
Expand All @@ -32,47 +78,36 @@ FORTIFY_FLAGS=-D_FORTIFY_SOURCE=2
# -Wno-covered-switch-default
# -Wno-used-but-marked-unused
#
CLANG := $(shell $(CROSS_COMPILE)$(CC) -v 2>&1 | grep clang)
ifneq ($(CLANG),)
WARNING_CFLAGS = -Weverything -Werror \
WARNING_CFLAGS += -Weverything \
-Wno-reserved-id-macro -Wno-padded \
-Wno-packed -Wno-covered-switch-default \
-Wno-used-but-marked-unused -Wno-switch-enum
# Add warnings if we are in pedantic mode
ifeq ($(PEDANTIC),1)
WARNING_CFLAGS += -Werror -Walloca -Wcast-qual -Wconversion -Wformat=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wvla -Warray-bounds -Warray-bounds-pointer-arithmetic -Wassign-enum -Wbad-function-cast -Wconditional-uninitialized -Wconversion -Wfloat-equal -Wformat-type-confusion -Widiomatic-parentheses -Wimplicit-fallthrough -Wloop-analysis -Wpointer-arith -Wshift-sign-overflow -Wshorten-64-to-32 -Wtautological-constant-in-range-compare -Wunreachable-code-aggressive -Wthread-safety -Wthread-safety-beta -Wcomma
endif
# Clang version >= 13? Adapt
CLANG_VERSION_GTE_13_EXPRESSION := $(shell echo `$(CROSS_COMPILE)$(CC) -dumpversion | cut -f1-2 -d.` \>= 13.0 | sed -e 's/\./*100+/g')
CLANG_VERSION_GTE_13 := $(shell awk "BEGIN{printf \"%d\n\", $(CLANG_VERSION_GTE_13_EXPRESSION)}")
ifeq ($(CLANG_VERSION_GTE_13), 1)
# We have to do this because the '_' prefix seems now reserved to builtins
WARNING_CFLAGS += -Wno-reserved-identifier
# Add warnings if we are in pedantic mode
ifeq ($(PEDANTIC),1)
WARNING_CFLAGS += -Walloca -Wcast-qual -Wnull-dereference -Wstack-protector -Wvla -Warray-bounds -Warray-bounds-pointer-arithmetic -Wassign-enum -Wbad-function-cast -Wconditional-uninitialized -Wfloat-equal -Wformat-type-confusion -Widiomatic-parentheses -Wimplicit-fallthrough -Wloop-analysis -Wpointer-arith -Wshift-sign-overflow -Wshorten-64-to-32 -Wtautological-constant-in-range-compare -Wunreachable-code-aggressive -Wthread-safety -Wthread-safety-beta -Wcomma
endif
ifeq ($(CLANG_VERSION_GTE_13), true)
# We have to do this because the '_' prefix seems now reserved to builtins
WARNING_CFLAGS += -Wno-reserved-identifier
endif
ifeq ($(CLANG_VERSION_GTE_17), true)
# NOTE: XXX: this is really a shame to remove this, but
# we have to wait until this is less sensitive and false positive
# prone to use it!
WARNING_CFLAGS += -Wno-unsafe-buffer-usage
endif
else
WARNING_CFLAGS = -W -Werror -Wextra -Wall -Wunreachable-code
# Add warnings if we are in pedantic mode
ifeq ($(PEDANTIC),1)
WARNING_CFLAGS += -Wpedantic -Wformat=2 -Wformat-overflow=2 -Wformat-truncation=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wtrampolines -Walloca -Wvla -Warray-bounds=2 -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wcast-qual -Wstringop-overflow=4 -Wconversion -Warith-conversion -Wlogical-op -Wduplicated-cond -Wduplicated-branches -Wformat-signedness -Wshadow -Wstrict-overflow=2 -Wundef -Wstrict-prototypes -Wswitch-default -Wcast-align=strict -Wjump-misses-init
endif
endif

# In case of clang version >= 16, the new -Wunsafe-buffer-usage
# flag is really picky for many false positives, remove it
ifneq ($(CLANG),)
CLANG_VERSION_GTE_16_EXPRESSION := $(shell echo `$(CROSS_COMPILE)$(CC) -dumpversion | cut -f1-2 -d.` \>= 16.0 | sed -e 's/\./*100+/g')
CLANG_VERSION_GTE_16 := $(shell awk "BEGIN{printf \"%d\n\", $(CLANG_VERSION_GTE_16_EXPRESSION)}")
ifeq ($(CLANG_VERSION_GTE_16), 1)
# NOTE: XXX: this is really a shame to remove this, but
# we have to wait until this is less sensitive and false positive
# prone to use it!
WARNING_CFLAGS += -Wno-unsafe-buffer-usage
WARNING_CFLAGS += -W -Wextra -Wall -Wunreachable-code
# Add warnings if we are in pedantic mode
ifeq ($(PEDANTIC),1)
WARNING_CFLAGS += -Wpedantic -Wformat-overflow=2 -Wformat-truncation=2 -Wnull-dereference -Wstack-protector -Wtrampolines -Walloca -Wvla -Warray-bounds=2 -Wimplicit-fallthrough=3 -Wshift-overflow=2 -Wcast-qual -Wstringop-overflow=4 -Warith-conversion -Wlogical-op -Wduplicated-cond -Wduplicated-branches -Wformat-signedness -Wstrict-overflow=2 -Wundef -Wstrict-prototypes -Wswitch-default -Wcast-align=strict -Wjump-misses-init
endif
endif

ifeq ($(WNOERROR), 1)
# Sometimes "-Werror" might be too much, this can be overriden
WARNING_CFLAGS := $(subst -Werror,,$(WARNING_CFLAGS))
# Sometimes "-Werror" might be too much, this can be overriden
WARNING_CFLAGS := $(subst -Werror,,$(WARNING_CFLAGS))
endif

# If the user has overridden the CFLAGS or LDFLAGS, let's detect it
Expand All @@ -84,7 +119,7 @@ ifdef LDFLAGS
USER_DEFINED_LDFLAGS = $(LDFLAGS)
endif

CFLAGS ?= $(WARNING_CFLAGS) -pedantic -fno-builtin -std=c99 \
CFLAGS ?= $(WARNING_CFLAGS) $(SYS_ROOT) -pedantic -fno-builtin -std=c99 \
$(FORTIFY_FLAGS) $(STACK_PROT_FLAG) -O3
LDFLAGS ?=

Expand Down Expand Up @@ -262,9 +297,7 @@ ifeq ($(USE_SANITIZERS),1)
CFLAGS += -fsanitize=undefined -fsanitize=address -fsanitize=leak
ifneq ($(CLANG),)
# Clang version < 12 do not support unsigned-shift-base
CLANG_VERSION_GTE_12_EXPRESSION := $(shell echo `$(CROSS_COMPILE)$(CC) -dumpversion | cut -f1-2 -d.` \>= 12.0 | sed -e 's/\./*100+/g')
CLANG_VERSION_GTE_12 := $(shell awk "BEGIN{printf \"%d\n\", $(CLANG_VERSION_GTE_12_EXPRESSION)}")
ifeq ($(CLANG_VERSION_GTE_12), 1)
ifeq ($(CLANG_VERSION_GTE_12), true)
CFLAGS += -fsanitize=integer -fno-sanitize=unsigned-integer-overflow -fno-sanitize=unsigned-shift-base
endif
endif
Expand Down Expand Up @@ -347,6 +380,7 @@ CFLAGS += -Wno-deprecated
CFLAGS := $(patsubst -Wstrict-prototypes,,$(CFLAGS))
CFLAGS := $(patsubst -Wjump-misses-init,,$(CFLAGS))
CFLAGS := $(patsubst -Wduplicated-branches,,$(CFLAGS))
CFLAGS := $(patsubst -Wno-declaration-after-statement,,$(CFLAGS))
endif
# clang++ case
ifneq ($(CLANGPP),)
Expand Down
Loading

0 comments on commit 18be808

Please sign in to comment.