0
votes

I am studying a Makefile obtained from a compiler course project. Only a part of it is pasted here.

# Retain intermediate bitcode files
.PRECIOUS: %.bc

# The default target builds the plugin
plugin:
    make -C lib/p1

# create .bc from source
%.bc:   %.c
    clang -emit-llvm -O0 -c $*.c -o $*.bc

# run printCode on a .bc file
%.printCode: %.bc plugin
    opt -load Debug/lib/P1.so -printCode $*.bc 

As you see, the target 'plugin' has no dependencies, which, if I understand correctly, should mean that its recipe never runs (unless it is declaared as a phony target, which is not the case here)

However, when I type 'make printCode', (printCode is the last target in the list) the plugin target does execute. How is this made possible? Is there some implicit rule stating that the first target of a Makefile is regarded as a phony target, such as 'all'?

1

1 Answers

1
votes

You've got things a little backward.

A rule like the plugin rule can run. You can run it by executing 'make plugin', or 'make' if it's the default target (which it is in this case by virtue of being the first), or if it is a prerequisite of another target that must be built.

I'm not sure exactly what happens when you 'make printCode', since you are showing us only part of the makefile and there is no rule that fits, but judging by this rule:

%.printCode: %.bc plugin
    opt -load Debug/lib/P1.so -printCode $*.bc 

I'd guess that the printCode rule depends on either plugin or something like foo.printCode that depends on plugin. So Make sees that plugin is a prerequisite, sees that no such file exists, and determines therefore that plugin must be built. It then looks for a rule to build plugin, finds it and runs it.