Allow missing files in GNU make with SECONDARY

2024年8月5日


Assume the following is the dependnecy graph to make our final product F4.

F1 -> F2 -> F3 -> F4

If F1 and F2 are missing or deleted, can we make F4 from F3? Technically yes, but the makefile must be written in a special way. Let’s first try the normal way in Listing .

F2 : F1
	@echo making F2

F3 : F2
	@echo making F3

F4 : F3
	@echo making F4
: A very basic makefile for creating F4

When we have the seed F1, then F2, F3, and all the way to F4 can be created.

$ touch F1
$ make --debug F4
GNU Make 4.4.1
Built for Windows32
Copyright (C) 1988-2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Updating makefiles....
Updating goal targets....
 File 'F4' does not exist.
  File 'F3' does not exist.
   File 'F2' does not exist.
  Must remake target 'F2'.
making F2
  Successfully remade target file 'F2'.
 Must remake target 'F3'.
making F3
 Successfully remade target file 'F3'.
Must remake target 'F4'.
making F4
Successfully remade target file 'F4'.

Then, remove F1 and F2, and leave F3. Listing shows that even though F4 can be technically created from F3, make insists on checking the prerequisites of F3, which are missing right now.

$ rm F*; touch F3
$ ll
total 1
-rw-r--r-- 1 gqqnbig 197121   0 Aug  6 12:12 F3
-rw-r--r-- 1 gqqnbig 197121 364 Aug  6 11:45 makefile
$ make F4
make: *** No rule to make target 'F1', needed by 'F2'.  Stop.
: Even though F3 exits, make is not confident to use F3 to create F4

Intermediate Targets

Intermediate files is a file needed somewhere on way from a source file to target file.[1] Hence, F2 and F3 can be considered intermediate.

In GNU Make, if a file is marked as .INTERMEDIATE, if the file is needed and created for a target by Make, Make will later delete the file when Make exits. A secondary target is a special kind of an intermediate target, in that Make will not delete the file when Make exits.[2] What’s more, secondary files will not be created if their targets exist.

In light of this, if we allow to skip F1 and F2, and only use F3 to create F4, F2 fits more to the definition of secondary files.

In other words, with .SECONDARY : F1, the dependency chain is

F2 -> F3 -> F4
  • As long as the targets of F1 (namely F2) exit, make F2, make F3, and make F4 will all succeed.
  • If F1 is missing and F2 is missing, make F2 will of course fail.
  • If F1 exists, make of any target will succeed.

With .SECONDARY : F2, the dependency chain is

F1 -> F3 -> F4

.SECONDARY : F2 merely removes the node F2 from the dependency chain. It is not removing the subtree of F2. That is, if we only have F3, make F4 will still fail.

$ make --debug F4
GNU Make 4.4.1
Built for Windows32
Copyright (C) 1988-2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Updating makefiles....
Updating goal targets....
 File 'F4' does not exist.
    File 'F1' does not exist.
   Must remake target 'F1'.
make: *** No rule to make target 'F1', needed by 'F2'.  Stop.

Final Words

In your project, if you prefer incrementally building something and deleting materials, you probahaly need .SECONDARY : that makes all targets secondary. Another option is that you can place the raw materials to be deleted in a folder, say raws, and add some pattern matching like .SECONDARY : raws\*.

Plus, you can consider combining with order-only prerequisits (|).[3]

参考资料

  1. brabect1. GNU Make and the effects of intermediate targets. . 2016-10-14 [2024-08-06].
  2. . 10.4 Chains of Implicit Rules. . 2023-02-26 [2024-08-06]. “If you do not want make to create a file merely because it does not already exist, but you also do not want make to automatically delete the file, you can mark it as a secondary file.”
  3. Paul D. Smith. Re: Can make ignore timestamp of prerequisite?. . 2006-02-25 [2024-08-06].