Makefiles for Dummies

Don't tie yourself in knots

#!/usr/bin/env make -f
# the first line is a "shebang" to allow the script to be directly executed

# The first target found is the default "goal"
#  it will be executed if 'make' is not given a goal on the command line
# Convention is to have it depend on the targets ("prerequisites")
#  that will create the project without further intervention

all: build install
	echo "target: $(@)"
	# This is a "recipe" - a list of shell commands to execute
	# It MUST be indented by one or more tab characters NOT spaces
	# It can contain Automatic Variables as shown here
	# $(@) expands to "all" - the name of the current target

# If the first target isn't the default goal set it explicitly
#  rather than re-ordering targets in the Makefile
.DEFAULT_GOAL := build

# If a target is not a real file tell 'make' so it handles it correctly
.PHONY: all

# For portability recipes should only use POSIX shell syntax
#  however, sometimes a specific shell may be required
SHELL := /usr/bin/env bash 

# ideally options could be passed to the shell
# but only recent versions of "env" have the -S|--split option
# so this would not be portable:

SHELL := /usr/bin/env --split bash -o pipefail

# However,  'make' has a way to pass arguments to the shell
#  so this would be (more) portable:

SHELL := /usr/bin/bash
.SHELLFLAGS := -o pipefail -c

build:
	@echo "Must build the project"



Index