12

I have been using cmake to build my projects out of source, which is really convenient as you avoid polluting your source directory with unnecessary files.

Assuming the CMakeLists.txt is in the current directory, this could be done as follows:

mkdir build
cd build
cmake ..
make

How can I do the same in scons?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
D R
  • 21,936
  • 38
  • 112
  • 149

1 Answers1

14

In your SConstruct file, you use a variant dir:

SConscript("main.scons", variant_dir="build", duplicate=0)

Then in main.scons you set up everything as usual:

env = Environment()
env.Program(target='foo', source=Split('foo.c bar.c'))

It's possible to do this without hardcoding the variant dir into the SConstruct by (ab)using repositories, but that approach has its bugs. For the record, you would run the above as follows to build in another directory:

mkdir mybuild
cd mybuild
scons -Y .. -f ../main.scons

The easiest and most workable is to just use variant_dir. You then run this as usual from the top level source directory. All the build artefacts get produced in the build sub directory.

In response to JesperE's comment, here is how you could write the top level SConstruct to add an optionally named build directory:

AddOption('--build', default='build')
SConscript("main.scons", variant_dir=GetOption('build'), duplicate=0)

Then you would call this from the command line as follows, to create a build directory called "baz":

$ scons --build=baz
Community
  • 1
  • 1
richq
  • 55,548
  • 20
  • 150
  • 144
  • But can't you just pass the value of the `variant_dir` parameter as a command-line argument to avoid hardcoding it? – JesperE Jan 02 '10 at 08:05
  • You can, but you have to remember to pass it to the `scons` command line each time. – richq Jan 02 '10 at 14:09
  • 2
    You could set `variant_dir=GetLaunchDir()` to get CMake-like behaviour: put build outputs in the directory where `scons` was run from. Source of this idea: https://github.com/memsharded/conan-scons-template/blob/master/src/SConstruct – Thomas Feb 11 '19 at 10:29