Reputation: 92314
I am trying to figure out why I can't specify a target as being in different directory.
I am trying to generate some php classes from json files, but I've changed the line that actually generates the php to a dummy echo statement, so somebody else can just copy paste and test if they feel very generous.
There are two other things to add to the directory containing the makefile:
If I have a makefile with the following:
.SUFFIXES: .php .json
.json.php:
echo "HelloMe" > PatientDbPath.php
PatientDbPath.php: PatientDbPath.json
clean:
$(RM) PatientDbPath.php
Then everything works when I run make; PatientDbPath.php is correctly created and the next time I run make, I get the message make: 'PatientDbPath.php' is up to date.
However, I want to generate the php file in a separate directory, so I updated my makefile to the following:
.SUFFIXES: .php .json
.json.php:
echo "HelloMe" > out/PatientDbPath.php
out/PatientDbPath.php: PatientDbPath.json
clean:
$(RM) out/PatientDbPath.php
As soon as I do that, Make tells me make: Nothing to be done for 'out/PatientDbPath.php'
even though there is no file PatientDbPath.php in the out directory.
So I thought maybe it was something with the suffix rules and I created a third makefile.
out/PatientDbPath.php: PatientDbPath.json
echo "Whatever" > out/PatientDbPath.php
clean:
rm out/PatientDbPath.php
This one works well, like the first one. Can anybody see what I'm doing wrong in my 2nd makefile?
Thanks.
Upvotes: 6
Views: 12881
Reputation: 12704
There's another way to do the same thing, perhaps more elegantly:
$(JSON_FILES): out/%.php: %.json
action
This will create a rule for every file listed in JSON_FILES
, so if it were defined JSON_FILES= foo.json bar.json
, then this would be equivalent to:
out/foo.php: foo.json
action
out/bar.php: bar.json
action
You could then use $(wildcard *.json)
to set that value if you really wanted all the .json files, or set it manually if the rule is unique to just a few.
Upvotes: 5
Reputation: 127527
There are two errors in your wildcard rule (.json -> .php), one conceptual and one practical.
The conceptual error is that this is a rule how to convert arbitrary json files (say, foo.json) to arbitrary-but-same-named .php files (say, json.php). Yet, your rule is specific for gnerating PatientDbPth.php. A better rule would be
.json.php:
echo "HelloMe" > $@
where $@
is a variable naming the output file.
This also shows the practical problem: These rules are really restricted to same-named files. If you have a rule translating .json in .php, and you have foo.php, and you want bar.php, then the wildcard rule won't match. Matching involves the entire file name, so the wildcard rule doesn't match when you ask for out/PatientDbPath.php. This leaves you with the rule that has no action, hence make infers that nothing is to be done.
If this is the only file you want to convert, it's best to write it the way you did in the third file. If you have many json files, and you all want to convert them to out/*.php, and you are using GNU make, you can use GNU make's generalized wildcard rules:
out/%.php: %.json
action
Upvotes: 4