A Makefile for ChordPro

31 March 2021

I'm maintaining a collection of 200+ songs and need to make sure the PDF song sheets and song book are always reflecting the most recent changes to the ChordPro source files.

To help me with this, I created a Makefile.

Given a directory structure like the one below, you can generate all song sheets and a songbook with the commands make and make songbook.

.
├── output/
├── src/
│   ├── a-song.cho
│   └── another-song.cho
└── Makefile

The generated files will be located in output/.

Here is the Makefile:

# Directory containing source files 
source := src

# Directory containing pdf files
output := output

# Songbook output path
songbook := $(output)/songbook.pdf

# All .cho files in src/ are considered sources
sources := $(wildcard $(source)/*.cho)


# Convert the list of source files in src/
# into a list of output files in public/.
objects := $(patsubst %.cho,%.pdf,$(subst $(source)/,$(output)/,$(sources)))

all: $(objects)

# Recipe for converting a ChordPro file into PDF
$(output)/%.pdf: $(source)/%.cho

# Create output directory if it does not yet exist
	@[ -d $(output) ] || mkdir -p $(output)
	@echo Making "$(@)"...
	@chordpro "$(<)" -o "$(@)"


.PHONY: songbook
songbook:
# Create output directory if it does not yet exist
	@[ -d $(output) ] || mkdir -p $(output)
	@echo Making "$(songbook)"...
# Remove songbook.txt in case the previous making of songbook did not complete
	@rm -f $(source)/songbook.txt
	@ls $(source)/* > $(source)/songbook.txt
	@chordpro --filelist=$(source)/songbook.txt --no-csv -o "$(songbook)"
	@rm $(source)/songbook.txt


.PHONY: clean
clean:
	rm -f $(output)/*.pdf

You can see the this Makefile in an example repo here and a more complex version here.