This repository has been archived on 2024-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
toolchain/Makefile

227 lines
6.4 KiB
Makefile
Raw Normal View History

2023-04-04 16:42:46 +00:00
lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
altarch = $(subst x86_64,amd64,$(subst aarch64,arm64,$1))
2023-02-03 05:30:11 +00:00
DEFAULT_GOAL := $(or $(DEFAULT_GOAL),toolchain)
ARCH := $(or $(ARCH),x86_64)
TARGET := $(or $(TARGET),$(ARCH))
2023-04-04 16:42:46 +00:00
HOST_ARCH := $(call lc,$(shell uname -m))
HOST_ARCH_ALT := $(call altarch,$(HOST_ARCH))
HOST_OS := $(call lc,$(shell uname -s))
2023-02-06 20:54:17 +00:00
PLATFORM := $(or $(PLATFORM),linux)
2023-02-13 20:36:17 +00:00
NAME := $(shell basename $(shell git rev-parse --show-toplevel | tr A-Z a-z ))
2023-02-15 21:57:45 +00:00
IMAGE := local/$(NAME)
UID := $(shell id -u)
GID := $(shell id -g)
USER := $(UID):$(GID)
2023-01-28 00:05:03 +00:00
CPUS := $(shell docker run -it debian nproc)
GIT_REF := $(shell git log -1 --format=%H)
GIT_AUTHOR := $(shell git log -1 --format=%an)
GIT_KEY := $(shell git log -1 --format=%GP)
2023-02-13 05:36:05 +00:00
GIT_TIMESTAMP := $(shell git log -1 --format=%cd --date=iso)
, := ,
2023-01-28 00:05:03 +00:00
ifeq ($(strip $(shell git status --porcelain 2>/dev/null)),)
GIT_STATE=clean
else
GIT_STATE=dirty
endif
2023-02-11 00:37:22 +00:00
VERSION := $(shell TZ=UTC0 git show --quiet --date='format-local:%Y.%m.%d' --format="%cd")
DIST_DIR := dist
2023-01-28 00:05:03 +00:00
CONFIG_DIR := config
2023-02-03 05:30:11 +00:00
CACHE_DIR_ROOT := cache
FETCH_DIR := $(CACHE_DIR_ROOT)/fetch
ifeq ($(TARGET),$(ARCH))
CACHE_DIR := $(CACHE_DIR_ROOT)/$(TARGET)
else
CACHE_DIR := $(CACHE_DIR_ROOT)/$(TARGET)/$(ARCH)
endif
BIN_DIR := $(CACHE_DIR_ROOT)/bin
2023-01-28 00:05:03 +00:00
SRC_DIR := src
2023-02-03 05:30:11 +00:00
KEY_DIR := keys
2023-01-28 00:05:03 +00:00
OUT_DIR := out
2023-02-03 05:30:11 +00:00
export
2023-01-28 00:05:03 +00:00
docker = docker
include $(CONFIG_DIR)/global.env
export $(shell sed 's/=.*//' $(CONFIG_DIR)/global.env)
## Use env vars from existing release if present
ifneq (,$(wildcard $(DIST_DIR)/release.env))
include $(DIST_DIR)/release.env
2023-01-28 00:05:03 +00:00
export
endif
executables = $(docker) git patch
.PHONY: toolchain
2023-02-03 05:30:11 +00:00
toolchain: \
$(CACHE_DIR) \
$(FETCH_DIR) \
$(BIN_DIR) \
$(OUT_DIR) \
2023-02-15 21:57:45 +00:00
$(CACHE_DIR_ROOT)/toolchain.state \
2023-02-03 05:30:11 +00:00
$(CACHE_DIR_ROOT)/toolchain.env
2023-01-28 00:05:03 +00:00
# Launch a shell inside the toolchain container
.PHONY: toolchain-shell
toolchain-shell: toolchain
$(call toolchain,bash --norc)
2023-01-28 00:05:03 +00:00
# Pin all packages in toolchain container to latest versions
.PHONY: toolchain-update
toolchain-update:
docker run \
--rm \
--tty \
--interactive \
2023-01-28 00:05:03 +00:00
--platform=linux/$(ARCH) \
--env LOCAL_USER=$(UID):$(GID) \
2023-01-28 00:05:03 +00:00
--volume $(PWD)/$(CONFIG_DIR):/config \
--volume $(PWD)/$(SRC_DIR)/toolchain/scripts:/usr/local/bin \
--cpus $(CPUS) \
--volume $(PWD):/home/build \
--workdir /home/build \
2023-01-28 00:05:03 +00:00
debian@sha256:$(DEBIAN_HASH) \
/usr/local/bin/packages-update
2023-01-28 00:05:03 +00:00
.PHONY: toolchain-clean
toolchain-clean:
2023-04-04 21:36:28 +00:00
chmod -R u+w $(CACHE_DIR_ROOT)
2023-02-13 00:54:54 +00:00
rm -rf $(CACHE_DIR_ROOT) $(OUT_DIR)
docker image rm -f $(IMAGE)
2023-04-04 21:16:53 +00:00
.PHONY: reproduce
reproduce: toolchain-clean
mkdir -p $(OUT_DIR)
cp $(DIST_DIR)/release.env $(OUT_DIR)/release.env
2023-02-08 00:31:09 +00:00
$(MAKE) TARGET=$(TARGET) VERSION=$(VERSION)
diff -q $(OUT_DIR)/manifest.txt $(DIST_DIR)/manifest.txt;
2023-01-28 00:05:03 +00:00
2023-03-02 17:27:27 +00:00
.PHONY: $(DIST_DIR)
$(DIST_DIR):
2023-02-27 22:26:04 +00:00
rm -rf $@/*
2023-03-02 17:27:27 +00:00
$(MAKE) toolchain-clean default
2023-02-27 22:26:04 +00:00
cp -R $(OUT_DIR)/* $@/
2023-01-28 00:05:03 +00:00
2023-02-03 05:30:11 +00:00
$(BIN_DIR):
mkdir -p $@
2023-01-28 00:05:03 +00:00
$(CACHE_DIR):
mkdir -p $@
2023-02-03 05:30:11 +00:00
$(FETCH_DIR):
mkdir -p $@
2023-01-28 00:05:03 +00:00
$(OUT_DIR):
mkdir -p $@
2023-02-03 05:30:11 +00:00
$(CACHE_DIR_ROOT)/toolchain.env: \
$(CACHE_DIR) \
$(SRC_DIR)/toolchain/scripts/environment
$(SRC_DIR)/toolchain/scripts/environment > $@
$(CACHE_DIR_ROOT)/toolchain.tar: \
$(SRC_DIR)/toolchain/Dockerfile \
$(CONFIG_DIR)/toolchain/package-hashes-$(ARCH).txt \
$(CONFIG_DIR)/toolchain/packages-base.list \
$(CONFIG_DIR)/toolchain/packages-$(ARCH).list \
$(CONFIG_DIR)/toolchain/sources.list
2023-01-28 00:05:03 +00:00
mkdir -p $(CACHE_DIR)
DOCKER_BUILDKIT=1 \
docker build \
--tag $(IMAGE) \
--build-arg DEBIAN_HASH=$(DEBIAN_HASH) \
--build-arg CONFIG_DIR=$(CONFIG_DIR) \
--build-arg SCRIPTS_DIR=$(SRC_DIR)/toolchain/scripts \
--platform=linux/$(ARCH) \
-f $(SRC_DIR)/toolchain/Dockerfile \
.
docker save "$(IMAGE)" -o "$@"
2023-02-15 21:57:45 +00:00
$(CACHE_DIR_ROOT)/toolchain.state: \
$(CACHE_DIR_ROOT)/toolchain.tar
docker load -i $(CACHE_DIR_ROOT)/toolchain.tar
docker images --no-trunc --quiet $(IMAGE) > $@
2023-01-28 00:05:03 +00:00
$(OUT_DIR)/release.env: | $(OUT_DIR)
2023-02-13 05:36:05 +00:00
echo 'VERSION=$(VERSION)' > $(OUT_DIR)/release.env
echo 'GIT_REF=$(GIT_REF)' >> $(OUT_DIR)/release.env
echo 'GIT_AUTHOR=$(GIT_AUTHOR)' >> $(OUT_DIR)/release.env
echo 'GIT_KEY=$(GIT_KEY)' >> $(OUT_DIR)/release.env
echo 'GIT_TIMESTAMP=$(GIT_TIMESTAMP)' >> $(OUT_DIR)/release.env
2023-01-28 00:05:03 +00:00
2023-03-02 17:27:27 +00:00
$(OUT_DIR)/manifest.txt: $(wildcard $(OUT_DIR)/*)
find -L $(OUT_DIR) \
2023-01-28 00:05:03 +00:00
-type f \
-not -path "$(OUT_DIR)/manifest.txt" \
-exec openssl sha256 -r {} \; \
2023-02-24 21:30:28 +00:00
| sed -e 's/ \*out\// /g' -e 's/ \.\// /g' \
2023-01-28 00:05:03 +00:00
| LC_ALL=C sort -k2 \
> $@
check_executables := $(foreach exec,$(executables),\$(if \
$(shell which $(exec)),some string,$(error "No $(exec) in PATH")))
define git_clone
2023-02-17 05:15:10 +00:00
[ -d $(1) ] || \
mkdir -p $(FETCH_DIR) && \
mkdir $(1).tmp && \
git -C $(1).tmp init && \
git -C $(1).tmp remote add origin $(2) && \
git -C $(1).tmp fetch origin $(3) && \
git -C $(1).tmp -c advice.detachedHead=false checkout $(3) && \
git -C $(1).tmp rev-parse --verify HEAD | grep -q $(3) || { \
echo 'Error: Git ref/branch collision.'; exit 1; \
} && \
mv $(1).tmp $(1);
2023-01-28 00:05:03 +00:00
endef
define apply_patches
[ -d $(2) ] && $(call toolchain," \
2023-01-28 00:05:03 +00:00
cd $(1); \
git restore .; \
find /$(2) -type f -iname '*.patch' -print0 \
| xargs -t -0 -n 1 patch -p1 --no-backup-if-mismatch -i ; \
")
endef
2023-02-03 05:30:11 +00:00
define fetch_pgp_key
mkdir -p $(KEY_DIR) && \
2023-03-17 03:11:43 +00:00
$(call toolchain,' \
for server in \
keys.openpgp.org \
hkp://keyserver.ubuntu.com:80 \
hkp://p80.pool.sks-keyservers.net:80 \
ha.pool.sks-keyservers.net \
pgp.mit.edu \
; do \
echo "Trying: $${server}"; \
gpg \
--keyserver "$${server}" \
--keyserver-options timeout=10 \
--recv-keys "$(1)" \
&& break; \
done; \
gpg --export -a $(1) > $(KEY_DIR)/$(1).asc; \
')
2023-02-03 05:30:11 +00:00
endef
2023-01-28 00:05:03 +00:00
define toolchain
docker run \
--rm \
--tty \
--interactive \
--env UID=$(UID) \
--env GID=$(GID) \
2023-01-28 00:05:03 +00:00
--platform=linux/$(ARCH) \
--privileged \
2023-01-28 00:05:03 +00:00
--cpus $(CPUS) \
--volume $(PWD):/home/build \
--workdir /home/build \
--env-file=$(CONFIG_DIR)/global.env \
2023-02-03 05:30:11 +00:00
--env-file=$(CACHE_DIR_ROOT)/toolchain.env \
2023-02-15 21:57:45 +00:00
$(shell cat cache/toolchain.state) \
$(SRC_DIR)/toolchain/scripts/host-env bash -c $(1)
2023-01-28 00:05:03 +00:00
endef