forked from livegrep/livegrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.lib
60 lines (45 loc) · 1.61 KB
/
Makefile.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
comma:=,
MAKEVARS:=.makevars
define BUILD_d_file
.%.d: %.$(1) $(MAKEVARS)/$(2) $(MAKEVARS)/CPPFLAGS $(MAKEVARS)/$(2)FLAGS
@set -e; trap 'rm -f $$@.$$$$$$$$' EXIT; rm -f $$@; \
$($(2)) -MM $$(CPPFLAGS) $($(2)FLAGS) $$< > $$@.$$$$$$$$; \
sed 's,\($$(notdir $$*)\)\.o[ :]*,$$*.o $@ : ,g' < $$@.$$$$$$$$ > $$@
endef
$(eval $(call BUILD_d_file,cc,CXX))
$(eval $(call BUILD_d_file,cpp,CXX))
$(eval $(call BUILD_d_file,C,CXX))
$(eval $(call BUILD_d_file,c,CC))
$(eval $(call BUILD_d_file,S,CC))
$(MAKEVARS):
@mkdir -p $@
$(MAKEVARS)/%.tmp: $(MAKEVARS) FORCE
@echo $(call $*) > $@
$(MAKEVARS)/%: $(MAKEVARS)/%.tmp
@cmp -s $< $@ || cp -f $< $@
@rm $<
override CPPFLAGS += $(patsubst %,-I%, $(DIRS))
SRC :=
TOOLS :=
include $(patsubst %, %/Makefrag, $(DIRS))
LIBOBJS := $(foreach src,$(SRC),$(basename $(src)).o)
define BUILD_tool
$(1)_OBJS := $$(if $$($(1)_SRC),$$(foreach obj,$$($(1)_SRC),$$(basename $$(obj)).o),$(1).o)
OBJECTS += $$($(1)_OBJS)
$(1): override CPPFLAGS+=$$($(1)_CPPFLAGS)
$(1): override CXXFLAGS+=$$($(1)_CXXFLAGS)
$(1): override LDFLAGS+=$$($(1)_LDFLAGS)
$(1): $$(LIBOBJS) $$($(1)_OBJS) $$(MAKEVARS)/LDFLAGS
$$(CXX) -o $$@ $$(LDFLAGS) $$(filter %.o,$$^) $$(LDLIBS)
endef
$(foreach tool,$(TOOLS),$(eval $(call BUILD_tool,$(tool))))
OBJECTS := $(LIBOBJS) $(foreach tool,$(TOOLS),$(call $(tool)_OBJS))
DEPFILES := $(foreach obj,$(OBJECTS),$(dir $(obj)).$(notdir $(obj:.o=)).d)
all: $(TOOLS) $(DEPFILES) $(EXTRA_TARGETS)
clean:
rm -rf $(TOOLS) $(OBJECTS) $(DEPFILES) $(EXTRA_CLEAN)
$(OBJECTS): $(MAKEVARS)/CXX $(MAKEVARS)/CXXFLAGS
-include $(DEPFILES)
.PHONY: FORCE
.PRECIOUS: $(MAKEVARS)/%
.DEFAULT_GOAL := all