Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2107 :: Makefiles for Everyday Use

I talk about how I use Makefiles in my Lilypond and HTML projects

<< First, < Previous, , Latest >>

Thumbnail of Jon Kulp
Hosted by Jon Kulp on Tuesday, 2016-08-30 is flagged as Clean and is released under a CC-BY-SA license.
Automation, Makefiles, Scripting, Programming. 3.
The show is available on the Internet Archive at: https://archive.org/details/hpr2107

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:23:12

general.

In this episode I talk about how I use Makefiles to ease the process of building complicated projects in Lilypond and HTML. You can use Makefiles to run any kinds of commands you want. It does not have to be building actual computer programs. In my case I use them to build musical scores and web pages. Keep in mind I'm not an expert on this, and I'm hoping I will make enough mistakes that it will prompt a series of follow-up episodes by people who actually know what they're talking about.

Here's an example. This is the Makefile for my Counterpoint workbook Gratis ad Parnassum, which I wrote in 2009. Written in a combination of LaTeX and Lilypond, this requires very complicated and long commands to build the workbook, and I found that the only way to do this project in a sane manner was to create a Makefile that would keep track of changes in the files and only rebuild when necessary. It also meant that the only commands I would have to type were very simple, because the long command line options were all stored in the Makefile.

SHELL=/bin/bash
FILE=workbook_main
OUTDIR=out
WEBDIR=htmlout
VIEWER=evince
BROWSER=firefox
LILYBOOK_PDF=lilypond-book --output=$(OUTDIR) --pdf $(FILE).lytex
LILYBOOK_HTML=lilypond-book --output=$(WEBDIR) $(FILE).lytex
PDF=cd $(OUTDIR) && pdflatex $(FILE)
HTML=cd $(WEBDIR) && latex2html $(FILE)
INDEX=cd $(OUTDIR) && makeindex $(FILE)
PREVIEW=$(VIEWER) $(OUTDIR)/$(FILE).pdf >& /dev/null

all: pdf web

pdf:
$(LILYBOOK_PDF)
$(PDF)
$(INDEX)
$(PDF)
$(PREVIEW)

web:
$(LILYBOOK_HTML)
$(HTML)
cp -R $(WEBDIR)/$(FILE)/ ./
sleep 1
sh html-sed-fixes.sh
$(BROWSER) $(FILE)/index.html &

keep: pdf
cp $(OUTDIR)/$(FILE).pdf gratis.pdf
pdftk gratis.pdf update_info gratis.info output GratisAdParnassum.pdf

clean:
rm -rf $(OUTDIR)

web-clean:
rm -rf $(WEBDIR)

archive:
tar -cvvf free-counterpoint.tar \
--exclude=out/* \
--exclude=*.tar \
--exclude=*.zip \
--exclude=htmlout/* \
--exclude=workbook_main/* \
--exclude=*midi \
--exclude=*pdf \
--exclude=*~ \
../FreeCounterpoint/*
tar -xvvf free-counterpoint.tar
zip -r free-counterpoint.zip FreeCounterpoint
rm -R FreeCounterpoint

And here is the Makefile for my song collection called Canciones para niños, using Lilypond source files.

SHELL=/bin/bash
piece = lorca
#CPU_CORES=`cat /proc/cpuinfo | grep -m1 "cpu cores" | sed s/".*: "//`
LILY_CMD = lilypond -ddelete-intermediate-files \
                    -dno-point-and-click #-djob-count=$(CPU_CORES)

notes = \
cancioncilla.ily \
cantada.ily \
caracola.ily \
china.ily \
lagarto.ily \
nana.ily \
paisaje.ily \
remanso.ily

.SUFFIXES: .ly .ily .pdf .midi

#CURDIR = $(shell pwd)
VPATH = $(CURDIR)/Scores $(CURDIR)/PDF $(CURDIR)/Parts $(CURDIR)/Notes

%.ly: %.ily
%.pdf %.midi:  %.ly 
$(LILY_CMD) $<
mv *.pdf PDF/
mv *.midi MIDI/

$(piece).pdf: $(notes) 

cancioncilla.pdf: cancioncilla.ly cancioncilla.ily
cantada.pdf: cantada.ly cantada.ily
caracola.pdf: caracola.ly caracola.ily
china.pdf: china.ly china.ily
lagarto.pdf: lagarto.ly lagarto.ily
nana.pdf: nana.ly nana.ily
paisaje.pdf: paisaje.ly paisaje.ily
remanso.pdf: remanso.ly remanso.ily

.PHONY: score
score: $(piece).pdf

keep: score
cp $(CURDIR)/PDF/$(piece).pdf $(CURDIR)/CancionesParaNinos.pdf

archive:
tar -cvvf lorca.tar \
--exclude=*.pdf \
--exclude=*.midi \
--exclude=*~ \
../Canciones/*
tar -xvvf lorca.tar
zip -r lorca.zip Canciones
rm -R Canciones

Links


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2016-08-31 11:29:26 by Fweeb

.PHONY

Did a quick `info make` and scanned through it a bit. The .PHONY target is kind of a safety net. See, normally, targets in a makefile share the name of the exact file being made. However, in the case of something like `clean`, there's usually no file with that name being produced, just a series of deletions. *However*, if there is a file named `clean` in the same directory as your makefile, that can cause some confusion for the make command. So, by using `.PHONY: clean`, you're telling make to disregard a file named clean if it happens to see it.

I'm less sure about .SUFFIXES. The section on that in the manual was long and starts with the phrase "Old-Fashioned"... so perhaps it's something that's not entirely necessary for your makefile at this point.

Comment #2 posted on 2016-09-01 11:17:34 by Jonathan Kulp

.REAL

Thanks for the info. It's funny I guess I could read info pages myself but normally I just look at other people's Makefiles for examples and never really understand what they're doing. Once it all works I'm happy. Bit of trial and more error...

Comment #3 posted on 2016-09-01 14:00:14 by Dave Morriss

"Copy and paste programming"

We have probably all been there. I believe the practice is called "copy and paste programming" nowadays. I have certainly written Makefiles by this method. I have tried to learn more about the subject by reading the GNU make manual but it's hard going!

I'd say it's certainly a subject for a series of HPR shows.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?