def _build_file(self, luafiles): # 添加文件头 llbc_file_path = op.join(Cfg.getscriptpath(), 'llbc.lua') f = open(llbc_file_path, 'wb') f.write('--[[\n') f.write('@file llbc.lua\n') f.write('@author {0}\n'.format(Cfg.getauthor())) f.write('@version {0}\n'.format(Cfg.getver())) f.write('@date ----/--/--\n') f.write('\n') f.write('@brief llbc lua wrap library(lullbc)\n') f.write('--]]\n\n') # 整合所有脚本 for luafile in luafiles: with open(luafile, 'r') as lf: f.write('-- ******** SCRIPT: {0} ********\n'.format( luafile[len(self._script_path) + 1:]).replace('\\', '/')) f.write(lf.read()) f.write('\n\n') # 最后返回lib table f.write('\n') f.write('-- return library\n') f.write('return llbc\n') # 关闭文件 f.close()
def _build_cpp_file(self): cpp_file_path = op.join(self.search_path, '_{0}Methods.h'.format(self.filename_base)) cpp_file = CppFile(cpp_file_path, author=Cfg.getauthor(), ver=Cfg.getver(), include_macro_prefix=self.proj_name.upper()) cpp_file.addincl('{0}/common/LibHeader.h'.format(self.proj_name)) cpp_file.addincl('{0}/common/Macro.h'.format(self.proj_name)) return cpp_file
def build(): """生成""" # Generate file head. imp = Cfg.getprojname().upper() cpp_file_path = op.join(Cfg.getcodepath(), 'pyllbc_scripts.h') cpp_file = CppFile(cpp_file_path, author=Cfg.getauthor(), ver=Cfg.getver(), include_macro_prefix=imp) cpp_file.addincl('pyllbc/common/LibHeader.h') cpp_file.addincl('pyllbc/common/ScriptIntegrator.h') # Generate constructor/destructor. class_name = 'pyllbc_Scripts' ctor = CppFun(class_name, rtn='', visit=CppVisit(CppVisit.PUBLIC)) for root, dirs, files in os.walk(Cfg.getscriptpath()): for f in files: fpath = op.join(root, f) if '.svn' in fpath: continue elif op.splitext(fpath)[1].lower() != '.py': continue script_cnt = PyIntegratorBuilder.__transfer_to_cpp_str(fpath) ctor.addstmt('_scripts.insert(::std::make_pair("{0}", new ::pyllbc_ScriptIntegrator({1})));' .format(op.basename(fpath), script_cnt)) dtor = CppFun('~{0}'.format(class_name), rtn='', visit=CppVisit(CppVisit.PUBLIC)) dtor.addstmt('LLBC_STLHelper::DeleteContainer(_scripts);') clazz = CppClass(class_name) clazz.addmethod(ctor) clazz.addmethod(dtor) # Generate integrate method. integrate_meth = CppFun('Integrate', rtn='int', visit=CppVisit(CppVisit.PUBLIC)) integrate_meth.addstmt("for (::std::map<LLBC_String, " "::pyllbc_ScriptIntegrator *>::iterator it = _scripts.begin();") integrate_meth.addstmt(' it != _scripts.end();') integrate_meth.addstmt(' it++)') integrate_meth.addstmt(' if (it->second->Integrate() != LLBC_OK)') integrate_meth.addstmt(' return LLBC_FAILED;') integrate_meth.addstmt('') integrate_meth.addstmt('return LLBC_OK;') clazz.addmethod(integrate_meth) # Generate data member. data_mem = CppData('::std::map<LLBC_String, ::pyllbc_ScriptIntegrator *>', '_scripts', visit=CppVisit(CppVisit.PRIVATE)) clazz.adddata(data_mem) cpp_file.addcls(clazz) cpp_file.adddef('pyllbc_s_Scripts LLBC_Singleton<{0}, LLBC_DummyLock>::Instance()'.format(class_name)) cpp_file.build()
def _build_cpp_file(self): cpp_file_path = op.join(self.search_path, '_{0}Methods.h'.format(self.filename_base)) cpp_file = CppFile(cpp_file_path, author=Cfg.getauthor(), ver=Cfg.getver(), include_macro_prefix=self.proj_name.upper()) cpp_file.custom_filehead = Cfg.getlicensehead( LangType.cplusplus ) + '\n//!!! This file is auto generated by script tool, do not modify it!!!\n' cpp_file.addincl('{0}/common/LibHeader.h'.format(self.proj_name)) cpp_file.addincl('{0}/common/Macro.h'.format(self.proj_name)) return cpp_file
def main(): # 收集所有lua native methods并进行整合 code_path = Cfg.getcodepath() LuNMC(code_path, classname_base='', filename_base=Cfg.getprojname().capitalize()).build() # 读取当前编译是否处于debug模式 debug = not (len(sys.argv) < 3 or sys.argv[2].upper() != 'DEBUG') print 'Build in {0} mode...'.format(debug and 'Debug' or 'Release') # 整合所有lua脚本 print 'Integrate lua scripts...' LuScriptIntegrator(debug).build() print 'Done!'
def build(self): """构建方法文件""" # 确认是否可构建 if not self._buildable(): return False # 创建cpp文件对象, 用于存放自动生成代码 proj_name = self.proj_name cpp_file = self._build_cpp_file() # 取得所有方法 methods = {} code_path = Cfg.getcodepath() r = self._build_filematch_re() for root, dirs, files in os.walk(self.search_path): for f in files: fpath = op.join(root, f) if '.svn' in fpath or '.git' in fpath: continue elif fpath == cpp_file.fpath: continue if not r.match(f): continue file_methods = self._parse_file(fpath) if not file_methods: continue incl = fpath[len(op.dirname(code_path)) + 1:] cpp_file.addincl(incl) methods.update(file_methods) # 创建c++类 cls_name = self._build_cpp_cls_name() cls = CppClass(cls_name) first_data = True ctor = CppFun(cls_name, rtn='', visit=CppVisit(CppVisit.PUBLIC)) for meth_name, meth in methods.iteritems(): ctor.addstmt('{0}.ml_name = {1};'.format(meth_name, meth['name'])) ctor.addstmt('{0}.ml_meth = {1};'.format(meth_name, meth['meth'])) ctor.addstmt('{0}.ml_flags = {1};'.format(meth_name, meth['flags'])) ctor.addstmt('{0}.ml_doc = {1};'.format(meth_name, meth['doc'])) visit = CppVisit(CppVisit.PUBLIC) if first_data else CppVisit(CppVisit.NONE) cls.adddata(CppData('::PyMethodDef', meth_name, visit=visit)) first_data = False cls.addmethod(ctor) cpp_file.addcls(cls) cpp_file.adddef('pyllbc_s_{0}Methods LLBC_Singleton< ::pyllbc_{0}Methods, LLBC_DummyLock>::Instance()' .format(self.search_base)) cpp_file.build() return True
def _build_file(self, luafiles): # 添加文件头 llbc_file_path = op.join(Cfg.getscriptpath(), 'llbc.lua') f = open(llbc_file_path, 'wb') f.write(Cfg.getlicensehead() + '\n') # 整合所有脚本 for luafile in luafiles: with open(luafile, 'r') as lf: f.write('-- ******** SCRIPT: {0} ********\n'.format( luafile[len(self._script_path) + 1:]).replace('\\', '/')) f.write(lf.read()) f.write('\n\n') # 最后返回lib table f.write('\n') f.write('-- return library\n') f.write('return llbc\n') # 关闭文件 f.close()
def build(self): """构建方法文件""" # 确认是否可构建 if not self._buildable(): return False # 创建cpp文件对象, 用于存放自动生成代码 proj_name = self.proj_name cpp_file = self._build_cpp_file() # 取得所有方法 methods = {} code_path = Cfg.getcodepath() r = self._build_filematch_re() for root, dirs, files in os.walk(self.search_path): for f in files: fpath = op.join(root, f) if '.svn' in fpath or '.git' in fpath: continue elif fpath == cpp_file.fpath: continue if not r.match(f): continue file_methods = self._parse_file(fpath) if not file_methods: continue incl = fpath[len(op.dirname(code_path)) + 1:] cpp_file.addincl(incl) methods.update(file_methods) # 构造luaL_Reg[] 定义 def _build_luareg(meth): if not meth: return '\n {NULL, NULL}' else: return '\n {{"{0}", {1}}},'.format(meth['lua_name'], meth['name']) luareg = 'static luaL_Reg lullbc_NativeMethods[] = {' for meth in methods.itervalues(): luareg += _build_luareg(meth) luareg += _build_luareg(None) luareg += '\n}' cpp_file.addcodeline(luareg) cpp_file.build() return True
def __init__(self, search_path, classname_base=None, filename_base=None): self.search_path = search_path self.proj_name = Cfg.getprojname() self.search_base = op.basename(search_path).capitalize() if classname_base is not None: self.classname_base = classname_base else: self.classname_base = self.search_base if filename_base is not None: self.filename_base = filename_base else: self.filename_base = self.search_base
def main(): # 复制llbc.lua到目标目录 target_path = sys.argv[3] is_debug = sys.argv[2].upper() == 'DEBUG' or False src = op.join(Cfg.getscriptpath(), 'llbc.lua') if not is_debug: target = op.join(target_path, 'llbc.lua') else: target = op.join(target_path, 'llbc_debug.lua') print 'Copy [{0}] to [{1}]...'.format(src, target) shutil.move(src, target) print 'Done!'
def main(): print 'Build methods...', code_path = Cfg.getcodepath() PyNMC(op.join(code_path, 'common')).build() PyNMC(op.join(code_path, 'testcase')).build() PyNMC(op.join(code_path, 'core')).build() PyNMC(op.join(code_path, 'comm')).build() PyNMC(op.join(code_path, 'application')).build() print 'Done' print 'Build script integrator...', PyIntegratorBuilder.build() print 'Done' sleep(1.618)
def __init__(self, debug): self._debug = debug self._script_path = Cfg.getscriptpath()