def _load_main_broc(self): """ load main module's BROC file Returns: return False if fail to load BROC file return True if load BROC file successfully """ # BROC file f = os.path.join(self._main_module.root_path, 'BROC') self._main_env = Environment.Environment(self._main_module) if self._build_mode == "release": self._main_env.DisableDebug() Environment.SetCurrent(self._main_env) try: execfile(f) except BaseException as ex: self._logger.LevPrint("ERROR", 'parsing %s failed(%s)' \ % (self._main_module.broc_cvspath, ex)) return False self._main_env.Action() self._add_env(self._main_module.module_cvspath, self._main_env) return True
def _load_main_broc(self): """ load main module's BROC file Returns: return False if fail to load BROC file return True if load BROC file successfully """ # BROC file f = os.path.join(self._main_module.root_path, 'BROC') self._main_env = Environment.Environment(self._main_module) if self._build_mode == "release": self._main_env.DisableDebug() Environment.SetCurrent(self._main_env) sys.argv = ['NOT PLANISH', None] try: execfile(f) except BaseException as ex: traceback.print_exc() self._logger.LevPrint("ERROR", 'parsing %s failed(%s)' \ % (self._main_module.broc_cvspath, ex)) self._load_ok = False return False self._main_env.Action() self._add_env(self._main_module.broc_cvspath, self._main_env) if not self.InitSubEnvironment(self._main_env): return False return True
def setUp(self): """ init """ sys.argv = ['NOT PLANISH'] module = BrocModule_pb2.Module() module.name = 'broc' module.module_cvspath = 'baidu/broc' module.broc_cvspath = 'baidu/broc/BROC' module.is_main = True module.repo_kind = BrocModule_pb2.Module.GIT module.revision = "1234" module.last_changed_rev = "1236" module.dep_level = 0 #get home dir home = os.environ['HOME'] module.workspace = '%s/unittest_broc/workspace' % home module.root_path = '%s/unittest_broc/workspace/baidu/broc' % home module.url = 'https://github.com/baidu/broc' module.br_kind = BrocModule_pb2.Module.BRANCH module.br_name = 'trunk' #module.commit_id = '5d9819900c2781873aa0ffce285d5d3e75b072a8' self._module = module Function.RunCommand("mkdir -p %s" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello.cpp" % module.root_path, ignore_stderr_when_ok = True) Function.RunCommand("touch %s/hello.h" % module.root_path, ignore_stderr_when_ok = True) self._env = Environment.Environment(module) Environment.SetCurrent(self._env)
def InitSubEnvironment(self, parent): """ to init child env object whose comes from DIRECTORY tag Args: parent : the parent environment object """ subdirs = parent.SubDirs() if not subdirs: return True for subdir in subdirs: child_broc_cvspath = os.path.join(parent.Module().module_cvspath, subdir, 'BROC') child_module = copy.deepcopy(parent.Module()) child_module.broc_cvspath = child_broc_cvspath child_env = Environment.Environment(child_module) if self._build_mode == "release": child_env.DisableDebug() f = os.path.join(parent.Module().workspace, child_broc_cvspath) Environment.SetCurrent(child_env) sys.argv = ['NOT PLANISH', None] try: execfile(f) except BaseException as ex: traceback.print_exc() self._logger.LevPrint("ERROR", 'parsing %s failed(%s)' \ % (self._main_module.broc_cvspath, ex)) self._load_ok = False return False child_env.Action() self._add_env(child_broc_cvspath, child_env) parent.AddSubEnv(child_env) return True
def DIRECTORY(v): """ Add sub directory Args: v : the name of subdirectory, v is relative path """ # gather all dependent module env = Environment.GetCurrent() child_broc_dir = os.path.abspath(os.path.join(env.ModulePath(), v)) if env.ModulePath() not in child_broc_dir: raise BrocArgumentIllegalError("DIRECTORY(%s) is wrong: %s not in %s" % \ (child_broc_dir, env.ModulePath())) child_broc_file = os.path.join(env.Module().root_path, v, 'BROC') if sys.argv[0] == 'PLANISH': parent = sys.argv[1] if not os.path.exists(child_broc_file): raise BrocArgumentIllegalError('Not found %s in Tag Directory(%s)' % (child_broc_file, v)) try: execfile(child_broc_file) except BaseException as err: traceback.print_exc() raise BrocArgumentIllegalError(err) else: # find all targets to build if not os.path.exists(child_broc_file): raise BrocArgumentIllegalError('Not found %s in Tag Directory(%s)' % (child_broc_file, v)) # Log.Log().LevPrint("INFO", 'add sub directory (%s) for module %s' % (v, env._module.module_cvspath)) env.AddSubDir(v)
def Sources(*ss): """ create a group of Sources object all source file should beneathes the directory of module, if not will raise NotInSelfModuleError avoid containing wildcard in ss, so you can use GLOB gathering files firstly, and take the result of GLOB as ss Args: ss : a variable number of artuments, all string arguments in ss will be regarded as source file, the file can be .c, .cpp and so on, and the reset arguments are regarded as compile flags Returns: TagSources Object """ tag = SyntaxTag.TagSources() if sys.argv[0] == 'PLANISH': return tag # FIX ME: you can extend wildcard files, args = _ParseNameAndArgs(*ss) env = Environment.GetCurrent() all_files = GLOB(" ".join(files)).split(' ') for f in all_files: if not f.startswith(os.path.join("broc_out", env.ModuleCVSPath())): f = os.path.join(os.path.dirname(env.BrocCVSPath()), f) src = _CreateSources(f, args) tag.AddSV(src) return tag
def Libs(*ss): """ add .a files to target tag, such as APPLICATION, UT_APPLICATION, STATIC_LIBRARY ss may contain multiple arguments, each argument can contain muitiple lib's path, each lib's path should start with $OUT_ROOT for example Libs("$ROOT_OUT/test/output/lib/libutil.a", "$ROOT_OUT/foo/output/lib/libcommon.a") Args: ss : a variable number of string objects Returns: SyntaxTag.TagLibs() """ tag = SyntaxTag.TagLibs() if sys.argv[0] == 'PLANISH': return tag for s in ss: if not isinstance(s, str): raise BrocArgumentIllegalError("argument %s is illegal in tag Libs" % s) if os.path.isabs(s): tag.AddSV(s) continue elif s.startswith("$OUT_ROOT"): tag.AddSV(os.path.normpath(s.replace("$OUT_ROOT", "broc_out"))) continue elif s.startswith('$WORKSPACE'): tag.AddSV(os.path.normpath(s.replace("$WORKSPACE/", ""))) continue elif s.startswith("$OUT"): env = Environment.GetCurrent() out = os.path.join('broc_out', env.ModuleCVSPath(), 'output') tag.AddSV(os.path.normpath(s.replace("$OUT", out))) continue else: raise BrocArgumentIllegalError("args(%s) should be a abs path or startswith $WORKSPACE|$OUT|$OUT_ROOT in tag Libs" % s) return tag
def GIT_TAG(): """ return the tag of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def GIT_COMMIT_ID(): """ return the commit id of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def GIT_BRANCH(): """ return the branch name of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def GIT_URL(): """ return url of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def GIT_PATH(): """ return local path of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def SVN_LAST_CHANGED_REV(): """ return last changed rev """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def SVN_REVISION(): """ return revision of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent()
def INCLUDE(*ss): """ set the global head file search path Default head files search path includes $WORKSPACE and $OUT_ROOT Args: ss : a variable number of string objects ss may contain multiple string objects, each object can contain multiple paths 1. if path does not beneath module, in other words, if user want to specified other modules' path, should start with $WORKSPACE, 2. if path couldn't be founed in module itself and path does not start with $WORKSPACE rasie NotInSelfModuleError 3. if path is output directory, it must start with 'broc_out/' for example: ss can be "./include ./include/foo", "$WORKSPACE/include", "broc_out/test/include" """ env = Environment.GetCurrent() tag = env.IncludePaths() broc_dir = env.BrocDir() for s in ss: ps = s.split() for x in ps: if x.startswith("$WORKSPACE"): tag.AddSV(x.replace("$WORKSPACE/", '')) elif x.startswith("broc_out/") or os.path.isabs(x): tag.AddSV(x) else: _x = os.path.normpath(os.path.join(broc_dir, x)) if env.ModulePath() not in _x: raise NotInSelfModuleError(_x, env.ModulePath()) else: tag.AddSV(_x)
def ProtoFlags(*ss): """ add the command options for protoc Args: ss : a variable number of string objects ss may contain multiple string objects, each object can contain multiple options one option may be: 1. option may contains $WORKSPACE, $OUT Macros value Returns: return a """ env = Environment.GetCurrent() tag = SyntaxTag.TagProtoFlags() for s in ss: ps = s.split() for x in ps: if "$WORKSPACE" in x: tag.AddSV(x.replace("$WORKSPACE/", '')) elif "$OUT" in x: tag.AddSV(x.replace("$OUT", env.OutputPath())) elif "$OUT_ROOT" in x: tag.AddSV(x.replace("$OUT_ROOT", env.OutputRoot())) else: tag.AddSV(x) return tag
def STATIC_LIBRARY(name, *args): """ create one StaticLibrary object Args: name : the name of .a file args : the variable number of SyntagTag.TagSources, SyntaxTag.TagLibs object """ # to check name of result file if not Function.CheckName(name): raise BrocArgumentIllegalError( "name(%s) in STATIC_LIBRARY is illegal" % name) tag_libs = SyntaxTag.TagLibs() tag_sources = SyntaxTag.TagSources() for arg in args: if isinstance(arg, SyntaxTag.TagSources): tag_sources.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagLibs): tag_libs.AddSVs(arg.V()) else: raise BrocArgumentIllegalError("arguments (%s) in STATIC_LIBRARY is illegal" % \ type(arg)) env = Environment.GetCurrent() lib = Target.StaticLibrary(name, env, tag_sources, tag_libs) if len(tag_sources.V()): if not env.AppendTarget(lib): raise BrocArgumentIllegalError( "STATIC_LIBRARY(%s) exists already" % name) else: # .a file has been built already, just copy it from code directory to output directory lib.DoCopy()
def UT_APPLICATION(name, sources, *args): """ create one UT Application object Args: name : the name of target sources : the SyntaxTag.TagSource object args : a variable number of SyntaxTag.TagLinkLDFlags, SyntaxTag.TagLibs, SyntaxTag.TagUTArgs """ # to check name of result file if not Function.CheckName(name): raise BrocArgumentIllegalError("name(%s) in UT_APPLICATION is illegal") tag_links = SyntaxTag.TagLDFlags() tag_libs = SyntaxTag.TagLibs() tag_utargs = SyntaxTag.TagUTArgs() for arg in args: if isinstance(arg, SyntaxTag.TagLDFlags): tag_links.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagLibs): tag_libs.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagUTArgs): tag_utargs.AddSVs(arg.V()) else: raise BrocArgumentIllegalError( "In UT_APPLICATION(%s) don't support %s" % (name, arg)) env = Environment.GetCurrent() app = Target.UTApplication(name, env, sources, tag_links, tag_libs, tag_utargs) if not env.AppendTarget(app): raise BrocArgumentIllegalError("UT_APPLICATION(%s) exists already" % name)
def test_Include(self): """ test Syntax.Include """ Environment.SetCurrent(self._env) # arg starts with $WORKSPACE tag = Syntax.Include("$WORKSPACE/baidu/broc") self.assertTrue("baidu/broc" in tag.V()) self.assertTrue("baidu/agile" not in tag.V()) # arg starts with broc_out tag = Syntax.Include("broc_out/baidu/broc") self.assertTrue("broc_out/baidu/broc" in tag.V()) self.assertTrue("broc_out/baidu/agile" not in tag.V()) # arg is abs path tag = Syntax.Include("/opt/include") self.assertTrue("/opt/include" in tag.V()) self.assertTrue("/home/include" not in tag.V()) # arg in self module tag = Syntax.Include("./include") #incpath = os.path.normpath(os.path.join(self._module.workspace, \ # self._module.module_cvspath, "include")) incpath=os.path.join(self._module.module_cvspath, 'include') self.assertTrue(incpath in tag.V())
def GLOB(*ss): """ gather all files belonging to module, if argument specified in ss does not beneath the directory of module, raise BrocArgumentIllegalError Args: ss : a variable number of string objects, support regrex Returns: string containing the relative path of files or directories, each path separeated by blank character """ env = Environment.GetCurrent() strs = [] for s in ss: ps = string.split(s) for p in ps: if p.startswith(os.path.join("broc_out", env.ModuleCVSPath())): norm_path = os.path.normpath(os.path.join(env.Workspace(), p)) len_abandon = len(os.path.normpath(env.Workspace())) else: norm_path = os.path.normpath(os.path.join(env.BrocDir(), p)) len_abandon = len(os.path.normpath(env.BrocDir())) if env.ModuleCVSPath() not in norm_path: raise NotInSelfModuleError(norm_path, env.ModuleCVSPath()) else: gs = glob.glob(norm_path) gs.sort() file_list = list() for file_name in gs: # remove workspace path from file path file_list.append(file_name[len_abandon + 1:]) strs.extend(file_list) if not strs: raise BrocArgumentIllegalError("GLOB(%s) is empty" % str(ss)) return str(' '.join(strs))
def test_INCLUDE(self): """ test Syntax.INCLUDE """ Environment.SetCurrent(self._env) # arg starts with $WORKSPACE Syntax.INCLUDE("$WORKSPACE/baidu/broc") self.assertTrue("baidu/broc" in self._env.IncludePaths().V()) self.assertTrue("baidu/agile" not in self._env.IncludePaths().V()) # arg starts with broc_out Syntax.INCLUDE("broc_out/baidu/broc") self.assertTrue("broc_out/baidu/broc" in self._env.IncludePaths().V()) self.assertTrue("broc_out/baidu/agile" not in self._env.IncludePaths().V()) # arg is abs path Syntax.INCLUDE("/opt/include") self.assertTrue("/opt/include" in self._env.IncludePaths().V()) self.assertTrue("/home/include" not in self._env.IncludePaths().V()) # arg in self module Syntax.INCLUDE("./include") incpath = os.path.normpath(os.path.join(self._module.workspace, \ self._module.module_cvspath, "include")) self.assertTrue(incpath in self._env.IncludePaths().V())
def test_CXXFLAGS(self): """ test Syntax.CXXFLAGS """ #test case of debug mode Environment.SetCurrent(self._env) self._env._build_mode = 'debug' Syntax.CXXFLAGS("-g -Wall", "-g -O2") self.assertTrue("-g -Wall" in self._env._g_cxxflags.V() \ and "-g -O2" not in self._env._g_cxxflags.V()) #test case of muti CPPFLAGS Syntax.CXXFLAGS("-W -Wshadow", "-g -O2") Syntax.CXXFLAGS("-finline-functions", "-g -O2") self.assertTrue("-g -Wall" in self._env._g_cxxflags.V() \ and "-g -O2" not in self._env._g_cxxflags.V()) self.assertTrue("-W -Wshadow" in self._env._g_cxxflags.V() \ and "-g -O2" not in self._env._g_cxxflags.V()) self.assertTrue("-finline-functions" in self._env._g_cxxflags.V() \ and "-g -O2" not in self._env._g_cxxflags.V()) #reset g_cxxflags self._env._g_cxxflags = SyntaxTag.TagCXXFLAGS() #test case of release mode self._env._build_mode = 'release' Syntax.CXXFLAGS("-g -Wall", "-g -O2") self.assertTrue("-g -O2" in self._env._g_cxxflags.V() \ and "-g -Wall" not in self._env._g_cxxflags.V())
def Include(*ss): """ set the local file search path Args: ss : a variable number of string objects ss may contain multiple string objects, each object can contain multiple paths 1. if path does not beneath module, in other words, if user want to specified other modules' path, should start with $WORKSPACE, 2. if path couldn't be founed in module itself and path does not start with $WORKSPACE rasie NotInSelfModuleError 3. if path is output directory, it must start with 'broc_out/' for example: ss can be "./include ./include/foo", "$WORKSPACE/include", "broc_out/test/include" """ env = Environment.GetCurrent() broc_abs_dir = env.BrocDir() broc_cvs_dir = env.BrocCVSDir() tag = SyntaxTag.TagInclude() for s in ss: ps = string.split(s) for x in ps: if x.startswith("$WORKSPACE"): tag.AddSV(x.replace("$WORKSPACE/", "")) continue elif x.startswith('broc_out/') or os.path.isabs(x): tag.AddSV(x) continue elif x.startswith("$OUT_ROOT"): tag.AddSV(x.replace("$OUT_ROOT", 'broc_out')) continue else: _x = os.path.normpath(os.path.join(broc_abs_dir, x)) if env.ModulePath() not in _x: raise NotInSelfModuleError(_x, env.ModulePath()) else: tag.AddSV(os.path.normpath(os.path.join(broc_cvs_dir, x))) return tag
def DIRECOTYR(v): """ Add sub directory Args: v : the name of subdirectory, v is relative path """ env = Environment.GetCurrent() env.AppendSubDirectory(os.path.normpath(v))
def SVN_URL(): """ return url of module """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent() return env.SvnUrl()
def _load_all_broc(self): """ thread function loading all BROC files, each thread object fetches one module(BrocModule_pb2.Module object) from queue, runs the BROC file of the module and creates one Environment object if execfile(BROC) throw exception, stop all thread objects """ while not self._load_done: module = None try: module = self._queue.get(True, 1) except Queue.Empty: continue # BROC file f = os.path.join(module.root_path, 'BROC') env = Environment.Environment(module) if self._build_mode == "release": env.DisableDebug() Environment.SetCurrent(env) try: execfile(f) except BaseException as ex: traceback.print_exc() self._logger.LevPrint( "ERROR", 'parsing %s failed(%s)' % (module.broc_cvspath, ex)) # discard all module in queue while not self._queue.empty(): self._queue.get() self._queue.task_done() self._load_done = True self._load_ok = False self._queue.task_done() break env.SetCompilerDir(self._main_env.CompilerDir()) env.Action() self._add_env(module.broc_cvspath, env) if not self.InitSubEnvironment(env): while not self._queue.empty(): self._queue.get() self._queue.task_done() self._load_done = True self._load_ok = False break self._queue.task_done()
def COMPILER_PATH(k): """ set global path of compiler's directory influence main moudle and all dependent modules Args: k : string object, compiler's directory, for example: if you set k as '/usr/bin', we should find 'gcc' and 'g++' in directory /usr/bin """ env = Environment.GetCurrent() env.SetCompilerDir(k)
def PUBLISH(srcs, out_dir): """ copy srcs to out_dir Args: srcs: the files needed to move should belongs to the module out_dir: the destination directory that must start with $OUT if argument is illeagl, raise BrocArgumentIllegalError """ if sys.argv[0] == 'PLANISH': return env = Environment.GetCurrent() if not out_dir.strip().startswith('$OUT'):
def LDFLAGS(d_flags, r_flags): """ add global link flags Args: d_flags : link flags in debug mode r_flags : link flags in release mode """ env = Environment.GetCurrent() if env.BuildMode() == "debug": env.LDFlags().AddSV(d_flags) else: env.LDFlags().AddSV(r_flags)
def CXXFLAGS(d_flags, r_flags): """ set the global compile options for c++ files, this influences the all of cxx files in module Args: d_flags : debug mode preprocess flags r_flags : release mode preprocess flags """ env = Environment.GetCurrent() if env.BuildMode() == "debug": env.CxxFlags().AddSV(d_flags) else: env.CxxFlags().AddSV(r_flags)