OPTIMIZE ?= -O3 DEBUG ?= -g AR ?= ar CXX ?= g++ IWYU ?= iwyu CHRONIC ?= chronic PKGS := fmt PKG_FLAGS := $(shell pkg-config --cflags ${PKGS}) PKG_LIBS := -Wl,--push-state,--as-needed $(shell pkg-config --libs ${PKGS}) -Wl,--pop-state CXXFLAGS := -Wall -Werror -std=c++20 -fPIC -iquote . ${PKG_FLAGS} ${OPTIMIZE} ${DEBUG} .DEFAULT_GOAL := all VERSION := $(shell git describe --always --dirty --long --tags 2> /dev/null) ifndef VERSION VERSION := unknown endif $(info Version ${VERSION}) define GITTAGCPP = #include std::string GIT_TAG = "$(subst ",\",${VERSION})"; endef export GITTAGCPP ifneq ($(shell which ${IWYU}),) iwyu = ${CHRONIC} ${IWYU} -Xiwyu --error -Xiwyu --mapping_file=tools/iwyu.imp ${CXXFLAGS} $(1) else $(warning Not using IWYU) iwyu = endif libname = $(shell realpath --canonicalize-missing --relative-to . build/$(dir $(1))/lib$(notdir $(1))) define mklib = ifneq ($(wildcard $(1)/*.cpp),) $(call libname,$(1).a): $(patsubst %.cpp,build/%.o,$(wildcard $(1)/*.cpp)) @mkdir -p $$(dir $$@) $${AR} cr $$@ $$+ $(call libname,$(1).so): $(patsubst %.cpp,build/%.o,$(wildcard $(1)/*.cpp)) @mkdir -p $$(dir $$@) $${CXX} $${CXXFLAGS} -shared -o $$@ $$+ ${PKG_LIBS} PARTARS += $(call libname,$(1).a) PARTSOS += $(call libname,$(1).so) endif ifdef iwyu build/$(1)/iwyu.cpp: @mkdir -p $$(dir $$@) @tools/iwyu-header $$(wildcard $(1)/*.h) > $$@ @$$(call iwyu,$$@) .PHONY: build/$(1)/iwyu.cpp IWYU_CPPS += build/$(1)/iwyu.cpp endif endef PARTS := $(patsubst ./%,%,$(shell find -mindepth 1 -type d -\( -name .\* -prune -o -name build -prune -o -name tools -prune -o -print -\))) PARTARS := PARTSOS := IWYU_CPPS := $(foreach part,${PARTS},$(eval $(call mklib,${part}))) MAINOBJS := $(patsubst %.cpp,build/%.o,$(wildcard *.cpp)) MAINOBJS += build/git-tag.o $(info ) all: ${IWYU_CPPS} issim issim-static issim: build/issim-dynamic @ln -sf $< $@ .PHONY: issim issim-static: build/issim-static @ln -sf $< $@ .PHONY: issim-static build/issim-dynamic: ${MAINOBJS} ${PARTSOS} @mkdir -p $(dir $@) ${CXX} ${CXXFLAGS} -o $@ $+ ${PKG_LIBS} build/issim-static: ${MAINOBJS} ${PARTARS} @mkdir -p $(dir $@) ${CXX} ${CXXFLAGS} -o $@ $+ ${PKG_LIBS} clean: rm -rf build issim issim-static .PHONY: clean build/git-tag.cpp: @mkdir -p $(dir $@) @echo "$$GITTAGCPP" > $@ .PHONY: build/git-tag.cpp include $(shell find -type f -name \*.d) build/%.o: %.cpp @mkdir -p $(dir $@) @$(call iwyu,$<) ${CXX} ${CXXFLAGS} -MMD -c -o $@ $< build/%.o: build/%.cpp @mkdir -p $(dir $@) ${CXX} ${CXXFLAGS} -MMD -c -o $@ $< .SUFFIXES: .SECONDARY: