-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpp.mk
53 lines (42 loc) · 1.46 KB
/
cpp.mk
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
CXX := g++
CXXFLAGS := -std=c++11 -O3 -msse4.2
LDFLAGS :=
SRCEXT := cc
INC := -I src
LIB := -L lib
INPUT := ./data/sample_100k.txt
BINDIR := bin
SRCDIR := src
TESTDIR := tests
BUILDDIR := build
ALL_SOURCES := $(wildcard $(SRCDIR)/*.$(SRCEXT) $(TESTDIR)/*.$(SRCEXT))
RUN_SOURCES := $(wildcard $(SRCDIR)/*_main.$(SRCEXT) $(TESTDIR)/*_main.$(SRCEXT))
RUN_OBJECTS := $(patsubst %, $(BUILDDIR)/%, $(RUN_SOURCES:.$(SRCEXT)=.o))
RUN_TARGETS := $(patsubst $(BUILDDIR)/%.o, $(BINDIR)/%, $(RUN_OBJECTS))
TEST_SOURCES := $(wildcard $(TESTDIR)/test_*.$(SRCEXT))
TEST_OBJECTS := $(patsubst %, $(BUILDDIR)/%, $(TEST_SOURCES:.$(SRCEXT)=.o))
TEST_TARGETS := $(patsubst $(BUILDDIR)/%.o, $(BINDIR)/%, $(TEST_OBJECTS))
SOURCES := $(filter-out $(RUN_SOURCES) $(TEST_SOURCES), $(ALL_SOURCES))
OBJECTS := $(patsubst %, $(BUILDDIR)/%, $(SOURCES:.$(SRCEXT)=.o))
.SECONDARY: $(RUN_OBJECTS) $(TEST_OBJECTS) $(OBJECTS)
$(BUILDDIR)/%.o: %.$(SRCEXT)
@mkdir -p $(dir $@)
$(CC) $(INC) $(CXXFLAGS) -c $< -o $@
$(BINDIR)/%: $(BUILDDIR)/%.o $(OBJECTS)
@mkdir -p $(dir $@)
$(CXX) $(LIB) $(LDFLAGS) $^ -o $@
.PHONY: cpp-clean
cpp-clean: ## clean up C++ project
rm -rf ./$(BINDIR)/ ./$(BUILDDIR)/
.PHONY: cpp-run
cpp-run: $(RUN_TARGETS) ## compile and run C++ program
@for target in $(RUN_TARGETS); do \
echo $$target >&2; \
time ./$$target $(INPUT); \
done
.PHONY: cpp-test
cpp-test: $(TEST_TARGETS) ## run C++ tests
@for target in $(TEST_TARGETS); do \
echo $$target >&2; \
./$$target; \
done