在项目的 makefile 中包含 cpputest

Including cpputest in makefile for project

我想将 cpputest 作为 git 子模块包含在我项目的源代码树中,但我对 autotools 不是很熟悉。

我一直在看this page as a guide

我创建了以下 Makefile,cpputest 在它下面的子目录中:

CPPUTEST_BUILD_DIR := cpputest/cpputest_build

CPPUTEST_LIBS :=                           \
    $(CPPUTEST_BUILD_DIR)/libCppUTest.a    \
    $(CPPUTEST_BUILD_DIR)/libCppUTestExt.a

CPPUTEST_TESTS :=                          \
    $(CPPUTEST_BUILD_DIR)/CppUTestTests    \
    $(CPPUTEST_BUILD_DIR)/CppUTestExtTests

cpputest:                           \
    cpputest/configure              \
    $(CPPUTEST_BUILD_DIR)/Makefile  \
    $(CPPUTEST_LIBS)                \
    $(CPPUTEST_TESTS)

cpputest/configure: cpputest/configure.ac cpputest/autogen.sh
    cd cpputest && ./autogen.sh

cpputest/cpputest_build/Makefile: cpputest/configure
    cd $(CPPUTEST_BUILD_DIR) && ../configure

$(CPPUTEST_LIBS): $(CPPUTEST_BUILD_DIR)/Makefile
    cd $(CPPUTEST_BUILD_DIR) && make

$(CPPUTEST_TESTS): $(CPPUTEST_LIBS)
    cd $(CPPUTEST_BUILD_DIR) && make tdd

运行 这个 makefile 做了我想要的,但我不确定依赖关系有多健壮。除了干净的规则之外,还有什么我应该考虑添加的吗?


这是我在 之后的最终 Makefile。

它位于 CppUTest 目录的根目录中。

# This makefile will generate a platform specific makefile, build CppUTest
# library outputs, and build and run tests on CppUTest.
BUILD_DIR := cpputest_build

LIBS := $(BUILD_DIR)/lib/libCppUTest.a $(BUILD_DIR)/lib/libCppUTestExt.a

TESTS := $(BUILD_DIR)/CppUTestTests $(BUILD_DIR)/CppUTestExtTests

# All builds both libraries, to build and run all of the tests for CppUTest use
# the phony target run_tests.
all: $(LIBS)

.PHONY: all run_tests clean FORCE

# The Makefile rule depends on the "configure" shell script existing. If
# CppUTest is updated from upstream or configure doesn't exist then run
# autogen.sh or uncomment the rule below. All of the outputs autogen.sh should
# be tracked in the develop branch.
#
#configure: configure.ac autogen.sh
#   ./autogen.sh

$(BUILD_DIR)/Makefile: configure
    mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && ../configure

$(LIBS) : $(BUILD_DIR)/Makefile FORCE
    cd $(BUILD_DIR) && make $(subst $(BUILD_DIR)/,,$@)

$(TESTS) : $(BUILD_DIR)/Makefile FORCE
    cd $(BUILD_DIR) && make $(@F)

run_tests: $(TESTS)
    for test in $(TESTS); do $$test -c || exit 1; done


clean:
    rm -rf $(BUILD_DIR)

Is there anything else I should consider adding to this, besides a clean rule?

TL;DR: 你应该预先 运行 ccptest 的 autogen.sh 脚本,并将所有生成的文件与所有其他文件一起包含在你的分发中.然后,您还可以考虑省略构建 cpputest/configure 的规则(我本人确实会省略它)。


Cpputest 显然正在追随最近的趋势,即从源代码管理中省略 Autotools 生成的文件。这在表面上是有道理的,因为这些文件确实可以从源代码管理中可用的其他文件生成。从源代码控制系统作为项目维护和开发工具的角度来看,这很好。 很好,但是,关于用作分发工具的源代码控制。

Autotools 的设计目标之一是它们本身仅供项目维护人员使用。 运行 它们不打算成为例行构建过程的一部分,例如只想构建并且 运行 软件会使用的人。因此,由 Autotools 构建系统本身准备的分发包包括实现这一点所需的所有位:configure 脚本、支持实用程序脚本、Makefile.in 文件,有时还有一些其他杂项文件。这是分发的预期形式。

这样的话,Autotools 的兼容性压力比很多项目都小,这确实体现在不同 AT 版本之间的兼容性较弱。如果您使用与项目维护者自己不同的 AT 版本构建构建系统,那么您可能会遇到错误。你肯定会得到一个与维护者使用的构建系统有一定差异的构建系统。通常结果仍然会毫无问题地构建项目,但有时不会。所以帮自己一个忙,回避这个问题。

Running this makefile does what I want but I am not sure how robust the dependencies are.

makefile 本身对我来说很不错。我看到批评的主要是

  • cpputest/cpputest_build/Makefile 的规则按字面拼写目录部分,而不是使用 $(CPPUTEST_BUILD_DIR) 变量。

  • cpputest 目标的先决条件列表不应包括 cpputest/configure$(CPPUTEST_BUILD_DIR)/Makefile。这些是构建过程所必需的是偶然的,并且已经被列为直接依赖于它们的目标的先决条件的那些工件所解决。作为风格、可维护性和一般良好实践的问题,make 规则应仅列出目标的直接先决条件。

尽管我建议您分发构建系统组件而不是让每个人都重建它们,但是,我也将删除构建规则 cpputest/configure。如果您听从我的建议,那么您将分发一个预构建的,这样用户就不需要构建它。省略规则并从出现它的那些先决条件列表中拉出 cpputest/configure 将消除在时间戳被打乱的情况下一切都失败的小风险,就像复制源代码树时有时会发生的那样。

至于 clean 规则,由于您在顶层 make 的控制下进行 cpputest 配置,相应的清理方法是 cd $(CPPUTEST_BUILD_DIR); make distclean。但是,由于您正在执行源外构建,您还可以选择递归地删除构建目录。但是请注意,如果您坚持使用 运行ning autogen.sh 作为构建过程的一部分,那么生成的文件将不限于构建目录。在那种情况下,相应的清理可能涉及 make maintainer-clean.