Makefile中的ShellScript

时间:2015-05-20 22:13:22

标签: bash shell makefile

我写了以下makefile:

# make the iMe program
#

SDIR=src
IDIR=include
CFLAGS=-I$(IDIR)

ODIR=obj
EDIR=bin

LIBS=-lrt -lpthread

_DEPS = 1.h 2.h 3.h 4.h 5.h 6.h 7.h 8.h 9.h 10.h 11.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = 1.o 2.o 3.o 4.o 5.o 6.o 7.o 8.o 9.o 10.o 11.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
    gcc -c -o $@ $< $(CFLAGS) -O3

$(EDIR)/iMe: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

# Clean 

clean:
    -rm -f ./$(ODIR)/* $(OBJ) ./$(EDIR)/*

# Backup 

backup:
    tar -czvf backup_iMe_`date +%d-%m-%Y`.tar.gz *


# Run the program with first cenario
run_first:
./$(EDIR)/iMe ./tests/in/cenario1 ./tests/out/cenario1 -l cenario1.log -t 1000

#Run the program with second cenario
run_second:
./$(EDIR)/iMe ./tests/in/cenario2 ./tests/out/cenario2 -l cenario2.log -t 1000

虽然一切运行完美,但我想将运行部分分离到另一个名为tests.sh的文件,然后在makefile中调用它们。

这样的事情:

#!/bin/sh
# Run the program with first cenario
run_first:
./bin/iMe ./tests/in/cenario1 ./tests/out/cenario1 -l cenario1.log -t 1000

#Run the program with second cenario
run_second:
./bin/iMe ./tests/in/cenario2 ./tests/out/cenario2 -l cenario2.log -t 1000

然而,这不起作用,因为我不知道如何在makefile中调用它...任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要将选项作为参数传递给tests.sh脚本。这是makefile的内容

# make the iMe program
#

SDIR=src
IDIR=include
CFLAGS=-I$(IDIR)

ODIR=obj
EDIR=bin

LIBS=-lrt -lpthread

_DEPS = 1.h 2.h 3.h 4.h 5.h 6.h 7.h 8.h 9.h 10.h 11.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = 1.o 2.o 3.o 4.o 5.o 6.o 7.o 8.o 9.o 10.o 11.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
    gcc -c -o $@ $< $(CFLAGS) -O3

$(EDIR)/iMe: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

# Clean 

clean:
    -rm -f ./$(ODIR)/* $(OBJ) ./$(EDIR)/*

# Backup 

backup:
    tar -czvf backup_iMe_`date +%d-%m-%Y`.tar.gz *


./tests.sh 1
#./tests.sh 2

然后是tests.sh

的内容
#!/bin/sh
# Run the program with first scenario
scenario=$1
if [ $1 -eq 1 ]
then
./bin/iMe ./tests/in/cenario1 ./tests/out/cenario1 -l cenario1.log -t 1000
elif [ $1 -eq 2 ]
then
#Run the program with second scenario
#run_second:
./bin/iMe ./tests/in/cenario2 ./tests/out/cenario2 -l cenario2.log -t 1000
fi