2

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

LANGUAGE=python
COUNT_SRC=countwords.py
COUNT_EXE=$(LANGUAGE) $(COUNT_SRC)
ZIPF_SRC=testzipf.py
ZIPF_EXE=$(LANGUAGE) $(ZIPF_SRC)

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... – Stephen Kitt Jun 27 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? – Krishna Jun 27 at 12:45
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
          close($2)
     } else
          print
}

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

  • Awesome. That just works. Thanks a lot for the brilliant suggestion. – Krishna Jun 27 at 12:58
  • what if the file is actually a macro, as in include $(foo)? what about the ifdefs, ifeqs? what about include *.mk? – pizdelect Jun 27 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! – Krishna Jun 27 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. – pizdelect Jun 27 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.