Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU make: add a file as a dependency only if it doesn't exist yet

GNU make has the following option to avoid recompilation:

   -o file, --old-file=file, --assume-old=file
        Do not remake the file file even if it is older than  its  dependen‐
        cies,  and  do  not  remake  anything on account of changes in file.
        Essentially the file is treated  as  very  old  and  its  rules  are
        ignored.

So given the following Makefile:

A : B
    touch A
B : C
    touch B
C :
    touch C

Assuming all files exist at some point, I can run the following commands:

$ touch C
$ make A -o B
make: `A' is up to date.     # Good. C is out-of-date, but A does not get updated.

Question

How can I change my Makefile such that B is always assumed old, but only when rebuilding A?

A : <WHAT_GOES_HERE> B
    touch A
B : C
    touch B
C :
    touch C

I'm specifically looking for the following results:

Result 1: when no files exist yet, make A should create all of them, as before.

$ make A
touch C
touch B
touch A

Result 2: when C is out-of-date, make B should update B, as before. This means we can't have an order-only dependency between B and C (i.e. B : | C). Assuming all files exist again:

$ touch C
$ make B
touch B    # Good. Don't loose this property.

Result 3: when C is out-of-date, make A should be a no-op, without extra flags necessary.

$ touch C
$ make A
make: `A' is up to date.
like image 258
thomie Avatar asked Feb 22 '16 11:02

thomie


1 Answers

In other words, when deciding whether to rebuild A, make should ignore the timestamp of B. In other words, B is not a pre-requisite of A. Just leave B off in A's dependency line.

Reading your answer though, it appears that you want to build B iff it doesn't already exist.

A: $(if $(wildcard B),,B)
    ⋮

or, more directly,

A: $(wildcard B)
    ⋮
like image 166
bobbogo Avatar answered Nov 15 '22 07:11

bobbogo