How to specify a makefile target in a different directory?

10,577

Solution 1

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

Solution 2

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.

Share:
10,577
Ruan Mendes
Author by

Ruan Mendes

Client side/ middle tier web developer. Have programmed in C, C++, C#, Groovy, Java, ActionScript, Lingo, JavaScript, PHP, TypeScript. Basic My language of choice is TypeScript, on the browser or Deno. Technologies I've worked with substantially: HTML, CSS, DOM, AJAX, Angular, React, jQuery, Google Closure Templates, Sencha touch, Ext-JS ASP, PHP, JSP, Struts, Velocity, Node.js, Kohana Windows, Unix, OpenVMS, Solaris Ant, make, maven XML-RPC, RESTful services JSUnit, JUnit, PhpUnit, Karma, Jasmine, js-test-driver, NUnit, YUI tests Selenium, Cucumber, Cypress Grails ASP.NET

Updated on June 09, 2022

Comments

  • Ruan Mendes
    Ruan Mendes almost 2 years

    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:

    • A dummy dependency file called PatientDbPath.json
    • A directory called out

    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.

  • Ruan Mendes
    Ruan Mendes over 13 years
    Thanks for the reply, I'm going to try to change my real makefile to use the suggested notation. I'll follow up or mark it answered real soon. Regarding the conceptual problems, what I posted here is just a sample makefile that shows the problem, not how my real makefile.
  • Ruan Mendes
    Ruan Mendes over 13 years
    Alright, I tested it and you were absolutely right, changing my makefile to use out/%.php : %.json did the trick. I have a follow-up question. Do I need to create a separate rule for each .json file in that directory?
  • Ruan Mendes
    Ruan Mendes over 13 years
    I found a solution for what I wanted, basically I need to add the following line: all:: $(patsubst %.json, out/%.php, $(wildcard *.json)) That will add a dependency of each of the php files to be generated from each of the .json in that directory