How to specify makefile target in another directory?

I am trying to understand why I cannot specify the target as being in another directory.

I am trying to generate some php classes from json files, but I changed the line that php actually generates for a fictitious statement echo, so someone else can just copy the paste and see if they feel very generous.

Two more things can be added to the directory containing the makefile:

  • Dummy dependency file called PatientDbPath.json
  • Called directory

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 a 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 this, Make tells me make: Nothing to be done for 'out/PatientDbPath.php' , although the PatientDbPath.php file is not in the out directory.

So, I thought it was something with the suffix rules, and I created a third make file.

 out/PatientDbPath.php: PatientDbPath.json echo "Whatever" > out/PatientDbPath.php clean: rm out/PatientDbPath.php 

This works well, like the first. Can someone see what I am doing wrong in my second makefile?

Thanks.

+4
source share
2 answers

There are two errors in your wildcard rule (.json β†’ .php): one conceptual and one practical.

A conceptual error is that this rule is how to convert arbitrary json files (say foo.json) to arbitrary but named .php files (say json.php). However, your rule is specific for PatientDbPth.php infection. The best rule would be

 .json.php: echo "HelloMe" > $@ 

where $@ is a variable indicating the output file.

It also shows a practical problem: these rules are really limited to files of the same name. If you have a rule translating .json to .php and you have foo.php and you want bar.php then the substitution rule will not match. Reconciliation includes the entire file name, so the substitution rule does not match when you request the output /PatientDbPath.php. This leaves you with a rule that has no effect, therefore, it makes information that nothing should be done.

If this is the only file you want to convert, it is best to write it the way you did in the third file. If you have a lot of json files and you all want to convert them to / *. Php, and you use GNU make, you can use GNU make generalized wildcard rules:

 out/%.php: %.json action 
+3
source

There is another way to do the same, perhaps more elegantly:

 $(JSON_FILES): out/%.php: %.json action 

This will create a rule for each file specified in JSON_FILES , so if it was defined JSON_FILES= foo.json bar.json , this would be equivalent:

 out/foo.php: foo.json action out/bar.php: bar.json action 

You can then use $(wildcard *.json) to set this value if you really wanted all .json files, or set it manually if the rule is unique to several.

+2
source

All Articles