Lab Session 6: Automation and Make

  • Statistics 159/259, Spring 2022

  • Prof. F. Pérez and GSI F. Sapienza, Department of Statistics, UC Berkeley.

  • 03/14/2022

Useful links:

Acknowledgment: The contents we are following for this course is based on the amazing tutorial about Automation and Make created by The Carpentries

Make is called a build tool: it builds files, plots, papers, etc. Today we are going to do everything from the the shell. If you haven’t done the setup for the tutorial during last lecture, please follow the setup page in order to start working with the contents of the tutorial.

We will start from the following version of our Makefile created inside the folder make-lesson provided in the tutorial.

from IPython import display
display.Code('Makefile')
# Generate summary table.
results.txt : testzipf.py isles.dat abyss.dat last.dat
	python $< *.dat > $@

# Count words.
.PHONY : dats
dats : isles.dat abyss.dat last.dat

isles.dat : books/isles.txt countwords.py
	python countwords.py $< $@

abyss.dat : books/abyss.txt countwords.py
	python countwords.py $< $@

last.dat : books/last.txt countwords.py
	python countwords.py $< $@

.PHONY : clean
clean :
	rm -f *.dat
	rm -f results.txt

Before continuing, be sure to know the answer to the following questions.

  1. What is a phony target? When and how to use them?

  2. What does $^, $< and $@ designate?

  3. What happen is we modify the Python script we used to generate dat?

  4. What does it means to do a dry run of your make file?

  5. How do you execute a make file? Does the name of the file needs to be Makefile?

  6. Try to make a schematic plot of the tree of dependencies of this makefiles.

Part 1: Recap

  1. Add to your Makefile the command to create a plot for the different books.

  2. Replace the repeated commands by using a pattern rule.

    1. What does % designates?

    2. What does $* do and how to use it?

    3. When can and cannot use or the other?

Part 2: Variables and functions

All this exercises can be found in the Variables and Functions chapters.

  1. Update Makefile so that the %.dat rule references the variables COUNT_SRC and COUNT_EXE. Then do the same for the testzipf.py script and the results.txt rule, using ZIPF_SRC and ZIPF_EXE as variable names.

  2. Move the varaible declaration to a new file config.mk that you import into Makefiles. What happens when you touch config.mk and then make again? Why? Try changing LANGUAGE=python to LANGUAGE=python3 to see if there is any difference.

  3. Follow the functions tutorial to simplify all the unnecessary explicit syntax in Makefile. This includes the use of both function wildcard and patsubst. If you feel adventurous, you can add more books to books and test Zipf’s Law.

  4. Explore how to add documentation to the makefiles.

Part 3: Make graduation

The tutorial we follow has a final exercises for you to try. Test how much have you learned about makefiles and try to do it! You can use as an starting point the end of the previous tutorial.