Skip to content

Commit 96638ea

Browse files
authored
Merge pull request #214 from sysprog21/detect-cflags
Filter out all unsupported flags
2 parents 6ef2402 + 468ed99 commit 96638ea

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
CC ?= gcc
22
CFLAGS := -O -g \
3-
-std=c99 -pedantic \
3+
-std=c99 -pedantic
4+
5+
CFLAGS_TO_CHECK := \
46
-fwrapv \
57
-Wall -Wextra \
68
-Wno-unused-but-set-variable \
@@ -14,6 +16,19 @@ CFLAGS := -O -g \
1416
-Wno-format \
1517
-Wno-format-pedantic
1618

19+
SUPPORTED_CFLAGS :=
20+
# Check if a specific compiler flag is supported, attempting a dummy compilation
21+
# with flags. If successful, it returns the flag string; otherwise, it returns
22+
# an empty string.
23+
# Usage: $(call check_flag, -some-flag)
24+
check_flag = $(shell $(CC) $(1) -S -o /dev/null -xc /dev/null 2>/dev/null; \
25+
if test $$? -eq 0; then echo "$(1)"; fi)
26+
27+
# Iterate through the list of all potential flags, effectively filtering out all
28+
# unsupported flags.
29+
$(foreach flag, $(CFLAGS_TO_CHECK), $(eval SUPPORTED_CFLAGS += $(call check_flag, $(flag))))
30+
CFLAGS += $(SUPPORTED_CFLAGS)
31+
1732
BUILD_SESSION := .session.mk
1833

1934
-include $(BUILD_SESSION)

0 commit comments

Comments
 (0)