scons 是否知道 SConscript 文件驻留在哪个目录中? [英] Does scons know in which directory a SConscript file resides?

查看:49
本文介绍了scons 是否知道 SConscript 文件驻留在哪个目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在评估 scons 作为构建系统,但我在适应我们的旧系统时遇到了问题.在我们的一些源代码子目录中,我们有一个sources.lib"文件,其中列出了需要编译以组装该目录的目标库的 C++ 文件的名称.但是,在同一个目录下还有额外的 C++ 文件,所以我不能只使用 Glob() 来找到合适的.

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.

如何找出 SConscript 文件所在的目录?os.getcwd() 总是返回构建目录.即使文档指出 SConscript 中的路径是相对于源目录的(否则 Glob('*.cpp') 将不起作用),只是执行 open('sources.lib') 失败,因为它寻找构建目录中的文件.最后,该 SConscript 文件中的构建环境不包含实际的当前源目录.

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.

编辑来自这个回复看起来像

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

返回正确的文件名和目录,它不会告诉你它是否存在(必须使用 os.path.isfile ).看来也是

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

会告诉您 SConstruct 文件所在的位置.

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'))

而是宁愿构造一个函数,该函数首先检查sources.lib"是否存在,如果不存在,则使用通配符.所以我像这样定义我的图书馆

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)

这似乎有效.直到今天我才知道 File.srcnode().abspath.

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

推荐答案

我使用以下代码:

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

这篇关于scons 是否知道 SConscript 文件驻留在哪个目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