【问题标题】:Exporting recursive makefile variables to submakes将递归生成文件变量导出到子生成
【发布时间】:2011-10-17 04:29:49
【问题描述】:

我需要将a user-defined GNU make function(即a recursively expanded makefile variable)导出到子制作。但是,make 似乎将所有这些变量都扩展为简单的扩展变量,这使得它们毫无用处。

这是一个例子:

“parent.make”的内容:

export myfunc = my arg is $1
$(info parent testing myfunc: $(call myfunc,parent))
all: ; $(MAKE) -f child.make

“child.make”的内容:

$(info child testing myfunc: $(call myfunc,child))
nullrule: ; @true

运行“make -f parent.make”会产生以下输出:

parent testing myfunc: my arg is parent
make -f child.make
child testing myfunc: my arg is 

Parent 将 myfunc 作为包含“my arg is”的简单扩展变量导出到子 make,使其无法作为函数使用。

【问题讨论】:

    标签: recursion makefile export gnu-make user-defined-functions


    【解决方案1】:

    为什么不能将公共变量和函数放入父和子makefile都包含的单独makefile中?

    【讨论】:

      【解决方案2】:

      我不确定这是否能满足您的要求,但这里是:

      parent.make:

      export myfunc1 = my arg is $1
      export myfunc2 = $(value myfunc1)
      $(info parent testing myfunc1: $(call myfunc1,parent))
      all: ; $(MAKE) -f child.make
      

      child.make:

      $(info child testing myfunc2: $(call myfunc2,child))
      nullrule: ; @true
      

      编辑:
      好吧,这个怎么样:

      child.make:

      myfunc1=$(myfunc2)
      
      $(info child testing myfunc1: $(call myfunc1,child))
      nullrule: ; @true
      

      编辑:

      没那么快。看看这个:

      parent.make:

      myfunc_base = my arg is $1
      
      export myfunc = $(value myfunc_base)
      
      $(info parent testing myfunc: $(call myfunc_base,parent))
      all: ; @$(MAKE) -f child.make
      

      child.make:

      $(info child testing myfunc: $(call myfunc,child))
      nullrule: ;  @$(MAKE) -f grandchild.make
      

      grandchild.make:

      $(info grandchild testing myfunc: $(call myfunc,grandchild))
      nullrule: ; @true
      

      这完全不需要修改child.makegrandchild.make。它确实要求parent.make 调用myfunc_base 而不是myfunc;如果这是一个问题,有办法解决它,也许不止一个。

      一种可能性,我们将定义向上移动到任何调用parent,它实际上根本不会被调用:

      祖父母制作:

      myfunc_base = my arg is $1
      
      export myfunc = $(value myfunc_base)
      
      all: ; @$(MAKE) -f child.make
      

      parent.make:

      $(info parent testing myfunc: $(call myfunc,parent))
      all: ; @$(MAKE) -f child.make
      

      我意识到这可能行不通,因为myfunc 的定义可能需要parent.make 中定义的其他内容。看看这是否适合您的情况;可能还有其他方法...

      【讨论】:

      • 这是一个巧妙的技巧。但是,它并没有真正解决我当前的问题,因为我需要 parent 和 child 都能够使用相同的名称引用该函数。 (这是在一个预先存在的、过时的、巨大的和复杂的制作设置中)。我的后备选项是在单独的文件中定义这些函数,并为每个 submake 实例包含该文件,但这也不太理想。
      • 关于您的编辑,这并不实际。有很多函数,为每个函数添加 2 行太冗长了。此外,有几个级别的制造运行子制造,这使这进一步复杂化。无论如何,我会接受你的回答,因为这似乎是唯一可用的选择!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      相关资源
      最近更新 更多