Skip to content

Commit 454e5bc

Browse files
committed
tooling: Improve Makefile maintainability
- Fixed incorrect use of .DEFAULT target. - Refactor .PHONY targets to be stated next to their targets, instead of all in one line. This makes it easier to update code without forgetting to maintain the .PHONY targets.
1 parent e7aa389 commit 454e5bc

File tree

9 files changed

+154
-81
lines changed

9 files changed

+154
-81
lines changed

Makefile

+17-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ LOCKFILE_OPTION := --lockfile="${CLOE_ROOT}/${BUILD_LOCKFILE}"
2626
INSTALL_DIR := /usr/local
2727
CONAN_OPTIONS :=
2828

29+
.DEFAULT_GOAL := help
2930
.PHONY: help
30-
.DEFAULT: help
3131
.SILENT: help
3232
help::
3333
$(call print_help_usage)
@@ -37,7 +37,6 @@ help::
3737
include Makefile.setup
3838

3939
# Workspace targets -----------------------------------------------------------
40-
.PHONY: lockfile status deploy sphinx doxygen purge-all export-cli smoketest smoketest-deps
4140
help::
4241
$(call print_help_section, "Available workspace targets")
4342
$(call print_help_target, status, "show status of each of the Conan packages")
@@ -53,55 +52,65 @@ help::
5352
${BUILD_LOCKFILE}:
5453
${MAKE} -f Makefile.package SOURCE_CONANFILE=/dev/null LOCKFILE_SOURCE=${LOCKFILE_SOURCE} ${BUILD_LOCKFILE}
5554

55+
.PHONY: lockfile
5656
lockfile: ${BUILD_LOCKFILE}
5757

58+
.PHONY: status
5859
status: ${BUILD_LOCKFILE}
5960
@for pkg in ${ALL_PKGS}; do \
6061
[ -d $${pkg} ] || continue; \
6162
${MAKE} LOCKFILE_SOURCE="" LOCKFILE_OPTION=${LOCKFILE_OPTION} -C $${pkg} status || true; \
6263
done
6364

