如何为相同 library/executable 的多个变体编写 makefile?

How do I write a makefile for multiple variants of the same library/executable?

我正在使用 Intel 的 ifort 和 GNU make 在 linux 机器上构建我的项目。 lib依赖源目录下的a.f90、b.f90、c.f90源文件

我正在通过

调用 make
make FLAG

这些 FLAGS 在我的 makefile 中定义并对应于编译器的不同优化标志。

  1. 我希望我的 makefile 为每个标志创建一个新的子目录,这样我就可以一次或一个接一个地编译不同的目标,而不必先删除另一个。这怎么能做到?

  2. 稍后,在我的库编译之后,我想 link 我的可执行文件到那个库的特定变体,所以我必须告诉可执行文件的 makefile,哪个变体它应该采取。我如何以可维护的方式做到这一点?

到目前为止我的 makefile 示例:

#
# Include system specific include.mk file.
# This sets the correct compiler and PREFIX path.
#
# sth like:
# PREFIX=~/local
# FC=ifort
# LDR=$(FC)
#
include include.mk
#
#
# Extra Flags for optimization, debug and style.
#
INSPECTOR   := -O0 -g -check none -fPIC
O1          := -O1 -fPIC
O2          := -O2 -fPIC
OPT         := -O3 -fPIC -fast
PEDANTIC    := -fPIC -warn all -warn errors
DEBUG       := -g -fPIC -traceback -debug -check -fpe0

#
# Dircetories for .o, .mod and lib
#
DOC_DIR     := doc
BUILD_DIR   := .build
INSTALL_LIB := $(addprefix $(PREFIX),/lib)
INSTALL_INC := $(addprefix $(PREFIX),/mod)
#
# SOURCE
#
SRC_DIR     := src
vpath %.f90 $(SRC_DIR)

BASIC_OBJ  := a.o b.o c.o

OBJECTS     := $(addprefix $(BUILD_DIR)/,$(BASIC_OBJ) )

MOD_LIST := a_mod.mod b_mod.mod c_mod.mod

MODLIST := $(addprefix $(INSTALL_INC)/,$(MOD_LIST) )


# 
# RULES
#
.PHONY: clean inspector o1 o2 opt pedantic debug

all: $(BUILD_DIR)/libmyexample.so

#
inspector:
    @EXTRA="$(INSPECTOR)" make

#
o1:
    @EXTRA="$(O1)" make

#
o2:
    @EXTRA="$(O2)" make
#
opt:
    @EXTRA="$(OPT)" make

#
pedantic:
    @EXTRA="$(OPT) $(PEDANTIC)" make
#
debug:
    @EXTRA="$(DEBUG)" make
#
install: $(INSTALL_LIB) $(INSTALL_INC) $(INSTALL_LIB)/libmyexample.so

#
uninstall:
    rm $(INSTALL_LIB)/libmyexample.so
    rm $(MODLIST)
#
reinstall: uninstall install
#
doc: $(BUILD_DIR)
    doxygen $(SRC_DIR)/myexample.dxy
    mv warnlog.dxy $(BUILD_DIR)/
#
$(INSTALL_LIB)/libmyexample.so:
    cp $(BUILD_DIR)/libmyexample.so $(INSTALL_LIB)/

#
$(BUILD_DIR)/libmyexample.so: $(OBJECTS)
    $(LDR) -shared -o $@ $(OBJECTS) $(INCDIRS) $(LDFLAGS) 
#
clean:
    rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.mod 
    rm -rf $(BUILD_DIR)
    rm -rf $(DOC_DIR)/html $(DOC_DIR)/latex
#
$(BUILD_DIR)/%.o : %.f90
    $(FC) -o $@ -c $(EXTRA) $(INCDIRS) $(MODDIR) $<
#
$(OBJECTS): | $(BUILD_DIR) $(INSTALL_LIB) $(INSTALL_INC)
#
$(BUILD_DIR):
    mkdir -p $(BUILD_DIR)
#
$(INSTALL_LIB):
    mkdir -p $(INSTALL_LIB)
#
$(INSTALL_INC):
    mkdir -p $(INSTALL_INC)
#

关于您的 makefile 的一些清理说明。

如果您使用 O1/etc. instead ofo1/etc. for the target names (oro1`/等。对于变量名)然后你可以清理这些目标:

PEDANTIC: EXTRA = $(PEDANTIC)

O1 O2 OPT DEBUG PEDANTIC INSPECTOR:
        @$(MAKE) EXTRA="$(EXTRA) $($@)"

这还允许您在原始 make 手动调用上传递额外的 EXTRA 标志(例如 EXTRA=-newoption make O1)。

此外,底部的所有 mkdir 个目标都可以合并为一个目标:

$(BUILD_DIR) $(INSTALL_LIB) $(INSTALL_INC):
        mkdir -p $@

至于你的问题的主旨,如果你根据标志级别(或其他)设置 BUILD_DIR 那么你会得到你想要的并且可以在稍后的应用程序链接期间使用正确的目录.

类似这样的事情(不是我实际会怎么做,但可以用于一个简单的例子):

O1 O2 OPT DEBUG PEDANTIC INSPECTOR:
        @$(MAKE) EXTRA="$(EXTRA) $($@)" BUILD_DIR=.$@

.O1 .O2 .OPT .DEBUG .PEDANTIC .INSPECTOR:
        mkdir $@

这将用目标值覆盖 BUILD_DIR 的文件内定义并构建到它们中。

应用程序目标只需要在链接时将 -L$@(或其他)添加到适当的标志。