- src
- foo.cpp
- foo.h
- bar/buz/
- qux.cpp
- qux.h
- tests
- foo_test.cpp
- obj
- bin
Last active
August 22, 2018 01:39
-
-
Save tosik/610de682bff0f68cf8145cccbab8fd48 to your computer and use it in GitHub Desktop.
ヘッダの依存関係も解決してくれる Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CXX = clang++ | |
TARGET = bin/main | |
DEBUG = -g -O0 | |
#RELEASE = -O3 | |
SRCDIRS := $(shell find src -type d) | |
SRCS = $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.cpp)) | |
OBJS = $(subst src/,obj/,$(SRCS:.cpp=.o)) | |
DEPS = $(subst src/,obj/,$(SRCS:.cpp=.dep)) | |
INCLUDES = -I./src | |
CXXFLAGS = $(DEBUG) $(RELEASE) -Wall -std=c++11 $(INCLUDES) | |
LDFLAGS = | |
TARGET_TEST = bin/test | |
CXXFLAGS_TEST = -g -O0 -Wall -std=c++11 $(INCLUDES) -I. | |
TARGET_OBJS_FOR_TEST = $(filter-out obj/main.o,$(OBJS)) | |
SRCDIRS_TEST := $(shell find tests -type d) | |
SRCS_TEST = $(foreach dir, $(SRCDIRS_TEST), $(wildcard $(dir)/*.cpp)) | |
OBJS_TEST = $(subst tests/,obj/tests/,$(SRCS_TEST:.cpp=.o)) | |
DEPS_TEST = $(subst tests/,obj/tests/,$(SRCS_TEST:.cpp=.dep)) | |
UNAME = ${shell uname} | |
ifeq ($(UNAME),Darwin) | |
SED = gsed | |
endif | |
ifeq ($(UNAME),Linux) | |
SED = sed | |
endif | |
.PHONY: build | |
build: obj/ bin/ $(TARGET) | |
.PHONY: all | |
all: build | |
obj/: | |
mkdir obj | |
bin/: | |
mkdir bin | |
obj/%.o : src/%.cpp | |
mkdir -p $(dir $@) | |
$(CXX) $(CXXFLAGS) -c $< -o $@ | |
$(TARGET): $(OBJS) $(DEPS) | |
$(CXX) $(OBJS) $(LDFLAGS) \ | |
$(INCLUDES) \ | |
-o $(TARGET) | |
obj/tests/%.o : tests/%.cpp | |
mkdir -p $(dir $@) | |
$(CXX) $(CXXFLAGS_TEST) -c $(subst obj/tests/,tests/,$(subst .o,.cpp,$@)) -o $@ | |
.PHONY: clean | |
clean: | |
rm -f $(OBJS) | |
rm -f $(DEPS) | |
rm -f $(TARGET) | |
rm -f $(OBJS_TEST) | |
rm -f $(DEPS_TEST) | |
rm -f $(TARGET_TEST) | |
.PHONY: run | |
run: $(TARGET) | |
./$(TARGET) | |
.PHONY: test | |
test: build $(OBJS_TEST) $(DEPS_TEST) | |
$(CXX) $(CXXFLAGS_TEST) $(TARGET_OBJS_FOR_TEST) $(OBJS_TEST) \ | |
$(LDFLAGS) -lgtest \ | |
-o $(TARGET_TEST) | |
./$(TARGET_TEST) | |
-include $(DEPS) | |
-include $(DEPS_TEST) | |
obj/%.dep: src/%.cpp | |
mkdir -p $(dir $@) | |
$(CXX) -MM $(CXXFLAGS) $(CXXFLAGS_TEST) $< | $(SED) '1i$(dir $@)' | tr -d '\n' | tr -d '\\' > $@ | |
obj/tests/%.dep: tests/%.cpp | |
mkdir -p $(dir $@) | |
$(CXX) -MM $(CXXFLAGS) $(CXXFLAGS_TEST) $< | $(SED) '1i$(dir $@)' | tr -d '\n' | tr -d '\\' > $@ | |
obj/%.o: obj,%.dep | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment