CXX := g++ CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -fwrapv -I. -I/usr/local/lib LIBS := -L/usr/local/lib -lsfml-{system,window,graphics} BIN := game BIN2C := $(shell command -v bin2c 2>/dev/null) ifeq ($(BIN2C),) BIN2C := xxd -i endif object_src := $(wildcard src/objects/*.cpp) object_obj := $(patsubst src/objects/%.cpp,build/objects/%.o,$(object_src)) object_head := $(patsubst src/objects/%.cpp,build/objects/%.h,$(object_src)) object_xlist := $(shell echo $$(for f in $(object_src); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/X(&)/'; done)) sprite_allfiles := $(wildcard src/sprites/*) sprite_fnames := $(filter %.png,$(sprite_allfiles)) sprite_skipped := $(filter-out %.png,$(sprite_allfiles)) sprite_obj := $(patsubst src/sprites/%.png,build/sprites/%.o,$(sprite_fnames)) sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Sprite *&;/'; done)) font_allfiles := $(wildcard src/fonts/*) font_fnames := $(filter %.ttf,$(font_allfiles)) font_skipped := $(filter-out %.ttf %/LICENSE,$(font_allfiles)) font_obj := $(patsubst src/fonts/%.ttf,build/fonts/%.o,$(font_fnames)) font_externs := $(shell echo $$(for f in $(font_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Font *&;/'; done)) base_src := $(wildcard *_base.cpp) base_src += library.cpp sprite.cpp font.cpp main.cpp base_obj := $(patsubst %.cpp,build/%.o,$(base_src)) categories := object sprite font category_headers := $(foreach c,$(categories),build/$(c)s.h) all_objects := $(base_obj) $(foreach c,$(categories),$($(c)_obj)) .PHONY: all clean remake run #keep all intermediate files! .SECONDARY: #we handle our own printing .SILENT: all: $(BIN) clean: echo Cleaning rm -rf build $(BIN) remake: clean make all run: all printf "\x1B[1mRUNNING...\x1B[0m\n" ./$(BIN) $(BIN): $(all_objects) echo Linking $(CXX) $(CXXFLAGS) -o $(BIN) $^ $(LIBS) build/main.o: main.cpp $(category_headers) echo Building main mkdir -p $(dir $@) $(CXX) $(CXXFLAGS) -c -o "$@" "$<" "-DOBJECT_CLASSES_XLIST=$(object_xlist)" "-DGAME_NAME=\"$(BIN)\"" build/%.o: %.cpp echo Compiling $* mkdir -p $(dir $@) $(CXX) $(CXXFLAGS) -c -o "$@" $^ build/objects/%.o: build/objects/%.cpp $(category_headers) echo Building $* mkdir -p $(dir $@) $(CXX) $(CXXFLAGS) -c -o "$@" "$<" build/objects/%.cpp: src/objects/%.cpp echo Generating source for $* mkdir -p $(dir $@) $(CXX) -E object_wrapper.cpp "-D__OBJECT_FNAME__=\"$<\"" "-D__OBJECT_NAME__=$(basename $(notdir $<))" | sed 's/^# [0-9]\+ ".*//' >"$@" build/objects/%.h: src/objects/%.cpp echo Generating header for $* mkdir -p $(dir $@) $(CXX) -E object_header_maker.cpp "-D__OBJECT_NAME__=$(basename $(notdir $<))" "-DEVENT_XLIST=$$($(CXX) -E object_header_extractor.cpp "-D__OBJECT_FNAME__=\"$<\"" | sed 's/__________EVENT_NEWLINE__________/\n/g' | grep -oe '__________EVENT_COUNTER_TAG_[^_]*__________' | sed 's/[^G]*G_\(.*\)__________.*/X(\1)/g' | xargs)" | sed 's/^# [0-9]\+ ".*//' >"$@" build/objects.h: $(object_head) echo Collecting header files echo $(object_head) | xargs -n 1 basename | sed 's,.*,#include "objects/&",' >"$@" build/sprites/%.o: build/sprites/%.cpp echo Building sprite $* mkdir -p $(dir $@) $(CXX) $(CXXFLAGS) -c -o "$@" "$<" build/sprites/%.cpp: src/sprites/%.png echo Generating for sprite $* mkdir -p $(dir $@) printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s\n};\n\nstatic Sprite struct_data=Sprite(data,%d);\nSprite *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@" build/sprites.h: $(sprite_fnames) echo Writing collected sprites header mkdir -p $(dir $@) printf "#include \"sprite.h\"\n\n$(sprite_externs)\n" >"$@" test -z "$(sprite_skipped)" || printf "\x1B[1mWARNING: skipped files in src/sprites: $(notdir $(sprite_skipped))\x1B[0m\n" build/fonts/%.o: build/fonts/%.cpp echo Building font $* mkdir -p $(dir $@) $(CXX) $(CXXFLAGS) -c -o "$@" "$<" build/fonts/%.cpp: src/fonts/%.ttf echo Generating for font $* mkdir -p $(dir $@) printf "#include \"font.h\"\n\nstatic const unsigned char data[]={\n%s\n};\n\nstatic Font struct_data=Font(data,%d);\nFont *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@" build/fonts.h: $(font_fnames) echo Writing collected fonts header mkdir -p $(dir $@) printf "#include \"font.h\"\n\n$(font_externs)\n" >"$@" test -z "$(font_skipped)" || printf "\x1B[1mWARNING: skipped files in src/fonts: $(notdir $(font_skipped))\x1B[0m\n"