Week 11 workshop exercises

Simple makefiles

The file sample_makefile.zip is a .zip file, containing a Makefile. You can unzip it to a directory either using the desktop file manager, and right-clicking, or by downloading it somewhere and typing

unzip sample_makefile.zip

It contains three rules: one for saying hello, one for making HTML files out of text files, and one for cleaning up stray .html files in our working directory.

Phony targets

The line

.PHONY: hello clean

says that hello and clean are not actually files we want to make – just convenient names for a set of tasks we want to perform.

Add a new target, goodbye, which bids the user farewell, and try executing it. Now amend the recipe for saying “goodbye”, so that before doing so, it removes all .html files in the directory.

Note that lines in the “recipe” portion of a rule must all begin with a tab character. If you open the Makefile in a good text editor (vim or emacs or atom), it should highlight lines where you haven’t done so.

Abstract rules

The rule %.html: %.txt gives a general method for creating .html files from .txt files – we will discuss the details in class.

Try to write a general rule for making .csv (comma-separated value) files out of .tsv (tab-separated value files). As an example, the ENROLMENTS-2017 file is a tab-separated-value file, and the weather data for the project is a comma-separated-value file.

Create or copy a .tsv file into your working directory and test your rule – does it work? (If you use the ENROLMENTS-2017 file, you will need to rename it, to give it the extension .tsv.)

Rules with no prerequisite

A rule needn’t have a prerequisite – these represent things we can make “from scratch”.

For instance, add the following rule to your Makefile:

good_advice.txt:
  echo 'Work hard, be good to your mother' > good_advice.txt

and test that it does indeed create the file it says it does. Now try creating a rule for “creating” a weather data file, by fetching files from the BOM website (http://www.bom.gov.au/climate/dwo/IDCJDW6111.latest.shtml).