-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
71 lines (56 loc) · 1.73 KB
/
Makefile
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
# Compiler and flags
# CC = gcc-14 # GNU gcc on MacOS: remove -fsanitize=address to run
CC = gcc
CFLAGS_COMMON = -std=c17 -D_GNU_SOURCE -Wall -Wextra -Wpedantic -pthread
CFLAGS_DEV = -g -Werror -fsanitize=address
CFLAGS_RELEASE = -O2
# Directories
SRC_DIR = src
BUILD_DIR = build
BIN_DIR = bin
BIN_DEV = $(BIN_DIR)/clabdev
BIN_RELEASE = $(BIN_DIR)/clab
# Find all .c files in SRC_DIR and subdirectories
SRCS = $(shell find $(SRC_DIR) -name '*.c')
# Create a list of .o files based on the .c files
OBJS_DEV = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/dev/%.o,$(SRCS))
OBJS_RELEASE = $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/release/%.o,$(SRCS))
# Default target
all: dev release
# Development build
dev: CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_DEV)
dev: $(BIN_DEV)
# Release build
release: CFLAGS=$(CFLAGS_COMMON) $(CFLAGS_RELEASE)
release: $(BIN_RELEASE)
# Link object files to create the executable for development
$(BIN_DEV): $(OBJS_DEV)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) -o $@ $^
# Link object files to create the executable for release
$(BIN_RELEASE): $(OBJS_RELEASE)
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) -o $@ $^
# Compile source files to object files for development
$(BUILD_DIR)/dev/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c -o $@ $<
# Compile source files to object files for release
$(BUILD_DIR)/release/%.o: $(SRC_DIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c -o $@ $<
# Clean up build artifacts
clean:
rm -rf $(BUILD_DIR) $(BIN_DIR)
# Apple Silicon debug
debug: $(BIN_DEV)
lldb -o 'run' $(BIN_DEV)
# Run the program
runclabdev: dev
pkill clabdev || true
./$(BIN_DEV) $(cmd1) $(cmd2)
runclab: release
pkill clab || true
./$(BIN_RELEASE) $(cmd1) $(cmd2)
# Phony targets
.PHONY: all clean dev release debug runclabdev runclab