日期:2014-05-16  浏览次数:20616 次

Autotools编写Makefile.am问题
利用autotlls工具编译程序,需要自己编写makefile.am文件。我在编写过程中遇到些问题,请教下各位。
我有程序想编译一个并行版本和一个串行版本,我现在只能做到并行版本用一个makefile.am文件,串行版本用另一个makefile.am文件,有没有可能做到就只用一个makefile.am文件来实现? 只要在configure的时候指定一下是并行还是串行?
并行版本makefile.am

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=test.mpi
FC=mpif90
test_mpi_LDADD=指定库
test_mpi_SOURCES=指定源文件

串行版本makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=test.nompi
FC=ifort
test_nompi_LDADD=指定库
test_nompi_SOURCES=源文件

串行和并行版本的makefile.am文件都没有问题。但是我没法做到统一到一个makefile.am文件中。

以下我的设想,但是没实现成功,求帮助。
综合版本makefile.am
AUTOMAKE_OPTIONS=foreign
BASIC_NAME=test
如果是并行版本{
ADD_NAME=mpi
FC=mpif90}
如果是串行版本{
ADD_NAME=nompi
FC=ifort}
bin_PROGRAMS=$(BASIC_NAME).$(ADD_NAME)
$(BASIC_NAME)_$(ADD_NAME)_LDADD=指定库
$(BASIC_NAME)_$(ADD_NAME)_SOURCES=指定源文件


------最佳解决方案--------------------
复习了一下autotools,楼主可以AM_CONDITIONAL
试试下面的方法

Makefile.am

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = demo
if MPI
demo_SOURCES = demo_mpi.c
else
demo_SOURCES = demo.c
endif

configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(demo, 1.0)
AM_INIT_AUTOMAKE
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

AC_ARG_ENABLE([mpi],
        [  --enable-mpi Enable MPI],
        [case "${enableval}" in 
                yes) mpi=true ;;
                no)  mpi=false ;;
        esac], [mpi=false])
AM_CONDITIONAL([MPI], [test x$mpi = xtrue])

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

然后可以用
CC=mpicc ./configure --enable-mpi
CC=cc ./configure --disable-mpi
分别处理MPI启用和禁止的情况

注: 似乎不能在Makefile.am里设置CC
------其他解决方案--------------------
方法都有了,改改应该可以的。
$ cat Makefile.am

AUTOMAKE_OPTIONS = foreign
if MPI
bin_PROGRAMS = demo_mpi
demo_mpi_SOURCES = demo_mpi.c
else
bin_PROGRAMS = demo
demo_SOURCES = demo.c
endif

$ autoreconf
$ ./configure --enable-mpi
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... nawk
checking&n