Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Makefile rule only once

Tags:

linux

makefile

I have a Makefile rule like the following:

%.b : %.a
    [run some commands...]

I'd like to rewrite my Makefile such that all conversions from %.a to %.b are handled by a single execution of the commands rather than once for every file.

I've tried the following:

%.b : someDummyFile

someDummyFile : 
    [run some commands]

The problem is that the rule for creating someDummyFile gets executed even when the first rule doesn't request it, leading to undesirable behavior. I'm guessing that rules without dependencies get executed all the time even when their targets are not used by some other rule?

What am I missing here?


In trying to debug this problem, I removed all references to the second rule. For instance, I insert the following rule into an existing Makefile, why does it get executed even when no other rules make reference to it?

HELLOWORLD :
    echo "# HELLO WORLD! THE NEXT LINE WILL TRIGGER AN ERROR"
    DFASDA
like image 455
Eugenio De Hoyos Avatar asked Jul 30 '12 23:07

Eugenio De Hoyos


1 Answers

Here's a quick example that I think does what you're looking for:

all: file1.x file2.x

%.x: doWork
    :

doWork:
    touch file1.x
    touch file2.x

The doWork rule will get run once, and then the : (no-op) in the %.x rule gets run twice. You do need the :, or at least something as a recipe for that rule, or else you'll get an error about there not being a rule to make the .x file. That's a side effect of the syntax used to cancel a pattern rule being a recipe-less rule.

like image 115
Carl Norum Avatar answered Sep 28 '22 08:09

Carl Norum