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 __init__(self, module): """ Args: module : BrocModule_pb.Module object """ self._module = module self._build_mode = 'debug' # debug | release # compiler infos self._cc = 'gcc' self._cxx = 'g++' self._compiler_dir = '' # global preporcess and compile flags in one BROC file self._g_cppflags = SyntaxTag.TagCPPFLAGS() self._g_cflags = SyntaxTag.TagCFLAGS() self._g_cxxflags = SyntaxTag.TagCXXFLAGS() self._g_incpaths = SyntaxTag.TagINCLUDE() self._g_incpaths.AddV('. broc_out') self._g_linkflags = SyntaxTag.TagLDFLAGS() # for tag PUBLISH self._publish_cmd = [] self._sources = [] self._targets = []
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 UTArgs(v): """ tag UTArgs """ tag = SyntaxTag.TagUTArgs() tag.AddV(v) return tag
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 test_CPPFLAGS(self): """ test Syntax.CPPFLAGS """ #test case of debug mode self._env._build_mode = 'debug' Syntax.CPPFLAGS("-g -Wall", "-g -O2") self.assertTrue("-g -Wall" in self._env._g_cppflags.V() \ and "-g -O2" not in self._env._g_cppflags.V()) #test case of muti CPPFLAGS Syntax.CPPFLAGS("-W -Wshadow", "-g -O2") Syntax.CPPFLAGS("-finline-functions", "-g -O2") self.assertTrue("-g -Wall" in self._env._g_cppflags.V() \ and "-g -O2" not in self._env._g_cppflags.V()) self.assertTrue("-W -Wshadow" in self._env._g_cppflags.V() \ and "-g -O2" not in self._env._g_cppflags.V()) self.assertTrue("-finline-functions" in self._env._g_cppflags.V() \ and "-g -O2" not in self._env._g_cppflags.V()) #reset g_cppflags self._env._g_cppflags = SyntaxTag.TagCPPFLAGS() #test case of release mode self._env._build_mode = 'release' Syntax.CPPFLAGS("-g -Wall", "-g -O2") self.assertTrue("-g -O2" in self._env._g_cppflags.V() \ and "-g -Wall" not in self._env._g_cppflags.V())
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() 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 if not s.startswith("$OUT_ROOT"): raise BrocArgumentIllegalError("args %s should startswith $OUT_ROOT in tag Libs" % s) else: tag.AddSV(os.path.normpath(s.replace("$OUT_ROOT", "broc_out"))) return tag
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 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 CFlags(d_flags, r_flags): """ set the local compile options for c files, this influences the all of c files in target tag Args: d_flags : debug mode preprocess flags r_flags : release mode preprocess flags """ tag = SyntaxTag.TagCFlags() if sys.argv[0] == 'PLANISH': return tag env = Environment.GetCurrent() tag = SyntaxTag.TagCFlags() if env.BuildMode() == "debug": tag.AddSV(d_flags) else: tag.AddSV(r_flags) return tag
def UTArgs(v): """ tag UTArgs """ tag = SyntaxTag.TagUTArgs() if sys.argv[0] == 'PLANISH': return tag tag.AddV(v) return tag
def LDFlags(d_flags, r_flags): """ add local link flags Args: d_flags : link flags in debug mode r_flags : link flags in release mode """ env = Environment.GetCurrent() tag = SyntaxTag.TagLDFlags() if env.BuildMode() == "debug": tag.AddSV(d_flags) else: tag.AddSV(r_flags) return tag
def CppFlags(d_flags, r_flags): """ set local prrprocess flags, override the global CPPFLAGS Args: d_flags : debug mode preprocess flags r_flags : release mode preprocess flags """ env = Environment.GetCurrent() tag = SyntaxTag.TagCppFlags() # default build mode is debug, it can be modified by command 'broc' by user if env.BuildMode() == "debug": tag.AddSV(d_flags) else: tag.AddSV(r_flags) return tag
def test_LDFlags(self): """ test Syntax.LDFlags """ #test case of debug mode Environment.SetCurrent(self._env) self._env._build_mode = 'debug' tag = Syntax.LDFlags("-lpthread -lcrypto", "-lprotobuf -lpthread") self.assertTrue("-lpthread -lcrypto" in tag.V() \ and "-lprotobuf -lpthread" not in tag.V()) #reset g_linkflags self._env._g_linkflags = SyntaxTag.TagLDFLAGS() #test case of release mode self._env._build_mode = 'release' tag = Syntax.LDFlags("-lpthread -lcrypto", "-lprotobuf -lpthread") self.assertTrue("-lprotobuf -lpthread" in tag.V() \ and "-lpthread -lcrypto" not in tag.V())
def PROTO_LIBRARY(name, files, *args): """ compile proto files into a static library .a when parse proto files failed, raise Exception BrocProtoError Args: name: the name of proto static library files: a string sperearated by blank character, representing the relative path of proto files args: a variable number of SyntaxTag.TagProtoFlags, SyntaxTag.TagInclude, SyntaxTag.CppFlags, SyntaxTag.CXXFlags, SyntaxTag.TagLibs """ # check validity of name if not Function.CheckName(name): raise BrocArgumentIllegalError("name(%s) PROTO_LIBRARY is illegal" % name) # check proto file whether belongs to module proto_files = files.split() env = Environment.GetCurrent() for _file in proto_files: abs_path = os.path.normpath(os.path.join(env.BrocDir(), _file)) if not env.ModulePath() in abs_path: raise NotInSelfModuleError(abs_path, env.ModulePath()) # to check args tag_protoflags = SyntaxTag.TagProtoFlags() tag_cppflags = SyntaxTag.TagCppFlags() tag_cxxflags = SyntaxTag.TagCxxFlags() tag_libs = SyntaxTag.TagLibs() tag_include = SyntaxTag.TagInclude() for arg in args: if isinstance(arg, SyntaxTag.TagProtoFlags): tag_protoflags.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagCppFlags): tag_cppflags.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagCxxFlags): tag_cxxflags.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagLibs): tag_libs.AddSVs(arg.V()) elif isinstance(arg, SyntaxTag.TagInclude): tag_include.AddSVs(arg.V()) else: raise BrocArgumentIllegalError("don't support tag(%s) in PROTO_LIBRARY in %s" \ % (str(arg), env.BrocPath())) include = set() source = set() for f in proto_files: root, _ = os.path.splitext(f) result_file = os.path.join(os.path.join('broc_out', env.ModuleCVSPath()), \ "%s.pb.cc" % root) include.add(os.path.dirname(result_file)) if os.path.dirname(f): tag_include.AddV( os.path.join(env.ModuleCVSPath(), os.path.dirname(f))) source.add(result_file) protolib = Target.ProtoLibrary(env, files, tag_include, tag_protoflags) ret, msg = protolib.PreAction() if not ret: raise BrocProtoError(msg) tag_include.AddSVs(include) broc_out = os.path.join("broc_out", env.BrocCVSDir()) if broc_out not in tag_include.V(): tag_include.AddV(os.path.join("broc_out", env.BrocCVSDir())) tag_sources = Sources(" ".join(list(source)), tag_include, tag_cppflags, tag_cxxflags) if not env.AppendTarget( Target.StaticLibrary(name, env, tag_sources, tag_libs)): raise BrocArgumentIllegalError( "PROTO_LIBRARY(%s) name exists already" % name) return protolib
def test_CreateSources(self): """ test Syntax._CreateSource """ #init env global flags self._env._g_cppflags = SyntaxTag.TagCPPFLAGS() self._env._g_cflags = SyntaxTag.TagCFLAGS() self._env._g_cxxflags = SyntaxTag.TagCXXFLAGS() self._env._g_incflags = SyntaxTag.TagINCLUDE() self._env._g_incflags.AddV('. broc_out') self._env._build_mode = 'debug' #set local flags tag cpptags = Syntax.CppFlags("-DDEBUG_LOCAL", "-DRELEASE_LOCAL") cxxtags = Syntax.CxxFlags("-Wwrite-strings", "-Wswitch") ctags = Syntax.CFlags("-Wwrite-strings", "-Wswitch") incflag = Syntax.Include("$WORKSPACE/baidu/bcloud") #no flags src = Syntax._CreateSources("baidu/broc/hello.cpp", []) Source.Source.Action(src) self.assertEqual(src.cppflags, []) self.assertEqual(src.cxxflags, []) self.assertEqual(src.cflags, []) self.assertEqual(src.includes, [".", "broc_out"]) self.assertEqual(src.infile, "baidu/broc/hello.cpp") #only local flags src = Syntax._CreateSources("baidu/broc/hello.cpp", \ [cpptags, cxxtags, ctags, incflag]) Source.Source.Action(src) self.assertEqual(src.cppflags, ["-DDEBUG_LOCAL"]) self.assertEqual(src.cxxflags, ["-Wwrite-strings"]) self.assertEqual(src.cflags, ["-Wwrite-strings"]) self.assertEqual(src.includes, [".", "broc_out", "baidu/bcloud"]) #only global flags Syntax.CFLAGS("-Werror -O2", "-W") Syntax.CXXFLAGS("-Werror -O2", "-W") Syntax.CPPFLAGS("-DDEBUG", "-DRELEASE") Syntax.INCLUDE("$WORKSPACE/baidu/broc") src = Syntax._CreateSources("baidu/broc/hello.cpp", []) Source.Source.Action(src) self.assertEqual(src.cppflags, ["-DDEBUG"]) self.assertEqual(src.cxxflags, ["-Werror -O2"]) self.assertEqual(src.cflags, ["-Werror -O2"]) self.assertEqual(src.includes, [".", "broc_out", "baidu/broc"]) self.assertEqual(src.infile, "baidu/broc/hello.cpp") #more value of global flags Syntax.CFLAGS("-Wall", "-Wall") Syntax.CXXFLAGS("-Wall", "-Wall") Syntax.CPPFLAGS("-DBROC", "-DBROC") src = Syntax._CreateSources("baidu/broc/hello.cpp", []) Source.Source.Action(src) self.assertEqual(src.cppflags, ["-DDEBUG", "-DBROC"]) self.assertEqual(src.cxxflags, ["-Werror -O2", "-Wall"]) self.assertEqual(src.cflags, ["-Werror -O2", "-Wall"]) self.assertEqual(src.includes, [".", "broc_out", "baidu/broc"]) self.assertEqual(src.infile, "baidu/broc/hello.cpp") #local flags cover golbal flags src = Syntax._CreateSources("baidu/broc/hello.cpp", [cpptags, cxxtags]) Source.Source.Action(src) self.assertEqual(src.cppflags, ["-DDEBUG_LOCAL"]) self.assertEqual(src.cxxflags, ["-Wwrite-strings"]) self.assertEqual(src.cflags, ["-Werror -O2", "-Wall"]) self.assertEqual(src.includes, [".", "broc_out", "baidu/broc"]) self.assertEqual(src.infile, "baidu/broc/hello.cpp")