9
votes

We are evaluating scons as a build system, and I am having a problem accomodating our old system. In some of our source code subdirectories, we have a "sources.lib" file that lists the names of the C++ files that need to be compiled to assemble that directory's target library. But, there are additional C++ files in the same directory, so I can't just use Glob() to find the appropriate ones.

How do I find out which directory a SConscript file resides in? os.getcwd() always returns the build directory. Even though the documentation states that paths in a SConscript are relative to the source directory (or else Glob('*.cpp') wouldn't work), just doing an open('sources.lib') fails because it looks for the file in the build directory. Finally, the build environment in that SConscript file doesn't contain the actual current source directory.

Edit From this reply it looks like

File('sources.lib').srcnode().abspath

returns the proper filename and directory, but it won't tell you if it exists (must use os.path.isfile for that). It also appears that

Dir('.').srcnode().abspath

will tell you where the SConstruct file resides.

Example When defining which source files to compile for a library, I don't want to use

lib = env.SharedLibrary('mylib', Glob('*.cpp'))

but instead would rather construct a function that first checks for the existence of "sources.lib" and if it does not exist, use globbing. So I'm defining my library like so

lib = env.SharedLibrary('mylib', env.getSources('*.cpp'))

and making a function that reads the file if it exists

def getSources(self, pattern):

    # list of source files to assign to a target
    sources = []
    # srcFile = 'sources.lib' # failed
    # srcFile = os.path.join(os.getcwd(), 'sources.lib') # failed
    srcFile = File('sources.lib').srcnode().abspath # works

    # look for sources.lib
    try:
        infile = open(srcFile,'r')
    except IOError:
        #print "Globbing to get sources"
        sources = Glob(pattern, strings=True)
    else:
        #print "Reading sources.lib"
        for line in infile.readlines():
            line = line.rstrip('\n\r')
            if line != '':
                sources.append(line)

    return sources

buildEnv.AddMethod(getSources)

This seems to work. I didn't know about File.srcnode().abspath until today.

2
To determine if a file exists, you can use os.path.exists() - Brady
I don't understand your problem, create small example which illustrate it. - Torsten
Your question saved me :) Dir('.').srcnode().abspath worked for me. I use this option env.SConscriptChdir(0) to avoid directory "slides". Thanks! - Destroyica
Me too! I was looking to form an additional python import path (for sys.path.append), based on the current SConscript location, and os.path.dirname(os.path.abspath(file)) did not work (no file defined), but your Dir('.') method worked fine. - Evgen

2 Answers

5
votes

I use the following code:

this_sconscript_file = (lambda x:x).func_code.co_filename
code_base = os.path.dirname(this_sconscript_file)
2
votes

There are 3 types of paths in SCons:

  1. Relative to the root SConstruct prepending '#' to the path
  2. Relative to the SConscript not using the '#'.
  3. Absolute path. I think this is self-explanatory :)

If you need to deal with paths outside of the directory where the SConscript is, you should use the '#'

It should work both ways in this example, but the path with the '#' seems more explicit and intuitive to me:

./SConstruct
./dirA/SConscript - use '#dirA/sources.lib' OR 'sources.lib'
./dirB/SConscript - use '#dirB/sources.lib' OR 'sources.lib'

Hope this helps,

Brady