【问题标题】:Display the results of foreach / eval / call expansion显示foreach/eval/call扩展的结果
【发布时间】:2013-10-16 20:15:03
【问题描述】:

我正在尝试为一个大型项目调试 makefile,我正在为 define TEMPLATE/endefforeach/eval/call 构造而苦苦挣扎。特别是我觉得我很难弄清楚我需要用$ 引用哪些变量,以及我需要用$$ 引用哪些变量。

如果我能在变量扩展之前看到eval/call 扩展的实际结果,我认为调试起来会更容易。

例如,如果我们将eval documentation 中的示例用于,我们有以下makefile-fragment:

 PROGRAMS    = server client

 server_OBJS = server.o server_priv.o server_access.o
 server_LIBS = priv protocol

 client_OBJS = client.o client_api.o client_mem.o
 client_LIBS = protocol
...
 define PROGRAM_template =
  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
  ALL_OBJS   += $$($(1)_OBJS)
 endef

 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))

我认为这个foreach 在变量扩展之前应该有效地扩展为以下内容:

server: $(server_OBJS) $(server_LIBS:%=-l%)
ALL_OBJS   += $(server_OBJS)

client: $(client_OBJS) $(client_LIBS:%=-l%)
ALL_OBJS   += $(client_OBJS)

我手动计算了上述扩展(欢迎更正),但我正在寻找一种通用方法来显示此扩展以用于更复杂的示例。有这样的方法吗?

我研究了 make -dmake -pn 选项,据我所知,我认为这两个选项都不会提供这种特定的输出。

我正在使用 make-3.81。

【问题讨论】:

    标签: gnu-make debugging foreach makefile eval


    【解决方案1】:

    $(info ...) 替换所有$(eval ...) 呼叫。实际上,如果您坚持使用 3.81,您可能不得不使用 $(warning ...) 并忽略额外的输出,因为我认为 $(info ...) 可能直到 3.82 版本才存在。

    不管怎样,写作:

    $(foreach prog,$(PROGRAMS),$(info $(call PROGRAM_template,$(prog))))
    

    (或警告)你会看到 make 正在解析什么。

    【讨论】:

    • 优秀 - 这正是我所需要的。谢谢!
    • 顺便说一句,$(info ...)make-3.81 对我来说效果很好。
    【解决方案2】:

    我编写了一个宏来帮助查看 $(eval ...) 的结果:

    # This version prints out rules for debugging
    #c = $(eval $(call $(strip $1),$(strip $2),$(strip $3),$(strip $4),$(strip $5),$(strip $6),$(strip $7),$(strip $8),$(strip $9))) $(info $(call $(strip $1),$(strip $2),$(strip $3),$(strip $4),$(strip $5),$(strip $6),$(strip $7),$(strip $8),$(strip $9)))
    
    # This version is the production version
    c = $(eval $(call $(strip $1),$(strip $2),$(strip $3),$(strip $4),$(strip $5),$(strip $6),$(strip $7),$(strip $8),$(strip $9)))
    

    像这样使用它:

    $(call c,function,arg1,arg2,...)
    

    要查看 eval 生成的内容,只需使用调试版本即可。我需要这个而不是简单地用 $(info ...) 替换 $(eval ...) 因为 makefile 中稍后的功能取决于 $(eval ...) 的结果

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2016-05-10
      • 2018-04-25
      • 2014-05-30
      • 2021-04-12
      相关资源
      最近更新 更多