gnu make: list the values of all variables (or “macros”) in a particular run

后端 未结 6 1480
面向向阳花
面向向阳花 2020-12-07 16:41

How can I list the current value of all variables (also called macros) in a Makefile when running make?

E.g. if this is in the Makefile:

CUR-DIR := $         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 17:16

    Thanks to @kevinf for the foreach solution -- if one wants to export this list as a somewhat machine-readable file, one will have a hard time with uneven quotes or newlines when using echo or printf, since Make isn't able to quote the data correctly -- one needs to use the $(file ...) function to write the data to avoid sh/bash complaining about invalid syntax. For example, use this in your rule -- it prints variable name, definition and expanded value:

    $(file > $(MAKEFILE_ENV_FILE),)
    $(foreach v, $(.VARIABLES), \
        $(file >> $(MAKEFILE_ENV_FILE),$(v)) \
        $(file >> $(MAKEFILE_ENV_FILE),    := $(value $(v))) \
        $(file >> $(MAKEFILE_ENV_FILE),    == $($(v))) \
        $(file >> $(MAKEFILE_ENV_FILE),) \
    )
    

    (This will still not allow to always distinguish malicious variables with double newlines from two variables, for this one now add a sufficiently unique separator infront of each Makefile-generated newline just after each comma inside $(file >> NAME,TEXT))

    Set MAKEFILE_ENV_FILE to some filename, e.g.:

    MAKEFILE_ENV_FILE := $(abspath $(lastword $(MAKEFILE_LIST))).env
    

提交回复
热议问题