|
| 1 | +PROJECT := cgl |
| 2 | +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) |
| 3 | +SHA1 := $(shell git rev-parse --verify HEAD) |
| 4 | + |
| 5 | +# General commands |
| 6 | +.PHONY: help |
| 7 | +BOLD:=\e[1m |
| 8 | +RESET:=\e[0m |
| 9 | + |
| 10 | +help: |
| 11 | + @echo -e "${BOLD}SYNOPSIS${RESET}" |
| 12 | + @echo -e "\tmake <target> [NOCACHE=1] [VERBOSE=1]" |
| 13 | + @echo |
| 14 | + @echo -e "${BOLD}DESCRIPTION${RESET}" |
| 15 | + @echo -e "\ttest build inside docker container to have a reproductible build." |
| 16 | + @echo |
| 17 | + @echo -e "${BOLD}MAKE TARGETS${RESET}" |
| 18 | + @echo -e "\t${BOLD}help${RESET}: display this help and exit." |
| 19 | + @echo |
| 20 | + @echo -e "\tBuild using docker and the host platform." |
| 21 | + @echo -e "\t${BOLD}<distro>_<stage>${RESET}: build a <stage> docker image for a specific distro." |
| 22 | + @echo -e "\t${BOLD}save_<distro>_<stage>${RESET}: Save a <stage> docker image for a specific distro." |
| 23 | + @echo -e "\t${BOLD}sh_<distro>_<stage>${RESET}: run a container using the <stage> docker image specified (debug purpose)." |
| 24 | + @echo -e "\t${BOLD}clean_<distro>_<stage>${RESET}: Remove a <stage> docker image for a specific distro." |
| 25 | + @echo -e "\t${BOLD}clean_native${RESET}: Remove ALL caches and docker images." |
| 26 | + @echo |
| 27 | + @echo -e "\tWith ${BOLD}<distro>${RESET}:" |
| 28 | + @echo -e "\t\t${BOLD}almalinux${RESET} (latest)" |
| 29 | + @echo -e "\t\t${BOLD}alpine${RESET} (edge)" |
| 30 | + @echo -e "\t\t${BOLD}archlinux${RESET} (latest)" |
| 31 | + @echo -e "\t\t${BOLD}debian${RESET} (latest)" |
| 32 | + @echo -e "\t\t${BOLD}fedora${RESET} (latest)" |
| 33 | + @echo -e "\t\t${BOLD}opensuse${RESET} (tumbleweed)" |
| 34 | + @echo -e "\t\t${BOLD}rockylinux${RESET} (9)" |
| 35 | + @echo -e "\t\t${BOLD}ubuntu${RESET} (latest)" |
| 36 | + @echo -e "\t\t${BOLD}all${RESET}: trigger ALL DISTROS." |
| 37 | + @echo |
| 38 | + @echo -e "\tWith ${BOLD}<stage>${RESET}:" |
| 39 | + @echo -e "\t\t${BOLD}env${RESET}" |
| 40 | + @echo -e "\t\t${BOLD}devel${RESET}" |
| 41 | + @echo -e "\t\t${BOLD}build${RESET}" |
| 42 | + @echo -e "\t\t${BOLD}test${RESET}" |
| 43 | + @echo -e "\t\t${BOLD}install_env${RESET}" |
| 44 | + @echo -e "\t\t${BOLD}install_devel${RESET}" |
| 45 | + @echo -e "\t\t${BOLD}install_build${RESET}" |
| 46 | + @echo -e "\t\t${BOLD}install_test${RESET}" |
| 47 | + @echo -e "\te.g. 'make ubuntu_test'" |
| 48 | + @echo |
| 49 | + @echo -e "\tBuild using docker buildx with a platform specified." |
| 50 | + @echo -e "\t${BOLD}<platform>_<stage>${RESET}: build <stage> docker images for ALL DISTROS." |
| 51 | + @echo -e "\t${BOLD}<platform>_<distro>_<stage>${RESET}: build <stage> docker image for a specific distro." |
| 52 | + @echo -e "\t${BOLD}save_<platform>_<stage>${RESET}: Save <stage> docker images for ALL DISTROS." |
| 53 | + @echo -e "\t${BOLD}save_<platform>_<distro>_<stage>${RESET}: Save the <stage> docker image for a specific distro." |
| 54 | + @echo -e "\t${BOLD}sh_<platform>_<distro>_<stage>${RESET}: run a container using the <stage> docker image specified (debug purpose)." |
| 55 | + @echo -e "\t${BOLD}clean_<platform>_<distro>_<stage>${RESET}: Remove cache and docker image." |
| 56 | + @echo -e "\t${BOLD}clean_platforms${RESET}: Remove ALL cache and docker image." |
| 57 | + @echo |
| 58 | + @echo -e "\tWith ${BOLD}<platform>${RESET}:" |
| 59 | + @echo -e "\t\t${BOLD}amd64${RESET}: linux/amd64 (x86_64)" |
| 60 | + @echo -e "\t\t${BOLD}386${RESET}: linux/386 (x86)" |
| 61 | + @echo -e "\t\t${BOLD}arm${RESET}: linux/arm (armv7)" |
| 62 | + @echo -e "\t\t${BOLD}arm64${RESET}: linux/arm64 (aarch64, arm64v8)" |
| 63 | + @echo -e "\t\t${BOLD}mips${RESET}: linux/mips (mips 32bits)" |
| 64 | + @echo -e "\t\t${BOLD}mipsle${RESET}: linux/mipsle (mips 32bits Little Endian)" |
| 65 | + @echo -e "\t\t${BOLD}mips64${RESET}: linux/mips64 (mips 64bits)" |
| 66 | + @echo -e "\t\t${BOLD}mips64le${RESET}: linux/mips64le (mips 64bits Little Endian)" |
| 67 | + @echo -e "\t\t${BOLD}ppc64${RESET}: linux/ppc64 (PowerPC 64Bits)" |
| 68 | + @echo -e "\t\t${BOLD}ppc64le${RESET}: linux/ppc64le (PowerPC 64Bits Little Endian)" |
| 69 | + @echo -e "\t\t${BOLD}riscv64${RESET}: linux/riscv64 (RISC-V 64bits)" |
| 70 | + @echo -e "\t\t${BOLD}s390x${RESET}: linux/s390x (IBM System/390x)" |
| 71 | + @echo -e "\te.g. 'make amd64_ubuntu_test'" |
| 72 | + @echo -e "\tDocker image unavailable: arm64_archlinux" |
| 73 | + @echo |
| 74 | + @echo -e "\tGlobal targets." |
| 75 | + @echo -e "\t${BOLD}clean${RESET}: Remove ALL caches and docker images." |
| 76 | + @echo -e "\t${BOLD}distclean${RESET}: Remove everything." |
| 77 | + @echo |
| 78 | + @echo -e "\t${BOLD}NOCACHE=1${RESET}: use 'docker build --no-cache' when building container (default use cache)." |
| 79 | + @echo -e "\t${BOLD}VERBOSE=1${RESET}: use 'docker build --progress=plain' when building container." |
| 80 | + @echo |
| 81 | + @echo -e "branch: $(BRANCH)" |
| 82 | + @echo -e "sha1: $(SHA1)" |
| 83 | + |
| 84 | +# Need to add cmd_distro to PHONY otherwise target are ignored since they do not |
| 85 | +# contain recipe (using FORCE do not work here) |
| 86 | +.PHONY: all |
| 87 | +all: all_test |
| 88 | + |
| 89 | +# Delete all implicit rules to speed up makefile |
| 90 | +MAKEFLAGS += --no-builtin-rules |
| 91 | +.SUFFIXES: |
| 92 | +# Remove some rules from gmake that .SUFFIXES does not remove. |
| 93 | +SUFFIXES := |
| 94 | +# Keep all intermediate files |
| 95 | +# ToDo: try to remove it later |
| 96 | +.SECONDARY: |
| 97 | + |
| 98 | +# Docker image name prefix. |
| 99 | +IMAGE := ${PROJECT} |
| 100 | + |
| 101 | +DOCKER_BUILD_CMD := docker build |
| 102 | +DOCKER_BUILDX_CMD := docker buildx build |
| 103 | +ifdef NOCACHE |
| 104 | +DOCKER_BUILD_CMD := ${DOCKER_BUILD_CMD} --no-cache |
| 105 | +DOCKER_BUILDX_CMD := ${DOCKER_BUILDX_CMD} --no-cache |
| 106 | +endif |
| 107 | +ifdef VERBOSE |
| 108 | +DOCKER_BUILD_CMD := ${DOCKER_BUILD_CMD} --progress=plain |
| 109 | +DOCKER_BUILDX_CMD := ${DOCKER_BUILDX_CMD} --progress=plain |
| 110 | +endif |
| 111 | +DOCKER_RUN_CMD := docker run --rm --init --net=host |
| 112 | + |
| 113 | +# Currently supported distro |
| 114 | +DISTROS := \ |
| 115 | + almalinux \ |
| 116 | + alpine \ |
| 117 | + archlinux \ |
| 118 | + debian \ |
| 119 | + fedora \ |
| 120 | + opensuse \ |
| 121 | + rockylinux \ |
| 122 | + ubuntu |
| 123 | + |
| 124 | +# $* stem |
| 125 | +# $< first prerequist |
| 126 | +# $@ target name |
| 127 | + |
| 128 | +############ |
| 129 | +## STAGES ## |
| 130 | +############ |
| 131 | +STAGES := env devel build test install_env install_devel install_build install_test |
| 132 | + |
| 133 | +define make-stage-target = |
| 134 | +#$$(info STAGE: $1) |
| 135 | +#$$(info Create targets: all_$1 $(addsuffix _$1, $(DISTROS)).) |
| 136 | +targets_$1 := $(addsuffix _$1, $(DISTROS)) |
| 137 | +.PHONY: all_$1 $$(targets_$1) |
| 138 | +all_$1: $$(targets_$1) |
| 139 | +$$(targets_$1): %_$1: docker/%/Dockerfile |
| 140 | + #@docker image rm -f ${IMAGE}:$$*_$1 2>/dev/null |
| 141 | + ${DOCKER_BUILD_CMD} --target=$1 --tag ${IMAGE}:$$*_$1 -f $$< .. |
| 142 | + |
| 143 | +#$$(info Create targets: save_all_$1 $(addprefix save_, $(addsuffix _$1, $(DISTROS))) (debug).) |
| 144 | +save_targets_$1 := $(addprefix save_, $(addsuffix _$1, $(DISTROS))) |
| 145 | +.PHONY: save_all_$1 $$(save_targets_$1) |
| 146 | +save_all_$1: $$(save_targets_$1) |
| 147 | +$$(save_targets_$1): save_%_$1: cache/%/docker_$1.tar |
| 148 | +cache/%/docker_$1.tar: %_$1 |
| 149 | + @rm -f $$@ |
| 150 | + mkdir -p cache/$$* |
| 151 | + docker save ${IMAGE}:$$*_$1 -o $$@ |
| 152 | + |
| 153 | +#$$(info Create targets: $(addprefix sh_, $(addsuffix _$1, $(DISTROS))) (debug).) |
| 154 | +sh_targets_$1 := $(addprefix sh_, $(addsuffix _$1, $(DISTROS))) |
| 155 | +.PHONY: $$(sh_targets_$1) |
| 156 | +$$(sh_targets_$1): sh_%_$1: %_$1 |
| 157 | + ${DOCKER_RUN_CMD} -it --name ${IMAGE}_$$*_$1 ${IMAGE}:$$*_$1 |
| 158 | + |
| 159 | +#$$(info Create targets: clean_all_$1 $(addprefix clean_, $(addsuffix _$1, $(DISTROS))).) |
| 160 | +clean_targets_$1 := $(addprefix clean_, $(addsuffix _$1, $(DISTROS))) |
| 161 | +.PHONY: clean_all_$1 $$(clean_targets_$1) |
| 162 | +clean_all_$1: $$(clean_targets_$1) |
| 163 | +$$(clean_targets_$1): clean_%_$1: |
| 164 | + docker image rm -f ${IMAGE}:$$*_$1 2>/dev/null |
| 165 | + rm -f cache/$$*/docker_$1.tar |
| 166 | +endef |
| 167 | + |
| 168 | +$(foreach stage,$(STAGES),$(eval $(call make-stage-target,$(stage)))) |
| 169 | + |
| 170 | +## MERGE ## |
| 171 | +.PHONY: clean_all |
| 172 | +clean_all: $(addprefix clean_all_, $(STAGES)) |
| 173 | + rm -f $(addprefix cache/, $(DISTROS)) |
| 174 | + |
| 175 | +############## |
| 176 | +## PLATFORM ## |
| 177 | +############## |
| 178 | +# ref: https://go.dev/doc/install/source#environment |
| 179 | +# ref: https://github.com/containerd/containerd/blob/269548fa27e0089a8b8278fc4fc781d7f65a939b/platforms/platforms.go#L80-L94 |
| 180 | +PLATFORMS := \ |
| 181 | + 386 amd64 \ |
| 182 | + arm arm64 \ |
| 183 | + mips mipsle mips64 mips64le \ |
| 184 | + ppc64 ppc64le \ |
| 185 | + riscv64 \ |
| 186 | + s390x |
| 187 | + |
| 188 | +define make-platform-stage-target = |
| 189 | +#$$(info PLATFORM: '$1' STAGE: '$2') |
| 190 | +#$$(info Create targets: $1_all_$2 $(addprefix $1_, $(addsuffix _$2, $(DISTROS))).) |
| 191 | +targets_$1_$2 := $(addprefix $1_, $(addsuffix _$2, $(DISTROS))) |
| 192 | +.PHONY: $1_all_$2 $$(targets_$1_$2) |
| 193 | +$1_all_$2: $$(targets_$1_$2) |
| 194 | +$$(targets_$1_$2): $1_%_$2: docker/%/Dockerfile |
| 195 | + #@docker image rm -f ${IMAGE}:$1_$$*_$2 2>/dev/null |
| 196 | + ${DOCKER_BUILDX_CMD} --platform linux/$1 --target=$2 --tag ${IMAGE}:$1_$$*_$2 -f $$< .. |
| 197 | + |
| 198 | +#$$(info Create save targets: save_$1_all_$2 $(addprefix save_$1_, $(addsuffix _$2, $(DISTROS))) (debug).) |
| 199 | +save_targets_$1_$2 := $(addprefix save_$1_, $(addsuffix _$2, $(DISTROS))) |
| 200 | +.PHONY: save_$1_all_$2 $$(save_targets_$1_$2) |
| 201 | +save_$1_all_$2: $$(save_targets_$1_$2) |
| 202 | +$$(save_targets_$1_$2): save_$1_%_$2: cache/$1/%/docker_$2.tar |
| 203 | +cache/$1/%/docker_$2.tar: $1_%_$2 |
| 204 | + @rm -f $$@ |
| 205 | + mkdir -p cache/$1/$$* |
| 206 | + docker save ${IMAGE}:$1_$$*_$2 -o $$@ |
| 207 | + |
| 208 | +#$$(info Create sh targets: $(addprefix sh_$1_, $(addsuffix _$2, $(DISTROS))) (debug).) |
| 209 | +sh_targets_$1_$2 := $(addprefix sh_$1_, $(addsuffix _$2, $(DISTROS))) |
| 210 | +.PHONY: $$(sh_targets_$1_$2) |
| 211 | +$$(sh_targets_$1_$2): sh_$1_%_$2: $1_%_$2 |
| 212 | + ${DOCKER_RUN_CMD} --platform linux/$1 -it --name ${IMAGE}_$1_$$*_$2 ${IMAGE}:$1_$$*_$2 |
| 213 | + |
| 214 | +#$$(info Create targets: clean_$1_all_$2 $(addprefix clean_$1_, $(addsuffix _$2, $(DISTROS))).) |
| 215 | +clean_targets_$1_$2 := $(addprefix clean_$1_, $(addsuffix _$2, $(DISTROS))) |
| 216 | +.PHONY: clean_$1_all_$2 $$(clean_targets_$1_$2) |
| 217 | +clean_$1_all_$2: $$(clean_targets_$1_$2) |
| 218 | +$$(clean_targets_$1_$2): clean_$1_%_$2: |
| 219 | + docker image rm -f ${IMAGE}:$1_$$*_$2 2>/dev/null |
| 220 | + rm -f cache/$1/$$*/docker_$2.tar |
| 221 | +endef |
| 222 | + |
| 223 | +define make-platform-target = |
| 224 | +#$$(info PLATFORM: $1) |
| 225 | +$(foreach stage,$(STAGES),$(eval $(call make-platform-stage-target,$1,$(stage)))) |
| 226 | + |
| 227 | +# merge |
| 228 | +.PHONY: clean_$1 |
| 229 | +clean_$1: $(addprefix clean_$1_all_, $(STAGES)) |
| 230 | + -rmdir $(addprefix cache/$1/, $(DISTROS)) |
| 231 | + -rmdir cache/$1 |
| 232 | +endef |
| 233 | + |
| 234 | +$(foreach platform,$(PLATFORMS),$(eval $(call make-platform-target,$(platform)))) |
| 235 | + |
| 236 | +## MERGE ## |
| 237 | +.PHONY: clean_platforms |
| 238 | +clean_platforms: $(addprefix clean_, $(PLATFORMS)) |
| 239 | + |
| 240 | +########### |
| 241 | +## CLEAN ## |
| 242 | +########### |
| 243 | +.PHONY: clean |
| 244 | +clean: clean_all clean_platforms |
| 245 | + docker container prune -f |
| 246 | + docker image prune -f |
| 247 | + -rmdir cache |
| 248 | + |
| 249 | +.PHONY: distclean |
| 250 | +distclean: clean |
| 251 | + -docker container rm -f $$(docker container ls -aq) |
| 252 | + -docker image rm -f $$(docker image ls -aq) |
0 commit comments