0

I have my project targets (binaries and libraries) specified in json files.

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

So my question is: is it possible to create a SConscript object dynamically, at run time?

Thanks in advance.

user1192525
  • 657
  • 4
  • 20
  • Why are the targets specified in json files? To use SCons, a SConstruct file must exist, whose syntax is Python. SConscript files are typically for subdir hierarchy builds. Can you please specify more what your intentions are and show an example json file. – Brady Sep 29 '12 at 17:23
  • Hi Brady. I have a SConstruct file where I build the scons environment based on the information specified on the json files. This works. Regarding the json files there are two two types. The first type specifies platform, toolchain, etc. and the second type the describes a module (binary or library), sources, dependencies, etc. The idea is that you can build modules for different profiles (platform, build options.etc). – user1192525 Sep 29 '12 at 17:35
  • I just realized you are asking about creating SConscript Objects. I originally thought you were asking about creating dynamic SConscript files. I updated the answer to reflect this. – Brady Oct 04 '12 at 06:26

2 Answers2

0

You can specify the build directory with the VariantDir() function, or as part of the SConscript() call. 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.

Here is a simple example:

    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. Unless you have some very compelling reasons to do so, I would suggest keeping it all in SCons, which is Python.

EDIT: I just realized you asked about creating a SConscript object, not a file.

I looked through the SCons programming APIs and didnt find anything that allows you to create a SConscript object. 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.

So, to summarize: You'll either have to create subsidiary SConscript files, or work with calls to VariantDir(). Depending on your project directory structure, it may not be necessary to create SConscript files. You could just do everything from the root SConstruct. The SConscript files arent necessary, they just help organize the build scripts better.

Brady
  • 10,207
  • 2
  • 20
  • 59
0

VariantDir doesn't work with SConscturct file (may be i wrong, but i don't found any way to do it). Just create SConscript file with variant dir and doing what you need.

#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. State 1 - generated SConscript files. State 2 - run generated SConscript files.

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)
Torsten
  • 21,726
  • 5
  • 24
  • 31