1

我有以下代码的makefile:

 .PHONY: $(PROJECTDIR)/projectroot
 $(PROJECTDIR)/projectroot:
if [ -f $(@)/etc/release ]; then \
         #$(RELEASE_VERSION) will have value something like 00.01.02.03
     IFS=. read major minor micro build <<<"${RELEASE_VERSION}" \
         echo major=${major} > $(@)/etc/release \
         echo major=${minor} >> $(@)/etc/release; \
         echo major=${micro} >> $(@)/etc/release; \
         echo major=${build} >> $(@)/etc/release; \
fi

我期待 $(@)/etc/release 获得如下内容:

 major=00
 minor=01
 micro=02
 build=03

我需要更换 IFS=。使用 gnu make 中的一些等效命令,你能帮我解决这个问题吗?谢谢。

4

2 回答 2

2

问题是您正在将值读入名为 等的 shell 变量major中,minor但随后您正在打印具有相同名称的 Make 变量。shell 不能设置 Make 变量。

要修复现有代码,您需要重复$字符,$$以便 Make 将 a 输出$到 shell,shell 将解释它们(而且您每次都说“主要”,并且缺少一些分号):

     IFS=. read major minor micro build <<<"${RELEASE_VERSION}" ; \
     echo major=$${major} > $(@)/etc/release ;\
     echo minor=$${minor} >> $(@)/etc/release; \
     echo micro=$${micro} >> $(@)/etc/release; \
     echo build=$${build} >> $(@)/etc/release; \

但是要在 make 中执行此操作,您可以使用substandword函数:

REL_WORDS := $(subst ., ,${RELEASE_VERSION})
...
     echo major=$(word 1,${REL_WORDS}) >  $(@)/etc/release ;\
     echo minor=$(word 2,${REL_WORDS}) >> $(@)/etc/release ;\
     echo micro=$(word 3,${REL_WORDS}) >> $(@)/etc/release ;\
     echo build=$(word 4,${REL_WORDS}) >> $(@)/etc/release ;\
于 2013-11-12T00:09:21.850 回答
1
.PHONY: $(PROJECTDIR)/projectroot
$(PROJECTDIR)/projectroot: $(PROJECTDIR)/projectroot/etc/release
$(PROJECTDIR)/projectroot/etc/release:
    echo "${RELEASE_VERSION}" | \
    sed 's/^/major=/; s/\./\nminor=/; s/\./\nmicro=/; s/\./\nbuild=/' >$@

将依赖项添加到发布文件。

于 2013-11-11T23:49:11.623 回答