make中的动态目标

And*_*rea 4 makefile

我是make的新手,我正在尝试使用它来部署一些javascript文件.我一直在努力解决以下问题,但没有成功.

我的目录结构如下:

helpers/
    foo/
        foo.js
        test/
            test1.js
            test2.js
            ...
    bar/
        bar.js
        test/
            test1.js
            test2.js
            ...
    other helpers...
distrib/
    files ready for distribution
other stuff...
Run Code Online (Sandbox Code Playgroud)

我的makefile应该构建帮助器等.对于每个帮助foo我想生产,下DISTRIB,以下文件:foo-version.js,foo-version-uncommented.js,foo-version-packed.jsfoo-version-tests.zip.前三个是由foo.js获得的,分别作为副本,通过剥离注释和运行javascript minifier.我已经有命令来执行这些任务.

应该在文件本身的注释中读取版本号,我可以轻松地使用它

def version
    $(shell cat $1 | grep @version | sed -e"s/.*version *//")
endef
Run Code Online (Sandbox Code Playgroud)

我的问题是像目标一样foo-version.js是动态的,因为它们取决于运行make时读取的版本号.我试图使用模式,但我没有做到这一点.问题是像这样的东西是行不通的

helpers := foo bar
helpers: $(helpers)
$(helpers): %: $(call version, %)
Run Code Online (Sandbox Code Playgroud)

因为第二个%在宏调用中没有扩展,但它是按字面意思使用的.

我需要能够make helpers构建所有帮助程序或make foo构建单个帮助程序.第二步是删除distrib版本号较低的所有文件.任何想法如何做到这一点?

作为一个附带问题:使用不同的构建工具,这样的任务会更容易吗?我不是专家,可能值得学习别的东西.

rei*_*ost 10

在GNU化妆,你可以使用函数calleval,通常与组合foreach:

%-version.js: %.js
   # your recipe here
%-version-uncommented.js: %.js
   # your recipe here
%-version-packed.js: %.js
   # your recipe here
%-version-tests.zip: %.js
   # your recipe here

versions_sfxs := .js -uncommented.js -packed.js -tests.zip
helpers := $(shell ls $(HELPERSDIR))

define JS_template

helpers: $(1)-version$(2)

endef

$(foreach h, $(helpers), \
  $(foreach sfx, $(versions_sfxs), \
    $(eval $(call JS_template,$(h),$(sfx)) \
  ) \
)
Run Code Online (Sandbox Code Playgroud)

此代码未经测试,但它提供了一般的想法.期待花一个下午调试你对空格,制表符,美元符号和反斜杠的使用,就像在shell脚本中一样. 搜索Stack Overflowmake eval或更多细节和指针.

  • 关于度过一个下午的评论特别令人沮丧... :-( (2认同)