views:

40

answers:

2
xpi_built := $(build_dir)/$(install_rdf) \
             $(build_dir)/$(chrome_manifest) \
             $(chrome_jar_file) \
             $(default_prefs)

xpi_built_no_dir := $(subst $(build_dir)/,,$(xpi_built))

$(xpi_file): $(build_dir) $(xpi_built)
 @echo "Creating XPI file."
 cd $(build_dir); $(ZIP) ../$(xpi_file) $(xpi_built_no_dir)
 @echo "Creating XPI file. Done!"

$(build_dir)/%: %
 cp -f $< $@

$(build_dir):
 @if [ ! -x $(build_dir) ]; \
  then \
    mkdir $(build_dir); \
  fi

can anyone explain me this makefile part? particularly interested in

  • $(build_dir)/%: % as well as $< and $@ directives
  • two labels $(build_dir) exists, I guess both are executed, but in which order?
A: 

See Pattern Rules and Automatic Variables in the GNU make documentation. The first rule matches files inside $(build_dir), not $(build_dir) itself. $< expands to the list of prerequisites of the current rule, $@ is the target for the current rule.

Lukáš Lalinský
not quite right -- `$<` expands to (just) the first prerequisite. `$^` expands to all the prerequisites
Chris Dodd
+2  A: 
$(build_dir)/%: %
    cp -f $< $@

This is a static pattern rule which uses automatic variables in its command; $< expands to the leftmost prerequisite, $@ expands to the target. If you try to make $(build_dir)/foo (whatever $(build_dir) is), Make will treat this rule as

$(build_dir)/foo: foo
    cp -f foo $(build_dir)/foo

The next rule,

$(build_dir):
    @if [ ! -x $(build_dir) ]; \
  then \
  mkdir $(build_dir); \
  fi

is for $(build_dir) itself, and is unnecessarily complicated. It says "if $(build_dir) doesn't exist, then mkdir it", and it could be written this way:

$(build_dir):
    mkdir $@

It looks as if your primary target is $(xpi_file):

$(xpi_file): $(build_dir) $(xpi_built)

So Make will first make $(build_dir) (if necessary), then the members of the list %(xpi_built), which includes a couple of things of the form $(build_dir)/%. Once those are done, it will execute the commands of this rule: it will cd into $(build_dir), zip some things up, and echo a couple of messages.

Beta
@BetaThanks for detailed explanation. Do you know any good e-book for make?
Michael
@Michael e-book? Lukáš Lalinský has cited the GNUMake manual, which is the best documentation I know of. There are also "Advanced Auto-Dependency Generation", which is, well, advanced, and "Recursive Make Considered Harmful", a popular reference which I disagree with.
Beta