# -*- makefile-gmake -*-
# $Id: Makefile.meta_p 87445 2019-08-30 13:34:26Z ucko $

### Project-list-based recursive builds

include $(builddir)/Makefile.mk

PROJECTS = $(builddir)/projects.pseudo.i

# Some preprocessors only accept files with appropriate extensions....
$(builddir)/projects.pseudo.c: $(PROJECTS_)
        $(RM) $@
        $(if $(wildcard $(PROJECTS_)),$(LN_S) "$(PROJECTS_)",echo ".*" >) $@

PROJECTS_CPPFLAGS=$(if $(PROJECTS_),-I$(dir $(PROJECTS_)),) \
            -I$(top_srcdir)/scripts/projects \
            -I$(top_srcdir)/scripts/internal/projects

$(PROJECTS): $(builddir)/projects.pseudo.c
        $(CPP) $(PROJECTS_CPPFLAGS) $(builddir)/projects.pseudo.c \
            | $(SED) -e '/#/d' > $@

$(builddir)/projects.pseudo.d: $(builddir)/projects.pseudo.c
        $(CC) $(PROJECTS_CPPFLAGS) $(DEPFLAGS) $(builddir)/projects.pseudo.c \
          $(DEPFLAGS_POST) \
            | $(SED) -e '/^\#/b' -e 's%[^ :\\][^ :\\]*%$$(wildcard &)%g' \
               -e 's%\$$(wildcard [^:]*$(obj_ext)) *:%$(PROJECTS) $@ :%g' > $@_
        test -s $@_
        /bin/mv -f $@_ $@

-include $(builddir)/projects.pseudo.d

# $(RELEVANT_FILE) contains the actual patterns to grep for, formed by
# prepending "build/" to each original pattern as an anchor.  Creating
# it correctly requires a kludge under Cygwin. :-/

ifeq "$(OSTYPE)" "cygwin"
  PATTERN_FILTER = | tr -d '\r'
else
  PATTERN_FILTER =
endif

RELEVANT_FILE = $(builddir)/relevant.patterns
$(RELEVANT_FILE): $(PROJECTS) $(top_srcdir)/src/build-system/Makefile.meta.in
        @if test -s "$(PROJECTS)"; then \
            $(SED) -e '/ update-only$$/d; /^[   ]*$$/d; /^-/d; s@^@build/@' \
                -e 's:\([^$$/\\]\)\(\$$*\)$$:\1/\2:' \
              $(PROJECTS); \
        else \
            echo '.*'; \
        fi $(PATTERN_FILTER) > $@

IRRELEVANT_FILE = $(builddir)/irrelevant.patterns
$(IRRELEVANT_FILE): $(PROJECTS) $(top_srcdir)/src/build-system/Makefile.meta.in
        echo '^$$' $(PATTERN_FILTER) > $@
ifneq "$(wildcard $(PROJECTS_))" ""
        $(SED) -ne 's:\([^$$/\\]\)\(\$$*\)$$:\1/\2:; s@^-@build/@p' \
            $(PROJECTS) $(PATTERN_FILTER) >> $@
endif

# Commands to see whether project directories are relevant.
# 1: dir, 2: sub?
define is_relevant
echo $(CURDIR)/$(1) | $(EGREP) -f $(RELEVANT$(2)_FILE) | \
    $(EGREP_Q) -v -f $(IRRELEVANT$(2)_FILE)
endef

RELEVANT_SUB_FILE = $(builddir)/relevant.prefixes
$(RELEVANT_SUB_FILE): $(RELEVANT_FILE)
        @while read path; do \
            case "$$path" in \
                build/*) ;; \
                *) echo "relevant.prefixes: Ignoring bad pattern $$path" >&2; \
                   continue ;; \
            esac; \
            echo $$path; \
            while [ "$$path" != build ]; do \
                path=`/usr/bin/dirname $$path` ; \
                echo "$$path/\$$"; \
            done; \
        done < $(RELEVANT_FILE) | \
            /usr/bin/sort -u $(PATTERN_FILTER) > $(RELEVANT_SUB_FILE)

IRRELEVANT_SUB_FILE = $(builddir)/irrelevant.prefixes
$(IRRELEVANT_SUB_FILE): $(IRRELEVANT_FILE)
        $(GREP) -v '[^^]\$$$$' $(IRRELEVANT_FILE) \
            $(PATTERN_FILTER) > $(IRRELEVANT_SUB_FILE)

auto-relevance.mk: $(RELEVANT_SUB_FILE) $(IRRELEVANT_SUB_FILE)
        @(if $(call is_relevant,,_SUB) ; then echo "RELEVANT_SUB = 1" ; fi ; \
            if $(call is_relevant,,) ; then echo "RELEVANT = 1" ; fi) > $@

# 1: name, 2: target, 3: is_expendable?
define make_one_sub_proj_p
+@if $(call is_relevant,$(1)/,)  ||  $(call is_relevant,$(1)/,_SUB) ; then \
    $(echo_unless_silent) $(MAKE) -C $(1) $(MFLAGS_NR) $(2) ; \
    $(call make_one_sub_proj_r_nonl,$(1),$(2),$(3)) ; \
fi
endef

# 1: target
define make_each_sub_proj_p
$(foreach p,$(SUB_PROJ) $(POTENTIAL_SUB_PROJ),$(ign)$(call make_one_sub_proj_x,$(p),_p,$(1)))
$(foreach p,$(EXPENDABLE_SUB_PROJ),-$(call make_one_sub_proj_x,$(p),_p,$(1),1))
endef

deps_p=
cmds_p=
rec_targets_p = $(base_targets:%=%_p)
$(rec_targets_p): %_p: %_p.needs-reqs

-include auto-relevance.mk

ifeq "$(RELEVANT)" "1"
  # 1: target
  deps_p=$(1)
endif

ifeq "$(RELEVANT_SUB)" "1"
  # 1: target
  cmds_p=$(call make_each_sub_proj_p,$(1)_p)
endif

# .SECONDEXPANSION:

all_p.real: $(call deps_p,all_l) configurables
        @-$(RM) .built_p
        @-echo "`$(DATE)`: building in subdirectories of $(CURDIR)" > .building
        $(call cmds_p,all)
        @-$(RM) .building
        @-echo "`$(DATE)`: recursive build succeeded in $(CURDIR)" > .built_p

check_p.real:
        $(call make_check,check_add_p)

# $$* is new in 3.81, and may be overkill
$(addsuffix _p.real,$(generic_targets)): %_p.real: $(call deps_p,%_l)
        $(call cmds_p,$*)

mark-as-disabled_p.needs-reqs: mark-as-disabled_p.real ;

base_req_needers += $(filter-out mark-as-disabled_p,$(rec_targets_p))
.PHONY: $(rec_targets_p) $(rec_targets_p:%=%.real)
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143

-