Search code examples
makefilegnu-makegnu

How can I create a make file to run other make files independently?


I want to be able to run a command like make subdir in a make file located in the root of a folder which will in turn run another make file that is located in a sub directory of the current one.

From the official GNU make file documents, I was able to find the code below and test it which did what I needed to do

subsystem:
     $(MAKE) -C ./subdir/

I placed the code in a make file in the root and when I run make in the root, it runs the make file in the subdir folder.

But say for example, I have several sub folders each with their own make file that does something pertaining to the contents in each respective folder. My intention is to have the root make file serve as a place where I can launch make files in other sub folders throughout the folder but do so independently, not run all of them at once.

So if I was to say make subfolder1 in the root, the make file in subfolder1 will run, and if I was to say make subfolder2, the make file in subfolder2 will run, and so on and so forth. Is this achievable?


Solution

  • I was able to achieve what I needed by using the code in my original question and just renaming them to fit my needs.

    name1:
         $(MAKE) -C ./subdir1/
    
    name2:
         $(MAKE) -C ./subdir2/
    

    Thanks to @MadScientist as well for pointing out that I had figured out how to do what I needed, I was overthinking it for sure.

    I did run into an issue in which when I ran the make file with name1 or name2, I was prompted with a message saying that the directory was up to date. Even if I went into the sub folders and deleted any populated files as a result of calling their respective make files, the issue still persisted.

    I was able to bypass it by adding the code below to the bottom of my root make file.

    .PHONY: name1 name2...
    

    So now when I call make name1 or make name2, the make files in those sub folders work and do what they are supposed to!