Search code examples
pythonbuildscons

How to compile with two different compilers using SCons?


I'm trying to set up a complete build environment with SCons and I came across this problem:

My project can be compiled with two different compilers (c or cpp compilers) and the resulting object files linked with the same linker.

Because of this, I need to know how to split the compilation part from the linking part.

Also, there are cases when I only need the .o files so I want to avoid linking.

Is this possible using the same environment ?


Solution

  • Here is a minimal working example to demonstrate the capabilities you describe.

    >> tree
    .
    ├── main.cpp
    └── SConstruct
    
    0 directories, 2 files
    
    >> cat main.cpp
    #include <iostream>
    int main() { std::cout << "Hello World" << std::endl; }
    
    >> cat SConstruct 
    # Create Build Environment
    env = Environment()
    
    # Create Option for Toolchain
    AddOption('--toolchain', dest='toolchain', choices=['gnu', 'clang'],
              default='gnu', help='Toolchain Specification')
    env.Replace(TOOLCHAIN=GetOption('toolchain'))
    if env['TOOLCHAIN'] == 'clang':
        env.Replace(CXX='clang++')
    
    # Build Object File
    obj = env.Object('main.cpp')
    
    # Conditionally Build Executable
    AddOption('--build-exe', action='store_true', dest='build_exe', default=False,
              help='Build Executable')
    if GetOption('build_exe'):
        env.Program(obj)
    
    >> scons --version
    SCons by Steven Knight et al.:
        script: v2.3.6.rel_2.3.5:3347:d31d5a4e74b6[MODIFIED], 2015/07/31 14:36:10, by bdbaddog on hpmicrodog
        engine: v2.3.6.rel_2.3.5:3347:d31d5a4e74b6[MODIFIED], 2015/07/31 14:36:10, by bdbaddog on hpmicrodog
        engine path: ['/usr/lib/scons/SCons']
    Copyright (c) 2001 - 2015 The SCons Foundation
    
    >> scons --help
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    usage: scons [OPTION] [TARGET] ...
    
    SCons Options:
      -b, -d, -e, -m, -S, -t, -w, --environment-overrides, --no-keep-going,
      --no-print-directory, --print-directory, --stop, --touch
                                  Ignored for compatibility.
      -c, --clean, --remove       Remove specified targets and dependencies.
      -C DIR, --directory=DIR     Change to DIR before doing anything.
      --cache-debug=FILE          Print CacheDir debug info to FILE.
      --cache-disable, --no-cache
                                  Do not retrieve built targets from CacheDir.
      --cache-force, --cache-populate
                                  Copy already-built targets into the CacheDir.
      --cache-readonly            Do not update CacheDir with built targets.
      --cache-show                Print build actions for files from CacheDir.
      --config=MODE               Controls Configure subsystem: auto, force,
                                    cache.
      -D                          Search up directory tree for SConstruct,
                                    build all Default() targets.
      --debug=TYPE                Print various types of debugging information:
                                    count, duplicate, explain, findlibs, includes,
                                    memoizer, memory, objects, pdb, prepare,
                                    presub, stacktrace, time.
      --diskcheck=TYPE            Enable specific on-disk checks.
      --duplicate=DUPLICATE       Set the preferred duplication methods. Must be
                                    one of hard-soft-copy, soft-hard-copy,
                                    hard-copy, soft-copy, copy
      -f FILE, --file=FILE, --makefile=FILE, --sconstruct=FILE
                                  Read FILE as the top-level SConstruct file.
      -h, --help                  Print defined help message, or this one.
      -H, --help-options          Print this message and exit.
      -i, --ignore-errors         Ignore errors from build actions.
      -I DIR, --include-dir=DIR   Search DIR for imported Python modules.
      --implicit-cache            Cache implicit dependencies
      --implicit-deps-changed     Ignore cached implicit dependencies.
      --implicit-deps-unchanged   Ignore changes in implicit dependencies.
      --interact, --interactive   Run in interactive mode.
      -j N, --jobs=N              Allow N jobs at once.
      -k, --keep-going            Keep going when a target can't be made.
      --max-drift=N               Set maximum system clock drift to N seconds.
      --md5-chunksize=N           Set chunk-size for MD5 signature computation to
                                    N kilobytes.
      -n, --no-exec, --just-print, --dry-run, --recon
                                  Don't build; just print commands.
      --no-site-dir               Don't search or use the usual site_scons dir.
      --profile=FILE              Profile SCons and put results in FILE.
      -q, --question              Don't build; exit status says if up to date.
      -Q                          Suppress "Reading/Building" progress messages.
      --random                    Build dependencies in random order.
      -s, --silent, --quiet       Don't print commands.
      --site-dir=DIR              Use DIR instead of the usual site_scons dir.
      --stack-size=N              Set the stack size of the threads used to run
                                    jobs to N kilobytes.
      --taskmastertrace=FILE      Trace Node evaluation to FILE.
      --tree=OPTIONS              Print a dependency tree in various formats: all,
                                    derived, prune, status.
      -u, --up, --search-up       Search up directory tree for SConstruct,
                                    build targets at or below current directory.
      -U                          Search up directory tree for SConstruct,
                                    build Default() targets from local SConscript.
      -v, --version               Print the SCons version number and exit.
      --warn=WARNING-SPEC, --warning=WARNING-SPEC
                                  Enable or disable warnings.
      -Y REPOSITORY, --repository=REPOSITORY, --srcdir=REPOSITORY
                                  Search REPOSITORY for source and target files.
    
    Local Options:
      --toolchain=TOOLCHAIN       Toolchain Specification
      --build-exe                 Build Executable
    
    >> scons
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    g++ -o main.o -c main.cpp
    scons: done building targets.
    
    >> tree
    .
    ├── main.cpp
    ├── main.o
    └── SConstruct
    
    0 directories, 3 files
    
    >> scons --build-exe
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    g++ -o main main.o
    scons: done building targets.
    
    >> tree
    .
    ├── main
    ├── main.cpp
    ├── main.o
    └── SConstruct
    
    0 directories, 4 files
    
    >> scons --toolchain=clang --build-exe
    scons: Reading SConscript files ...
    scons: done reading SConscript files.
    scons: Building targets ...
    clang++ -o main.o -c main.cpp
    clang++ -o main main.o
    scons: done building targets.