bugfix
[dpa-image-builder.git] / chroot-build-helper / real-makefile
1 include helper.mk
2 include $(GENERATED_DEPENDENCIES_FILE)
3
4 UEXEC = $(project_root)/build/bin/uexec
5
6 all: $(patsubst %,bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build,$(PACKAGES_TO_BUILD))
7 repo: $(addprefix repo//,$(PACKAGES_TO_BUILD))
8
9 build//%: DONT_BUILD_IF_IN_REPO=no
10 build//%: bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build
11         @true
12
13 $(UEXEC):
14         make -C .. build/bin/usernsexec
15
16 $(DEBOOTSTRAP_SCRIPT):
17         $(MAKE) -C .. "$(DEBOOTSTRAP_SCRIPT)"
18
19 build-env/$(DISTRO)/$(RELEASE)/rootfs: build-env/.dir | $(DEBOOTSTRAP_SCRIPT) $(UEXEC)
20         set -ex; \
21         mkdir -p "$@.tmp"; \
22         exec 8>"$(dir $@).lock"; \
23         flock 8; \
24         if [ -d "$@" ]; then exit 0; fi; \
25         uexec rm -rf "$@.tmp"; \
26         debootstrap-base.sh "$@.tmp"; \
27         CHROOT_REPO="$$REPO" getrfsfile.sh "rootfs/etc/apt/sources.list" >"$@.tmp/etc/apt/sources.list"; \
28         chns "$@.tmp" apt-get -y update; \
29         if [ -n "$(BUILD_DEP_RELEASE)" ]; \
30           then chns "$@.tmp" apt-get -y -t "$(BUILD_DEP_RELEASE)" install --no-install-recommends build-essential dh-make debhelper devscripts meson wget gnupg2 gawk $(EXTRA_BUILD_TOOLS); \
31           else chns "$@.tmp" apt-get -y install --no-install-recommends build-essential dh-make debhelper devscripts meson wget gnupg2 gawk $(EXTRA_BUILD_TOOLS); \
32         fi; \
33         if [ -f "$@" ]; then mv "$@" "$@.old"; fi; \
34         mv "$@.tmp" "$@"; \
35         uexec rm -rf "$@.old";
36
37 repo-add//%:
38         set -x; \
39         cd "bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/$(patsubst repo-add//%,%,$@)/"; \
40         name="$$(printf '%s' *.changes)"; \
41         addrepo.sh "$$name"
42
43 clean-repo: $(addprefix clean-repo//,$(PACKAGES_TO_BUILD))
44 update-repo: $(addprefix update-repo//,$(PACKAGES_TO_BUILD))
45
46 buildenv: build-env/$(DISTRO)/$(RELEASE)/rootfs
47
48 clean-buildenv: | $(UEXEC)
49         uexec rm -rf "build-env/$(DISTRO)/$(RELEASE)"
50         rmdir "build-env/$(DISTRO)/" || true
51         rm -f "build-env/.dir"
52         rmdir "build-env/" || true
53
54 clean-bin:
55         rm -rf "bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)"
56         rmdir bin/$(BUILDER_PLATFORM)/$(DISTRO)/ || true
57         rmdir bin/$(BUILDER_PLATFORM)/ || true
58         rmdir bin || true
59
60 clean-build: clean-buildenv clean-bin
61
62 clean-build-all:
63         uexec rm -rf "build-env/" "bin/"
64
65 .SECONDEXPANSION:
66 bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build: $$(call repodir,%) build-env/$(DISTRO)/$(RELEASE)/rootfs | $(UEXEC) bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.dir
67         DO_LOCK=2 chns "build-env/$(DISTRO)/$(RELEASE)/rootfs/" sh -ex -c "apt-get -y update && apt-get -y dist-upgrade" || true; \
68         # If repo is needed & packages to be built for it, make sure it's created if necessary
69         if [ "$(USE_IMAGE_BUILDER_REPO)" = yes ] && [ -n "$(IMAGE_BUILDER_REPO)" ] && [ -n "$(IMAGE_BUILDER_REPO_KEY)" ] && [ "$(ADD_TO_EXTERNAL_REPO)" = "yes" ]; then addrepo.sh; fi
70         project="$(patsubst bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build,%,$@)" \
71         branch="$(repo-branch@$(patsubst bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build,%,$@))" \
72         repodir="$(call repodir,$(patsubst bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build,%,$@))" \
73         CHNS_OVERLAY=1 \
74         CHNS_EXTRA="( \
75           set -x; \
76           mkdir -p tmp/repo/pool/ build/; \
77           mount -o rbind,rprivate \"\$$project_root/chroot-build-helper/bin/\$$BUILDER_PLATFORM/\$$DISTRO/\$$RELEASE/\" tmp/repo/pool/; \
78           git clone --shared -n \"$$project_root/\$$repodir\" build/src; \
79           ( cd build/src; git checkout \"\$$branch\"; ); \
80         ); \
81         export commit=\"\$$(cd build/src/ && git rev-parse HEAD)\$$([ -z \"\$$(cd build/src/ && git status --porcelain)\" ] || echo -dirty)\"; \
82         export version=\"\$$(cd build/src/ && dpkg-parsechangelog --show-field Version | grep -o '^[^+~-]*' | tr -d '\n')+ib~c\$$(cd build/src/ && git rev-list HEAD --count)-\$$commit\"; \
83         " \
84         DO_LOCK=3 chns "build-env/$(DISTRO)/$(RELEASE)/rootfs/" sh -ex -c "\
85           if [ \"\$$USE_IMAGE_BUILDER_REPO\" = yes ] && [ \"\$$DONT_BUILD_IF_IN_REPO\" = yes ] && [ -n \"\$$IMAGE_BUILDER_REPO\" ] && [ -n \"\$$IMAGE_BUILDER_REPO_KEY\" ]; \
86           then \
87             wget -O- \"\$$IMAGE_BUILDER_REPO_KEY\" | apt-key add -; \
88             printf '%s' \"\$$IMAGE_BUILDER_REPO\" >/etc/apt/sources.list.d/dpa-image-builder.list; \
89           fi; \
90           ( \
91             cd /tmp/repo/; \
92             dpkg-scanpackages -m pool > Packages; \
93             gzip -k Packages; \
94             xz -k Packages; \
95           ); \
96           echo \"deb [trusted=yes] file:///tmp/repo/ ./\" >\"/etc/apt/sources.list.d/devrepo.list\"; \
97           apt-get -y update && apt-get -y dist-upgrade; \
98           cd /build/src/; \
99           if [ \"\$$DONT_BUILD_IF_IN_REPO\" = yes ] \
100             && ( \
101               for package in \$$(gawk 'BEGIN{RS=\"\\n\\n\";FS=\"\\n\"}{ if (\$$0 ~ /\\nArchitecture:.* (arm64|any|all).*\\n/ && \$$1 ~ /Package:/) { match(\$$1, /Package: *([^ ]*) */, package); print package[1] } }' <debian/control); \
102                 do if apt-cache show \"\$$package=\$$version\" 2>&1 | grep -q '^Package: '; then exit 0; fi; \
103               done; \
104               exit 1; \
105             ); \
106             then echo 'Not building package, this version is already in the repo, and DONT_BUILD_IF_IN_REPO is set to yes'; exit 0; \
107           fi; \
108           dch -b -D \"\$$DISTRO-\$$RELEASE\" --force-distribution -v \"\$$version\" \"Version suffix automatically added by dpa-image-builder. Git commit \$$commit.\"; \
109           apt-get -y build-dep .; \
110           if [ \"$(DO_BUILD_SOURCE_PACKAGES)\" = yes ]; \
111             then debuild -us -uc; \
112             else debuild -b -us -uc; \
113           fi; \
114           mkdir -p \"/tmp/repo/pool/\$$project/\"; \
115           rm -f \"/tmp/repo/pool/\$$project/\"*; \
116           find .. -maxdepth 1 -type f -exec cp {} \"/tmp/repo/pool/\$$project/\" ';' -print; \
117         "
118         if [ "$(ADD_TO_EXTERNAL_REPO)" = "yes" ]; \
119           then make -f real-makefile "repo-add//$(patsubst bin/$(BUILDER_PLATFORM)/$(DISTRO)/$(RELEASE)/%/.build,%,$@)" || true; \
120         fi
121         touch "$@"