[Scons-users] Running two commands after build

Adam Wysocki scons at chmurka.net
Tue Aug 11 08:33:28 EDT 2015


Hello,

Hi,

I'm trying to use scons as my build environment and I have some problems. 
I use scons 2.3.6 with Python 2.7 on Windows 7 64-bit, but the solution 
should be portable to Linux.

I need to compile and link C++ sources into an executable. That goes fine. 
However, I need to execute two commands after a file has been linked. First 
one (let's call it c1) modifies the generated executable file, second one 
(let's call it c2) generates another file basing on (modified by c1) 
executable file.

Also I need to have separate source and build directories. So the sequence 
is:

- compile src/test/test.cpp to build/test/test.o
- link build/test/test.o to build/test.out (.out is my executable suffix)
- execute c1 on build/test.out (build/test.out will be modified)
- execute c2 on build/test.out (build/test.out will NOT be modified, 
  build/test.out.x will be created)

My simple test environment can be created with:

mkdir build
mkdir src
mkdir src\test
echo int main; > src\test\test.cpp

My sconstruct file for this environment:

---------------------------------------
# My executable file suffix is .out
env = Environment(PROGSUFFIX = '.out')

# I have sources in src directory, but all output should be in build directory
env.VariantDir('build', 'src', duplicate = 0)

# Let's compile the program, it goes fine
p = env.Program('build/test.out', Glob('build/test/*.cpp'))

# After a program is compiled, I need to modify it's executable
c1 = env.Command(None, 'build/test.out', 'echo test >> ${TARGET}.out')

# After this executable has been modified, I need to generate new file (test.out.x)
c2 = env.Command('build/test.out.x', 'build/test.out', 'echo test >> ${TARGET}')

# Let's add dependencies
env.Depends(c1, p)
env.Depends(c2, c1)
---------------------------------------

Now how it works and how I expect it will work.

---------------------------------------
T:\tmp\scons>scons -Q
echo test >> build\test.out
g++ -o build\test\test.o -c src\test\test.cpp
Assembler messages:
Fatal error: can't create build\test\test.o: No such file or directory
scons: *** [build\test\test.o] Error 1
---------------------------------------

Two things happened here.

1. Command "c1" was executed before command "p", but it should be executed 
   after command "p"

2. scons did not create subdirectory for object files (it does it when c1, 
   c2 and Depends are commented out)

When I create this intermediate directory by hand, all goes fine.

---------------------------------------
T:\tmp\scons>mkdir build\test

T:\tmp\scons>scons -Q
g++ -o build\test\test.o -c src\test\test.cpp
g++ -o build\test.out build\test\test.o
echo test >> build\test.out
echo test >> build\test.out.x
---------------------------------------

Which leads to my second problem. When I execute scons once again, command c2 
gets executed once again:

---------------------------------------
T:\tmp\scons>scons -Q
echo test >> build\test.out.x
---------------------------------------

When I execute scons one more time, it does not execute c2 (which is correct):

---------------------------------------
T:\tmp\scons>scons -Q
scons: `.' is up to date.
---------------------------------------

Ok, now to my questions.

1. Is it possible to make scons behave as expected even when no intermediate 
   directory for object files is found? Is it a bug in scons, or in my 
   sconstruct file?

2. Is it possible to make scons execute c2 only one time (so the second 
   invocation will lead to "`.' is up to date")?

Thanks for any feedback!

-- 
AW


More information about the Scons-users mailing list