nowox
nowox

Reputation: 29066

How to put objects into a separated build folder in SCons?

I have this very simple project:

.
├── hello.c
├── lib
│   ├── foo.c
│   ├── foo.h
│   └── SConstruct
├── Makefile
└── SConstruct

After the build I would like to get this:

.
├── build
│   ├── hello
│   ├── hello.o
│   └── lib
│       ├── libfoo.a
│       └── foo.o
│
├── config.log
├── hello.c
├── lib
│   ├── foo.c
│   ├── foo.h
│   └── SConstruct
├── Makefile
└── SConstruct

I tried to add

VariantDir('build', '.')

But it doesn't work. Here is my SConstruct file

env = Environment(CC = 'gcc',
    CCFLAGS=['-O2', '-std=c99'],
)

Progress(['-\r', '\\\r', '|\r', '/\r'], interval=5)
env.Program('Hello', 'hello.c',
    LIBS=['foo'],
    LIBPATH='lib/',
    CPPPATH=['lib']
)
Decider('MD5-timestamp')

VariantDir('build', '.')
SConscript(['lib/SConstruct'])

Edit

I also tried to add the variant_dir directly to the SConscript directive:

SConscript(['lib/SConstruct'], variant_dir='build')

But I have an error:

$ scons -Q
gcc -o Hello hello.o -Llib -lfoo
/usr/bin/ld: cannot find -lfoo
collect2: error: ld returned 1 exit status
scons: *** [Hello] Error 1

It seems SCons does not consider the CPPPATH anymore because I don't have any -Ilib during the build.

Upvotes: 0

Views: 680

Answers (2)

bdbaddog
bdbaddog

Reputation: 3511

From reading the above:

env = Environment(CC = 'gcc',
    CCFLAGS=['-O2', '-std=c99'],
)

Progress(['-\r', '\\\r', '|\r', '/\r'], interval=5)

# explicitly specify target dir and file base so object 
# isn't in source directory.
object = env.Object('build/hello','hello.c')

env.Program('Hello', object,
    LIBS=['foo'],
    LIBPATH='build/lib/',
    CPPPATH=['build/lib']
)


Decider('MD5-timestamp')

VariantDir('build', '.')
SConscript('lib/SConstruct','build/lib')

Since you don't want to move source files into a subdir of the directory the SConstruct is to make this work, you'll need to manually specify the target directories (This is in line with make's functionality)

Upvotes: 0

dirkbaechle
dirkbaechle

Reputation: 4052

Don't use the VariantDir() method directly, but the variant_dir keyword of the SConscript call instead. For this, it would be a good idea to move your sources into a separate "src" folder (see example below).

The relevant chapter in the User Guide is 15 "Separating Source and Build Directories".

You can also find a working example at https://bitbucket.org/dirkbaechle/scons_talks in the pyconde_2013/examples/exvar folder.

Upvotes: 1

Related Questions