|
| 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]" |
| 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}<stage>${RESET}: build <stage> docker images for ALL DISTROS." |
| 21 | + @echo -e "\t${BOLD}<distro>_<stage>${RESET}: build <stage> docker image for a specific distro." |
| 22 | + @echo -e "\t${BOLD}save_<stage>${RESET}: Save <stage> docker images for ALL DISTROS." |
| 23 | + @echo -e "\t${BOLD}save_<distro>_<stage>${RESET}: Save the <stage> docker image for a specific distro." |
| 24 | + @echo -e "\t${BOLD}sh_<distro>_<stage>${RESET}: run a container using the <stage> docker image specified (debug purpose)." |
| 25 | + @echo |
| 26 | + @echo -e "\tWith ${BOLD}<stage>${RESET}:" |
| 27 | + @echo -e "\t\t${BOLD}env${RESET}" |
| 28 | + @echo -e "\t\t${BOLD}devel${RESET}" |
| 29 | + @echo -e "\t\t${BOLD}build${RESET}" |
| 30 | + @echo -e "\t\t${BOLD}test${RESET}" |
| 31 | + @echo -e "\t\t${BOLD}install_env${RESET}" |
| 32 | + @echo -e "\t\t${BOLD}install_devel${RESET}" |
| 33 | + @echo -e "\t\t${BOLD}install_build${RESET}" |
| 34 | + @echo -e "\t\t${BOLD}install_test${RESET}" |
| 35 | + @echo -e "\te.g. 'make build'" |
| 36 | + @echo |
| 37 | + @echo -e "\tWith ${BOLD}<distro>${RESET}:" |
| 38 | + @echo -e "\t\t${BOLD}alpine${RESET} (edge)" |
| 39 | + @echo -e "\t\t${BOLD}archlinux${RESET} (latest)" |
| 40 | + @echo -e "\t\t${BOLD}centos${RESET} (latest)" |
| 41 | + @echo -e "\t\t${BOLD}debian${RESET} (latest)" |
| 42 | + @echo -e "\t\t${BOLD}fedora${RESET} (latest)" |
| 43 | + @echo -e "\t\t${BOLD}opensuse${RESET} (leap)" |
| 44 | + @echo -e "\t\t${BOLD}ubuntu${RESET} (latest)" |
| 45 | + @echo -e "\te.g. 'make ubuntu_test'" |
| 46 | + @echo |
| 47 | + @echo -e "\t${BOLD}clean${RESET}: Remove cache and ALL docker images." |
| 48 | + @echo -e "\t${BOLD}clean_<distro>${RESET}: Remove cache and docker images for the specified distro." |
| 49 | + @echo -e "\t${BOLD}clean_<vm>${RESET}: Remove virtual machine for the specified vm." |
| 50 | + @echo |
| 51 | + @echo -e "\t${BOLD}NOCACHE=1${RESET}: use 'docker build --no-cache' when building container (default use cache)." |
| 52 | + @echo |
| 53 | + @echo -e "branch: $(BRANCH)" |
| 54 | + @echo -e "sha1: $(SHA1)" |
| 55 | + |
| 56 | +# Need to add cmd_distro to PHONY otherwise target are ignored since they do not |
| 57 | +# contain recipe (using FORCE do not work here) |
| 58 | +.PHONY: all |
| 59 | +all: build |
| 60 | + |
| 61 | +# Delete all implicit rules to speed up makefile |
| 62 | +MAKEFLAGS += --no-builtin-rules |
| 63 | +.SUFFIXES: |
| 64 | +# Remove some rules from gmake that .SUFFIXES does not remove. |
| 65 | +SUFFIXES = |
| 66 | +# Keep all intermediate files |
| 67 | +# ToDo: try to remove it later |
| 68 | +.SECONDARY: |
| 69 | + |
| 70 | +# Docker image name prefix. |
| 71 | +IMAGE := ${PROJECT} |
| 72 | + |
| 73 | +ifdef NOCACHE |
| 74 | +DOCKER_BUILD_CMD := docker build --no-cache |
| 75 | +else |
| 76 | +DOCKER_BUILD_CMD := docker build |
| 77 | +endif |
| 78 | + |
| 79 | +DOCKER_RUN_CMD := docker run --rm --init --net=host |
| 80 | + |
| 81 | +# Currently supported distro |
| 82 | +DISTROS = alpine archlinux centos debian fedora opensuse ubuntu |
| 83 | + |
| 84 | +# $* stem |
| 85 | +# $< first prerequist |
| 86 | +# $@ target name |
| 87 | + |
| 88 | +############ |
| 89 | +## STAGES ## |
| 90 | +############ |
| 91 | +STAGES = env devel build test install_env install_devel install_build install_test |
| 92 | +define make-stage-target = |
| 93 | +#$$(info STAGE: $1) |
| 94 | +#$$(info Create targets: $1 $(addsuffix _$1, $(DISTROS)).) |
| 95 | +targets_$1 = $(addsuffix _$1, $(DISTROS)) |
| 96 | +.PHONY: $1 $$(targets_$1) |
| 97 | +$1: $$(targets_$1) |
| 98 | +$$(targets_$1): %_$1: docker/%/Dockerfile |
| 99 | + #@docker image rm -f ${IMAGE}:$$*_$1 2>/dev/null |
| 100 | + ${DOCKER_BUILD_CMD} --target=$1 --tag ${IMAGE}:$$*_$1 -f $$< .. |
| 101 | + |
| 102 | +#$$(info Create targets: save_$1 $(addprefix save_, $(addsuffix _$1, $(DISTROS))) (debug).) |
| 103 | +save_targets_$1 = $(addprefix save_, $(addsuffix _$1, $(DISTROS))) |
| 104 | +.PHONY: save_$1 $$(save_targets_$1) |
| 105 | +save_$1: $$(save_targets_$1) |
| 106 | +$$(save_targets_$1): save_%_$1: cache/%/docker_$1.tar |
| 107 | +cache/%/docker_$1.tar: %_$1 |
| 108 | + @rm -f $$@ |
| 109 | + mkdir -p cache/$$* |
| 110 | + docker save ${IMAGE}:$$*_$1 -o $$@ |
| 111 | + |
| 112 | +#$$(info Create targets: $(addprefix sh_, $(addsuffix _$1, $(DISTROS))) (debug).) |
| 113 | +sh_targets_$1 = $(addprefix sh_, $(addsuffix _$1, $(DISTROS))) |
| 114 | +.PHONY: $$(sh_targets_$1) |
| 115 | +$$(sh_targets_$1): sh_%_$1: %_$1 |
| 116 | + ${DOCKER_RUN_CMD} -it --name ${IMAGE}_$$*_$1 ${IMAGE}:$$*_$1 |
| 117 | + |
| 118 | +#$$(info Create targets: $(addprefix clean_, $(addsuffix _$1, $(DISTROS))).) |
| 119 | +clean_targets_$1 = $(addprefix clean_, $(addsuffix _$1, $(DISTROS))) |
| 120 | +.PHONY: clean_$1 $$(clean_targets_$1) |
| 121 | +clean_$1: $$(clean_targets_$1) |
| 122 | +$$(clean_targets_$1): clean_%_$1: |
| 123 | + docker image rm -f ${IMAGE}:$$*_$1 2>/dev/null |
| 124 | + rm -f cache/$$*/docker_$1.tar |
| 125 | +endef |
| 126 | + |
| 127 | +$(foreach stage,$(STAGES),$(eval $(call make-stage-target,$(stage)))) |
| 128 | + |
| 129 | +## CLEAN ## |
| 130 | +clean_targets = $(addprefix clean_, $(DISTROS)) |
| 131 | +.PHONY: clean $(clean_targets) |
| 132 | +clean: $(clean_targets) |
| 133 | + docker container prune -f |
| 134 | + docker image prune -f |
| 135 | + -rmdir cache |
| 136 | +$(clean_targets): clean_%: $(addprefix clean_%_, $(STAGES)) |
| 137 | + -rmdir cache/$* |
| 138 | + |
| 139 | +.PHONY: distclean |
| 140 | +distclean: clean |
| 141 | + -docker container rm -f $$(docker container ls -aq) |
| 142 | + -docker image rm -f $$(docker image ls -aq) |
0 commit comments