-
Notifications
You must be signed in to change notification settings - Fork 523
/
Makefile
249 lines (199 loc) · 8.48 KB
/
Makefile
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
PROJECT_DIR := $(shell pwd)
DL_DIR ?= $(PROJECT_DIR)/dl
OUTPUT_DIR ?= $(PROJECT_DIR)/output
CCACHE_DIR ?= $(PROJECT_DIR)/buildroot-ccache
LOCAL_MK ?= $(PROJECT_DIR)/batocera.mk
EXTRA_OPTS ?=
DOCKER_OPTS ?=
MAKE_JLEVEL ?= $(shell nproc)
BATCH_MODE ?=
PARALLEL_BUILD ?=
DIRECT_BUILD ?=
-include $(LOCAL_MK)
ifdef PARALLEL_BUILD
EXTRA_OPTS += BR2_PER_PACKAGE_DIRECTORIES=y
MAKE_OPTS += -j$(MAKE_JLEVEL)
endif
TARGETS := $(sort $(shell find $(PROJECT_DIR)/configs/ -name 'b*.board' | sed -n 's/.*\/batocera-\(.*\).board/\1/p'))
UID := $(shell id -u)
GID := $(shell id -g)
OS := $(shell uname)
ifeq ($(OS),Darwin)
$(if $(shell which gfind 2>/dev/null),,$(error "gfind not found! Please install findutils from Homebrew."))
FIND ?= gfind
else
FIND ?= find
endif
UC = $(shell echo '$1' | tr '[:lower:]' '[:upper:]')
# define build command based on whether we are building direct or inside a docker build container
ifdef DIRECT_BUILD
define MAKE_BUILDROOT
make $(MAKE_OPTS) O=$(OUTPUT_DIR)/$* \
BR2_EXTERNAL=$(PROJECT_DIR) \
BR2_DL_DIR=$(DL_DIR) \
BR2_CCACHE_DIR=$(CCACHE_DIR) \
-C $(PROJECT_DIR)/buildroot
endef
else # DIRECT_BUILD
DOCKER ?= docker
ifndef BATCH_MODE
DOCKER_OPTS += -i
endif
DOCKER_REPO ?= batoceralinux
IMAGE_NAME ?= batocera.linux-build
define RUN_DOCKER
$(DOCKER) run -t --init --rm \
-e HOME \
-v $(PROJECT_DIR):/build \
-v $(DL_DIR):/build/buildroot/dl \
-v $(OUTPUT_DIR)/$*:/$* \
-v $(CCACHE_DIR):$(HOME)/.buildroot-ccache \
-w /$* \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/group:/etc/group:ro \
-u $(UID):$(GID) \
$(DOCKER_OPTS) \
$(DOCKER_REPO)/$(IMAGE_NAME)
endef
define MAKE_BUILDROOT
$(RUN_DOCKER) make $(MAKE_OPTS) O=/$* \
BR2_EXTERNAL=/build \
-C /build/buildroot
endef
endif # DIRECT_BUILD
vars:
@echo "Supported targets: $(TARGETS)"
@echo "Project directory: $(PROJECT_DIR)"
@echo "Download directory: $(DL_DIR)"
@echo "Build directory: $(OUTPUT_DIR)"
@echo "ccache directory: $(CCACHE_DIR)"
@echo "Extra options: $(EXTRA_OPTS)"
ifndef DIRECT_BUILD
@echo "Docker repo/image: $(DOCKER_REPO)/$(IMAGE_NAME)"
@echo "Docker options: $(DOCKER_OPTS)"
endif
@echo "Make options: $(MAKE_OPTS)"
_check_docker:
$(if $(DIRECT_BUILD),$(error "This is a direct build environment"))
$(if $(shell which $(DOCKER) 2>/dev/null),, $(error "$(DOCKER) not found!"))
build-docker-image: _check_docker
$(DOCKER) build . -t $(DOCKER_REPO)/$(IMAGE_NAME)
@touch .ba-docker-image-available
.ba-docker-image-available: _check_docker
@$(DOCKER) pull $(DOCKER_REPO)/$(IMAGE_NAME)
@touch .ba-docker-image-available
batocera-docker-image: $(if $(DIRECT_BUILD),,$(.ba-docker-image-available))
update-docker-image: _check_docker
-@rm .ba-docker-image-available > /dev/null
@$(MAKE) batocera-docker-image
publish-docker-image: _check_docker
@$(DOCKER) push $(DOCKER_REPO)/$(IMAGE_NAME):latest
output-dir-%: %-supported
@mkdir -p $(OUTPUT_DIR)/$*
ccache-dir:
@mkdir -p $(CCACHE_DIR)
dl-dir:
@mkdir -p $(DL_DIR)
%-supported:
$(if $(findstring $*, $(TARGETS)),,$(error "$* not supported!"))
%-clean: batocera-docker-image output-dir-%
@$(MAKE_BUILDROOT) clean
%-config: batocera-docker-image output-dir-%
@$(PROJECT_DIR)/configs/createDefconfig.sh $(PROJECT_DIR)/configs/batocera-$*
@for opt in $(EXTRA_OPTS); do \
echo $$opt >> $(PROJECT_DIR)/configs/batocera-$*_defconfig ; \
done
@$(MAKE_BUILDROOT) batocera-$*_defconfig
%-build: batocera-docker-image %-config ccache-dir dl-dir
@$(MAKE_BUILDROOT) $(CMD)
%-source: batocera-docker-image %-config ccache-dir dl-dir
@$(MAKE_BUILDROOT) source
%-show-build-order: batocera-docker-image %-config ccache-dir dl-dir
@$(MAKE_BUILDROOT) show-build-order
%-kernel: batocera-docker-image %-config ccache-dir dl-dir
@$(MAKE_BUILDROOT) linux-menuconfig
# force -j1 or graph-depends python script will bail
%-graph-depends: batocera-docker-image %-config ccache-dir dl-dir
@$(MAKE_BUILDROOT) -j1 BR2_GRAPH_OUT=svg graph-depends
%-shell: batocera-docker-image output-dir-% _check_docker
$(if $(BATCH_MODE),$(if $(CMD),,$(error "not supported in BATCH_MODE if CMD not specified!")),)
@$(RUN_DOCKER) $(CMD)
%-ccache-stats: batocera-docker-image %-config ccache-dir dl-dir
@$(MAKE_BUILDROOT) ccache-stats
%-build-cmd:
@echo $(MAKE_BUILDROOT)
%-cleanbuild: %-clean %-build
@echo
%-pkg:
$(if $(PKG),,$(error "PKG not specified!"))
@$(MAKE) $*-build CMD=$(PKG)
%-webserver: output-dir-%
$(if $(wildcard $(OUTPUT_DIR)/$*/images/batocera/*),,$(error "$* not built!"))
$(if $(shell which python 2>/dev/null),,$(error "python not found!"))
ifeq ($(strip $(BOARD)),)
$(if $(wildcard $(OUTPUT_DIR)/$*/images/batocera/images/$*/.*),,$(error "Directory not found: $(OUTPUT_DIR)/$*/images/batocera/images/$*"))
python3 -m http.server --directory $(OUTPUT_DIR)/$*/images/batocera/images/$*/
else
$(if $(wildcard $(OUTPUT_DIR)/$*/images/batocera/images/$(BOARD)/.*),,$(error "Directory not found: $(OUTPUT_DIR)/$*/images/batocera/images/$(BOARD)"))
python3 -m http.server --directory $(OUTPUT_DIR)/$*/images/batocera/images/$(BOARD)/
endif
%-rsync: output-dir-%
$(eval TMP := $(call UC, $*)_IP)
$(if $(shell which rsync 2>/dev/null),, $(error "rsync not found!"))
$(if $($(TMP)),,$(error "$(TMP) not set!"))
rsync -e "ssh -o 'UserKnownHostsFile /dev/null' -o StrictHostKeyChecking=no" -av $(OUTPUT_DIR)/$*/target/ root@$($(TMP)):/
%-tail: output-dir-%
@tail -F $(OUTPUT_DIR)/$*/build/build-time.log
%-snapshot: %-supported
$(if $(shell which btrfs 2>/dev/null),, $(error "btrfs not found!"))
@mkdir -p $(OUTPUT_DIR)/snapshots
-@sudo btrfs sub del $(OUTPUT_DIR)/snapshots/$*-toolchain
@btrfs subvolume snapshot -r $(OUTPUT_DIR)/$* $(OUTPUT_DIR)/snapshots/$*-toolchain
%-rollback: %-supported
$(if $(shell which btrfs 2>/dev/null),, $(error "btrfs not found!"))
-@sudo btrfs sub del $(OUTPUT_DIR)/$*
@btrfs subvolume snapshot $(OUTPUT_DIR)/snapshots/$*-toolchain $(OUTPUT_DIR)/$*
%-flash: %-supported
$(if $(DEV),,$(error "DEV not specified!"))
@gzip -dc $(OUTPUT_DIR)/$*/images/batocera/images/$*/batocera-*.img.gz | sudo dd of=$(DEV) bs=5M status=progress
@sync
%-upgrade: %-supported
$(if $(DEV),,$(error "DEV not specified!"))
-@sudo umount /tmp/mount
-@mkdir -p /tmp/mount
@sudo mount $(DEV)1 /tmp/mount
@lsblk
@ls /tmp/mount
@echo "continue BATOCERA upgrade $(DEV)1 with $* build? [y/N]"
@read line; if [ "$$line" != "y" ]; then echo aborting; exit 1 ; fi
-@sudo rm /tmp/mount/boot/batocera
@sudo tar xvf $(OUTPUT_DIR)/$*/images/batocera/images/$*/boot.tar.xz -C /tmp/mount --no-same-owner --exclude=batocera-boot.conf --exclude=config.txt
@sudo umount /tmp/mount
-@rmdir /tmp/mount
@sudo fatlabel $(DEV)1 BATOCERA
%-toolchain: %-supported
$(if $(shell which btrfs 2>/dev/null),, $(error "btrfs not found!"))
-@sudo btrfs sub del $(OUTPUT_DIR)/$*
@btrfs subvolume create $(OUTPUT_DIR)/$*
@$(MAKE) $*-config
@$(MAKE) $*-build CMD=toolchain
@$(MAKE) $*-build CMD=llvm
@$(MAKE) $*-snapshot
%-find-build-dups: %-supported
@$(FIND) $(OUTPUT_DIR)/$*/build -maxdepth 1 -type d -printf '%T@ %p %f\n' | sed -r 's:\-[0-9a-f\.]+$$::' | sort -k3 -k1 | uniq -f 2 -d | cut -d' ' -f2
%-remove-build-dups: %-supported
@while [ -n "`$(FIND) $(OUTPUT_DIR)/$*/build -maxdepth 1 -type d -printf '%T@ %p %f\n' | sed -r 's:\-[0-9a-f\.]+$$::' | sort -k3 -k1 | uniq -f 2 -d | cut -d' ' -f2 | grep .`" ]; do \
$(FIND) $(OUTPUT_DIR)/$*/build -maxdepth 1 -type d -printf '%T@ %p %f\n' | sed -r 's:\-[0-9a-f\.]+$$::' | sort -k3 -k1 | uniq -f 2 -d | cut -d' ' -f2 | xargs rm -rf ; \
done
find-dl-dups:
@$(FIND) $(DL_DIR)/ -maxdepth 2 -type f -name "*.zip" -o -name "*.tar.*" -print0 -printf '%T@ %p %f\n' | sed -r 's:\-[-_0-9a-fvrgit\.]+(\.zip|\.tar\.[2a-z]+)$$::' | sort -k3 -k1 | uniq -f 2 -d | cut -d' ' -f2
remove-dl-dups:
@while [ -n "`$(FIND) $(DL_DIR)/ -maxdepth 2 -type f -name "*.zip" -o -name "*.tar.*" -print0 -printf '%T@ %p %f\n' | sed -r 's:\-[-_0-9a-fvrgit\.]+(\.zip|\.tar\.[2a-z]+)$$::' | sort -k3 -k1 | uniq -f 2 -d | cut -d' ' -f2 | grep .`" ] ; do \
$(FIND) $(DL_DIR) -maxdepth 2 -type f -name "*.zip" -o -name "*.tar.*" -print0 -printf '%T@ %p %f\n' | sed -r 's:\-[-_0-9a-fvrgit\.]+(\.zip|\.tar\.[2a-z]+)$$::' | sort -k3 -k1 | uniq -f 2 -d | cut -d' ' -f2 | xargs rm -rf ; \
done
uart:
$(if $(shell which picocom 2>/dev/null),, $(error "picocom not found!"))
$(if $(SERIAL_DEV),,$(error "SERIAL_DEV not specified!"))
$(if $(SERIAL_BAUDRATE),,$(error "SERIAL_BAUDRATE not specified!"))
$(if $(wildcard $(SERIAL_DEV)),,$(error "$(SERIAL_DEV) not available!"))
@picocom $(SERIAL_DEV) -b $(SERIAL_BAUDRATE)