wafwafwaf
wafwafwaf

Reputation: 191

Use configure and/or build attributes in Task.Task in waf

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?

Upvotes: 1

Views: 648

Answers (1)

Gaslight Deceive Subvert
Gaslight Deceive Subvert

Reputation: 20428

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

Upvotes: 2

Related Questions