-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
80 lines (64 loc) · 2.58 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
79
include Makefile.tools
#Defines special targets
.PHONY: gen, clean, mrproper, all, checkdirs
SRC_DIR := src
SRC_DIR += $(addprefix src/, $(MODULES))
BUILD_DIR := build
BUILD_DIR += $(addprefix build/, $(MODULES))
GEN_DIR := gen
GEN_DIR += $(addprefix gen/, $(MODULES))
SRC := $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.cpp))
SRC += $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.c))
SRC += $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.ypp))
SRC += $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.l))
GEN := $(patsubst src/%.ypp, gen/%.cpp, $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.ypp)))
GEN += $(patsubst src/%.l, gen/%.c, $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.l)))
OBJ := $(patsubst src/%.cpp, build/%.o, $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.cpp)))
OBJ += $(patsubst src/%.c, build/%.o, $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.c)))
INCLUDES += -iquote gen -iquote src
GEN_OBJ := $(patsubst src/%.l, build/%.o, $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.l)))
GEN_OBJ += $(patsubst src/%.ypp, build/%.o, $(foreach sdir, $(SRC_DIR), $(wildcard $(sdir)/*.ypp)))
vpath %.cpp $(SRC_DIR)
vpath %.c $(SRC_DIR)
vpath %.l $(SRC_DIR)
vpath %.ypp $(SRC_DIR)
EXEC_NAME := $(EXEC) $(VERSION)
ifeq ($(TARGET), macos)
EXEC_NAME := $(EXEC)
BUNDLE_NAME := $(EXEC) $(VERSION).app
endif
define make-build-goal
$1/%.o: %.c
$(C) -c $$< -o $$@ $(CFLAGS) $(INCLUDES)
$1/%.o: %.cpp
$(CXX) -c $$< -o $$@ $(CXXFLAGS) $(INCLUDES)
endef
define make-gen-goal
$1/%.c: %.l
$(LEX) $(LFLAGS) --header-file=$$@.h -o $$@ $$^
$1/%.cpp: %.ypp
$(YACC) $(YFLAGS) -d $$^ -o $$@
endef
#Build all
all: checkdirs $(GEN_OBJ) $(OBJ)
$(LD) $(GEN_OBJ) $(OBJ) -o "bin/$(EXEC_NAME)" $(LDFLAGS) $(INCLUDES) $(LIBRARIES)
$(ECHO) "\nBinary generated in bin/"
#Generate object files from generated files
$(GEN_OBJ): $(GEN)
$(foreach gen, $(patsubst gen/%.cpp, build/%.o, $(foreach gdir, $(GEN_DIR), $(wildcard $(gdir)/*.cpp))), $(CXX) -c $(patsubst build/%.o, gen/%.cpp, $(gen)) -o $(gen) $(CXXFLAGS) $(INCLUDES))
$(foreach gen, $(patsubst gen/%.c, build/%.o, $(foreach gdir, $(GEN_DIR), $(wildcard $(gdir)/*.c))), $(C) -c $(patsubst build/%.o, gen/%.c, $(gen)) -o $(gen) $(CFLAGS) $(INCLUDES))
#Check for directories existences
checkdirs:
$(MKDIR) $(BUILD_DIR)
$(MKDIR) $(GEN_DIR)
$(MKDIR) bin
#Clean temporary object files
clean:
$(RM) build/*
#Clean all except sources
mrproper: clean
$(RM) bin/*
$(RM) gen/*
#Build implicit rules for building all files
$(foreach gdir, $(GEN_DIR), $(eval $(call make-gen-goal, $(gdir))))
$(foreach bdir, $(BUILD_DIR), $(eval $(call make-build-goal, $(bdir))))