3

I have a makefile containing an include statement. I have no control over the content of the included makefile. Still, I want to be able to add some pre-processing steps before "some" (not all) of the targets. Consider the following example:

install:
      @echo "install target"

include othermakefile.mk

othermakefile.mk contains install target as well. If I run this script, make gives a warning and ignores the first install target.

Javad
  • 313
  • 3
  • 13

1 Answers1

3

Nothing easier:

install: pre_install

.PHONY: pre_install
pre_install:
    do preinstallation things

include othermakefile.mk

EDIT:
If you want to run pre_install before install or any of its preqs, here is a way. It's crude and ugly, but it does the job:

install: pre_install
    $(MAKE) -f othermakefile.mk $@

.PHONY: pre_install
pre_install:
    do preinstallation things

Note that this will not necessarily rebuild all prerequisites of install, so some of them may remain in their old states and not reflect the effects of pre_install. If that's not good enough, and you want pre_install before all of the other prerequisites, you can add an option flag:

install: pre_install
    $(MAKE) --always-make -f othermakefile.mk $@

Now Make will assume that all targets are out of date, and rebuild from the ground up (after pre_install).

Beta
  • 96,650
  • 16
  • 149
  • 150
  • 1
    +1. But is it guaranteed that Make will process `install`s dependencies in top-to-bottom order? i.e. If `othermakefile.mk` contains `install: foobar`, is it guaranteed that `pre_install` gets processed before `foorbar`? – Oliver Charlesworth Jun 20 '12 at 17:51
  • @OliCharlesworth: no, there's no such guarantee; Make will build `pre_install` before `install` (which is how I read the question), but not necessarily before `install`'s other prerequisites. There *is* a way to put it ahead of all other targets, but it's more drastic. – Beta Jun 20 '12 at 18:02
  • Can you please tell me how I can *put it ahead of all other prerequisites*? – Javad Jun 20 '12 at 18:21