Search code examples
makefilegnu-makegnu

changing the directory and executing the makefile commands


I have the following directory structure

project
|-- aws-cdk
|-- data
    |-- some_project_files
    |-- Makefile

Using the Makefile, I'm executing the directory some_project_files and this is working fine. In addition to that, I want to execute aws-cdk as well. But I do not see a way to change the directory within the makefile. I tried cd aws-cdk and then execute but I keep on getting error make: *** No rule to make target. Stop.

How do I go back/change directory and then execute Makefile


Solution

  • Within one makefile you can change directory inside the rules, but you can't say this set of rules runs in one directory and this one runs in the other. Which does not prevent you running rules on files in a different directory by simply giving their path. The data/Makefile can contain rules for ../aws-cdk/something and even ../aws-cdk/%.out: ../aws-cdk/%.in and that's just fine.

    If you do want to actually change directory, you need a separate makefile. I strongly recommend just putting it in the directory where it will run, so aws-cdk/Makefile and then you can just call it from the one in data like $(MAKE) -C ../aws-cdk. You can also put it anywhere else and give make an explicit argument like $(MAKE) -C ../aws-cdk -f aws-cdk.make, but putting it in the directory will be easier to understand when it needs fixing two years down the line and/or by someone who's never seen it.