Let us say we have the following Makefile

include config.mk

# Generate summary table.
results.txt : $(ZIPF_SRC) isles.dat abyss.dat last.dat
    $(ZIPF_EXE) *.dat > $@

# Count words.
.PHONY : dats
dats : isles.dat abyss.dat last.dat

%.dat : books/%.txt $(COUNT_SRC)
    $(COUNT_EXE) $< $*.dat

.PHONY : clean
clean :
    rm -f *.dat
    rm -f results.txt

where all the variables are defined in config.mk


Is there any tool available that can expand all the include lines within the makefile so as to produce a bigMakefile? If such a tool does not currently exist, can a python/perl/awk/bash script be cooked up to achieve this goal?

  • With GNU Make, make -qprR will dump all the variables and rules loaded as a result of processing the main Makefile and all included files, along with the environment variables. I suspect it would need quite a lot of post-processing to meet your requirements... Jun 27, 2019 at 12:43
  • @StephenKitt I think -qprR diverges a lot (and almost an overkill) for my requirements which needlessly increases post-processing complexity. I just need a simple in-place expansion of the include line. Can an awk script do the job? Jun 27, 2019 at 12:45

1 Answer 1


The GNU Awk manual's section on getline has an almost perfect example:

For example, the following program copies all the input files to the output, except for records that say @include filename. Such a record is replaced by the contents of the file filename:

     if (NF == 2 && $1 == "@include") {
          while ((getline line < $2) > 0)
               print line
     } else

You just need to modify it to match include instead of @include.

  • Awesome. That just works. Thanks a lot for the brilliant suggestion. Jun 27, 2019 at 12:58
  • what if the file is actually a macro, as in include $(foo)? what about the ifdefs, ifeqs? what about include *.mk?
    – user313992
    Jun 27, 2019 at 13:12
  • @pizdelect these are all valid cases to be accounted for a generic makefile parser. But my question was about a specific case, and the accepted answer above tackles just that! Jun 27, 2019 at 14:27
  • It's great that you're happy with the answer, but this is public place and other people may wish to know all the pitfalls and limitations of this approach, and if @muru won't mention them, I'll happily do it for him, no matter if he find my help welcome or not.
    – user313992
    Jun 27, 2019 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .