getting the scons root directory

Jason S picture Jason S · Sep 22, 2009 · Viewed 7.6k times · Source

I need to run two programs in sequence as part of a custom builder.

One of them is a program that I'm stuck with and can't deal with absolute/relative paths so I have to use the chdir=1 option of the Builder so that its actions run in the same directory as the target.

The second is a script that is located in the tools subdirectory of the project; the SConstruct file is in the root of the project. I need to create an action to run this script, and am having trouble because I have neither the absolute path to the project, nor a relative path from the directory in which the target is located back up to the tools subdirectory where the script is located. If I could somehow get the absolute path to the root directory of my project, I'd be all set, I could just concatenate `tools/myscript.bar' and be done with it.

Here's what I have, more or less:

env['BUILDERS']['FooBar'] = Builder(action = [
    'c:/bin/foo.exe ${SOURCE.filebase}',
    'c:/bin/bar-interpreter.exe myscript.bar ${SOURCE.filebase}',
    ], chdir=1);

The problem is that I need to change the action in question so that "myscript.bar" can be found, something like:

env['BUILDERS']['FooBar'] = Builder(action = [
    'c:/bin/foo.exe ${SOURCE.filebase}',
    'c:/bin/bar-interpreter.exe $PATHTOHERE/tools/myscript.bar ${SOURCE.filebase}',
    ], chdir=1);

This seems so simple but I can't figure out how.

Answer

richq picture richq · Sep 22, 2009

You should use "#" to indicate the top of the source directory.

print Dir('#').abspath

This version works if you use a variant directory too. For example in SConstruct:

SConscript('main.scons', variant_dir="build")

Then in main.scons:

print Dir('.').abspath
print Dir('#').abspath

The first will print /path/to/project/build, whereas the second will show the correct /path/to/project.