From b71f9a58a9af53b097aeca2d84413544695643ea Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sat, 25 Jun 2022 20:56:30 -0700 Subject: Include what you use. --- Makefile | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index debe4b5..9e84cc8 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,12 @@ DEBUG ?= -g AR ?= ar CXX ?= g++ +IWYU ?= iwyu +CHRONIC ?= chronic CXXFLAGS := -Wall -Werror -std=c++20 -fPIC -iquote . ${OPTIMIZE} ${DEBUG} -.DEFAULT: issim +.DEFAULT_GOAL := all VERSION := $(shell git describe --always --dirty --long --tags 2> /dev/null) ifndef VERSION @@ -21,14 +23,19 @@ std::string GIT_TAG = "$(subst ",\",${VERSION})"; endef export GITTAGCPP +ifneq ($(shell which ${IWYU}),) +iwyu = ${CHRONIC} ${IWYU} -Xiwyu --error ${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),) -$$(info Component $(call libname,$(1))) - $(call libname,$(1).a): $(patsubst %.cpp,build/%.o,$(wildcard $(1)/*.cpp)) @mkdir -p $$(dir $$@) $${AR} cr $$@ $$+ @@ -42,15 +49,22 @@ PARTSOS += $(call libname,$(1).so) else -$$(info Header-only component $(1)) +$(call libname,$(1).cpp): + @mkdir -p $$(dir $$@) + @tools/iwyu-header $$(wildcard $(1)/*.h) > $$@ + @$$(call iwyu,$$@) +.PHONY: $(call libname,$(1).cpp) + +EXTRA_TARGETS += $(call libname,$(1).cpp) endif endef -PARTS := $(patsubst ./%,%,$(shell find -mindepth 1 -type d -\( -name .\* -prune -o -name build -prune -o -print -\))) +PARTS := $(patsubst ./%,%,$(shell find -mindepth 1 -type d -\( -name .\* -prune -o -name build -prune -o -name tools -prune -o -print -\))) PARTARS := PARTSOS := +EXTRA_TARGETS := $(foreach part,${PARTS},$(eval $(call mklib,${part}))) @@ -59,6 +73,8 @@ MAINOBJS += build/git-tag.o $(info ) +all: ${EXTRA_TARGETS} issim issim-static + issim: build/issim-dynamic @ln -sf $< $@ .PHONY: issim @@ -88,6 +104,7 @@ include $(shell find -type f -name \*.d) build/%.o: %.cpp @mkdir -p $(dir $@) + @$(call iwyu,$<) ${CXX} ${CXXFLAGS} -MMD -c -o $@ $< build/%.o: build/%.cpp -- cgit v1.2.3