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