Search code examples
makefilegnu-make

How can I include another Makefile from a target?


Makefile:

include:
    wget <tarball with another Makefile>.tar.gz
    tar -zxvf <tarball with another Makefile>.tar.gz
    include Makefile.from_tarball
    echo ${VAR_ONLY_EXIST_IN_TARBALL_MAKEFILE}

I would like to do like the above where i want to include a Makefile upon running a target

Is this possible?


Solution

  • You can include another makefile in GNU make with an include command at the top level:

    -include foobar.mak
    

    If you put a dash in front of it (as above), that will not give an error and will instead defer loading the makefile on demand. If that makefile has any dependencies, GNU make will first make sure they're up to date and will reread the makefile if necessary once they are updated

    foobar.mak: foobar.tar.gz
            tar xf $< $@    # extract the makefile
    
    foobar.tar.gz:
            wget <URL for tarball>
    

    these rules will download the tarball (if it hasn't been downloaded yet), extract the makefile (if the tarball is newer) and then include it.