Search code examples
makefile

How do I write all makefile variables and values to a file?


I'm using this snippet to get all makefile variables and values:

.PHONY: test
test:
    $(foreach v,                                        \
          $(filter-out $(VARS_OLD) VARS_OLD,$(.VARIABLES)), \
          $(info $(v) = $($(v))))

But I want to write this output to a file.

None of the following works:

...$(info $(v) = $($(v)))) > myfile.txt

...$(info $(v) = $($(v))) >> myfile.txt)

ALL_MAKEFILE_VARS:; $(foreach v, $(filter-out $(VARS_OLD) VARS_OLD,$(.VARIABLES)), $(info $(v)=$($(v))))
.PHONY: test
test:
    $(file > myfile.txt,$(ALL_MAKEFILE_VARS))

echo -e $(foreach v, $(filter-out $(VARS_OLD) VARS_OLD,$(.VARIABLES)), $(info $(v)=$($(v)))) > derp.txt

Its so easy to print them, I just want to redirect that output to a file.

Edit

I'm trying to use the file function but I'm running into a few problems.

First problem isn't the file function its the target itself, this always returns make: Nothing to be done for 'test'.

VARS_OLD:= $(.VARIABLES)
.PHONY: test
test:
    $(foreach v,                                        \
          $(filter-out $(VARS_OLD) VARS_OLD,$(.VARIABLES)), \
          $(file >myfile.txt,$(v) = $($(v))))

If I remove the VARS_OLD:= $(.VARIABLES) I can get it to run but then I see this:

*** Undefined  required by target `test`.  Stop

It does create a file but it only has this content:

% cat myfile.txt 
.LIBPATTERNS = lib%.dylib lib%.a

Solution

  • You can't do this because $(info ...) is a make function, and > redirection is a shell feature. When make goes to run a recipe it will first expand the recipe which expands all make macros and functions, then it passes the resulting string to the shell to run.

    So, the info functions are all invoked by make and always write to stdout, as they're defined to do, and expand to the empty string, as they're defined to do, then the resulting recipe (just the redirection) is passed to the shell.

    You have to use shell commands if you want to use shell redirection. For example:

    .PHONY: test
    test:
            ( :; $(foreach v,                                       \
                  $(filter-out $(VARS_OLD) VARS_OLD,$(.VARIABLES)), \
                  echo '$(v) = $($(v))'; ) ) >  myfile.txt
    

    Note I didn't actually try this.

    Alternatively if you have a "new enough" version of GNU Make you can investigate the $(file ...) function.