-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
111 lines (87 loc) · 3.03 KB
/
Copy pathMakefile
File metadata and controls
111 lines (87 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Comment below line to see actual linker and compiler flags while running makefile
.SILENT:
# Compiler and flags
CC := gcc
CFLAGS := -Wall -Wextra -Iinc
LDLIBS := -lpthread -lrt -lssl -lcrypto #-lz -lbrotlienc
SRC_DIR := src
INC_DIR := inc
LIB_DIR := lib
BUILD_DIR := bld
# Build modes and flags
DEBUG_FLAGS := -O0 -g -Wformat=2 -Wconversion -Wimplicit-fallthrough -DDEBUG
RELEASE_FLAGS := -O2 -fstack-protector-strong -D_FORTIFY_SOURCE=2
RELEASE_LDFLAGS := -s -Wl,-z,noexecstack -Wl,-z,defs -Wl,-z,nodump
# Library directories
THREADPOOL_SOURCES := $(wildcard $(LIB_DIR)/threadpool/*.c)
HASHTALBE_SOURCES := $(wildcard $(LIB_DIR)/hashtable/*.c)
LOGGGER_SOURCES := $(wildcard $(LIB_DIR)/logger/*.c)
# Static libraries
LIB_THREADPOOL := $(BUILD_DIR)/libthreadpool.a
LIB_HASHTABLE := $(BUILD_DIR)/libhashtable.a
LIB_LOGGER := $(BUILD_DIR)/liblogger.a
# Generated static libraries
LIB_NAMES := -lthreadpool -lhashtable -llogger
#Source Files
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
# Object files
SRC_OBJECTS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC_FILES))
# Final executable
TARGET := $(BUILD_DIR)/legion
# Default target
.PHONY: all
all: debug
# Debug build target
.PHONY: debug
debug: CFLAGS += $(DEBUG_FLAGS)
debug: LDFLAGS += $(LDLIBS)
debug: $(TARGET)
# Release build target
.PHONY: release
release: CFLAGS += $(RELEASE_FLAGS)
release: LDFLAGS += $(RELEASE_LDFLAGS) $(LDLIBS)
release: $(TARGET)
# Build executable target
$(TARGET): $(LIB_THREADPOOL) $(LIB_HASHTABLE) $(LIB_LOGGER) $(SRC_OBJECTS)
@echo "Linking executable $(TARGET)"
$(CC) $(CFLAGS) $(SRC_OBJECTS) -L$(BUILD_DIR) $(LDFLAGS) $(LIB_NAMES) -o $(TARGET)
# Build static libraries
$(LIB_THREADPOOL): $(BUILD_DIR)/threadpool.o | $(BUILD_DIR)
@echo "Creating static library $(LIB_THREADPOOL)"
ar rcs $@ $<
$(LIB_HASHTABLE): $(BUILD_DIR)/hashtable.o | $(BUILD_DIR)
@echo "Creating static library $(LIB_HASHTABLE)"
ar rcs $@ $<
$(LIB_LOGGER): $(BUILD_DIR)/logger.o | $(BUILD_DIR)
@echo "Creating static library $(LIB_LOGGER)"
ar rcs $@ $<
# Compile library object files
$(BUILD_DIR)/threadpool.o: $(THREADPOOL_SOURCES) | $(BUILD_DIR)
@echo "Compiling $<"
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/hashtable.o: $(HASHTALBE_SOURCES) | $(BUILD_DIR)
@echo "Compiling $<"
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/logger.o: $(LOGGGER_SOURCES) | $(BUILD_DIR)
@echo "Compiling $<"
$(CC) $(CFLAGS) -c $< -o $@
# Compile source object files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
@echo "Compiling $<"
$(CC) $(CFLAGS) -c $< -o $@
# Create the build directory
$(BUILD_DIR):
@echo "Creating build directory $(BUILD_DIR)"
mkdir -p $(BUILD_DIR)
.PHONY: test
test: $(BUILD_DIR)
@echo "Building test executables"
gcc -o bld/runtest test/sanity_test.c -g -lssl -lcrypto
# Clean up build files
.PHONY: clean
clean:
@echo "Cleaning up build files"
rm -rf $(BUILD_DIR)
# Keeping this here. might use it in future
# RELEASE_C_FLAGS := -O2 -s -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE
# RELEASE_LD_FLAGS :=-pie -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,defs -Wl,-z,nodump