4

I have a recipe in a Makefile which should always be executed, without any condition and no matter what (and if a) target is specified at the invocation of make.

Executing this recipe will have an effect on some other rules' preconditions, so I'd like this recipe to be executed before make considers any other rules in the Makefile.

Since executing this recipe will not always cause other rules' preconditions to be met (i.e. such that make considers them outdated), I can't put this recipe in a .PHONY target and have this as a precondition to other targets, because make would then consider all dependent targets as outdated, even if they're not.

I thought of defining a dummy variable which gets assigned the result of a ${shell my ; recipe ; commands} but that doesn't seem to execute reliably.

FriendFX
  • 2,929
  • 1
  • 34
  • 63
  • 2
    Possible duplicate of [Force Makefile to execute script before building targets](https://stackoverflow.com/questions/2122602/force-makefile-to-execute-script-before-building-targets) – tripleee Oct 09 '18 at 13:54
  • Possible duplicate of [How can I force to run a command all the time](https://stackoverflow.com/q/26226106/608639) – jww Apr 18 '19 at 20:25

1 Answers1

1

There is likely a more elegant answer, but a phony target that is a dependency for every other target in your makefile should satisfy your requirements.

Rich
  • 640
  • 5
  • 12
  • As I said in the question, I can't do that because then it would always re-make even when it isn't required. – FriendFX Oct 10 '14 at 04:29
  • What is the artifact of the operation?Can you not put conditionals in the steps to avoid redundant action? Or perhaps use a real file to indicate the operation has occurred? – Rich Oct 10 '14 at 04:31
  • The operation is mainly making a copy of a bunch of files, which I don't know in advance. Only new files are copied and if they are newer than one of my targets, it will be considered outdated by make. So the copy operation should *always* be carried out, to check for the need to re-build the target. – FriendFX Oct 10 '14 at 04:44
  • Interesting. Odd that you'd want to just copy files. I assume they are modified in some way by the make operations? Or you just want to collect them in some common location? But you don't have a way to make a pattern rule for the copy? – Rich Oct 10 '14 at 04:47
  • Its a little more complicated, but the copy operation is the most important bit. I could probably come up with some complicated code to check if there are newer files to be copied and then (and only then) copy them and make the target think it's outdated. I was just hoping for a simple solution to "just run this code before doing anything else" when calling `make`. – FriendFX Oct 10 '14 at 05:39
  • If you post your Makefile perhaps we can make something of it. – Rich Oct 10 '14 at 06:44