简体   繁体   English

在scons中创建SConscript对象

[英]creating SConscript objects in scons

I have my project targets (binaries and libraries) specified in json files. 我在json文件中指定了我的项目目标(二进制文件和库)。

I can create an environment for the specified target without any problem. 我可以为指定目标创建环境,而不会出现任何问题。 Now I'm trying to support a specific build directory. 现在,我正在尝试支持特定的构建目录。

My knowledge about scons is still very basic but it seems that the right way to do that is using a SConscript together with VariantDir. 我对scons的了解仍然很基础,但似乎正确的方法是将SConscript与VariantDir一起使用。 But I already have my targets specified in json and creating a SConscript file for each target would be redundant (plus a cost in maintenance). 但是我已经在json中指定了我的目标,并且为每个目标创建一个SConscript文件将是多余的(加上维护成本)。

So my question is: is it possible to create a SConscript object dynamically, at run time? 所以我的问题是:是否可以在运行时动态创建SConscript对象?

Thanks in advance. 提前致谢。

You can specify the build directory with the VariantDir() function, or as part of the SConscript() call. 您可以使用VariantDir()函数或在SConscript()调用中指定构建目录。 All of the different options are discussed here . 这里讨论了所有不同的选项。 Considering you dont want to use several SConstruct files, you should just use the VariantDir() function as described in more detail here . 考虑到你不想使用几个SConstruct文件,你应该只使用VariantDir()函数更详细的描述在这里

Here is a simple example: 这是一个简单的示例:

env = Environment() env = Environment()

# It may be as simple as setting src_dir='.', but set accordingly
# duplicate=0 tells SCons NOT to copy source files to variantDir, set accordingly
# VariantDir() can be called multiple times so as to change dirs per builder call
VariantDir(variant_dir = 'pathToBuildDir', src_dir = 'pathToSource', duplicate=0)
# Now call the builders here

Its still not clear why you want to mix json with SCons. 仍然不清楚为什么要将JSON与SCons混合使用。 Unless you have some very compelling reasons to do so, I would suggest keeping it all in SCons, which is Python. 除非您有非常令人信服的理由,否则建议将其全部保存在SCons(Python)中。

EDIT: I just realized you asked about creating a SConscript object, not a file. 编辑:我刚刚意识到你问有关创建一个SConscript对象,而不是一个文件。

I looked through the SCons programming APIs and didnt find anything that allows you to create a SConscript object. 我浏览了SCons编程API,没有找到任何可以创建SConscript对象的东西。 Actually, I dont think the concept of a SConscript object exists, since it just treats calls to the SConscript() function as files that need to be opened and processed, and they are almost treated as an extension to the SConstruct. 实际上,我认为SConscript对象的概念不存在,因为它只是将对SConscript()函数的调用视为需要打开和处理的文件,并且几乎将它们视为SConstruct的扩展。

So, to summarize: You'll either have to create subsidiary SConscript files, or work with calls to VariantDir(). 因此,总结一下:您要么必须创建辅助SConscript文件,要么使用对VariantDir()的调用。 Depending on your project directory structure, it may not be necessary to create SConscript files. 根据您的项目目录结构,可能没有必要创建SConscript文件。 You could just do everything from the root SConstruct. 您可以从根SConstruct进行所有操作。 The SConscript files arent necessary, they just help organize the build scripts better. 没有必要的SConscript文件,它们只是有助于更好地组织构建脚本。

VariantDir doesn't work with SConscturct file (may be i wrong, but i don't found any way to do it). VariantDir不能与SConscturct文件一起使用(可能是我错了,但是我找不到任何方法来执行此操作)。 Just create SConscript file with variant dir and doing what you need. 只需创建带有变体dir的SConscript文件并执行所需的操作即可。

#SConsruct
env = CreateEnvironment()
SConscript('SConscript', variant_dir = 'mybuilddir', exports = 'env', duplicate = 0)

# Do all work in SConscript
Import('env')
env.Program(...)
env.SharedLibrary(...)
...

Also, you can split your process into 2 states. 另外,您可以将过程分为2个状态。 State 1 - generated SConscript files. 状况1 - 生成SConscript文件。 State 2 - run generated SConscript files. 状态2-运行生成的SConscript文件。

if 'generate' in COMMAND_LINE_TARGETS:
  # your code to generated SConscript from json
  Exit(0)

sconscriptFiles = getSconscriptFiles() # some code to get your sconscript, by mask for example
if len(sconscriptFiles) < 1:
    print "You need to generate files at first: scons generate"
    Exit(1)
for file in sconscriptFiles :
   SConscript(file, variant_dir = 'build' + file, duplicate = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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