Search code examples
scons

How to specify variant directory in SConscript


I am new to SCons and need help in understanding why my build script is not producing the required output. Any pointers to what I am doing wrong would be greatly appreciated.

Below is my file structure:

.
├── prog_1
│   ├── hello.c
│   └── SConscript
└── SConstruct

Heres how I want it to look after running scons -u from the prog_1 sub-directory.

.
├── prog_1
│   ├── build
│   │   └── hello.o
│   ├── hello.c
│   ├── prog_1.out
│   └── SConscript
└── SConstruct

Contents of the SConstruct file:

env = Environment()
env['CC'] = 'gcc'
env['CCFLAGS'] = Split("""
-std=c99
-Wall
""")

Export('env')

Contents of the prog_1/SConscript file:

Import('env')

build = env.Clone()
build['CCFLAGS'] += ['-DENABLE_FEAT_1']

build.VariantDir('build', '.', duplicate=0)
build_src = build.Glob('build/*.c')

import os
prog_name = os.getcwd().split(os.sep)[-1]
build.Program(prog_name +'.out', source = build_src)

Clean('.','build')

When I run scons -u from the prog_1 sub-directory, I get this as output:

[ananya@firenze prog_1]$ scons -u
scons: Entering directory `/home/ananya/test/scons-test'
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `prog_1' is up to date.
scons: done building targets.

Edit: Added output description.

Edit2: Added script correction.


Solution

  • You are very close. You just need to specify your SConscript file in the SConstruct file.

    Your SConstruct should be as follows...

    import os
    env = Environment()
    env['CC'] = 'gcc'
    env['CCFLAGS'] = Split("""
                           -std=c99
                           -Wall
                           """)
    
    Export('env')
    
    SConscript(os.path.join('prog_1', 'SConscript'))
    

    Then when you run will get the following output...

    >> scons --version
    SCons by Steven Knight et al.:
        script: v2.3.4, 2014/09/27 12:51:43, by garyo on lubuntu
        engine: v2.3.4, 2014/09/27 12:51:43, by garyo on lubuntu
        engine path: ['/usr/lib/scons/SCons']
    Copyright (c) 2001 - 2014 The SCons Foundation
    
    >> tree ..
    ..
    ├── prog_1
    │   ├── hello.c
    │   └── SConscript
    └── SConstruct
    
    1 directory, 3 files
    
    >> scons -u
    scons: Entering directory `/nfs/users/bellockk/SandBox/tmp'
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    scons: building associated VariantDir targets: prog_1/build
    gcc -o prog_1/build/hello.o -c -std=c99 -Wall -DENABLE_FEAT_1 prog_1/hello.c
    gcc -o prog_1/prog_1.out prog_1/build/hello.o
    scons: done building targets.
    
    >> tree ..
    ..
    ├── prog_1
    │   ├── build
    │   │   └── hello.o
    │   ├── hello.c
    │   ├── prog_1.out
    │   └── SConscript
    └── SConstruct
    
    2 directories, 5 files