如何在其他一切之前运行一些命令?

时间:2016-10-20 04:26:11

标签: makefile gnu-make

我正在编写一个makefile,它将在HPC群集上下载并构建我需要的所有程序。在做任何其他事情之前,我希望它能够运行它:

module add openmpi/1.8.4-gcc
module add slurm

这两个命令修改了一些环境变量,如MPICCPATH,这样我就可以编译mpi程序并安排它们运行。现在我把它们放在.bashrc中。如何将它们从那里移动到我的makefile?

1 个答案:

答案 0 :(得分:0)

If you run them before running make, it will work. If you want those lines to appear in the Makefile, you can use .ONESHELL:

Otherwise, make will spawn one shell process per line, making the module load lines useless.

$ cat Makefile
CC=g++
SHELL=/bin/bash

.ONESHELL:
all:
    module load R
    R --version

While the module is not loaded

$ R
-bash: R: command not found

running make will succeed:

$ make
module load R
R --version
R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
[...]

But this will need every rule to include the module load commands.