Skip to main content
 首页 » 编程设计

python之scons 顺序命令在依赖树中不符合预期

2025年05月04日7linjiqin

我正在运行一个由 SConstruct 调用的 SConscript,它除了设置环境和 Export('env') 外什么都不做。 SConscript 应该遍历文件名类似于 mod_abc.c 的文件,并且对于这些文件中的每一个 - 首先创建一个 xml 目录,生成一个 structdoc,创建一个文件 mod_abc_post.c,然后创建一个目标文件和一个“.so”文件。之后,它应该删除 xml 文件并重新启动下一个 mod_*.c 文件的进程。 这是脚本:

import os 
Import('env') 
 
my_libs = 'jansson' 
postc_files = Glob('mod_*_post.c') 
all_mods = Glob('mod_*.c') 
 
mods = set(all_mods) - set(postc_files) 
mods = list(mods) 
 
env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME']=1 
 
xml_cmd_str = '(cat ../Doxyfile.configxml; echo "INPUT=%s";) | doxygen - > xml%s' 
structdoc_cmd_str = 'python ../prep_structdoc.py xml mod_config mod_mtx update_mtx serialize_mtx "mod_evt_" > %s' 
preprocess_cmd_str = 'python ../preprocess_mod.py xml %s %s > %s' 
 
 
for mod in mods: 
    #create doxy file 
    xml_dir = env.Command('xml%s' % mod.name, mod, xml_cmd_str % (mod.name, mod.name)) 
 
    mod_name = mod.name[:-2] 
    struct_doc = '%s.structdoc' % mod_name 
 
    #using Command instead of os.popen as clean can take care of structdoc 
    sdoc = env.Command(struct_doc, xml_dir, structdoc_cmd_str % struct_doc) 
 
    processed_file= '%s_post.c' % mod_name 
    pfile = env.Command(processed_file, sdoc, preprocess_cmd_str % (mod_name, struct_doc, processed_file)) 
 
    obj_file = env.Object(target='%s.o' % mod_name, source=pfile) 
 
    shared_target = '%s.so' % mod_name 
    env.SharedLibrary(target=shared_target, source=obj_file, LIBS=my_libs) 
 
    py_wrapper = env.Command('%s.py' % mod_name, pfile, 'ctypesgen.py %s %s -o %s' % (processed_file, mod.name, '%s.py' % mod_name)) 
 
    # remove xml once done 
    remove_xml = env.Command('dummy%s' %mod.name, py_wrapper, 'rm -rf xml') 

我已经注意 xml_dir 目标获得一个特定的名称,因为该 xml 命令应该只针对该 mod_name 运行。问题是依赖树不符合预期。 我希望每个文件都有这样的树

-remove xml   
--create py_wrapper   
---create so file   
----create o file   
-----create _post.c file   
------create .structdoc file   
-------create xml directory   

但是我通过执行 scons --tree=ALL 得到的只是其中之一 mod_serialize_example.c 是:

不是按顺序来的,中间还有一些东西是给其他 mod_*.c 文件用的。

[Some other things before this] 
 +-dummymod_serialize_example.c 
  | +-mod_serialize_example.py 
  | | +-mod_serialize_example_post.c 
  | | | +-mod_serialize_example.structdoc 
  | | | | +-xmlmod_serialize_example.c 
  | | | | | +-mod_serialize_example.c 
  | | | | +-/usr/bin/python 
  | | | +-/usr/bin/python 
  | | +-/usr/local/bin/ctypesgen.py 
  | +-/bin/rm 
[Some other things after this] 
 
 +-libmod_serialize_example.so 
  | +-mod_serialize_example.o 
  | | +-mod_serialize_example_post.c 
  | | | +-mod_serialize_example.structdoc 
  | | | | +-xmlmod_serialize_example.c 
  | | | | | +-mod_serialize_example.c 
  | | | | +-/usr/bin/python 
  | | | +-/usr/bin/python 
  | | +-mod_serialize_example.c 
  | | +-/path/to/header files included 
  | | +-/usr/bin/gcc 
  | +-/usr/bin/gcc 
 +-mod_addition.c [ Some other module ] 
 
 +-mod_serialize_example.c 
  +-mod_serialize_example.o 
  | +-mod_serialize_example_post.c 
  | | +-mod_serialize_example.structdoc 
  | | | +-xmlmod_serialize_example.c 
  | | | | +-mod_serialize_example.c 
  | | | +-/usr/bin/python 
  | | +-/usr/bin/python 
  | +-mod_serialize_example.c 
  | +-/path/to/header files included... 
  | +-/usr/bin/gcc 
 +-mod_serialize_example.py 
  | +-mod_serialize_example_post.c 
  | | +-mod_serialize_example.structdoc 
  | | | +-xmlmod_serialize_example.c 
  | | | | +-mod_serialize_example.c 
  | | | +-/usr/bin/python 
  | | +-/usr/bin/python 
  | +-/usr/local/bin/ctypesgen.py 
  +-mod_serialize_example.structdoc 
  | +-xmlmod_serialize_example.c 
  | | +-mod_serialize_example.c 
  | +-/usr/bin/python 
  +-mod_serialize_example_post.c 
  | +-mod_serialize_example.structdoc 
  | | +-xmlmod_serialize_example.c 
  | | | +-mod_serialize_example.c 
  | | +-/usr/bin/python 
  | +-/usr/bin/python 
  +-pfile 
  +-xml 
[some other stuff] 
 +-xmlmod_serialize_example.c 
    +-mod_serialize_example.c 

我对 mod_serialize_example.c 的期望是

+-rm xml 
 |+-libmod_serialize_example.so 
      | +-mod_serialize_example.o 
      | | +-mod_serialize_example_post.c 
      | | | +-mod_serialize_example.structdoc 
      | | | | +-xmlmod_serialize_example.c 
      | | | | | +-mod_serialize_example.c 
      | | | | +-/usr/bin/python 
      | | | +-/usr/bin/python 
      | | +-mod_serialize_example.c 
      | | +-/path/to/header files included 
      | | +-/usr/bin/gcc 
      | +-/usr/bin/gcc 

但是,我看到了这一点,而且比要求的要多得多。 (上面的也是手动完成的,以提供一个过程的概念,请原谅 + 和 | 的缩进)
他们不应该聚在一起吗? (如预期的树所示,并针对不同的文件名像循环一样重复)。
此外,我刚刚开始使用 scons,任何有助于使此设计更简洁的帮助都会有所帮助。
1.我想知道如何得到期望的树
2. 我怎样才能让这个脚本接受一个模块名称并只在该名称上运行 for 循环代码。
示例:scons mod_abc.c 应该只为此创建 .so 文件。 截至目前,如果我这样做,这不会产生任何结果。

请您参考如下方法:

为什么你会期待这样一棵树?例如,您的共享库没有(显式或隐式)任何其他内容的依赖性。因此它将作为图表顶部的目标之一。