I have a makefile such as :
A : B
echo "made A"
B : D
echo "made B"
Here, B exists but D doesn’t (sometimes, it does, sometimes it doesn’t)
Now if I execute make, it shouts at me :
make: *** No rule to make target 'D', needed by 'B'. Stop.
Is there any way to behave correctly i.e. :
- B and D don’t exist : fail
- B exists, D does and is newer than B , apply recipe
- B exists, D exists but is older : don’t do anything
- if B exists and D doesn’t : don’t rebuild B (you can’t) but you can use B to build A
Using an empty line with D as target would always execute recipe to build B, but without D it cannot, so it’s not wanted !
Is there any way ?
I found the solution of using an empty rule
D :
at the end, so that missing D will not fail. So now all is OK except B is redone if D is missing, which can be tested in B recipe :
B : D
if [ D ] ; then echo "doing B with D"; fi
But it really seems hackish …
You can get around the re-generation of B from an older D by marking B as intermediate:
You can then remove the empty rule for D, and your four criteria for “correct” behaviour are matched.