|
| 1 | +CXX ?= g++ |
| 2 | + |
| 3 | +# path # |
| 4 | +SRC_PATH = src |
| 5 | +BUILD_PATH = build |
| 6 | +BIN_PATH = $(BUILD_PATH)/bin |
| 7 | + |
| 8 | +# executable # |
| 9 | +BIN_NAME = simulator |
| 10 | + |
| 11 | +# extensions # |
| 12 | +SRC_EXT = cpp |
| 13 | + |
| 14 | +# code lists # |
| 15 | +# Find all source files in the source directory, sorted by |
| 16 | +# most recently modified |
| 17 | +SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' | sort -k 1nr | cut -f2-) |
| 18 | +# Set the object file names, with the source directory stripped |
| 19 | +# from the path, and the build path prepended in its place |
| 20 | +OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) |
| 21 | +# Set the dependency files that will be used to add header dependencies |
| 22 | +DEPS = $(OBJECTS:.o=.d) |
| 23 | + |
| 24 | +# flags # |
| 25 | +COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g |
| 26 | +INCLUDES = -I include/ -I /usr/local/include |
| 27 | +# Space-separated pkg-config libraries used by this project |
| 28 | +LIBS = |
| 29 | + |
| 30 | +.PHONY: default_target |
| 31 | +default_target: release |
| 32 | + |
| 33 | +.PHONY: release |
| 34 | +release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) |
| 35 | +release: dirs |
| 36 | + @$(MAKE) all |
| 37 | + |
| 38 | +.PHONY: dirs |
| 39 | +dirs: |
| 40 | + @echo "Creating directories" |
| 41 | + @mkdir -p $(dir $(OBJECTS)) |
| 42 | + @mkdir -p $(BIN_PATH) |
| 43 | + |
| 44 | +.PHONY: clean |
| 45 | +clean: |
| 46 | + @echo "Deleting $(BIN_NAME) symlink" |
| 47 | + @$(RM) $(BIN_NAME) |
| 48 | + @echo "Deleting directories" |
| 49 | + @$(RM) -r $(BUILD_PATH) |
| 50 | + @$(RM) -r $(BIN_PATH) |
| 51 | + |
| 52 | +# checks the executable and symlinks to the output |
| 53 | +.PHONY: all |
| 54 | +all: $(BIN_PATH)/$(BIN_NAME) |
| 55 | + @echo "Making symlink: $(BIN_NAME) -> $<" |
| 56 | + @$(RM) $(BIN_NAME) |
| 57 | + @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) |
| 58 | + |
| 59 | +# Creation of the executable |
| 60 | +$(BIN_PATH)/$(BIN_NAME): $(OBJECTS) |
| 61 | + @echo "Linking: $@" |
| 62 | + $(CXX) $(OBJECTS) -o $@ |
| 63 | + |
| 64 | +# Add dependency files, if they exist |
| 65 | +-include $(DEPS) |
| 66 | + |
| 67 | +# Source file rules |
| 68 | +# After the first compilation they will be joined with the rules from the |
| 69 | +# dependency files to provide header dependencies |
| 70 | +$(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) |
| 71 | + @echo "Compiling: $< -> $@" |
| 72 | + $(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ |
0 commit comments