ed and a self documenting Makefile

Makefile is a powerful way to declare a chain of interdependent commands that create targets. In this article, we will explain how to create an auto-documenting Makefile that is able to print a help message when invoked without target.

The idea of a self-documenting Makefile comes from this almost perfect stackoverflow answer, if it wasn’t for some peculiarities of the unfortunately different version of macOS’s sed, compared to GNU.

ed as a compatible sed replacement

ed, one of the first three key elements of Unix according to Wikipedia, is the ancestor of sed.

Its legacy is an advantage for us because its interface hasn’t changed since ages, and thus remains a very interesting way of writing multi-platform scripts.

We will use ex, as its proximity with vi helps iterating quickly on the different options.

TL;DR, the Makefile

Here is an example Makefile that prints some help when invoked without target. We signify documentation with regular comments by doubling the hashtags trailing after some targets. For good measure, we also use the strict makefile headers. Continue reading for more details.

SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

.PHONY:help
help:  ## print these help instructions
	@ex -s  '+%g!/^[\\.A-Za-z_-]\+:[^#]\+##.*$$/d _' '+%s/^\([\\.A-Za-z_-]\+\):.*## \+\(.*\)$$/\"\1\" \"\2\"/' -c'%p|q!'  <(cat ${MAKEFILE_LIST}) | xargs printf "make %-20s# %s\n"

a:
	echo $@ > $@

b: # some internal help, not to be printed
	echo $@ > $@

c.txt: a b  ## this target is what you want to generate probably
	cat $? > $@
	echo "Generation complete"

Read more

Browse huge database with vim and clangd

Vim is a powerful text editor that I like to use for its flexibility and customizability. One of the most useful features of Vim is its ability to easily install plugins to gain functionality, here navigating through code sources using the clangd language server. In this article, I will explain how to configure Vim for easy code source browsing with clangd.

Read more

Browse huge codebase with vim tags

Vim is a fantastic text editor. I really understood the benefits of it when reading “You don’t grok vim”, a famous (289 captures 2 Apr 2010 – 28 Mar 2023 in the Wayback Machine) stackoverflow post that explains the design of such tool, and its powers. Please take a moment to appreciate the dithyrambic comments.

However, when transitioning from a full-fledged c++ IDE, I missed the ability to browse symbols by going to definition, implementation or references. Here is how you give vim the ability to browse huge codebase with vim tags.

Read more