Lab Session 8: Automation and Make#

  • Statistics 159/259, Spring 2022

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

  • 04/07/2023

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.

0. Setup#

Download the files form the setup page in the Carpentries tutorial. We will start from the following version of our Makefile created inside the folder make-lesson provided in the tutorial.) you will find the data and scripts for this exercise. Add the corresponding files to your JupyterHub session.

We will start working from the following template Makefile:

from IPython import display
display.Code('make-lesson/Makefile')
# Count words.

isles.dat : books/isles.txt
	python countwords.py books/isles.txt isles.dat

abyss.dat : books/abyss.txt
	python countwords.py books/abyss.txt abyss.dat

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

1. Recap#

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 data?

  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.

2. Growing our Makefile#

  1. Add a new .PHONY variable at the top of Makefile called outputs that generates both isles.dat and abyss.dat.

  2. Add more commands for generating last.dat and sierra.dat.

  3. Replace the name of target and dependencies files using the special characters $<, $@.

  4. 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?

  5. Add a clean command that removes all the .dat files.

[Extra] 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.