我在env
和wscript
步骤的configure
build
中设置了一些其他属性。我认为某个功能为Task.Task
,我需要检索env
中存储的一些信息,但这不起作用。
MWE和产生的错误:
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')
运行脚本会产生以下错误:
$ 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
当我无法使用conf.env
时,如何在bld.env
派生的任务中使用Task.Task
或bld.env
的属性?
在Task.Task的文档中,它说我必须提供env
- 但我该怎么做?
答案 0 :(得分:2)
您使用字符串插值。例如,请参阅asmprogram
任务的来源:
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}'
此处ASLINK
,ASLINKFLAGS
,ASLNK_TGT_F
,ASLNK_SRC_F
和BINDIR
来自环境。您还可以直接在任务方法中访问变量:
class Name(Task.Task):
# ...
def run(self):
# ...
print self.env.VAR_NAME