Skip to content

Commit 2ce50ac

Browse files
committed
Add CMake ci
1 parent e22bbdb commit 2ce50ac

File tree

11 files changed

+459
-0
lines changed

11 files changed

+459
-0
lines changed

.ci/Makefile

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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)

.ci/docker/alpine/Dockerfile

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Create a virtual environment with all tools installed
2+
# ref: https://hub.docker.com/_/alpine
3+
FROM alpine:edge AS env
4+
LABEL maintainer="[email protected]"
5+
# Install system build dependencies
6+
ENV PATH=$PATH:/usr/local/bin
7+
RUN apk add --no-cache git build-base linux-headers cmake
8+
9+
CMD [ "/bin/sh" ]
10+
11+
# Add the library src to our build env
12+
FROM env AS devel
13+
# Create lib directory
14+
WORKDIR /home/lib
15+
# Bundle lib source
16+
COPY . .
17+
# CMake configure
18+
RUN cmake -S. -Bbuild
19+
# CMake build
20+
RUN cmake --build build --target all -v
21+
# CMake build
22+
RUN cmake --build build --target install
23+
24+
# Create an install image
25+
FROM env AS install
26+
# Copy lib from devel to prod
27+
COPY --from=devel /usr/local /usr/local/
28+
# Copy sample
29+
WORKDIR /home/sample
30+
COPY .ci/sample .

.ci/docker/archlinux/Dockerfile

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Create a virtual environment with all tools installed
2+
# ref: https://hub.docker.com/_/archlinux/
3+
FROM archlinux/base AS env
4+
LABEL maintainer="[email protected]"
5+
# Install system build dependencies
6+
ENV PATH=$PATH:/usr/local/bin
7+
RUN pacman -Syu --noconfirm base-devel git cmake
8+
9+
CMD [ "/bin/sh" ]
10+
11+
# Add the library src to our build env
12+
FROM env AS devel
13+
# Create lib directory
14+
WORKDIR /home/lib
15+
# Bundle lib source
16+
COPY . .
17+
# CMake configure
18+
RUN cmake -S. -Bbuild
19+
# CMake build
20+
RUN cmake --build build --target all -v
21+
# CMake build
22+
RUN cmake --build build --target install
23+
24+
# Create an install image
25+
FROM env AS install
26+
# Copy lib from devel to prod
27+
COPY --from=devel /usr/local /usr/local/
28+
# Copy sample
29+
WORKDIR /home/sample
30+
COPY .ci/sample .

.ci/docker/centos/Dockerfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Create a virtual environment with all tools installed
2+
# ref: https://hub.docker.com/_/centos
3+
FROM centos:latest AS env
4+
LABEL maintainer="[email protected]"
5+
# Install system build dependencies
6+
ENV PATH=$PATH:/usr/local/bin
7+
RUN yum -y update \
8+
&& yum -y groupinstall "Development Tools" \
9+
&& yum -y install epel-release \
10+
&& yum -y install cmake3 \
11+
&& ln -s /usr/bin/cmake3 /usr/local/bin/cmake \
12+
&& yum clean all \
13+
&& rm -rf /var/cache/yum
14+
15+
CMD [ "/bin/sh" ]
16+
17+
# Add the library src to our build env
18+
FROM env AS devel
19+
# Create lib directory
20+
WORKDIR /home/lib
21+
# Bundle lib source
22+
COPY . .
23+
# CMake configure
24+
RUN cmake -H. -Bbuild
25+
# CMake build
26+
RUN cmake --build build --target all
27+
# CMake build
28+
RUN cmake --build build --target install
29+
30+
# Create an install image
31+
FROM env AS install
32+
# Copy lib from devel to prod
33+
COPY --from=devel /usr/local /usr/local/
34+
# Copy sample
35+
WORKDIR /home/sample
36+
COPY .ci/sample .

.ci/docker/debian/Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Create a virtual environment with all tools installed
2+
# ref: https://hub.docker.com/_/debian
3+
FROM debian:latest AS env
4+
LABEL maintainer="[email protected]"
5+
# Install system build dependencies
6+
ENV PATH=$PATH:/usr/local/bin
7+
RUN apt-get update -q && \
8+
apt-get install -yq git build-essential cmake && \
9+
apt-get clean && \
10+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
11+
12+
CMD [ "/bin/sh" ]
13+
14+
# Add the library src to our build env
15+
FROM env AS devel
16+
# Create lib directory
17+
WORKDIR /home/lib
18+
# Bundle lib source
19+
COPY . .
20+
# CMake configure
21+
RUN cmake -S. -Bbuild
22+
# CMake build
23+
RUN cmake --build build --target all
24+
# CMake build
25+
RUN cmake --build build --target install
26+
27+
# Create an install image
28+
FROM env AS install
29+
# Copy lib from devel to prod
30+
COPY --from=devel /usr/local /usr/local/
31+
# Copy sample
32+
WORKDIR /home/sample
33+
COPY .ci/sample .

.ci/docker/fedora/Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Create a virtual environment with all tools installed
2+
# ref: https://hub.docker.com/_/fedora
3+
FROM fedora:latest AS env
4+
LABEL maintainer="[email protected]"
5+
# Install system build dependencies
6+
ENV PATH=$PATH:/usr/local/bin
7+
RUN dnf -y update \
8+
&& dnf -y groupinstall "Development Tools" \
9+
&& dnf -y install cmake gcc-c++ \
10+
&& dnf clean all
11+
12+
CMD [ "/bin/sh" ]
13+
14+
# Add the library src to our build env
15+
FROM env AS devel
16+
# Create lib directory
17+
WORKDIR /home/lib
18+
# Bundle lib source
19+
COPY . .
20+
# CMake configure
21+
RUN cmake -S. -Bbuild
22+
# CMake build
23+
RUN cmake --build build --target all -v
24+
# CMake build
25+
RUN cmake --build build --target install
26+
27+
# Create an install image
28+
FROM env AS install
29+
# Copy lib from devel to prod
30+
COPY --from=devel /usr/local /usr/local/
31+
# Copy sample
32+
WORKDIR /home/sample
33+
COPY .ci/sample .

.ci/docker/opensuse/Dockerfile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Create a virtual environment with all tools installed
2+
# ref: https://hub.docker.com/r/opensuse/tumbleweed
3+
FROM opensuse/tumbleweed AS env
4+
LABEL maintainer="[email protected]"
5+
# Install system build dependencies
6+
ENV PATH=$PATH:/usr/local/bin
7+
RUN zypper up -y \
8+
&& zypper install -y gcc gcc-c++ cmake git \
9+
&& zypper clean -a
10+
11+
ENV CC=gcc CXX=g++
12+
13+
CMD [ "/bin/sh" ]
14+
15+
# Add the library src to our build env
16+
FROM env AS devel
17+
# Create lib directory
18+
WORKDIR /home/lib
19+
# Bundle lib source
20+
COPY . .
21+
# CMake configure
22+
RUN cmake -S. -Bbuild
23+
# CMake build
24+
RUN cmake --build build --target all -v
25+
# CMake build
26+
RUN cmake --build build --target install
27+
28+
# Create an install image
29+
FROM env AS install
30+
# Copy lib from devel to prod
31+
COPY --from=devel /usr/local /usr/local/
32+
# Copy sample
33+
WORKDIR /home/sample
34+
COPY .ci/sample .

0 commit comments

Comments
 (0)