|
| 1 | +PROJECT := coinutils |
| 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}env${RESET}: build a virtual env image." |
| 21 | + @echo -e "\t${BOLD}sh_env_<distro>${RESET}: run a container using the virtual env image (debug purpose)." |
| 22 | + @echo |
| 23 | + @echo -e "\t${BOLD}devel${RESET}: build the library using all devel images." |
| 24 | + @echo -e "\t${BOLD}devel_<distro>${RESET}: build the library using a specific devel image." |
| 25 | + @echo -e "\t${BOLD}test_<distro>${RESET}: auto test using the devel image." |
| 26 | + @echo -e "\t${BOLD}sh_<distro>${RESET}: run a container using the devel image (debug purpose)." |
| 27 | + @echo |
| 28 | + @echo -e "\t${BOLD}install${RESET}: execute the cmake target ${BOLD}install${RESET} using all devel image container, then create an install image with it." |
| 29 | + @echo -e "\t${BOLD}install_<distro>${RESET}: execute the cmake target ${BOLD}install${RESET} using the devel image container specified, then create an install image with it." |
| 30 | + @echo -e "\t${BOLD}test_install${RESET}: configure, build, install then test a sample project against it using all ${BOLD}install${RESET} image containers." |
| 31 | + @echo -e "\t${BOLD}test_install_<distro>${RESET}: configure, build, install then test a sample project against it using the ${BOLD}install${RESET} image container specified." |
| 32 | + @echo -e "\t${BOLD}sh_install_<distro>${RESET}: run a container using the install image (debug purpose)." |
| 33 | + @echo |
| 34 | + @echo -e "\t${BOLD}clean${RESET}: Remove cache and docker image." |
| 35 | + @echo -e "\t${BOLD}clean_<distro>${RESET}: Remove cache and docker image for the specified distro." |
| 36 | + @echo |
| 37 | + @echo -e "\t${BOLD}<distro>${RESET}:" |
| 38 | + @echo -e "\t\t${BOLD}alpine${RESET} (latest)" |
| 39 | + @echo -e "\t\t${BOLD}archlinux${RESET} (latest)" |
| 40 | + @echo -e "\t\t${BOLD}centos${RESET} (latest)" |
| 41 | + @echo -e "\t\t${BOLD}fedora${RESET} (latest)" |
| 42 | + @echo -e "\t\t${BOLD}opensuse${RESET} (tumbleweed)" |
| 43 | + @echo -e "\t\t${BOLD}debian${RESET} (latest)" |
| 44 | + @echo -e "\t\t${BOLD}ubuntu${RESET} (latest)" |
| 45 | + @echo -e "\te.g. 'make test_ubuntu'" |
| 46 | + @echo |
| 47 | + @echo -e "\t${BOLD}NOCACHE=1${RESET}: use 'docker build --no-cache' when building container (default use cache)." |
| 48 | + @echo |
| 49 | + @echo -e "branch: $(BRANCH)" |
| 50 | + @echo -e "sha1: $(SHA1)" |
| 51 | + |
| 52 | +# Need to add cmd_distro to PHONY otherwise target are ignored since they do not |
| 53 | +# contain recipe (using FORCE do not work here) |
| 54 | +.PHONY: all |
| 55 | +all: devel |
| 56 | + |
| 57 | +# Delete all implicit rules to speed up makefile |
| 58 | +MAKEFLAGS += --no-builtin-rules |
| 59 | +.SUFFIXES: |
| 60 | +# Remove some rules from gmake that .SUFFIXES does not remove. |
| 61 | +SUFFIXES = |
| 62 | +# Keep all intermediate files |
| 63 | +# ToDo: try to remove it later |
| 64 | +.SECONDARY: |
| 65 | + |
| 66 | +# Docker image name prefix. |
| 67 | +IMAGE := ${PROJECT} |
| 68 | + |
| 69 | +ifdef NOCACHE |
| 70 | +DOCKER_BUILD_CMD := docker build --no-cache |
| 71 | +else |
| 72 | +DOCKER_BUILD_CMD := docker build |
| 73 | +endif |
| 74 | + |
| 75 | +DOCKER_RUN_CMD := docker run --rm --init --net=host |
| 76 | + |
| 77 | +# Currently supported distro |
| 78 | +DISTROS = alpine archlinux centos fedora opensuse debian ubuntu |
| 79 | + |
| 80 | +# $* stem |
| 81 | +# $< first prerequist |
| 82 | +# $@ target name |
| 83 | + |
| 84 | +# ENV |
| 85 | +targets = $(addprefix env_, $(DISTROS)) |
| 86 | +.PHONY: env $(targets) |
| 87 | +env: $(targets) |
| 88 | +$(targets): env_%: cache/%/docker_env.tar |
| 89 | +cache/%/docker_env.tar: docker/%/Dockerfile |
| 90 | + @docker image rm -f ${IMAGE}_$*:env 2>/dev/null |
| 91 | + ${DOCKER_BUILD_CMD} --target=env --tag ${IMAGE}_$*:env -f $< docker/$* |
| 92 | + @rm -f $@ |
| 93 | + mkdir -p cache/$* |
| 94 | + docker save ${IMAGE}_$*:env -o $@ |
| 95 | + |
| 96 | +# Run a container using the env image. |
| 97 | +targets = $(addprefix sh_env_, $(DISTROS)) |
| 98 | +.PHONY: $(targets) |
| 99 | +$(targets): sh_env_%: cache/%/docker_env.tar |
| 100 | + ${DOCKER_RUN_CMD} -it --name ${IMAGE}_$* ${IMAGE}_$*:env /bin/sh |
| 101 | + |
| 102 | +# DEVEL |
| 103 | +targets = $(addprefix devel_, $(DISTROS)) |
| 104 | +.PHONY: devel $(targets) |
| 105 | +devel: $(targets) |
| 106 | +$(targets): devel_%: cache/%/docker_devel.tar |
| 107 | +cache/%/docker_devel.tar: docker/%/Dockerfile \ |
| 108 | + ../CMakeLists.txt ../cmake \ |
| 109 | + ../src |
| 110 | + @docker image rm -f ${IMAGE}_$*:devel 2>/dev/null |
| 111 | + ${DOCKER_BUILD_CMD} --target=devel --tag ${IMAGE}_$*:devel -f $< .. |
| 112 | + @rm -f $@ |
| 113 | + mkdir -p cache/$* |
| 114 | + docker save ${IMAGE}_$*:devel -o $@ |
| 115 | + |
| 116 | +# DOCKER BASH INSTALL (debug) |
| 117 | +targets = $(addprefix sh_devel_, $(DISTROS)) |
| 118 | +.PHONY: $(targets) |
| 119 | +$(targets): sh_devel_%: cache/%/docker_devel.tar |
| 120 | + ${DOCKER_RUN_CMD} -it --name ${IMAGE}_$* ${IMAGE}_$*:devel /bin/sh |
| 121 | + |
| 122 | +# TEST DEVEL |
| 123 | +targets = $(addprefix test_, $(DISTROS)) |
| 124 | +.PHONY: test $(targets) |
| 125 | +test: $(targets) |
| 126 | +$(targets): test_%: cache/%/docker_devel.tar |
| 127 | + ${DOCKER_RUN_CMD} -t --name ${IMAGE}_$* ${IMAGE}_$*:devel cmake --build build --target test |
| 128 | + |
| 129 | +# INSTALL |
| 130 | +targets = $(addprefix install_, $(DISTROS)) |
| 131 | +.PHONY: install $(targets) |
| 132 | +install: $(targets) |
| 133 | +$(targets): install_%: cache/%/docker_install.tar |
| 134 | +cache/%/docker_install.tar: docker/%/Dockerfile \ |
| 135 | + sample |
| 136 | + @docker image rm -f ${IMAGE}_$*:install 2>/dev/null |
| 137 | + ${DOCKER_BUILD_CMD} --target=install --tag ${IMAGE}_$*:install -f $< .. |
| 138 | + @rm -f $@ |
| 139 | + mkdir -p cache/$* |
| 140 | + docker save ${IMAGE}_$*:install -o $@ |
| 141 | + |
| 142 | +# DOCKER BASH INSTALL (debug) |
| 143 | +targets = $(addprefix sh_install_, $(DISTROS)) |
| 144 | +.PHONY: $(targets) |
| 145 | +$(targets): sh_install_%: cache/%/docker_install.tar |
| 146 | + ${DOCKER_RUN_CMD} -it --name ${IMAGE}_$* ${IMAGE}_$*:install /bin/sh |
| 147 | + |
| 148 | +# TEST INSTALL |
| 149 | +targets = $(addprefix test_install_, $(DISTROS)) |
| 150 | +.PHONY: test_install $(targets) |
| 151 | +test_install: $(targets) |
| 152 | +$(targets): test_install_%: cache/%/docker_install.tar sample |
| 153 | + @docker load -i cache/$*/docker_install.tar |
| 154 | + ${DOCKER_RUN_CMD} -t --name ${IMAGE}_$* ${IMAGE}_$*:install /bin/sh -c \ |
| 155 | + "cmake -H. -Bbuild; \ |
| 156 | + cmake --build build --target all; \ |
| 157 | + cmake --build build --target test; \ |
| 158 | + cmake --build build --target install" |
| 159 | + |
| 160 | +# CLEAN |
| 161 | +targets = $(addprefix clean_, $(DISTROS)) |
| 162 | +.PHONY: clean $(targets) |
| 163 | +clean: $(targets) |
| 164 | + -rmdir cache |
| 165 | +$(targets): clean_%: |
| 166 | + docker container prune -f |
| 167 | + docker image prune -f |
| 168 | + -docker image rm -f ${IMAGE}_$*:install 2>/dev/null |
| 169 | + -docker image rm -f ${IMAGE}_$*:devel 2>/dev/null |
| 170 | + -docker image rm -f ${IMAGE}_$*:env 2>/dev/null |
| 171 | + -rm -f cache/$*/docker_install.tar |
| 172 | + -rm -f cache/$*/docker_devel.tar |
| 173 | + -rm -f cache/$*/docker_env.tar |
| 174 | + -rmdir cache/$* |
| 175 | + |
| 176 | +.PHONY: distclean |
| 177 | +distclean: clean |
| 178 | + -docker container rm -f $$(docker container ls -aq) |
| 179 | + -docker image rm -f $$(docker image ls -aq) |
0 commit comments