-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
78 lines (64 loc) · 1.7 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
72
73
74
75
76
77
78
# Compiler setup
CC := gcc
CFLAGS := -std=c99 -Wall -Wextra
LDFLAGS :=
LDLIBS := -lcunit
# Platform detection
UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_S),Darwin) # MacOS
ifeq ($(UNAME_M),x86_64)
CFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib
else ifeq ($(UNAME_M),arm64)
CFLAGS += -I/opt/homebrew/include
LDFLAGS += -L/opt/homebrew/lib
endif
endif
ifeq ($(UNAME_S),Linux) # Linux
CFLAGS += -I/usr/include
LDFLAGS += -L/usr/lib
endif
ifneq ($(filter MSYS%,$(UNAME_S)),) # MSYS/MinGW (Windows)
CFLAGS += -IC:/msys64/mingw64/include
LDFLAGS += -LC:/msys64/mingw64/lib
endif
# Directories
SRCDIR := src
INCDIR := include
BUILDDIR := build
BINDIR := bin
TESTDIR := tests
TARGET := $(BINDIR)/lars
TESTTARGET := $(BINDIR)/test
# Source files
SRCS := $(wildcard $(SRCDIR)/*.c)
OBJS := $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRCS))
# Test files
TEST_SRCS := $(wildcard $(TESTDIR)/*.c)
TEST_OBJS := $(patsubst $(TESTDIR)/%.c,$(BUILDDIR)/%.o,$(TEST_SRCS))
# Default target
all: $(TARGET)
# Linking
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) $^ -o $@
# Compilation
$(BUILDDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@
$(BUILDDIR)/%.o: $(TESTDIR)/%.c
$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@
# Create directories if they don't exist
$(shell mkdir -p $(BUILDDIR))
$(shell mkdir -p $(BINDIR))
# Cleanup
clean:
rm -rf $(BUILDDIR) $(BINDIR)
# Test target
test: $(TESTTARGET)
./$(TESTTARGET)
$(TESTTARGET): $(filter-out build/main.o, $(OBJS)) $(TEST_OBJS)
$(CC) -DTEST $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
# Generate compile_commands.json using bear
bear: clean all
bear -- make
.PHONY: all clean test