Makefiles are great for a lot of things and are generally used to build executable programs and libraries from source code but they are not limited to only that. This talk will probably convince you to start adding makefiles in your Python or any other programming project.
software engineering Chapter 5 System modeling.pptx
Makefile for python projects
1. SARAO Tech Talk Tuesday 16th, February 2021
PRESENTER: Mpho Mphego
Why you should consider adding a
‘Makefile’ into your *Python project
2. Objectives
● A short intro on Makefiles.
● Who needs Makefiles, I use Shell
scripts!
● How Makefiles are structured.
● Why bother using Makefile?
● Demo
● Some resources to checkout.
3. A short intro on Makefiles.
● According to wikipedia, A Makefile is a file which contains a set of
directives used by a make build automation tool to generate target(s).
Sources:
● https://en.wikipedia.org/wiki/Makefile
● https://en.wikipedia.org/wiki/Make_(software)
● https://xkcd.com/149/
4. A short intro on Makefiles.
● According to wikipedia, A Makefile is a file which contains a set of
directives used by a make build automation tool to generate target(s).
● First introduces 44 years ago, and till this day you can still find it in
most programming projects. It’s widely used in Unix and Unix-like OS
for automation.
Sources:
● https://en.wikipedia.org/wiki/Makefile
● https://en.wikipedia.org/wiki/Make_(software)
● https://xkcd.com/149/
5. A short intro on Makefiles.
● According to wikipedia, A Makefile is a file which contains a set of
directives used by a make build automation tool to generate target(s).
● First introduces 44 years ago, and till this day you can still find it in
most programming projects. It’s widely used in Unix and Unix-like OS
for automation.
● Generally used to build executable programs and libraries from source
code.
Sources:
● https://en.wikipedia.org/wiki/Makefile
● https://en.wikipedia.org/wiki/Make_(software)
● https://xkcd.com/149/
6. A short intro on Makefiles.
● According to wikipedia, A Makefile is a file which contains a set of
directives used by a make build automation tool to generate target(s).
● First introduces 44 years ago, and till this day you can still find it in
most programming projects. It’s widely used in Unix and Unix-like OS
for automation.
● Generally used to build executable programs and libraries from source
code.
● Besides building programs, it can also manage any project where some
files must be updated automatically whenever other files change.
Sources:
● https://en.wikipedia.org/wiki/Makefile
● https://en.wikipedia.org/wiki/Make_(software)
● https://xkcd.com/149/
7. A short intro on Makefiles.
● According to wikipedia, A Makefile is a file which contains a set of
directives used by a make build automation tool to generate target(s).
● First introduces 44 years ago, and till this day you can still find it in
most programming projects. It’s widely used in Unix and Unix-like OS
for automation.
● Generally used to build executable programs and libraries from source
code.
● Besides building programs, it can also manage any project where some
files must be updated automatically whenever other files change.
● FYI: make is not limited to compiling C/C++ and programs
Sources:
● https://en.wikipedia.org/wiki/Makefile
● https://en.wikipedia.org/wiki/Make_(software)
● https://xkcd.com/149/
8. A short intro on Makefiles.
● According to wikipedia, A Makefile is a file which contains a set of
directives used by a make build automation tool to generate target(s).
● First introduces 44 years ago, and till this day you can still find it in
most programming projects. It’s widely used in Unix and Unix-like OS
for automation.
● Generally used to build executable programs and libraries from source
code.
● Besides building programs, it can also manage any project where some
files must be updated automatically whenever other files change.
● FYI: make is not limited to compiling C/C++ and programs
Sources:
● https://en.wikipedia.org/wiki/Makefile
● https://en.wikipedia.org/wiki/Make_(software)
● https://xkcd.com/149/
DISCLAIMER: I AM NOT A “GNU MAKE” EXPERT!
9. Who needs Makefiles, I use Shell
scripts!
● Functionality that the “Make” offers is far superior to a
regular Bash script.
10. Who needs Makefiles, I use Shell
scripts!
● Functionality that the “Make” offers is far superior to a
regular Bash script.
● Scripts are usually a sequence of instructions (procedural
paradigm). However, Makefile’s lets us specify
prerequisites and implications (declarative paradigm).
11. Who needs Makefiles, I use Shell
scripts!
● Functionality that the “Make” offers is far superior to a
regular Bash script.
● Scripts are usually a sequence of instructions (procedural
paradigm). However, Makefile’s lets us specify
prerequisites and implications (declarative paradigm).
● With a Makefile you could simply specify “rules” for
compilation, to deal with useful procedures for code
maintenances for example or decide when and if we need to
rebuild a file/executable.
12. Who needs Makefiles, I use Shell
scripts!
● Functionality that the “Make” offers is far superior to a
regular Bash script.
● Scripts are usually a sequence of instructions (procedural
paradigm). However, Makefile’s lets us specify
prerequisites and implications (declarative paradigm).
● With a Makefile you could simply specify “rules” for
compilation, to deal with useful procedures for code
maintenances for example or decide when and if we need to
rebuild a file/executable.
● Minimal rebuilds are possible in “Makefile” whereas the bash
script requires a lot of time and effort to achieve the same.
13. Who needs Makefiles, I use Shell
scripts!
● Functionality that the “Make” offers is far superior to a
regular Bash script.
● Scripts are usually a sequence of instructions (procedural
paradigm). However, Makefile’s lets us specify
prerequisites and implications (declarative paradigm).
● With a Makefile you could simply specify “rules” for
compilation, to deal with useful procedures for code
maintenances for example or decide when and if we need to
rebuild a file/executable.
● Minimal rebuilds are possible in “Makefile” whereas the bash
script requires a lot of time and effort to achieve the same.
● It’s a very old tool with somewhat old syntax, but still works
well
14. How Makefiles are structured.
● A makefile consists of “rules” in the following structure:
target: dependencies
<tab> system commands(s)
15. How Makefiles are structured.
Example
$ make venv
Target
$ cat Makefile
venv: ...
...
● target is a name of an action to carry out (eg: venv)
16. How Makefiles are structured.
Example
$ make venv
Target, prerequisite
$ cat Makefile
venv: clean
...
● target is a name of an action to carry out (eg: venv)
● dependency/prerequisite an action that needs to be run
before the current target can be run.
17. Example
$ make venv
Target, prerequisite and recipe
$ cat Makefile
venv: clean
@python -m virtualenv -p 3 .venv
● target is a name of an action to carry out (eg: venv)
● dependency/prerequisite an action that needs to be run
before the current target can be run.
● system command(s)/recipe an action that make carried out,
can be more than one command and indentation must consist
of a single <tab> character
How Makefiles are structured.
18. SHELL = /bin/sh
OBJS = main.o factorial.o hello.o
CFLAG = -Wall -g
CC = gcc
INCLUDE =
LIBS = -lm
hello:${OBJ}
${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}
clean:
-rm -f *.o core *.core
.cpp.o:
${CC} ${CFLAGS} ${INCLUDES} -c $<
This is a typical example of the Makefile for compiling a C/C++ hello program. This program
consists of three files main.cpp, factorial.cpp and hello.cpp.
How Makefiles are structured.
19. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
Why bother using Makefile?
20. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
Why bother using Makefile?
21. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
● Problems with this approach:
Why bother using Makefile?
22. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
● Problems with this approach:
○ One can easily make mistakes (eg. ports or volume mapping)
Why bother using Makefile?
23. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
● Problems with this approach:
○ One can easily make mistakes (eg. ports or volume mapping)
○ It’s time consuming to have to type or copy from file each time.
Why bother using Makefile?
24. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
● Problems with this approach:
○ One can easily make mistakes (eg. ports or volume mapping)
○ It’s time consuming to have to type or copy from file each time.
○ Affects your efficiency, the list goes on…
Why bother using Makefile?
25. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
● Problems with this approach:
○ One can easily make mistakes (eg. ports or volume mapping)
○ It’s time consuming to have to type or copy from file each time.
○ Affects your efficiency, the list goes on…
● What if this process could be simplified by just running a command like:
Why bother using Makefile?
26. ● If like me, you are probably tired of having to manually enter these type of commands (or have
them saved somewhere in some file), everytime you need to run a script in a container:
$ docker run -ti --rm --volume ${PWD}:/app --port 3000:8080
--env DISPLAY=${DISPLAY}
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw"
--device /dev/snd
--device /dev/video0
name/tag bash -c "python main.py --verbose --arg_1 ${ARG_1}
--arg_2 ${ARG_2} --arg_3 ${ARG_3}"
● Problems with this approach:
○ One can easily make mistakes (eg. ports or volume mapping)
○ It’s time consuming to have to type or copy from file each time.
○ Affects your efficiency, the list goes on…
● What if this process could be simplified by just running a command like:
$ make run_in_container
Why bother using Makefile?
28. ● GNU Make (ebook pdf)
● Introduction to Make and Makefile(blog)
● Generic Makefile for your projects(GitHub)
● Automation and Make (Tutorial)
● Youtube (search query)
● Why You Should Add Makefile Into Your Python Project (blog)
● How To Use Makefiles to Automate Repetitive Tasks on an Ubuntu
VPS(Tutorial)
● Using Tox with a Makefile to Automate Python related tasks(blog)
Some resources to checkout.
29. Mpho Mphego
Software Engineer
Email: mmphego@ska.ac.za
Contact information
SKA South Africa, a Business Unit of the National Research Foundation.
We are building the Square Kilometre Array radio telescope (SKA), located in South Africa and eight other African
countries, with part in Australia. The SKA will be the largest radio telescope ever built and will produce science that
changes our understanding of the universe