如何在SCons中构建目标之前和之后显示消息

时间:2015-02-19 09:42:33

标签: build makefile scons

我必须将Makefile项目转换为SCons,我遇到了一些问题。让我们假设一个make文件如下:

.PHONY : clean all
all : test_1 test_2

clean :
      rm -rf *.o
test_1 : 
      @echo "---------------Test_1 Build Started-------------------"
      g++ -std=gnu++11 test_1.cpp -o target_1
      @echo "---------------Test_1 Build Finished-------------------"
test_2 :
      @echo "---------------Test_2 Build Started-------------------"
      g++ -std=gnu++11 test_2.cpp -o target_2
      @echo "---------------Test_2 Build Finished-------------------"

如果我运行Makefile,它将首先运行test_1然后运行test_2。通过@echo,我们可以打印构建所在的每个步骤。 Scons的问题是,Scons首先读取所有的scirpt并打印消息(如果有的话)。然后它开始构建目标。这意味着

print "---------------Test_1 Build Started-------------------"
test_1 = env.Program(source = 'test_1.cpp', target = 'target_1')
print "---------------Test_1 Build Finished-------------------"

print "---------------Test_2 Build Started-------------------"
test_2 = env.Program(source = 'test_2.cpp', target = 'target_2')
print "---------------Test_2 Build Finished-------------------"

无法按预期工作。首先,它将打印所有消息,然后它将开始构建。如何创建我在Makefile中执行的确切方案?

此外,使用Makefile我只能通过运行" make test_1"来运行一个块。或" make test_2"。我怎样才能在Scons中这样做?在此先感谢:)

注意:使用Alias可以在Scons中完成,但是如果我使用像

这样的Alias

env.Alias('test_1', test_1)

并运行" scons test_1",它可以构建目标但是" scons -c"不会删除目标。有没有更好的方法呢?

2 个答案:

答案 0 :(得分:1)

您可以像这样使用AddPostActionAddPreAction

def pre_action(target, source, env):
    print("$TARGET build started")

AddPreAction(test_1, pre_action)

答案 1 :(得分:1)

要回答有关从命令行构建特定目标的问题,只需在命令行上指定它即可:

scons test_1.o -> builds test_1.o and anything it depends on
scons target_1 -> builds target_1 and anything it depends on