Professional Documents
Culture Documents
Make Utility
CS330E – Elements of Software Engineering I
Dr. Fares Fraij
On-line resources
target: dependencies
action
target: dependencies
action
Use case for executing the command “make target”:
When there are dependencies:
When one of the dependencies is changed, action will be
invoked to produce target.
When there are NO dependencies: the command will run
the corresponding action(s) from the command line.
How does make work (use case)?
target: dependencies
action
def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)
$ python Avg.py
The average of 1, 2, and 3 is 2.0
In class practice
On your machine, create a Python file “Avg.py” that contains the following.
def avg(marks):
assert len(marks) != 0
return sum(marks)/len(marks)
To redirect the standard output stream to the file “Output.txt”, we use the operator “>”
(it will not be visible in the terminal)
Remove Output.txt
$ rm Output.txt
Aliasing a command using a makefile
Now, we are going to alias the command “python Avg.py >
Output.txt” using a makefile.
In the same folder where “Avg.py” is, create a “makefile” that
has the following.
Output.txt:
@echo "Creating Output.txt"
python Avg.py > Output.txt
Output.txt:
@echo “Creating Output.txt"
python Avg.py > Output.txt
$ cat Output.txt
The average of 1, 2, and 3 is 2.0
Make interprets the rule to mean "execute Output.txt target to create the file
named Output.txt ". Since the “Output.txt” is already there, and its dependencies
didn't change (in our case there are no dependencies), nothing will be done.
Aliasing a command using a makefile
.PHONY: Output.txt
Output.txt:
@echo "creating Output.txt"
python Avg.py > Output.txt
Aliasing a command using a makefile
.PHONY: Output.txt
Output.txt:
@echo "creating Output.txt"
python Avg.py > Output.txt
create:
@echo "creating newfile.txt"
touch newfile.txt
clean:
@echo "deleteing Output.txt and newfile.txt"
rm Output.txt
rm newfile.txt
Aliasing a command using a makefile
Now, you can make any of these targets. For example, you can run
make on each target as follows.
$ make Output.txt
$ make create
$make clean
But what will happen if you run the make command without specifying
any target?
$ make
Aliasing a command using a makefile
.DEFAULT_GOAL := create
Aliasing a command using a makefile
If you have more than one file to be considered as the default goal,
you can place a target at the beginning of the makefile and specify
these files as dependencies.
Output.txt: Avg.py
python Avg.py > Output.txt
cat output.txt
https://opensource.com/article/18/8/what-how-makefile