【问题标题】:make action if up to date如果是最新的,请采取行动
【发布时间】:2021-01-03 17:04:18
【问题描述】:

我有一个在 Windows 上运行的 GNU Make 3.81 使用的 makefile,每次构建时我都会将一个定义传递给编译器,并给出一个版本号。版本号是从文件中获取的,并在 makefile 的开头使用带有 -get 选项的 perl 脚本递增。然后生成的程序可以显示构建版本。我遇到的麻烦是即使构建失败也会更新版本号。但这可以通过再次运行 perl 脚本和 -dec 选项来解决,以在发生错误时减少版本号。 makefile 的简化内容:

objects:= pov1.obj
ouf:= pov1
versionFile:= povVersion.txt
VERSION:=$(shell perl ../../../perl/uscripts/bump.pl -get -f $(versionFile))
CXX:= cl 
CXXFLAGS:= -c -EHsc -std:c++17 -D$(addprefix VER=,\"$(VERSION)\")

target:=$(addsuffix .exe, $(ouf))

%.obj : %.cpp
    $(CXX) $(CXXFLAGS) $< -Fo$@ || perl ../../../perl/uscripts/bump.pl -dec -f $(versionFile)
     
$(target) : $(objects)
    $(LINKER) $(LDFLAGS) $(objects) /OUT:$(target) || perl ../../../perl/uscripts/bump.pl -dec -f $(versionFile)

尽管如果我在最新版本时不小心运行了 make,这似乎可行,尽管没有进行构建,但版本号会更新。如果 make 检测到最新的,或者使 perl 脚本的第一次运行依赖于所需的更新,我如何运行 perl 脚本减量?

也许有更好的方法将构建版本号输入程序?

编辑以反映 dash-o 给出的答案,我的简化 make 文件现在包含以下内容:

objects:= pov1.obj
ouf:= pov1
versionFile:= povVersion.txt
VERSION:=$(shell perl ../../../perl/uscripts/bump.pl -inc -dry -f $(versionFile)) #versionFile is not updated until link actually completes
CXX:= cl 
CXXFLAGS:= -c -EHsc -std:c++17 -D$(addprefix VER=,\"$(VERSION)\")

target:=$(addsuffix .exe, $(ouf))

%.obj : %.cpp
    $(CXX) $(CXXFLAGS) $< -Fo$@
     
$(target) : $(objects)
    $(LINKER) $(LDFLAGS) $(objects) /OUT:$(target)
    perl ../../../perl/uscripts/bump.pl -inc -f $(versionFile) #update versionFile here

在我的 C++ 主文件 pov1.cpp 中有

#ifdef VER
std::string version{VER};
#else
std::string version{"version not set"};
#endif

它允许在程序中访问版本号。 perl 脚本是here

【问题讨论】:

  • 仅当您需要构建而不是之前才增加。无需通过减少任何东西来解决...
  • 使用日期时间作为内部版本号,例如20210103121050。那么,两个连续的构建被分配不连续的数字就不再是问题了。
  • @Klaus,谢谢,是的,这似乎是“使 perl 脚本的第一次运行依赖于所需的更新”的最佳方式,但我该怎么做呢?
  • @stegzzz 创建到$(versionfile) 的第一级依赖关系,例如%.obj : $(versionfile) %.cpp(当然还有另一个调用脚本的规则)。
  • 这似乎是这个小技巧的完美情况:make.mad-scientist.net/deferred-simple-variable-expansion

标签: c++ perl makefile versioning


【解决方案1】:

当前解决方案在构建开始时会增加版本号(存储在文件中),并在失败时将其回滚。但是,当脚本没有达到链接时间时,这可能会在各种情况下失败。

作为替代方案,请考虑不同的解决方案:

  • 新版本号将在构建开始时计算,但版本号不会在文件中更新。
  • 如果构建(链接)成功完成,则将新版本号提交到文件中

    # The '-get' should return N+1, and should NOT update the file
VERSION:=$(shell perl ../../../perl/uscripts/bump.pl -get -f $(versionFile))

$(target) : $(objects)
    $(LINKER) $(LDFLAGS) $(objects) /OUT:$(target)
    # Commit the new version number, only if build is success
    echo $(VERSION) > $(versionFile)

【讨论】:

  • 原始问题已更新以反映此答案
猜你喜欢
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2017-08-28
  • 2020-12-02
  • 2018-06-29
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多