Search code examples
scons

Scons - How to stop cleaning dependent targets/files on cleaning final target?


I have two targets A and B. B depends on A, therefore if we build B, also A is built. now if B is cleaned, A is cleaned as well. A is a collection of .obj-files and the resulting library . request: When cleaning B, nothing of target A is touched/removed Here is my code:

    {env = Environment()
     src1 = ['tridip.c', 'tridip1.c', 'tridip2.c']
     obj1 = [ env.StaticObject(sf) for sf in src1 ]
     lib = env.Library('tridip', obj1)
     Alias('library', lib)
     src2 = ['tridip3.c']
     obj2 = [ env.StaticObject(sf) for sf in src2 ]
     exe = Program(obj2, LIBS=lib, LIBPATH='.')
     Alias('exe', exe)
}

Target A is tridip.lib tridip.obj tridip1.obj tridip2.obj Target B is :tridip3.exe /LIBPATH:. tridip.lib tridip3.obj

Request: How do I stop cleaning target A on cleaning target B.


Solution

  • You need the NoClean function. Here is how to update your SConstruct.

    env = Environment()
    src1 = ['tridip.c', 'tridip1.c', 'tridip2.c']
    obj1 = [ env.StaticObject(sf) for sf in src1 ]
    lib = env.Library('tridip', obj1)
    if 'library' not in COMMAND_LINE_TARGETS:
        env.NoClean([obj1, lib])
    Alias('library', lib)
    src2 = ['tridip3.c']
    obj2 = [ env.StaticObject(sf) for sf in src2 ]
    exe = Program(obj2, LIBS=lib, LIBPATH='.')
    Alias('exe', exe)
    

    Which when run produces the following...

    >> 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
    .
    ├── SConstruct
    ├── tridip1.c
    ├── tridip1.h
    ├── tridip2.c
    ├── tridip2.h
    ├── tridip3.c
    ├── tridip.c
    └── tridip.h
    
    0 directories, 8 files
    
    >> scons library
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    gcc -o tridip.o -c tridip.c
    gcc -o tridip1.o -c tridip1.c
    gcc -o tridip2.o -c tridip2.c
    ar rc libtridip.a tridip.o tridip1.o tridip2.o
    ranlib libtridip.a
    scons: done building targets.
    
    >> tree
    .
    ├── libtridip.a
    ├── SConstruct
    ├── tridip1.c
    ├── tridip1.h
    ├── tridip1.o
    ├── tridip2.c
    ├── tridip2.h
    ├── tridip2.o
    ├── tridip3.c
    ├── tridip.c
    ├── tridip.h
    └── tridip.o
    
    0 directories, 12 files
    
    >> scons library -c
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Cleaning targets ...
    Removed tridip.o
    Removed tridip1.o
    Removed tridip2.o
    Removed libtridip.a
    scons: done cleaning targets.
    
    >> tree
    .
    ├── SConstruct
    ├── tridip1.c
    ├── tridip1.h
    ├── tridip2.c
    ├── tridip2.h
    ├── tridip3.c
    ├── tridip.c
    └── tridip.h
    
    0 directories, 8 files
    
    >> scons
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    gcc -o tridip.o -c tridip.c
    gcc -o tridip1.o -c tridip1.c
    gcc -o tridip2.o -c tridip2.c
    ar rc libtridip.a tridip.o tridip1.o tridip2.o
    ranlib libtridip.a
    gcc -o tridip3.o -c tridip3.c
    gcc -o tridip3 tridip3.o -L. libtridip.a
    scons: done building targets.
    
    >> scons -c
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Cleaning targets ...
    Removed tridip3.o
    Removed tridip3
    scons: done cleaning targets.
    
    >> tree
    .
    ├── libtridip.a
    ├── SConstruct
    ├── tridip1.c
    ├── tridip1.h
    ├── tridip1.o
    ├── tridip2.c
    ├── tridip2.h
    ├── tridip2.o
    ├── tridip3.c
    ├── tridip.c
    ├── tridip.h
    └── tridip.o
    
    0 directories, 12 files