1PROJECT := cpu_features 2BRANCH := $(shell git rev-parse --abbrev-ref HEAD) 3SHA1 := $(shell git rev-parse --verify HEAD) 4 5# General commands 6.PHONY: help 7BOLD=\e[1m 8RESET=\e[0m 9 10help: 11 @echo -e "${BOLD}SYNOPSIS${RESET}" 12 @echo -e "\tmake <target> [NOCACHE=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 "\t${BOLD}amd64_<stage>${RESET}: build <stage> docker image using an Ubuntu:latest x86_64 base image." 21 @echo -e "\t${BOLD}save_amd64_<stage>${RESET}: Save the <stage> docker image." 22 @echo -e "\t${BOLD}sh_amd64_<stage>${RESET}: run a container using the <stage> docker image (debug purpose)." 23 @echo -e "\t${BOLD}clean_amd64_<stage>${RESET}: Remove cache and docker image." 24 @echo 25 @echo -e "\tWith ${BOLD}<stage>${RESET}:" 26 @echo -e "\t\t${BOLD}env${RESET}" 27 @echo -e "\t\t${BOLD}devel${RESET}" 28 @echo -e "\t\t${BOLD}build${RESET}" 29 @echo -e "\t\t${BOLD}test${RESET}" 30 @echo -e "\t\t${BOLD}install_env${RESET}" 31 @echo -e "\t\t${BOLD}install_devel${RESET}" 32 @echo -e "\t\t${BOLD}install_build${RESET}" 33 @echo -e "\t\t${BOLD}install_test${RESET}" 34 @echo -e "\te.g. 'make amd64_build'" 35 @echo 36 @echo -e "\t${BOLD}<target>_<toolchain_stage>${RESET}: build <stage> docker image for a specific toolchain target." 37 @echo -e "\t${BOLD}save_<target>_<toolchain_stage>${RESET}: Save the <stage> docker image for a specific platform." 38 @echo -e "\t${BOLD}sh_<target>_<toolchain_stage>${RESET}: run a container using the <stage> docker image specified (debug purpose)." 39 @echo -e "\t${BOLD}clean_<target>_<toolchain_stage>${RESET}: Remove cache and docker image." 40 @echo 41 @echo -e "\tWith ${BOLD}<target>${RESET}:" 42 @echo -e "\t\t${BOLD}arm-linux-gnueabihf${RESET} (linaro toolchain)" 43 @echo -e "\t\t${BOLD}armv8l-linux-gnueabihf${RESET} (linaro toolchain)" 44 @echo -e "\t\t${BOLD}arm-linux-gnueabi${RESET} (linaro toolchain)" 45 @echo -e "\t\t${BOLD}armeb-linux-gnueabihf${RESET} (linaro toolchain)" 46 @echo -e "\t\t${BOLD}armeb-linux-gnueabi${RESET} (linaro toolchain)" 47 @echo -e "\t\t${BOLD}aarch64-linux-gnu${RESET} (linaro toolchain)" 48 @echo -e "\t\t${BOLD}aarch64${RESET} (bootlin toolchain)" 49 @echo -e "\t\t${BOLD}aarch64_be-linux-gnu${RESET} (linaro toolchain)" 50 @echo -e "\t\t${BOLD}aarch64be${RESET} (bootlin toolchain)" 51 @echo -e "\t\t${BOLD}mips32${RESET} (codespace toolchain)" 52 @echo -e "\t\t${BOLD}mips64${RESET} (codespace toolchain)" 53 @echo -e "\t\t${BOLD}mips32el${RESET} (codespace toolchain)" 54 @echo -e "\t\t${BOLD}mips64el${RESET} (codespace toolchain)" 55 @echo -e "\t\t${BOLD}ppc${RESET} (bootlin toolchain)" 56 @echo -e "\t\t${BOLD}ppc64${RESET} (bootlin toolchain)" 57 @echo -e "\t\t${BOLD}ppc64le${RESET} (bootlin toolchain)" 58 @echo -e "\t\t${BOLD}riscv32${RESET} (bootlin toolchain)" 59 @echo -e "\t\t${BOLD}riscv64${RESET} (bootlin toolchain)" 60 @echo -e "\t\t${BOLD}s390x${RESET} (bootlin toolchain)" 61 @echo 62 @echo -e "\tWith ${BOLD}<toolchain_stage>${RESET}:" 63 @echo -e "\t\t${BOLD}env${RESET}" 64 @echo -e "\t\t${BOLD}devel${RESET}" 65 @echo -e "\t\t${BOLD}build${RESET}" 66 @echo -e "\t\t${BOLD}test${RESET}" 67 @echo -e "\te.g. 'make aarch64_test'" 68 @echo 69 @echo -e "\t${BOLD}<VM>${RESET}: build the vagrant <VM> virtual machine." 70 @echo -e "\t${BOLD}clean_<VM>${RESET}: Remove virtual machine for the specified vm." 71 @echo 72 @echo -e "\t${BOLD}<VM>${RESET}:" 73 @echo -e "\t\t${BOLD}freebsd${RESET} (FreeBSD)" 74 @echo 75 @echo -e "\t${BOLD}clean${RESET}: Remove cache and ALL docker images." 76 @echo 77 @echo -e "\t${BOLD}NOCACHE=1${RESET}: use 'docker build --no-cache' when building container (default use cache)." 78 @echo 79 @echo -e "branch: $(BRANCH)" 80 @echo -e "sha1: $(SHA1)" 81 82# Need to add cmd_platform to PHONY otherwise target are ignored since they do not 83# contain recipe (using FORCE do not work here) 84.PHONY: all 85all: build 86 87# Delete all implicit rules to speed up makefile 88MAKEFLAGS += --no-builtin-rules 89.SUFFIXES: 90# Remove some rules from gmake that .SUFFIXES does not remove. 91SUFFIXES = 92# Keep all intermediate files 93# ToDo: try to remove it later 94.SECONDARY: 95 96# Docker image name prefix. 97IMAGE := ${PROJECT} 98 99ifdef NOCACHE 100DOCKER_BUILD_CMD := docker build --no-cache 101else 102DOCKER_BUILD_CMD := docker build 103endif 104 105DOCKER_RUN_CMD := docker run --rm --init --net=host 106 107# $* stem 108# $< first prerequist 109# $@ target name 110 111############ 112## NATIVE ## 113############ 114STAGES = env devel build test install_env install_devel install_build install_test 115 116targets_amd64 = $(addprefix amd64_, $(STAGES)) 117.PHONY: $(targets_amd64) 118$(targets_amd64): amd64_%: docker/amd64/Dockerfile 119 #@docker image rm -f ${IMAGE}:amd64_$* 2>/dev/null 120 ${DOCKER_BUILD_CMD} \ 121 --tag ${IMAGE}:amd64_$* \ 122 --target=$* \ 123 -f $< \ 124 ../.. 125 126#$(info Create targets: save_amd64 $(addprefix save_amd64_, $(STAGES)) (debug).) 127save_targets_amd64 = $(addprefix save_amd64_, $(STAGES)) 128.PHONY: $(save_targets_amd64) 129$(save_targets_amd64): save_amd64_%: cache/amd64/docker_%.tar 130cache/amd64/docker_%.tar: amd64_% 131 @rm -f $@ 132 mkdir -p cache/amd64 133 docker save ${IMAGE}:amd64_$* -o $@ 134 135#$(info Create targets: $(addprefix sh_amd64_, $(STAGES)) (debug).) 136sh_targets_amd64 = $(addprefix sh_amd64_, $(STAGES)) 137.PHONY: $(sh_targets_amd64) 138$(sh_targets_amd64): sh_amd64_%: amd64_% 139 ${DOCKER_RUN_CMD} -it --name ${IMAGE}_amd64_$* ${IMAGE}:amd64_$* 140 141#$(info Create targets: $(addprefix clean_amd64_, $(STAGES)).) 142clean_targets_amd64 = $(addprefix clean_amd64_, $(STAGES)) 143.PHONY: clean_amd64 $(clean_targets_amd64) 144clean_amd64: $(clean_targets_amd64) 145$(clean_targets_amd64): clean_amd64_%: 146 docker image rm -f ${IMAGE}:amd64_$* 2>/dev/null 147 rm -f cache/amd64/docker_$*.tar 148 149 150############### 151## TOOLCHAIN ## 152############### 153TOOLCHAIN_TARGETS = \ 154 aarch64 aarch64be \ 155 arm-linux-gnueabihf armv8l-linux-gnueabihf arm-linux-gnueabi armeb-linux-gnueabihf armeb-linux-gnueabi \ 156 aarch64-linux-gnu aarch64_be-linux-gnu \ 157 mips32 mips32el mips64 mips64el \ 158 ppc ppc64 ppc64le \ 159 riscv32 riscv64 \ 160 s390x 161TOOLCHAIN_STAGES = env devel build test 162define toolchain-stage-target = 163#$$(info STAGE: $1) 164#$$(info Create targets: toolchain_$1 $(addsuffix _$1, $(TOOLCHAIN_TARGETS)).) 165targets_toolchain_$1 = $(addsuffix _$1, $(TOOLCHAIN_TARGETS)) 166.PHONY: toolchain_$1 $$(targets_toolchain_$1) 167toolchain_$1: $$(targets_toolchain_$1) 168$$(targets_toolchain_$1): %_$1: docker/toolchain/Dockerfile 169 #@docker image rm -f ${IMAGE}:$$*_$1 2>/dev/null 170 ${DOCKER_BUILD_CMD} \ 171 --tag ${IMAGE}:$$*_$1 \ 172 --build-arg TARGET=$$* \ 173 --target=$1 \ 174 -f $$< \ 175 ../.. 176 177#$$(info Create targets: save_toolchain_$1 $(addprefix save_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))) (debug).) 178save_targets_toolchain_$1 = $(addprefix save_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))) 179.PHONY: save_toolchain_$1 $$(save_targets_toolchain_$1) 180save_toolchain_$1: $$(save_targets_toolchain_$1) 181$$(save_targets_toolchain_$1): save_%_$1: cache/%/docker_$1.tar 182cache/%/docker_$1.tar: %_$1 183 @rm -f $$@ 184 mkdir -p cache/$$* 185 docker save ${IMAGE}:$$*_$1 -o $$@ 186 187#$$(info Create targets: $(addprefix sh_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))) (debug).) 188sh_targets_toolchain_$1 = $(addprefix sh_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))) 189.PHONY: $$(sh_targets_toolchain_$1) 190$$(sh_targets_toolchain_$1): sh_%_$1: %_$1 191 ${DOCKER_RUN_CMD} -it --name ${IMAGE}_$$*_$1 ${IMAGE}:$$*_$1 192 193#$$(info Create targets: clean_toolchain_$1 $(addprefix clean_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))).) 194clean_targets_toolchain_$1 = $(addprefix clean_, $(addsuffix _$1, $(TOOLCHAIN_TARGETS))) 195.PHONY: clean_toolchain_$1 $$(clean_targets_toolchain_$1) 196clean_toolchain_$1: $$(clean_targets_toolchain_$1) 197$$(clean_targets_toolchain_$1): clean_%_$1: 198 docker image rm -f ${IMAGE}:$$*_$1 2>/dev/null 199 rm -f cache/$$*/docker_$1.tar 200endef 201 202$(foreach stage,$(TOOLCHAIN_STAGES),$(eval $(call toolchain-stage-target,$(stage)))) 203 204## MERGE ## 205.PHONY: clean_toolchain 206clean_toolchain: $(addprefix clean_toolchain_, $(TOOLCHAIN_STAGES)) 207 -rmdir $(addprefix cache/, $(TOOLCHAIN_TARGETS)) 208 209.PHONY: env devel build test 210env: amd64_env toolchain_env 211devel: amd64_devel toolchain_devel 212build: amd64_build toolchain_build 213test: amd64_test toolchain_test 214 215.PHONY: install_env install_devel install_build install_test 216install_env: amd64_install_env 217install_devel: amd64_install_devel 218install_build: amd64_install_build 219install_test: amd64_install_test 220 221############# 222## VAGRANT ## 223############# 224VMS = freebsd 225 226vms_targets = $(addsuffix _build, $(VMS)) 227.PHONY: $(vms_targets) 228$(vms_targets): %_build: vagrant/%/Vagrantfile 229 @cd vagrant/$* && vagrant destroy -f 230 cd vagrant/$* && vagrant up 231 232clean_vms_targets = $(addprefix clean_, $(VMS)) 233.PHONY: clean_vms $(clean_vms_targets) 234clean_vms: $(clean_vms_targets) 235$(clean_vms_targets): clean_%: 236 cd vagrant/$* && vagrant destroy -f 237 -rm -rf vagrant/$*/.vagrant 238 239########### 240## CLEAN ## 241########### 242.PHONY: clean 243clean: clean_amd64 clean_toolchain clean_vms 244 docker container prune -f 245 docker image prune -f 246 -rmdir cache 247 248.PHONY: distclean 249distclean: clean 250 -docker container rm -f $$(docker container ls -aq) 251 -docker image rm -f $$(docker image ls -aq) 252 -vagrant box remove -f generic/freebsd12 253