ramips: explicitly disable built-in switch when needed
[oweals/openwrt.git] / include / scan.mk
1 include $(TOPDIR)/include/verbose.mk
2 TMP_DIR:=$(TOPDIR)/tmp
3
4 all: $(TMP_DIR)/.$(SCAN_TARGET)
5
6 SCAN_TARGET ?= packageinfo
7 SCAN_NAME ?= package
8 SCAN_DIR ?= package
9 TARGET_STAMP:=$(TMP_DIR)/info/.files-$(SCAN_TARGET).stamp
10 FILELIST:=$(TMP_DIR)/info/.files-$(SCAN_TARGET)-$(SCAN_COOKIE)
11 OVERRIDELIST:=$(TMP_DIR)/info/.overrides-$(SCAN_TARGET)-$(SCAN_COOKIE)
12
13 export PATH:=$(TOPDIR)/staging_dir/host/bin:$(PATH)
14
15 define feedname
16 $(if $(patsubst feeds/%,,$(1)),,$(word 2,$(subst /, ,$(1))))
17 endef
18
19 ifeq ($(SCAN_NAME),target)
20   SCAN_DEPS=image/Makefile profiles/*.mk $(TOPDIR)/include/kernel*.mk $(TOPDIR)/include/target.mk image/*.mk
21 else
22   SCAN_DEPS=$(TOPDIR)/include/package*.mk
23 ifneq ($(call feedname,$(SCAN_DIR)),)
24   SCAN_DEPS += $(TOPDIR)/feeds/$(call feedname,$(SCAN_DIR))/*.mk
25 endif
26 endif
27
28 ifeq ($(IS_TTY),1)
29   ifneq ($(strip $(NO_COLOR)),1)
30     define progress
31         printf "\033[M\r$(1)" >&2;
32     endef
33   else
34     define progress
35         printf "\r$(1)" >&2;
36     endef
37   endif
38 else
39   define progress
40         :;
41   endef
42 endif
43
44 define PackageDir
45   $(TMP_DIR)/.$(SCAN_TARGET): $(TMP_DIR)/info/.$(SCAN_TARGET)-$(1)
46   $(TMP_DIR)/info/.$(SCAN_TARGET)-$(1): $(SCAN_DIR)/$(2)/Makefile $(foreach DEP,$(DEPS_$(SCAN_DIR)/$(2)/Makefile) $(SCAN_DEPS),$(wildcard $(if $(filter /%,$(DEP)),$(DEP),$(SCAN_DIR)/$(2)/$(DEP))))
47         { \
48                 $$(call progress,Collecting $(SCAN_NAME) info: $(SCAN_DIR)/$(2)) \
49                 echo Source-Makefile: $(SCAN_DIR)/$(2)/Makefile; \
50                 $(if $(3),echo Override: $(3),true); \
51                 $(NO_TRACE_MAKE) --no-print-dir -r DUMP=1 FEED="$(call feedname,$(2))" -C $(SCAN_DIR)/$(2) $(SCAN_MAKEOPTS) 2>/dev/null || { \
52                         mkdir -p "$(TOPDIR)/logs/$(SCAN_DIR)/$(2)"; \
53                         $(NO_TRACE_MAKE) --no-print-dir -r DUMP=1 FEED="$(call feedname,$(2))" -C $(SCAN_DIR)/$(2) $(SCAN_MAKEOPTS) > $(TOPDIR)/logs/$(SCAN_DIR)/$(2)/dump.txt 2>&1; \
54                         $$(call progress,ERROR: please fix $(SCAN_DIR)/$(2)/Makefile - see logs/$(SCAN_DIR)/$(2)/dump.txt for details\n) \
55                         rm -f $$@; \
56                 }; \
57                 echo; \
58         } > $$@.tmp
59         mv $$@.tmp $$@
60 endef
61
62 $(OVERRIDELIST):
63         rm -f $(TMP_DIR)/info/.overrides-$(SCAN_TARGET)-*
64         touch $@
65
66 ifeq ($(SCAN_NAME),target)
67   GREP_STRING=BuildTarget
68 else
69   GREP_STRING=(Build/DefaultTargets|BuildPackage|KernelPackage)
70 endif
71
72 $(FILELIST): $(OVERRIDELIST)
73         rm -f $(TMP_DIR)/info/.files-$(SCAN_TARGET)-*
74         find -L $(SCAN_DIR) $(SCAN_EXTRA) -mindepth 1 $(if $(SCAN_DEPTH),-maxdepth $(SCAN_DEPTH)) -name Makefile | xargs grep -aHE 'call $(GREP_STRING)' | sed -e 's#^$(SCAN_DIR)/##' -e 's#/Makefile:.*##' | uniq | awk -v of=$(OVERRIDELIST) -f include/scan.awk > $@
75
76 $(TMP_DIR)/info/.files-$(SCAN_TARGET).mk: $(FILELIST)
77         ( \
78                 cat $< | awk '{print "$(SCAN_DIR)/" $$0 "/Makefile" }' | xargs grep -HE '^ *SCAN_DEPS *= *' | awk -F: '{ gsub(/^.*DEPS *= */, "", $$2); print "DEPS_" $$1 "=" $$2 }'; \
79                 awk -F/ -v deps="$$DEPS" -v of="$(OVERRIDELIST)" ' \
80                 BEGIN { \
81                         while (getline < (of)) \
82                                 override[$$NF]=$$0; \
83                         close(of) \
84                 } \
85                 { \
86                         info=$$0; \
87                         gsub(/\//, "_", info); \
88                         dir=$$0; \
89                         pkg=""; \
90                         if($$NF in override) \
91                                 pkg=override[$$NF]; \
92                         print "$$(eval $$(call PackageDir," info "," dir "," pkg "))"; \
93                 } ' < $<; \
94                 true; \
95         ) > $@.tmp
96         mv $@.tmp $@
97
98 -include $(TMP_DIR)/info/.files-$(SCAN_TARGET).mk
99
100 $(TARGET_STAMP)::
101         +( \
102                 $(NO_TRACE_MAKE) $(FILELIST); \
103                 MD5SUM=$$(cat $(FILELIST) $(OVERRIDELIST) | mkhash md5 | awk '{print $$1}'); \
104                 [ -f "$@.$$MD5SUM" ] || { \
105                         rm -f $@.*; \
106                         touch $@.$$MD5SUM; \
107                         touch $@; \
108                 } \
109         )
110
111 $(TMP_DIR)/.$(SCAN_TARGET): $(TARGET_STAMP)
112         $(call progress,Collecting $(SCAN_NAME) info: merging...)
113         -cat $(FILELIST) | awk '{gsub(/\//, "_", $$0);print "$(TMP_DIR)/info/.$(SCAN_TARGET)-" $$0}' | xargs cat > $@ 2>/dev/null
114         $(call progress,Collecting $(SCAN_NAME) info: done)
115         echo
116
117 FORCE:
118 .PHONY: FORCE
119 .NOTPARALLEL: