title: Does scons know in which directory a SConscript file resides?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>Edit</strong> From <a href="https://stackoverflow.com/questions/6066545/scons-get-abspath-of-original-file-as-though-i-hadnt-set-variant-dir">this reply</a> it looks like</p>
  
  <pre><code>File('sources.lib').srcnode().abspath
  </code></pre>
  
  <p>returns the proper filename and directory, <strong>but</strong> it won't tell you if it exists (must use os.path.isfile for that). It also appears that</p>
  
  <pre><code>Dir('.').srcnode().abspath
  </code></pre>
  
  <p>will tell you where the SConstruct file resides.</p>
  
  <p><strong>Example</strong> When defining which source files to compile for a library, I don't want to use</p>
  
  <pre><code>lib = env.SharedLibrary('mylib', Glob('*.cpp'))
  </code></pre>
  
  <p>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</p>
  
  <pre><code>lib = env.SharedLibrary('mylib', env.getSources('*.cpp'))
  </code></pre>
  
  <p>and making a function that reads the file if it exists</p>
  
  <pre>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)</pre>
  
  <p>This seems to work. I didn't know about File.srcnode().abspath until today.</p>