简体   繁体   中英

scons error using Glob in SConscript

I've been running into a rather odd error involving the use of the scons function Glob in a SConscript. My actual build script is more complicated, but I have narrowed it down to the following minimal example.

In the base SConstruct:

SConscript('SConscript',
           variant_dir='build')

Then, in the SConscript:

Glob('*.cc')

This exits, with the error message TypeError : Tried to lookup Dir 'build' as a File. This has been tested with both v2.1.0 and v2.3.0.

I have found a number of workarounds, none of which are entirely satisfactory.

  • If I move the SConscript to a subdirectory, scons runs without error. However, that would then require my entire src tree to be moved to a subdirectory, which feels messy.
  • If I create the directory build prior to running scons, scons runs without error. However, this requires an extra step, and empty directories do not play nicely with git.
  • I could add a line Execute(Mkdir('build')) prior to calling the SConscript. However, this does not work when performing a dry run with scons -n .
  • I can call the SConscript with duplicate=False , which prevents the error. However, as I understand the documentation, this can cause errors in the build, depending on the location of include files.

I'm fumbling around, and not really understanding the root cause of the issue. Is there a clean solution to this problem?

Edit: It was requested that I add additional details of my intention, not just code that causes the error message. I am attempting to make a build file for cross-compiling both linux executable and Windows executables simultaneously.

First, setting up the compilation environments in the SConstruct.

import os

win32 = Environment()
win64 = Environment()
linux = Environment()

#Define the working directory
win32['SYS'] = 'win32'
win64['SYS'] = 'win64'
linux['SYS'] = 'linux'

#Define the compilers
win32.Replace(CXX='i686-w64-mingw32-g++')
win64.Replace(CXX='x86_64-w64-mingw32-g++')

#Define the appropriate file formats
win32.Replace(SHLIBPREFIX='')
win32.Replace(SHLIBSUFFIX='.dll')
win32.Replace(PROGSUFFIX='.exe')
win32.Append(LINKFLAGS='-static')
win64.Replace(SHLIBPREFIX='')
win64.Replace(SHLIBSUFFIX='.dll')
win64.Replace(PROGSUFFIX='.exe')
win64.Append(LINKFLAGS='-static')

for env in [win32,win64,linux]:
    build_dir = os.path.join('build',env['SYS'])
    exe = SConscript('SConscript',
                     variant_dir=build_dir,
                     exports=['env'])

Then, having the actual build rules in the SConscript.

Import('env')

env.Append(CPPPATH=['include'])

for main in Glob('*.cc'):
    env.Program([main, Glob('src/*.cc')])

This exhibits the error message shown above when called with scons -n .

Your problem is that you're using variant_dir to "link" your build folder to "." as your source directory. Combined with the default "duplicate=1" option, this means that SCons tries to replicate all sources into "build"...but the latter is part of the source folder too. This opens the door to all sorts of complications and circular dependencies.

The clean solution is to put all your sources into their own subdirectory, eg "src", such that you can reference the contained SConscript as

  SConscript('src/SConscript', variant_dir='build')

which will make the right things happen. This is the preferred setup, and even if you don't like this convention you should adopt it anyway, in order to save you from future headaches.

Additional tip: You can (and should) save some startup time by using

win32 = Environment()
win64 = win32.Clone()
linux = win32.Clone()

instead of

win32 = Environment()
win64 = Environment()
linux = Environment()

. In the latter case you're forcing SCons to search for compilers/tools in your system three times in a row...just do this once, then clone() to different environments and decorate them further with your build-specific settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM