summaryrefslogtreecommitdiff
path: root/build-aux/Makefile.head.mk
blob: f4eb51da5b81463867dfa6a25a1816dbedceaa12 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright (C) 2015-2017  Luke Shumaker
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

at.Makefile ?= Makefile
_at.MAKEFILE_LIST ?=
_at.MAKEFILE_LIST := $(strip $(_at.MAKEFILE_LIST) $(lastword $(filter %/$(at.Makefile),$(abspath $(MAKEFILE_LIST)))))

# This bit only gets evaluated once, at the very beginning
ifeq ($(origin _at.NO_ONCE),undefined)

# Internal functions ###################################################

# These 4 functions are all $(call _at.func,parent,child)
_at.is_strict_subdir  = $(filter $(abspath $1)/%,$(abspath $2))
_at.is_subdir         = $(filter $(abspath $1)/%,$(abspath $2)/.)
_at.relbase = $(strip                                                   \
  $(if $(call _at.is_subdir,$1,$2),                                     \
       $(patsubst %/.,%,$(patsubst $(abspath $1)/%,%,$(abspath $2)/.)), \
       $(abspath $2)))
_at.relto = $(strip                                                     \
  $(if $(call _at.is_subdir,$1,$2),                                     \
       $(patsubst %/.,%,$(patsubst $(abspath $1)/%,%,$(abspath $2)/.)), \
       ../$(call _at.relto,$(dir $1),$2)))

# These 3 functions only take one operand; we define public multi-operand
# versions below.
_at.path = $(strip                                                            \
  $(if $(call _at.is_subdir,$(topoutdir),$1),                                 \
       $(patsubst %/.,%,$(topoutdir)/$(call _at.relto,.,$1)),                 \
       $(if $(call _at.is_subdir,$(topsrcdir),$1),                            \
            $(patsubst %/.,%,$(topsrcdir)/$(call _at.relto,$(topsrcdir),$1)), \
            $(abspath $1))))
_at.out2src = $(call _at.path,$(if $(call _at.is_subdir,$(topoutdir),$1),$(topsrcdir)/$(call _at.path,$1),$1))
_at.addprefix = $(call _at.path,$(if $(filter-out /%,$2),$1/$2,$2))

_at.rest = $(wordlist 2,$(words $1),$1)
_at.reverse = $(if $1,$(call _at.reverse,$(_at.rest))) $(firstword $1)

_at.target_variable           = $(_at.target_variable.$(flavor $2))
_at.target_variable.recursive = $1: private $2  = $(subst $(at.nl),$$(at.nl),$(value $2))
_at.target_variable.simple    = $1: private $2 := $$($2)

_at.quote-pattern = $(subst %,\%,$(subst \,\\,$1))

# Sanity checking ######################################################
ifeq ($(filter undefine,$(.FEATURES)),)
$(error Autothing: We need a version of Make that supports 'undefine')
endif
ifeq ($(topsrcdir),)
$(error Autothing: topsrcdir must be set (and non-empty) before including Makefile.head.mk)
endif
ifeq ($(topoutdir),)
$(error Autothing: topoutdir must be set (and non-empty) before including Makefile.head.mk)
endif
ifneq ($(call _at.is_strict_subdir,$(topoutdir),$(topsrcdir)),)
$(error Autothing: topsrcdir=$(topsrcdir) must not be a subdirectory of topoutdir=$(topoutdir))
endif

# External provisions ##################################################

# These 4 functions are all $(call _at.func,parent,child)
at.is_subdir = $(_at.is_subdir)
at.is_strict_subdir = $(_at.is_strict_subdir)
#at.relbase = $(if $2,$(shell realpath -sm --relative-base=$1 -- $2))
at.relbase = $(foreach _at.tmp,$2,$(call _at.relbase,$1,$(_at.tmp)))
#at.relto   = $(if $2,$(shell realpath -sm --relative-to=$1   -- $2))
at.relto   = $(foreach _at.tmp,$2,$(call _at.relto,$1,$(_at.tmp)))

at.path    = $(foreach _at.tmp,$1,$(call _at.path,$(_at.tmp)))
at.out2src = $(foreach _at.tmp,$1,$(call _at.out2src,$(_at.tmp)))
at.addprefix = $(foreach _at.tmp,$2,$(call _at.addprefix,$1,$(_at.tmp)))

define at.nl


endef

# Include modules ######################################################
include $(sort $(wildcard $(topsrcdir)/build-aux/Makefile.once.head/*.mk))
_at.tmp_targets =
_at.tmp_subdirs =
_at.VARIABLES =
_at.VARIABLES := $(.VARIABLES)

endif # _at.NO_ONCE

# This bit gets evaluated for each Makefile

outdir := $(call _at.path,$(dir $(lastword $(_at.MAKEFILE_LIST))))
ifeq ($(call _at.is_subdir,$(topoutdir),$(outdir)),)
$(error Autothing: not a subdirectory of topoutdir=$(topoutdir): $(outdir))
endif

# Don't use at.out2src because we *know* that $(outdir) is inside $(topoutdir),
# and has already had $(_at.path) called on it.
srcdir := $(call _at.path,$(topsrcdir)/$(outdir))
ifeq ($(call _at.is_subdir,$(topsrcdir),$(srcdir)),)
$(error Autothing: not a subdirectory of topsrcdir=$(topsrcdir): $(srcdir))
endif

at.subdirs =
at.targets =

include $(sort $(wildcard $(topsrcdir)/build-aux/Makefile.each.head/*.mk))