@@ -14,21 +14,41 @@ BINDIR = $(BUILDDIR)/bin |
| 14 | 14 | TESTDIR = tests |
| 15 | 15 | DOCDIR = docs |
| 16 | 16 | |
| 17 | +# Platform detection |
| 18 | +UNAME_S := $(shell uname -s) |
| 19 | + |
| 17 | 20 | # Compiler and flags |
| 18 | 21 | CC = gcc |
| 19 | 22 | CFLAGS = -std=c11 -Wall -Wextra -Wstrict-prototypes \ |
| 20 | 23 | -Wmissing-prototypes -Wold-style-definition -Wredundant-decls \ |
| 21 | | - -Wbad-function-cast -Wnested-externs -Winit-self -Wlogical-op \ |
| 24 | + -Wbad-function-cast -Wnested-externs -Winit-self \ |
| 22 | 25 | -Wshadow -Wwrite-strings -Wcast-align -Wstrict-aliasing=2 \ |
| 23 | | - -Wmissing-include-dirs -Wdate-time -Wformat=2 -Winit-self \ |
| 26 | + -Wmissing-include-dirs -Wformat=2 -Winit-self \ |
| 24 | 27 | -Wswitch-default -Wunused -Werror-implicit-function-declaration |
| 25 | 28 | |
| 26 | | -# Security hardening flags |
| 27 | | -SECURITY_FLAGS_DEBUG = -fstack-protector-strong -fstack-clash-protection -fcf-protection \ |
| 28 | | - -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack |
| 29 | | -SECURITY_FLAGS_RELEASE = -D_FORTIFY_SOURCE=2 -fstack-protector-strong \ |
| 30 | | - -fstack-clash-protection -fcf-protection \ |
| 31 | | - -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack |
| 29 | +# Platform-specific flags |
| 30 | +ifeq ($(UNAME_S),Linux) |
| 31 | + # GCC-specific warnings |
| 32 | + CFLAGS += -Wlogical-op -Wdate-time |
| 33 | + # Linux-specific security flags |
| 34 | + SECURITY_FLAGS_DEBUG = -fstack-protector-strong -fstack-clash-protection -fcf-protection \ |
| 35 | + -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack |
| 36 | + SECURITY_FLAGS_RELEASE = -D_FORTIFY_SOURCE=2 -fstack-protector-strong \ |
| 37 | + -fstack-clash-protection -fcf-protection \ |
| 38 | + -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack |
| 39 | +endif |
| 40 | + |
| 41 | +ifeq ($(UNAME_S),Darwin) |
| 42 | + # macOS-specific security flags (no cf-protection, stack-clash-protection, or Linux linker flags) |
| 43 | + SECURITY_FLAGS_DEBUG = -fstack-protector-strong |
| 44 | + SECURITY_FLAGS_RELEASE = -D_FORTIFY_SOURCE=2 -fstack-protector-strong |
| 45 | + # macOS OpenSSL paths (Homebrew) |
| 46 | + OPENSSL_PREFIX := $(shell brew --prefix openssl@3 2>/dev/null || brew --prefix openssl 2>/dev/null) |
| 47 | + ifneq ($(OPENSSL_PREFIX),) |
| 48 | + INCLUDES += -I$(OPENSSL_PREFIX)/include |
| 49 | + LDFLAGS += -L$(OPENSSL_PREFIX)/lib |
| 50 | + endif |
| 51 | +endif |
| 32 | 52 | |
| 33 | 53 | # Debug/Release configurations |
| 34 | 54 | DEBUG_FLAGS = -g -O0 -DDEBUG -fsanitize=address -fsanitize=undefined \ |
@@ -92,7 +112,7 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.c $(HEADERS) | $(OBJDIR) |
| 92 | 112 | # Link main executable |
| 93 | 113 | $(BINDIR)/$(TARGET): $(OBJECTS) | $(BINDIR) |
| 94 | 114 | @echo "Linking $@..." |
| 95 | | - $(CC) $(CFLAGS) $(OBJECTS) -o $@ $(LIBS) |
| 115 | + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBS) |
| 96 | 116 | @echo "Build complete: $@" |
| 97 | 117 | |
| 98 | 118 | # Install target |
@@ -118,7 +138,7 @@ $(OBJDIR)/test_%.o: $(TESTDIR)/%.c $(HEADERS) | $(OBJDIR) |
| 118 | 138 | # Test executables (exclude main.o to avoid multiple main functions) |
| 119 | 139 | $(BINDIR)/test_%: $(OBJDIR)/test_%.o $(filter-out $(OBJDIR)/main.o,$(OBJECTS)) | $(BINDIR) |
| 120 | 140 | @echo "Linking test $@..." |
| 121 | | - $(CC) $(CFLAGS) $^ -o $@ $(LIBS) |
| 141 | + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS) |
| 122 | 142 | |
| 123 | 143 | # Build and run tests |
| 124 | 144 | .PHONY: test |