[Scons-users] Observation over Sources / Target variable substitution

RW garlicbready at googlemail.com
Sun Sep 16 06:40:27 EDT 2018


Hi,
One of the things I've been looking into recently is if some of the
builders I've written will work within subdirectories that contain spaces.
I've noticed some interesting behaviour, this might be a bug, maybe
expected behaviour or perhaps just a mis-use of the API

I've noticed that you can use env.subst() with $TARGETS and $SOURCES by
specifying a target or source parameter inside a generator function
env.subst('${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM} ${SOURCES}
>${TARGET}', source=source, target=target)
However this fails to wrap the sources or targets in double quotes.
I recognise this is more of a convenience than anything else since you
could just for loop over the target / source lists and add them in that way
adding in double quotes at the same time.

I've included some examples of things I've tried below, different ways or
calling an external command from within a builder
Also I realise in the below example I probably shouldn't be using
${_CCCOMCOM} but it just makes life easier for writing a gcc preprocessor.
Also I realise that what I'm doing with the below builder is simple enough
to fit into a single text string
this is just an example as I've also got some other more complicated
builders that require generator functions.


## Example 1

# This works fine, the sources and target are wrapped in double quotes for
when the path has a space in it
# arm-none-eabi-gcc -E -Wall -Werror -std=c99 -nostdlib "D:\Temp\Test
Folder 1\testbuild\src1\test1.c" "D:\Temp\Test Folder
1\testbuild\src1\test2.c" > "D:\Temp\Test Folder 1\testbuild\src1\test1.E"

def generate(env):
    act = [__Output_func,
           SCons.Action.Action("${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM}
${SOURCES} >${TARGET}")]
    bld = env.Builder(
        action=act,
        suffix=".E",
        src_suffix=".c")
    env.AppendUnique(BUILDERS={'PreProcessBuild': bld})


def __Output_func(target, source, env):
    print "PREPROCESS '%s'" % (target[0])


## Example 2

# This also works fine same as above

    bld = Builder(
        action='${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM} ${SOURCES}
>${TARGET}',
        suffix=".E",
        src_suffix=".c")


## Example 3

# Prefixing the Builder with .env results in no variable substitution
# just an "E >" in the output window

    bld = env.Builder(
        action='${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM} ${SOURCES}
>${TARGET}',
        suffix=".E",
        src_suffix=".c")

## Example 4

# No variable substitution same as before with just an "E >" in the output
window

    act = env.Action('${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM}
${SOURCES} >${TARGET}')
    bld = Builder(
        action=act,
        suffix=".E",
        src_suffix=".c")


## Example 5

# Lets switch to using a generator function
# CPP and CFLAGS and others are expanded out this time around which is
different from before but not SOURCES or TARGET
# looking at scons src with inbuilt builders Scons tends to use Executor
instead of Execute to expand SOURCES and TARGET
# but I suspect Executor is not a public api

def generate(env):
    bld = env.Builder(
        action=__Build_func,
        suffix=".E",
        src_suffix=".c")
    env.AppendUnique(BUILDERS={'PreProcessBuild': bld})


def __Build_func(target, source, env):
    print "PREPROCESS '%s'" % (target[0])
    act = env.Action('${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM}
${SOURCES} >${TARGET}')
    env.Execute(act)

## Example 6

# This nearly works, but env.subst doesn't wrap SOURCES or TARGET in double
quotes
# so will fail on paths containing spaces
# arm-none-eabi-gcc -E -Wall -Werror -std=c99 -nostdlib D:\\Temp\\Test
Folder 1\\testbuild\\src1\\test1.c D:\\Temp\\Test Folder
1\\testbuild\\src1\\test2.c >D:\\Temp\\Test Folder
1\\testbuild\\src1\\test1.E

def generate(env):
    bld = env.Builder(
        action=__Build_func,
        suffix=".E",
        src_suffix=".c")
    env.AppendUnique(BUILDERS={'PreProcessBuild': bld})


def __Build_func(target, source, env):
    print "PREPROCESS '%s'" % (target[0])
    tststr = env.subst('${CPP} -E ${CFLAGS} ${CCFLAGS} ${_CCCOMCOM}
${SOURCES} >${TARGET}', source=source, target=target)
    env.Execute(tststr)


Many Thanks
Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20180916/9cdd9df8/attachment.html>


More information about the Scons-users mailing list