-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
78 lines (58 loc) · 1.9 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
.DEFAULT_GOAL = compile
CC = gcc
CFLAGS = -Wall
# Detect the current OS:
ifeq ($(OS),Windows_NT)
UNAME := Windows
else
UNAME := $(shell uname -s)
endif
# Turn on debug info by default.
# To turn it off we need run make with RELEASE option
ifndef RELEASE
CFLAGS += -g
endif
BUILD_DIR := ./build
SRC_DIR := ./src
TEST_DIR := ./test
APP_MAIN := app.c
TEST_MAIN := all_tests.c
APP_EXEC := labyrinth
TEST_EXEC := run_tests
# Find all the C files we want to compile, except the source with main function
SRCS := $(shell find $(SRC_DIR) -name '*.c' -and -not -name $(APP_MAIN))
# Find all C files with tests
TEST_SRCS := $(shell find $(TEST_DIR) -name '*.c')
# Prepends BUILD_DIR and appends .o to every src file
# As an example, ./src/hello.c turns into ./build/./src/hello.c.o
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
# Concatenate src with test, but without APP_MAIN
TEST_OBJS := $(OBJS)
# Add APP_MAIN to build
OBJS += $(BUILD_DIR)/$(SRC_DIR)/$(APP_MAIN).o
# Add TEST_MAIN to build
TEST_OBJS += $(BUILD_DIR)/$(TEST_DIR)/$(TEST_MAIN).o
# Build step for C source
# Changes in Makefile should trigger compilation too
$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c Makefile
@[ -d $(BUILD_DIR)/$(SRC_DIR)/ ] || mkdir -p $(BUILD_DIR)/$(SRC_DIR)/
$(CC) $(CFLAGS) -c $< -o $@
# TEST_MAIN should be recompile on changes in any TEST_SRCS or Makefile
$(BUILD_DIR)/$(TEST_DIR)/$(TEST_MAIN).o: $(TEST_SRCS) Makefile
@echo "Compile tests..."
@[ -d $(BUILD_DIR)/$(TEST_DIR)/ ] || mkdir -p $(BUILD_DIR)/$(TEST_DIR)/
$(CC) $(CFLAGS) -I$(SRC_DIR) -c $(TEST_DIR)/$(TEST_MAIN) -o $@
# Build the game
compile: $(OBJS)
@echo "Build application..."
$(CC) $(OBJS) -o $(BUILD_DIR)/$(APP_EXEC) -lm
# Build tests
test: $(TEST_OBJS)
@echo "Build and run tests..."
$(CC) $(TEST_OBJS) -o $(BUILD_DIR)/$(TEST_EXEC) -lm
$(BUILD_DIR)/$(TEST_EXEC)
run: compile
$(BUILD_DIR)/$(APP_EXEC)
clean:
rm -r $(BUILD_DIR)
.PHONY: clean compile test run