1

I set some additional attributes in env in the wscript in the configure and the build step. I realize a feature as Task.Task and I need to retrieve some of the information stored in env, but this does not work.

MWE and the produced error:

from waflib import Context, Options
from waflib import Task, TaskGen
from waflib.Tools.compiler_c import c_compiler

def options(opt):
    opt.load('compiler_c')

def configure(cnf):
    cnf.load('compiler_c')
    cnf.env.abc = 'abc'

def build(bld):
    print('BUILD: bld.env.abc: {}'.format(bld.env.abc))  # works
    bld.program(features=['t_1'], source='main.c')

class t_1(Task.Task):
    print('T_1: bld.env.abc: {}'.format(bld.env.abc))  # does not work
    run_str = 'echo hello'
    color = 'RED'


@TaskGen.feature('t_1')
@TaskGen.after('apply_link')
def add_t_1_task(self):
    try:
        link_task = self.link_task
    except AttributeError as err:
        print err
        return
    self.create_task('t_1')

Running the script, produces the following error:

$ python waf-2.0.2 configure build
Waf: The wscript in '/cygdrive/c/test' is unreadable
Traceback (most recent call last):
  File "/cygdrive/c/test/.waf-2.0.2-b8fa647d13364cbe0c1c8ec06042b54d/waflib/Scripting.py", line 101, in waf_entry_point
    set_main_module(os.path.normpath(os.path.join(Context.run_dir,Context.WSCRIPT_FILE)))
  File "/cygdrive/c/test/.waf-2.0.2-b8fa647d13364cbe0c1c8ec06042b54d/waflib/Scripting.py", line 141, in set_main_module
    Context.g_module=Context.load_module(file_path)
  File "/cygdrive/c/test/.waf-2.0.2-b8fa647d13364cbe0c1c8ec06042b54d/waflib/Context.py", line 360, in load_module
    exec(compile(code,path,'exec'),module.__dict__)
  File "/cygdrive/c/test/wscript", line 16, in <module>
    class t_1(Task.Task):
  File "/cygdrive/c/test/wscript", line 17, in t_1
    print('T_1: bld.env.abc: {}'.format(bld.env.abc))  # does not work
NameError: name 'bld' is not defined

How can I use attributes of conf.env or bld.env in a task derived from Task.Task when I can't use bld.env?

In the documentation of Task.Task it says I have to provide an env - but how do I do it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
wafwafwaf
  • 191
  • 9

1 Answers1

2

You use string interpolation. See for example the source of the asmprogram task:

class asmprogram(link_task):
    "Links object files into a c program"
    run_str = '${ASLINK} ${ASLINKFLAGS} ${ASLNK_TGT_F}${TGT} ${ASLNK_SRC_F}${SRC}'
    ext_out = ['.bin']
    inst_to = '${BINDIR}'

Here ASLINK, ASLINKFLAGS, ASLNK_TGT_F, ASLNK_SRC_F and BINDIR comes from the environment. You can also access the variables directly in a method of your task:

class Name(Task.Task):
    # ...
    def run(self):
        # ...
        print self.env.VAR_NAME
neuro
  • 14,948
  • 3
  • 36
  • 59
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
  • I'm not sure if I understand your answer. Your answer only help me, if I want to use a **new** `env` variable in the Task I generate and then use it in the `run_str`. But I want to use a `env` variable I created in `configure`. – wafwafwaf Jan 22 '18 at 16:08
  • Why would it only help you if you want to use a new env variable? Using the method I described, environment variables defined in configure will be visible to the building tasks. – Björn Lindqvist Jan 22 '18 at 17:04
  • Yep - it works! Sorry didn't get it the first time. But it now works like I expected! – wafwafwaf Jan 22 '18 at 17:14