[Scons-users] Let directory depend on file? (auto generated sources)

Stijn De Ruyck Stijn.DeRuyck at onsemi.com
Thu Dec 4 05:22:05 EST 2014


Hi Dirk,

I came up with a similar regex python approach already, but thanks for reviewing the code :) (Note: I'm stuck on Python 2.4, so no "with open" for me)
I can't get rid of installModel.sh however. It does much more than just build cc's from odl's. (e.g. creates an EyeDB odl and runs EyeDB tools on it.)

Anyway, I'm still not there yet... :(
In the previous example the whole dependency tree behaved as expected. Building/rebuilding/generating/regenerating/cleaning all worked as expected.
But when I tried to add my existing variant_dir setup in the mix, shit started hitting the fan again...

Simplified example demonstrating the problem:

Works =>
            (SConstruct)
def autogen_emitter(target, source, env):
        target = ['autogen/source.c','autogen/source.h']#note: source.c has include directive for source.h
        return target, source

autogenBuilder = Builder(
        action = 'mkdir -p autogen; cp tmp.c.in autogen/source.c; cp tmp.h.in autogen/source.h;',
        emitter = autogen_emitter,
)

env = Environment()
env.Append(CFLAGS=['-w'])
env.Append(BUILDERS = {'Autogen' : autogenBuilder})

sources = ['hello.c']
env.Autogen('autogen/autogenDummyTarget', 'etc/mst.odl')# nonexisting dummy target, will be stripped of by emitter
#Don't directly use Autogen's return value as new input sources for Program() because it will include source.h.
sources.extend(['autogen/source.c'])
prog = env.Program('mstcore', sources)

scons: building `autogen/source.c' because it doesn't exist
mkdir -p autogen; cp tmp.c.in autogen/source.c; cp tmp.h.in autogen/source.h;
scons: building `autogen/source.o' because it doesn't exist
gcc -o autogen/source.o -c -w autogen/source.c
scons: building `hello.o' because it doesn't exist
gcc -o hello.o -c -w hello.c
scons: building `mstcore' because it doesn't exist
gcc -o mstcore hello.o autogen/source.o

Doesn't work =>
      (SConstruct)
def autogen_emitter(target, source, env):
        target = ['autogen/source.c','autogen/source.h']#note: source.c has include directive for source.h
        return target, source

autogenBuilder = Builder(
        action = 'mkdir -p autogen; cp tmp.c.in autogen/source.c; cp tmp.h.in autogen/source.h;',
        emitter = autogen_emitter,
)

env = Environment()
env.Append(CFLAGS=['-w'])
env.Append(BUILDERS = {'Autogen' : autogenBuilder})

sources = ['hello.c']
env.Autogen('autogen/autogenDummyTarget', 'etc/mst.odl')# nonexisting dummy target, will be stripped of by emitter
#Don't directly use Autogen's return value as new input sources for Program() because it will include source.h.
sources.extend(['autogen/source.c'])
Export('sources')
Export('env')
completedEnv = env.SConscript(
        'SConscript_debug',
        variant_dir="build/debug",
        duplicate=0,
)

(SConscript_debug)
Import('env')
Import('sources')
env.Program('#dist/debug/mstcore',sources)
Return("env")

scons: building associated VariantDir targets: build/debug
scons: building `build/debug/autogen/source.o' because it doesn't exist
scons: *** [build/debug/autogen/source.o] Source `autogen/source.c' not found, needed by target `build/debug/autogen/source.o'.

Partial fix I found was by replacing
sources.extend(['autogen/source.c'])
with
sources.extend(['#autogen/source.c'])

But then it dumps .o's in autogen instead of build/debug/autogen...

Best regards,

Stijn

From: Dirk Bächle [mailto:tshortik at gmx.de]
Sent: Wednesday, December 03, 2014 7:48 PM
To: Stijn De Ruyck; SCons users mailing list
Subject: Re: [Scons-users] Let directory depend on file? (auto generated sources)

Hey Stijn,

you're really making some progress there, nice. ;) Find some more tips below...

On 03.12.2014 11:39, Stijn De Ruyck wrote:
Got it, everything works as expected now!

This is what I have:

import os
import fnmatch
import glob
import re


def autogen_emitter(target, source, env):
        #TODO: replace with egrep/awk on "namespace" in mst.odl and fetch stdout
Don't start to "shell out", solve this in Python directly:

    fpath = str(source[0])
    target = []
    if os.path.isfile(fpath):
        with open(fpath, "r") as f:
            contents = f.read()
            re_namespace = re.compile(r"namespace ([^\s]+)")
            for m in re_namespace.finditer(contents):
                target.append(m.group(1)+".c")

, untested and just to give you a direction.

        target = ['source1.c','source2.c'] #don't include the original dummy target
        return target, source

env = Environment()
env.CacheDir(".sconsCache")

sources = ['hello.c']

autogenBuilder = Builder(
        #TODO: replace with call to installModel.sh
I would try to get rid of the "installModel.sh". Am I assuming right that it's not doing anything more than calling your ODLC compiler with a bunch of options? Then, you should try to write and use a proper ODL Builder instead...and pass the required flags and sources to it:

    env = Environment(tools=['default','odl'])
    env['ODLFLAGS'] = ['-whatever','-you','-need']
    env.Odl('mst.odl')
    env.Program(Glob('*.c'))

This is what it *could* look like...

Best regards,

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


More information about the Scons-users mailing list