[Scons-users] How to find the directory of the invoking SConstruct

Hans Peter Jepsen hpj at lodam.com
Fri Jul 19 05:28:34 EDT 2013


Did you find useful solution for this. Here is my approach.

I ended up with putting this code into my (toplevel) SConstruct file:

import os
this_sconscript_file = (lambda x:x).func_code.co_filename
BUILDDIR = os.path.dirname(this_sconscript_file)

This is based on a solution, that I found here: http://stackoverflow.com/questions/9806573/does-scons-know-in-which-directory-a-sconscript-file-resides

There is nothing scons specific about it. You define a lambda function, which you do you not use for anything else than taking the func_code.co_filename value (the filename, where the function is defined) from it.

I tried other things before this:

BUILDDIR = GetLaunchDir()
BUILDDIR= Dir('#/.').abspath

but both give me the current directory, when it I launch scons, and not the directory of the SConstruct file.

E.g. with the command
/home/hpj$ scons -f /project/p/SConstruct
I get /home/hpj with the last two approach, but /project/p with the first approach.

I use scons, version 2.3.0 under Cygwin.


Hans Peter Jepsen
Lodam Electronics, Denmark

> -----Original Message-----

> From: scons-users-bounces at scons.org [mailto:scons-users-

> bounces at scons.org] On Behalf Of Russell, J.J.

> Sent: 13. juli 2013 22:44

> To: scons-users at scons.org

> Subject: [Scons-users] How to find the directory of the invoking SConstruct


> For a variety of reasons, we discourage folk from cd'ing into directory where

> the SConstruct file lives to invoke the build. They have a tendency to drop

> crud in the directory, or worse, of accidentally invoking "rm *" via careless

> command line recall actions.


> We would prefer that they invoke the command using the -f <file> command

> line option, where 'file' usually includes a directory path. However, when

> doing this relative paths within the SConstruct file are misinterpreted, e.g.,

> SConscript ('src/Sconscript') fails. This is certainly consistent with SCons not

> 'cd'ing into the SConstruct directory ala 'make'.


> One way that works is -C<directory> followed by -f<file-name>. This seems

> a little clunky, but, if that's the way it is intended to work, then okay. I just

> worry that a user mistakenly specifying the full path to the SConstruct file via

> -f will be, at best, frustrated and, at worst, get something he did not bargain

> for.


> I want to make sure I am not missing the correct/intended way to do this.



> JJRussell

> _______________________________________________

> Scons-users mailing list

> Scons-users at scons.org

> http://four.pairlist.net/mailman/listinfo/scons-users

More information about the Scons-users mailing list