[Scons-users] Modify CXXFile Builder

Stijn De Ruyck Stijn.DeRuyck at onsemi.com
Wed Nov 19 06:18:43 EST 2014


Hi Dirk,

Thanks, actually, starting from your example, I got it to work in an even simpler way.
I didn't understand why I had to create a new Builder "by pulling out the required Actions/Emitters" like you said (I'm new to SCons), so I simply tried the following:

------
env = Environment(tools=['default','textfile'])
env.Replace(LEXFLAGS=['-Cfr','-p'])

grammarSource = env.CXXFile(target = 'format/ecf/BisonGrammar.cc', source = 'format/ecf/Grammar.yy')
scannerSource = env.CXXFile(target = 'format/ecf/FlexScanner_temp.cc', source = 'format/ecf/Scanner.ll')
replacedScannerSource = env.Textfile(target = 'format/ecf/FlexScanner.cc', source = scannerSource, SUBST_DICT = {'#include <FlexLexer.h>' : ''})

Depends(grammarSource, replacedScannerSource)#if scanner.cc changes, grammar.cc must be rebuilt
------

Which correctly gives me the "cleaned" output file. The downside is the intermediate FlexScanner_temp.cc that gets left behind...

I tried your approach, but it didn't work at first:

------
env = Environment(tools=['default','textfile'])
env.Replace(LEXFLAGS=['-Cfr','-p'])
flexBuilder = Builder(action = SCons.Tool.lex.LexAction, emitter = SCons.Tool.lex.lexEmitter)
env.Append(BUILDERS={'Flex' : flexBuilder})

grammarSource = env.CXXFile(target = 'format/ecf/BisonGrammar.cc', source = 'format/ecf/Grammar.yy')
env.Flex(target = 'format/ecf/FlexScanner_temp.cc', source = 'format/ecf/Scanner.ll')
replacedScannerSource = env.Textfile(target = 'format/ecf/FlexScanner.cc', source = 'format/ecf/FlexScanner_temp.cc', SUBST_DICT = {'#include <FlexLexer.h>' : ''})

Depends(grammarSource, replacedScannerSource) #if scanner.cc changes, grammar.cc must be rebuilt too
------

What happens here is format/ecf/FlexScanner.cc is created containing the text "format/ecf/FlexScanner_temp.cc" inside ... :)
I got it working by replacing "env.Flex" with "scannerSource = env.Flex" and using that variable as source in the env.Textfile call.

------
scannerSource = env.Flex(target = 'format/ecf/FlexScanner_temp.cc', source = 'format/ecf/Scanner.ll')
replacedScannerSource = env.Textfile(target = 'format/ecf/FlexScanner.cc', source = scannerSource, SUBST_DICT = {'#include <FlexLexer.h>' : ''})
------

Now it works like the first solution (but also leaving behind the temp file). Still the first solution is simpler, no?
What else could I be missing?

Best regards,

Stijn

From: Scons-users [mailto:scons-users-bounces at scons.org] On Behalf Of Dirk Bächle
Sent: Tuesday, November 18, 2014 6:08 PM
To: scons-users at scons.org
Subject: Re: [Scons-users] Modify CXXFile Builder

Hi Stijn,

On 18.11.2014 10:38, Stijn De Ruyck wrote:
Hello,

I'm struggling to find the best way to extend the CXXFile Builder functionality.
In short, in our make-based project we have this:

file.cc: scanner.l
      /usr/bin/flex++ -Cfr -p -file.cc scanner.l; cat file.cc | sed 's/#include <FlexLexer.h>//g' > file_new.cc; mv file_new.cc file.cc

What is the best way to translate this to SCons?
SCons already supports bison/flex out of the box, as well as replacing text in files:


  env = Environment(tools=['default','textfile'])
  env.Replace(LEXFLAGS=['-Cfr','-p'])
  env.Replace(LEXCOM='$LEX $LEXFLAGS -$TARGET $SOURCE')

  import SCons.Tool.lex
  import SCons.Builder.Builder
  flexBuilder = SCons.Builder.Builder(action = SCons.Tool.lex.LexAction,
                                                           emitter = SCons.Tool.lex.lexEmitter)
  env.Append(BUILDERS={'Flex' : flexBuilder})

  env.Flex('file.in','scanner.l')
  env.Substfile('file.cc','file.in', SUBST_DICT = {'#include <FlexLexer.h>' : ''})


(untested and from the top of my head, just to show the basic concept that should be followed)
The tricky part here is, that the "lex.py" Tool doesn't add any Builder methods to the current Environment that a user could call. It expects you to simply say:

  env.Program('foo', 'bar.l' + other_sources)

, which automatically calls (f)lex on the given file and returns the resulting C(++) file. But for the "sed" step one needs full control, so I grab into the lex.py Tool, pull out the required Actions/Emitters and add a new Method to the environment.

Hope you can get it to work like this, and if you're successful...maybe you could create a pull request for lex.py, adding the "Flex" method as Builder that can be called explicitly anytime?

Best regards,

Dirk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20141119/17027979/attachment.html>


More information about the Scons-users mailing list