-
Notifications
You must be signed in to change notification settings - Fork 4
/
make-helper-functions.mk
185 lines (141 loc) · 6.64 KB
/
make-helper-functions.mk
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# I don't like doing this, but dash removes environment variables which aren't named to it's liking
# I use some bash scripts anyway, and bash doesn't do such nonesense, so... yea, this'll use bash now too, sorry.
SHELL=/bin/bash
default_target: all
.SECONDARY:
project_root := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))..)
export project_root
define newline
endef
define include_config_if_exits
ifeq ($(shell test -e "$(project_root)/config/$(1)" && echo -n yes),yes)
include $(project_root)/config/$(1)
ifeq ($(shell test -e "$(project_root)/config/user_config_override" && echo -n yes),yes)
include $(project_root)/config/user_config_override
endif
endif
endef
VARS_OLD := $(subst %,,$(subst *,,$(.VARIABLES)))
ifeq ($(shell test -e "$(project_root)/config/user_config_override" && echo -n yes),yes)
include $(project_root)/config/user_config_override
endif
$(eval $(call include_config_if_exits,default/config))
$(foreach config,$(CONFIG_PATH),$(eval $(call include_config_if_exits,$(config)/config)))
ifdef REPO-$(DISTRO)
REPO = $(REPO-$(DISTRO))
endif
ifdef REPO-$(DISTRO)-$(RELEASE)
REPO = $(REPO-$(DISTRO)-$(RELEASE))
endif
ifdef CHROOT_REPO-$(DISTRO)
CHROOT_REPO = $(CHROOT_REPO-$(DISTRO))
endif
ifdef CHROOT_REPO-$(DISTRO)-$(RELEASE)
CHROOT_REPO = $(CHROOT_REPO-$(DISTRO)-$(RELEASE))
endif
CONFIG_VARS := $(sort $(filter-out $(VARS_OLD) VARS_OLD,$(subst %,,$(subst *,,$(.VARIABLES)))))
IMGSIZE := $(shell echo "$(IMGSIZE)" | sed 's/\s*//g')
export $(CONFIG_VARS)
CONF = user_config_override
X_DEBOOTSTRAP_DIR = $(project_root)/build/$(DISTRO)-$(RELEASE)/debootstrap_script/
DEBOOTSTRAP_SCRIPT = $(X_DEBOOTSTRAP_DIR)/usr/share/debootstrap/scripts/$(RELEASE)
PLATFORM_AGNOSTIC_TARGETS += config-set//% config-unset//% config-after-update//%
PLATFORM_AGNOSTIC_TARGETS += mirror//% chroot//% %/.dir
PLATFORM_AGNOSTIC_TARGETS += $(project_root)/build/$(DISTRO)-$(RELEASE)/%
PLATFORM_AGNOSTIC_TARGETS += build/$(DISTRO)-$(RELEASE)/%
PLATFORM_AGNOSTIC_TARGETS += $(project_root)/build/bin/%
PLATFORM_AGNOSTIC_TARGETS += build/bin/%
PLATFORM_AGNOSTIC_TARGETS += $(DEBOOTSTRAP_SCRIPT)
PLATFORM_AGNOSTIC_TARGETS += config-list
PLATFORM_AGNOSTIC_TARGETS += generate_make_build_dependencies_for_debs
PLATFORM_AGNOSTIC_TARGETS += clean-build-all
ifndef BUILDER_PLATFORM
ifneq (,$(MAKECMDGOALS))
ifeq (,$(filter-out $(PLATFORM_AGNOSTIC_TARGETS),$(MAKECMDGOALS)))
BUILDER_PLATFORM=none
endif
endif
endif
ifndef BUILDER_PLATFORM
KNOWN_BOARDS=$(shell basename -a "$(project_root)/config/default/"b-*/ | sed 's|^b-||')
$(error "$(newline)Please specify a board. Pass BOARD= to make, or set it in the config using `make config-set//BOARD TO=my-board`.$(newline)Availabe boards are: $(KNOWN_BOARDS)$(newline)")
endif
include $(project_root)/src/repositories.mk
export PATH := /helper/bin:$(project_root)/script/:/sbin:/usr/sbin:$(PATH):$(project_root)/build/bin:$(project_root)/bin
include $(project_root)/src/package_list.mk
export DEBIAN_FRONTEND=noninteractive
ifneq ($(BUILDER_PLATFORM),none)
include $(project_root)/platform/$(BUILDER_PLATFORM)/platform.mk
endif
export X_DEBOOTSTRAP_DIR
export DEBOOTSTRAP_SCRIPT
define repodir
repo/$(shell printf '%s\n' "$(repo-source@$(1))" | sed 's / ∕ g').git
endef
generate_make_build_dependencies_for_debs:
export DEP_PREFIX=$(DEP_PREFIX); \
export DEP_SUFFIX=$(DEP_SUFFIX); \
if [ -n "$(TMP_TARGET_FILE)" ]; \
then generate_make_build_dependencies_for_debs.sh >"$(TMP_TARGET_FILE)"; \
else generate_make_build_dependencies_for_debs.sh; \
fi
%/.dir:
mkdir -p "$(dir $@)"
touch "$@"
chroot//%:
export PROMPT_COMMAND="export PS1='$@ (\u)> '"; \
CHNS_INTERACTIVE=1 chns "$(realpath $(patsubst chroot//%,%,$@))" /bin/bash
clean:
@! printf "Please use one of:\n * make clean-build\t# remove all files built for the target image (includes the image)\n * make clean-build-all\t# remove all files that have been built\n * make clean-repo\t# remove the downloaded repos\n * make update-repo\t# clean up all changes made to the repo & update it if possible\n * make clean-all\t# same as 'make clean-repo clean-build'\n * make clean-all-all\t# same as 'make clean-repo clean-build-all'\n * make reset\t\t# same as 'make update-repo clean-build'\n * make reset-all\t# same as 'make update-repo clean-build-all'\n"
repo/%.git:
repo="$(shell echo "$(patsubst repo/%.git,%,$@)" | sed 's ∕ / g')"; \
git clone --mirror "$$repo" "$@"
mirror//%:
$(MAKE) "repo/$(shell echo "$(patsubst mirror//%,%,$@)" | sed 's / ∕ g').git"
repo//%:
$(MAKE) "$(call repodir,$(patsubst repo//%,%,$@))"
clean-repo//%:
rm -rf "$(call repodir,$(patsubst clean-repo//%,%,$@))"
update-repo//%:
repo="$(call repodir,$(patsubst update-repo//%,%,$@))"; \
if [ -d "$$repo" ] && ([ -z "$$UPDATE_REPO_ONLY_IF_OLDER_THAN" ] || [ "$$(($$(date '+%s') - $$(stat -c '%Y' "$$repo")))" -ge "$$UPDATE_REPO_ONLY_IF_OLDER_THAN" ]); \
then \
cd "$$repo" && git remote update && touch .; \
fi
config-list:
@$(foreach VAR,$(CONFIG_VARS), echo "$(VAR)" = "$($(VAR))"; )
config-after-update//%:
@ set -e; \
case "$(patsubst config-after-update//%,%,$@)" in \
"IMGSIZE" ) $(MAKE) clean-image ;; \
"REPO" ) $(MAKE) clean-fs ;; \
"CHROOT_REPO") $(MAKE) clean-fs ;; \
"KERNEL_DTB" ) $(MAKE) clean-fs ;; \
"IMAGE_NAME" ) $(MAKE) clean-image ;; \
"repo-branch@"*) $(MAKE) "update-repo//$(patsubst config-after-update//repo-branch@%,%,$@)" ;; \
"repo-source@"*) $(MAKE) "update-repo//$(patsubst config-after-update//repo-source@%,%,$@)" FETCH_REQUIRED_TO_SUCCEED=true ;; \
"UBOOT_DTB" | "UBOOT_CONFIG_TARGET" | "repo-source@uboot" | "repo-branch@uboot") \
$(MAKE) -C "$(project_root)/platform/$(BUILDER_PLATFORM)/" clean-build \
;; \
"KERNEL_CONFIG_TARGET" | "repo-source@linux" | "repo-branch@linux") \
$(MAKE) -C "$(project_root)/kernel/" clean-build \
;; \
esac
config-set//%:
@ if [ -z "$(TO)" ]; \
then echo "Usage: config-set//variablename TO=new_value"; \
false; \
fi
V="$(patsubst config-set//%,%,$@)"; \
sed -i "/^$$V[ ]*=/d" "$(project_root)/config/$(CONF)" 2>&-; \
echo "$$V = $(TO)" >> $(project_root)/config/$(CONF)
@ $(MAKE) --no-print-directory OLD_VALUE="$($(patsubst config-set//%,%,$@))" "config-after-update//$(patsubst config-set//%,%,$@)"
config-unset//%:
V="$(patsubst config-unset//%,%,$@)"; \
sed -i "/^$$V[ ]*=/d" "$(project_root)/config/$(CONF)"
@ $(MAKE) --no-print-directory OLD_VALUE="$($(patsubst config-unset//%,%,$@))" "config-after-update//$(patsubst config-unset//%,%,$@)"
clean-all: clean-repo clean-build
clean-all-all: clean-repo clean-build-all
reset: update-repo clean-build
reset-all: update-repo clean-build-all
.PHONY: all repo update-repo clean-repo clean clean-all clean-all-all clean-build clean-build-all