How To A Recreate A Makefie Folliwing Failed Make Clean Command
Makefiles
Overview
Teaching: 30 min
Exercises: 10 minQuestions
How do I write a unproblematic Makefile?
Objectives
Recognize the key parts of a Makefile, rules, targets, dependencies and deportment.
Write a uncomplicated Makefile.
Run Make from the vanquish.
Explain when and why to marker targets equally
.PHONY.Explain constraints on dependencies.
Create a file, chosen Makefile, with the following content:
# Count words. isles.dat : books/isles.txt python countwords.py books/isles.txt isles.dat This is a build file, which for Make is called a Makefile - a file executed past Make. Annotation how it resembles 1 of the lines from our shell script.
Let us go through each line in plow:
-
#denotes a comment. Any text from#to the stop of the line is ignored by Make. -
isles.datis a target, a file to be created, or built. -
books/isles.txtis a dependency, a file that is needed to build or update the target. Targets can have zero or more than dependencies. - A colon,
:, separates targets from dependencies. -
python countwords.py books/isles.txt isles.datis an action, a control to run to build or update the target using the dependencies. Targets can have zero or more than deportment. These actions form a recipe to build the target from its dependencies and can be considered to be a trounce script. - Deportment are indented using a single TAB grapheme, not 8 spaces. This is a legacy of Brand's 1970's origins. If the difference between spaces and a TAB character isn't obvious in your editor, attempt moving your cursor from i side of the TAB to the other. It should jump four or more spaces.
- Together, the target, dependencies, and actions form a rule.
Our rule to a higher place describes how to build the target isles.dat using the action python countwords.py and the dependency books/isles.txt.
Information that was implicit in our shell script - that we are generating a file chosen isles.dat and that creating this file requires books/isles.txt - is now made explicit past Make's syntax.
Let's first ensure we start from scratch and delete the .dat and .png files we created earlier:
By default, Brand looks for a Makefile, called Makefile, and we can run Make as follows:
By default, Make prints out the actions information technology executes:
python countwords.py books/isles.txt isles.dat If nosotros run across,
Makefile:three: *** missing separator. End. and so nosotros accept used a space instead of a TAB characters to indent one of our actions.
Permit's come across if we got what we expected.
The first five lines of isles.dat should look exactly like before.
Makefiles Do Non Have to be Called
MakefileWe don't have to telephone call our Makefile
Makefile. Withal, if we call it something else we demand to tell Brand where to find it. This we can do using-fflag. For example, if our Makefile is namedMyOtherMakefile:$ make -f MyOtherMakefileSometimes, the suffix
.mkwill be used to identify Makefiles that are not calledMakefilee.grand.install.mk,common.mketc.
When we re-run our Makefile, Make now informs us that:
make: `isles.dat' is up to engagement. This is because our target, isles.dat, has now been created, and Make will non create information technology once again. To see how this works, let's pretend to update one of the text files. Rather than opening the file in an editor, we can employ the shell bear on command to update its timestamp (which would happen if we did edit the file):
If we compare the timestamps of books/isles.txt and isles.dat,
$ ls -l books/isles.txt isles.dat then we see that isles.dat, the target, is now older than books/isles.txt, its dependency:
-rw-r--r-- ane mjj Administ 323972 Jun 12 ten:35 books/isles.txt -rw-r--r-- i mjj Administ 182273 Jun 12 09:58 isles.dat If we run Make again,
so it recreates isles.dat:
python countwords.py books/isles.txt isles.dat When it is asked to build a target, Brand checks the 'last modification time' of both the target and its dependencies. If whatever dependency has been updated since the target, so the actions are re-run to update the target. Using this approach, Make knows to only rebuild the files that, either direct or indirectly, depend on the file that changed. This is chosen an incremental build.
Makefiles as Documentation
By explicitly recording the inputs to and outputs from steps in our analysis and the dependencies betwixt files, Makefiles human action as a type of documentation, reducing the number of things we take to retrieve.
Let'southward add another dominion to the cease of Makefile:
abyss.dat : books/abyss.txt python countwords.py books/abyss.txt abyss.dat If we run Make,
and then we go:
brand: `isles.dat' is up to date. Zip happens because Make attempts to build the first target it finds in the Makefile, the default target, which is isles.dat which is already upwards-to-date. We demand to explicitly tell Make we want to build abyss.dat:
Now, nosotros get:
python countwords.py books/abyss.txt completeness.dat "Upwardly to Date" Versus "Nothing to exist Done"
If we enquire Make to build a file that already exists and is up to date, so Make informs united states of america that:
make: `isles.dat' is up to date.If we ask Make to build a file that exists but for which there is no dominion in our Makefile, so we get message similar:
make: Nil to be washed for `countwords.py'.
upwardly to appointmentmeans that the Makefile has a rule with one or more than deportment whose target is the name of a file (or directory) and the file is up to date.
Nada to exist doneways that the file exists merely either :
- the Makefile has no dominion for it, or
- the Makefile has a dominion for it, but that dominion has no actions
We may want to remove all our data files then we can explicitly recreate them all. We can innovate a new target, and associated rule, to do this. We volition call it make clean, as this is a mutual proper name for rules that delete auto-generated files, like our .dat files:
This is an example of a dominion that has no dependencies. clean has no dependencies on any .dat file equally it makes no sense to create these just to remove them. We just want to remove the data files whether or non they exist. If we run Make and specify this target,
then we become:
There is no actual thing built chosen make clean. Rather, information technology is a curt-hand that nosotros tin use to execute a useful sequence of actions. Such targets, though very useful, can atomic number 82 to problems. For example, let us recreate our information files, create a directory chosen clean, and then run Brand:
$ make isles.dat completeness.dat $ mkdir make clean $ brand make clean We get:
make: `clean' is upwards to date. Make finds a file (or directory) chosen make clean and, as its clean rule has no dependencies, assumes that make clean has been congenital and is up-to-engagement and so does not execute the rule's deportment. Equally we are using clean equally a short-paw, we need to tell Make to ever execute this rule if we run brand clean, past telling Brand that this is a phony target, that it does non build anything. This we do by mark the target as .PHONY:
.PHONY : clean clean : rm -f *.dat If we run Brand,
then nosotros get:
Nosotros can add together a similar command to create all the data files. We can put this at the top of our Makefile so that it is the default target, which is executed by default if no target is given to the make command:
.PHONY : dats dats : isles.dat abyss.dat This is an example of a dominion that has dependencies that are targets of other rules. When Brand runs, information technology volition bank check to come across if the dependencies exist and, if non, volition see if rules are bachelor that will create these. If such rules be it will invoke these first, otherwise Make will raise an error.
Dependencies
The order of rebuilding dependencies is arbitrary. Y'all should not assume that they will exist congenital in the order in which they are listed.
Dependencies must class a directed acyclic graph. A target cannot depend on a dependency which itself, or one of its dependencies, depends on that target.
This rule is as well an case of a rule that has no actions. It is used purely to trigger the build of its dependencies, if needed.
If nosotros run,
and so Brand creates the data files:
python countwords.py books/isles.txt isles.dat python countwords.py books/completeness.txt completeness.dat If nosotros run dats once more, then Make volition see that the dependencies (isles.dat and abyss.dat) are already up to appointment. Given the target dats has no actions, there is nothing to be done:
make: Nothing to be done for `dats'. Our Makefile at present looks similar this:
# Count words. .PHONY : dats dats : isles.dat abyss.dat isles.dat : books/isles.txt python countwords.py books/isles.txt isles.dat completeness.dat : books/abyss.txt python countwords.py books/completeness.txt abyss.dat .PHONY : make clean make clean : rm -f *.dat The post-obit figure shows a graph of the dependencies embodied inside our Makefile, involved in edifice the dats target:
Write Two New Rules
- Write a new rule for
last.dat, created frombooks/last.txt.- Update the
datsrule with this target.- Write a new rule for
results.txt, which creates the summary table. The rule needs to:
- Depend upon each of the three
.datfiles.- Invoke the action
python testzipf.py completeness.dat isles.dat terminal.dat > results.txt.- Put this rule at the peak of the Makefile so that it is the default target.
- Update
cleanso that it removesresults.txt.The starting Makefile is here.
Solution
See this file for a solution.
The following figure shows the dependencies embodied within our Makefile, involved in building the results.txt target:
Key Points
Use
#for comments in Makefiles.Write rules as
target: dependencies.Specify update actions in a tab-indented cake nether the rule.
Utilize
.PHONYto mark targets that don't represent to files.
Source: https://swcarpentry.github.io/make-novice/02-makefiles/
Posted by: benefieldateres.blogspot.com

0 Response to "How To A Recreate A Makefie Folliwing Failed Make Clean Command"
Post a Comment