The Best Build System

A simplistic fantasy UI with a bright label saying “Failure”.

Creative writing has managed to take decisive victory over me. I am now pivoting to programming.

…and the first thing you do when programming is to compile a program, of course, so let’s get started! I have heard that make is a decent enough build system, so let’s start out with a simple Makefile:

# /* <- This `/*` starts a multiline comment in C code, letting the C compiler skip the Makefile portion of this program.
#
# Compile with `make`, or `make -f [file]` if this file hasn't been named `Makefile`.

# You're not allowed to run this file as a shell script. Shame on you.
exit = 1

# Show most warnings
CFLAGS += -Wall -Wextra

# Disable optimizations, and enable debug information
CFLAGS += -O0 -g

# Enable UBSan and ASan (mandatory!)
CFLAGS += -fsanitize=undefined -fsanitize=address 

# `$(lastword $(MAKEFILE_LIST))` evaluates to the name of the file that `make` is currently interpreting, although `make` can evaluate variables in an unintuitive order. To be safe, let's just never invoke `make` from inside `make`.
program: $(lastword $(MAKEFILE_LIST))
	$(CC) -x c $^ \
		-o $@ \
		$(CFLAGS)

# An `ifeq` block that never executes, so `make` doesn't try to interpret C code.
ifeq (0,1) 
# And finally, end the multiline comment from the beginning of this file: */

Then the C code:

#include <stdio.h>

int main(int argc, char **argv) {
	printf("Hello, world!\n");
}

And finally, balance out the Makefile with a corresponding endif.

#if 0
# End this Makefile's conditional block, so `make` doesn't complain about "reaching the end of a file".

endif

#endif

Next up, I’m going to start writing an entire new programming language in C.