65+
.PHONY: deploy
6466
deploy:
6567
$(call print_header, "Deploying binaries to ${INSTALL_DIR}...")
6668
conan install ${CONAN_OPTIONS} --install-folder ${BUILD_DIR}/deploy -g deploy .
6769
mkdir -p ${INSTALL_DIR}
6870
cp -r ${BUILD_DIR}/deploy/cloe-*/* ${INSTALL_DIR}/
6971

72+
.PHONY: deploy-cli
7073
deploy-cli:
7174
$(call print_header, "Deploying cloe-launch binary with pip...")
7275
${MAKE} -C cli install
7376

77+
.PHONY: export-cli
7478
export-cli:
7579
${MAKE} -C cli conan-profile
7680

81+
.PHONY: sphinx
7782
sphinx:
7883
$(call print_header, "Generating Sphinx documentation...")
7984
mkdir -p ${BUILD_DIR}/sphinx
8085
${MAKE} -C docs html
8186

82-
doxygen:keyboard tilt
87+
.PHONY: doxygen
88+
doxygen:
8389
$(call print_header, "Generating Doxygen documentation...")
8490
mkdir -p ${BUILD_DIR}/doxygen
8591
doxygen Doxyfile
8692

93+
.PHONY: smoketest-deps
8794
smoketest-deps: export-cli smoketest-deps-select
8895

96+
.PHONY: smoketest
8997
smoketest: smoketest-select
9098

99+
.PHONY: purge-all
91100
purge-all:
92101
$(call print_header, "Removing all cloe Conan packages...")
93102
conan remove -f 'cloe-*'
94103
conan remove -f 'cloe'
95104
conan remove -f 'fable'
96105

97106
# Development targets ---------------------------------------------------------
98-
.PHONY: format todos find-missing-eol sanitize-files
99107
help::
100108
$(call print_help_section, "Available development targets")
101109
$(call print_help_target, format, "format Cloe source code with clang-format")
102110
$(call print_help_target, todos, "show all TODOs in Cloe source code")
103111
echo
104112

113+
.PHONY: format
105114
format:
106115
# When you do this, isolate the change in a single commit and then add the
107116
# commit hash to a file such as .git-blame-ignore-revs, so that git-blame
@@ -110,17 +119,21 @@ format:
110119
# See: https://www.moxio.com/blog/43/ignoring-bulk-change-commits-with-git-blame
111120
find . -type f -not -path '*/\.git/*' -and \( -name '*.cpp' -o -name '*.hpp' \) -exec ${CLANG_FORMAT} ${CLANG_FORMAT_ARGS} -i {} \;
112121

122+
.PHONY: todos
113123
todos:
114124
${AG} TODO
115125
${AG} FIXME
116126
${AG} XXX
117127

128+
.PHONY: grep-uuids
118129
grep-uuids:
119130
${AG} "\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b"
120131

132+
.PHONY: find-missing-eol
121133
find-missing-eol:
122134
find . -type f -size +0 -exec gawk 'ENDFILE{if ($0 == "") print FILENAME}' {} \;
123135

136+
.PHONY: sanitize-files
124137
sanitize-files:
125138
git grep --cached -Ilz '' | while IFS= read -rd '' f; do tail -c1 < "$$f" | read -r _ || echo >> "$$f"; done
126139

Makefile.all

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ endef
137137

138138
# --------------------------------------------------------------------------- #
139139

140+
.DEFAULT_GOAL := help
140141
.PHONY: help
141-
.DEFAULT: help
142142
.SILENT: help
143143
help::
144144
$(call print_help_section, "Available build targets")

Makefile.help

+45-2
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,57 @@ define print_help_define_align
9090
@printf " ${_grn}%-16s${_rst}=${_dim} %s${_rst}\n" ${1} ${2}
9191
endef
9292

93-
# Usage: $(call print_help_define_lines, VAR-NAME, ${VAR-NAME}}
93+
# Usage: $(call print_help_define_lines, VAR-NAME, ${VAR-NAME})
94+
#
95+
# For example, given a call with the following:
96+
#
97+
# $(call print_help_define_lines, FROM_VERSIONS, 18.04 20.04 22.04)
98+
#
99+
# we will get the following output:
100+
#
101+
# FROM_VERSIONS=\
102+
# 18.04
103+
# 20.04
104+
# 22.04
105+
#
106+
# If the contents of ${VAR-NAME} is empty or only contains whitespace,
107+
# then no extra newline is printed, and it behaves as print_help_define.
94108
define print_help_define_lines
95-
@printf " ${_grn}%s${_rst}=${_dim}\\ \n" ${1}
109+
@printf " ${_grn}%s${_rst}=${_dim}" ${1}
110+
@if [ -n "$(strip ${2})" ]; then printf "\\ \n"; fi
96111
@printf "%s" "$$(echo " ${2}" | fmt -5)"
97112
@printf "${_rst}\n"
98113
endef
99114

115+
# Usage: $(call print_help_args_lines, VAR-NAME, ${VAR-NAME})
116+
#
117+
# This function behaves as print_help_define_lines, except that
118+
# it doesn't split on words, but on arguments. Each argument (leading with -)
119+
# is prepended with a newline.
120+
#
121+
# For example, given a call with the following:
122+
#
123+
# $(call print_help_define_lines, DOCKER_ARGS, -it --rm --network host)
124+
#
125+
# we will get the following output:
126+
#
127+
# DOCKER_ARGS=\
128+
# -it
129+
# --rm
130+
# --network host
131+
#
132+
# If the contents of ${VAR-NAME} is empty, same behavior as print_help_define.
133+
define print_help_args_lines
134+
@printf " ${_grn}%s${_rst}=${_dim}" ${1}
135+
@if [ -n "$(strip ${2})" ]; then printf "\\ \n"; fi
136+
@printf "%s" "$$(echo " ${2}" | sed -r -e 's/\W--?[^-]/\n \0/g' -e 's/^\s*\n//')"
137+
@printf "${_rst}\n"
138+
endef
139+
100140
# Usage: $(call print_help_usage)
141+
#
142+
# This should be called only once, before all other print_help_* calls,
143+
# so that the help message starts with a usage statement.
101144
define print_help_usage
102145
@printf "Usage: make ${_blu}target${_rst}\n"
103146
endef

Makefile.package

+37-8
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ endif
132132

133133
# INFORMATIONAL TARGETS -------------------------------------------------------
134134
.DEFAULT_GOAL := help
135-
.SILENT: help status parse-info parse-editable info-name info-version info-channel info-fqn info smoketest smoketest-deps
136-
.PHONY: help status parse-info parse-editable info-name info-version info-channel info-fqn info smoketest smoketest-deps
135+
.SILENT: help
136+
.PHONY: help
137137
help:: parse-info parse-editable
138138
$(call print_help_usage)
139139
echo
@@ -193,6 +193,8 @@ endif
193193
$(call print_help_define_align, GIT_COMMIT_HASH, ${GIT_COMMIT_HASH})
194194
echo
195195

196+
.PHONY: parse-info
197+
.SILENT: parse-info
196198
parse-info: ${BUILD_LOCKFILE}
197199
# Fetch package information from Conan.
198200
#
@@ -205,9 +207,13 @@ parse-info: ${BUILD_LOCKFILE}
205207
$(eval PACKAGE_DIR := $(shell echo -e "${PACKAGE_INFO}" | sed -rn 's/^ *package_folder: *(.*)$$/\1/p'))
206208
$(eval PACKAGE_DATE := $(shell echo -e "${PACKAGE_INFO}" | sed -rn 's/^ *Creation date: *(.*)$$/\1/p'))
207209

210+
.PHONY: parse-editable
211+
.SILENT: parse-editable
208212
parse-editable:
209213
$(eval PACKAGE_EDITABLE := $(shell [ -e ~/.conan/editable_packages.json ] && jq -r "${JQ_PACKAGE_EDITABLE}" ~/.conan/editable_packages.json || echo "not-editable"))
210214

215+
.PHONY: status
216+
.SILENT: status
211217
status: parse-info parse-editable
212218
# Show the *approximate* status of each package in the cloe workspace.
213219
#
@@ -226,42 +232,54 @@ status: parse-info parse-editable
226232
fi \
227233
fi
228234

235+
.PHONY: info-name
236+
.SILENT: info-name
229237
info-name:
230238
echo ${PACKAGE_NAME}
231239

240+
.PHONY: info-version
241+
.SILENT: info-version
232242
info-version:
233243
echo ${PACKAGE_VERSION}
234244

245+
.PHONY: info-channel
246+
.SILENT: info-channel
235247
info-channel:
236248
echo ${PACKAGE_CHANNEL}
237249

250+
.PHONY: info-fqn
251+
.SILENT: info-fqn
238252
info-fqn:
239253
echo ${PACKAGE_FQN}
240254

255+
.PHONY: info
256+
.SILENT: info
241257
info: parse-info
242258
if [ -z "${PACKAGE_INFO}" ]; then \
243259
echo "Errors occurred, no output available."; \
244260
else \
245261
echo ${PACKAGE_INFO}; \
246262
fi
247263

264+
.PHONY: smoketest
248265
smoketest:
266+
# Ensure that you have built all smoketest dependencies!
249267
@for profile in tests/profile_*.py; do \
250268
test -f "$${profile}" || continue; \
251269
printf "Running BATS tests with profile: $${profile}\n\n"; \
252270
SHELL=/bin/bash ${CLOE_LAUNCH} shell -P "$${profile}" -- -c "source ${PROJECT_ROOT}/tests/setup_testname.bash; bats tests"; \
253271
done
254272

255273
smoketest-deps:
274+
# Ensure that you have exported all relevant packages!
256275
@for profile in tests/profile_*.py; do \
257276
test -f "$${profile}" || continue; \
258277
echo "Building dependencies for profile: $${profile}"; \
259278
${CLOE_LAUNCH} prepare -P "$${profile}"; \
260279
done
261280

262281
# CONFIGURATION TARGETS -------------------------------------------------------
263-
.PHONY: editable uneditable
264-
282+
.PHONY: editable
265283
editable:
266284
# Conan will now resolve references to the in-source build.
267285
#
@@ -274,6 +292,7 @@ editable:
274292
#
275293
conan editable add . --layout "${BUILD_LAYOUT}" ${PACKAGE_FQN}
276294

295+
.PHONY: uneditable
277296
uneditable:
278297
# Conan will now resolve references to the package in the cache.
279298
#
@@ -283,8 +302,7 @@ uneditable:
283302
conan editable remove ${PACKAGE_FQN}
284303

285304
# CONAN TARGETS ---------------------------------------------------------------
286-
.PHONY: export package package-all package-missing package-outdated purge list
287-
305+
.PHONY: export
288306
export: parse-editable
289307
# Export sources to Conan cache.
290308
#
@@ -293,6 +311,7 @@ export: parse-editable
293311
#
294312
if [ "${PACKAGE_EDITABLE}" = "not-editable" ] || [ "${PACKAGE_EDITABLE}" = "editable-other-name" ]; then conan export . ${PACKAGE_FQN}; fi
295313

314+
.PHONY: download
296315
download:
297316
# Try to download the package to Conan cache.
298317
#
@@ -307,6 +326,7 @@ download:
307326
conan create . ${PACKAGE_FQN} --build=never ${ALL_OPTIONS} || \
308327
conan create . ${PACKAGE_FQN} --build=${BUILD_POLICY} --build=${PACKAGE_NAME} ${ALL_OPTIONS}
309328

329+
.PHONY: package
310330
package: ${BUILD_LOCKFILE}
311331
# Build the package in Conan cache unconditionally.
312332
#
@@ -318,6 +338,7 @@ package: ${BUILD_LOCKFILE}
318338
conan create . ${PACKAGE_FQN} \
319339
--build=${BUILD_POLICY} --build=${PACKAGE_NAME} ${ALL_OPTIONS}
320340

341+
.PHONY: package-all
321342
package-all: ${BUILD_LOCKFILE}
322343
# Build the package in Conan cache unconditionally.
323344
#
@@ -326,6 +347,7 @@ package-all: ${BUILD_LOCKFILE}
326347
#
327348
conan create . ${PACKAGE_FQN} --build ${ALL_OPTIONS}
328349

350+
.PHONY: package-outdated
329351
package-outdated: ${BUILD_LOCKFILE}
330352
# Build the package in Conan cache if it is outdated.
331353
#
@@ -335,6 +357,7 @@ package-outdated: ${BUILD_LOCKFILE}
335357
#
336358
conan create . ${PACKAGE_FQN} --build=outdated ${ALL_OPTIONS}
337359

360+
.PHONY: purge
338361
purge:
339362
# Remove all instances of this package in the Conan cache.
340363
#
@@ -344,14 +367,14 @@ purge:
344367
#
345368
-conan remove -f ${PACKAGE_FQN}
346369

370+
.PHONY: list
347371
list: parse-info
348372
# List all files in the Conan cache package directory.
349373
#
350374
@tree ${PACKAGE_DIR}
351375

352376
# IN-SOURCE TARGETS -----------------------------------------------------------
353-
.PHONY: all clean conan configure test export-pkg
354-
377+
.PHONY: clean
355378
clean:
356379
# Clean the build directory and Python cache files.
357380
#
@@ -362,20 +385,25 @@ ifeq (${CLEAN_SOURCE_DIR}, true)
362385
endif
363386

364387
ifeq (${BUILD_IN_SOURCE}, false)
388+
.PHONY: all conan configure test export-pkg
365389
all conan configure test export-pkg ${SOURCE_DIR} ${SOURCE_CMAKELISTS} ${BUILD_CONANINFO} ${BUILD_CMAKECACHE}:
366390
@echo "Error: [in-source] targets are not supported for this package."
367391
@echo "Note: please use [conan-cache] targets, such as 'package'."
368392
exit 1
369393
else
394+
.PHONY: all
370395
all: ${BUILD_CONANINFO} | ${SOURCE_DIR}
371396
# Build the package in-source.
372397
#
373398
conan build . --source-folder="${SOURCE_DIR}" --build-folder="${BUILD_DIR}"
374399

400+
.PHONY: conan
375401
conan: ${BUILD_CONANINFO}
376402

403+
.PHONY: configure
377404
configure: ${BUILD_CMAKECACHE}
378405

406+
.PHONY: test
379407
test:
380408
# Run tests available to CMake ctest.
381409
#
@@ -387,6 +415,7 @@ test:
387415
true; \
388416
fi
389417

418+
.PHONY: export-pkg
390419
export-pkg:
391420
# Export in-source build artifacts to Conan cache.
392421
#

0 commit comments

Comments
 (0)