如何使用非默认编译器构建 autotools 项目的一些源代码

How to build some sources of an autotools project using a non default compiler

我有一个 autotools C 项目。该项目有一些测试需要 MPI 来构建。使用 mpicc“C 编译器”编译 MPI 程序很容易,因为这可以确保链接 MPI 库,而无需向 LDFLAGS 和 LIBS 变量显式添加任何标志。

不幸的是,我对 automake 的内部结构不够熟悉,也不确定如何动态更改编译器以构建某些源代码。我想我可以按照以下几行做一些事情:

EXTRA_PROGRAMS = src/test/mpi_prog_1 \
                 src/test/mpi_prog_2

# MPICC defined in configure.ac
src/test/src_test_mpi_prog_1-mpi_prog_1.lo:
    libtool --tag=MPICC --mode=compile ...

src/test/src_test_mpi_prog_2-mpi_prog_2.lo:
    libtool --tag=MPICC --mode=compile ...

mpi_prog_1 和 mpi_prog_2 的规则是否会覆盖 automake 默认规则以构建 MPI 程序?我如何使用自定义构建规则准确地挂钩 automake?

谢谢

Would the rules for mpi_prog_1 and mpi_prog_2 override the automake default rules to build the MPI programs? How do I exactly hook automake with a custom build rule?

根据 Automake Manual:

With some minor exceptions [...] the contents of a Makefile.am is copied to Makefile.in verbatim.

These copying semantics mean that many problems can be worked around by simply adding some make variables and rules to Makefile.am. Automake will ignore these additions.

[...] it is possible to have conflicting definitions of rules or variables. When building Makefile.in the following priorities are respected by automake to ensure the user always has the last word:

  • User defined variables in Makefile.am have priority over variables AC_SUBSTed from configure.ac, and AC_SUBSTed variables have priority over automake-defined variables.
  • As far as rules are concerned, a user-defined rule overrides any automake-defined rule for the same target.

(强调)

所以是的,如果您提供自定义规则——您可以将它们放入您的 Makefile.am 中,就像它是一个 makefile 一样——那么它们将覆盖任何自动生成的规则以构建相同的规则目标。

您也可以为其他目的提供自定义规则,例如提供从 autotools 不知道的来源构建文件。例如,这就是您使用自定义代码生成器的方式。