makefile - Using Make's file function

I'm trying to dump the value of make variables to a file for further processing.

So far, I've been able to print the values to the command line using the following rule:

print-%:
    @echo '$*=$($*)'

Which you call with the command line

make print-VAR

I really don't want it on stdout, but printed to a file. The GNU Make "file" function seems like the thing I should be using: http://www.gnu.org/software/make/manual/html_node/File-Function.html#File-Function

The page jumps into a complex example right away though, so I've implemented the following but can't seem to get it to work: it prints the @echo to stdout, but when I look in my directory there is no output.txt file.

printf-%:
    @echo '$*=$($*)'
    $(file > output.txt,$($*))

What am I missing?

EDIT: Well, looks like I can just use

@echo '$($*)' > output.txt

to do what I want, but that still doesn't explain why my file call wasn't working.

2 Answers

  1. Franklin- Reply

    2019-11-15

    Two Responses from comments section:

    1) To get the functionality desired, just redirect the "@echo" statement to a file

    printf-%:
        @echo '$*=$($*)' > output.txt
    

    2) The reason the file function did not work is that it is a feature of Make v4.0 and newer, not 3.8, which was the version I was using.

  2. Fred- Reply

    2019-11-15

    You can print out variables before make gets to running anything in the shell. This gives you all of them (well, apart from the ones with a space in their name)

    $(foreach _,${.VARIABLES},$(info $_ is $(flavor $_) with value [$(value $_)] which expands to [$_]))
    

    giving

    $ make
    GNUMAKEFLAGS comes from the [environment] and is simple with value [] which expands to []
    <D comes from the [automatic] and is recursive with value [$(patsubst %/,%,$(dir $<))] which expands to []
    ?F comes from the [automatic] and is recursive with value [$(notdir $?)] which expands to []
    ⋮
    

    This is often essential as variables can change as makefiles are parsed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>