简体   繁体   中英

echo of var to file in SConscript

In an SConscript file I have a env variable BUILDID_STR that contains a C string that I wish to output to a file.

bstr = env['BUILDID_STR']
print(bstr)

which when printed, print(bstr) correctly shows this:

//this file is automatically generated
static char* build_str="0|0.1.0|2014-05-29_16:16:51";

However, I can't get the var expanded/exported correctly, just the literal string is output instead of the above text:

cat src/log/src/version.c
env[BUILDID_STR]

Here's the pertinent part of my SConscript file

env.Command(target='#/src/log/src/version.c',
        source=libSrcfiles,
        action="echo env['BUILDID_STR'] > $TARGET")


env.SharedLibrary('log', [libSrcfiles, '#/src/log/src/version.c'])

I've also tried the code in a function and also passing to a shell script, all with the same result. The reason I have .../version.c in the SharedLibrary is that my goal is to have the .c file generated only when on of the libSrcfiles is built, thereby version.c is compiled-in.

The " textfile " Tool offers two Builders Textfile() and Substfile() for cases like this. You probably want to use the first:

env = Environment(tools=['textfile'])
env['BUILDID_STR'] = 'A test'
env.Textfile('test.txt', ['$BUILDID_STR'])

As you have seen, the action is not expanding the env variable. In the action, you can refer to env variables with the $VAR syntax (just like referring to the $SOURCE and $TARGET variables provided by SCons):

env.Command(target='#/src/log/src/version.c',
    source=libSrcfiles,
    action="echo $BUILDID_STR > $TARGET")

This solution may not handle a BUILDID_STR containing multiple lines.

You may want to investigate a system that builds your source file from a template (instead of constructing the file contents entirely within a string). The Substfile Builder referenced in this previous question might be a good starting point -- you can provide a list of (key, value) pairs to be substituted in an input file. The Substfile Builder is built into recent versions of SCons.

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