# Path: util/Function.py
# DIGITS = [str(x) for x in xrange(0, 10)] 
# ALPHABETS = []
# def CheckName(v):
# def DelFiles(path):
# def MoveFiles(src, dst):
# def Mkdir(target_dir):
# def CalcHash(data, method='MD5'):
# def GetFileHash(path, method='MD5'):
# def RunCommand(cmd, ignore_stderr_when_ok=False):
# def RunCommand_tty(cmd):
# 
# Path: util/Log.py
# class Log(object):
#     """
#     Log class
#     """
#     class __impl(object):
#         """ Implementation of singleton interface"""
#         def __init__(self):
#             """
#             """
#             self.levMap = {"ERROR":(0, "\033[31m%s%s\033[0m"), "WARNING":(1, "\033[33m%s%s\033[0m"), \
#                             "INFO":(2, "\033[34m%s%s\033[0m"), "MSG":(3, "\033[32m%s%s\033[0m"), \
#                             "UNKNOW":(4, "\033[31m%s%s\033[0m")}
#             self.config_level = 5
# 
#         def setLogLevel(self, lev):
#             """
#             Description : set log file level.The log message lower than this level will not be printed.
#             Args : lev : log level will be set.Value is [0-5],0 will not print any log message, 5 will print all log message
#             Return : NULL 
#             """
#             self.config_level = lev
# 
#         def LevPrint(self, level, msg, prefix=True):
#             """
#             Description :  print log message which has higher level than config level
#             Args: 
#                 level : ["ERROR", "WARNING", "INFO", "MSG", "UNKNOW"]
#                 msg : string, log message
#                 prefix : True , print time information
#                          False , don't print time information
#             Return : None
#             """
#             if level in self.levMap and self.levMap[level][0] > self.config_level:
#                 return 
# 
#             now = ""
#             pmsg = ""
#             if prefix:
#                 now = "[" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) \
#                     + " " + str(threading.current_thread().ident) + "] "
#             if (level in self.levMap):
#                     pmsg = self.levMap[level][1] % (now, msg)
#             else:
#                     pmsg = "UNKNOW log level(%s)" % level 
#                     pmsg += self.levMap["UNKNOW"][1] % (now, msg)
#                 
#             print(pmsg)
#             sys.stdout.flush()
# 
#     
#     # class Log
#     __instance = None
#     def __init__(self):
#         """ Create singleton instance """
#         # Check whether we already have an instance
#         if Log.__instance is None:
#             # Create and remember instance
#             Log.__instance = Log.__impl()
# 
#         # Store instance reference as the only member in the handle
#         self.__dict__['_Log__instance'] = Log.__instance
# 
#     def __getattr__(self, attr):
#         """ Delegate access to implementation """
#         return getattr(self.__instance, attr)
# 
#     def __setattr__(self, attr, value):
#         """ Delegate access to implementation """
#         return setattr(self.__instance, attr, value)


import os
import sys
from util import Function
from util import Log
            workspace : the abs path of workspace
        """ 
        Builder.__init__(self, obj, compiler, workspace)
        self.workspace = workspace
        self._includes = ""
        self._opts = None
        self._infile = infile
        self._header_cmd = None
        if includes:
            self._includes += "\t".join(map(lambda x: "-I%s \\\n" % os.path.normpath(x), includes))
        if opts: 
            self._opts = " \\\n\t".join(map(lambda x: x, opts))

        self.build_cmd = "mkdir -p %s && %s \\\n\t-c \\\n\t%s \\\n\t%s\t-o \\\n\t%s \\\n\t%s" % \
                         (self.obj_dir, self.compiler, self._opts, self._includes, self.obj, infile)

        self._header_cmd = "%s \\\n\t-MM -MG\\\n\t%s\t%s" % \
                            (self.compiler, self._includes, self._infile)

    def CalcHeaderFiles(self):
        """
        calculate the header files that source file dependends
        Returns:
            { ret : True | False, headers : set(), msg : 'error message' }
            calculate successfully ret is True; otherwise ret is False and msg contains error message
        """
        result = dict()
        result['ret'] = False
        result['headers'] = set()
        result['msg'] = ''

# Path: util/Function.py
# DIGITS = [str(x) for x in xrange(0, 10)] 
# ALPHABETS = []
# def CheckName(v):
# def DelFiles(path):
# def MoveFiles(src, dst):
# def Mkdir(target_dir):
# def CalcHash(data, method='MD5'):
# def GetFileHash(path, method='MD5'):
# def RunCommand(cmd, ignore_stderr_when_ok=False):
# def RunCommand_tty(cmd):
# 
# Path: util/Log.py
# class Log(object):
#     """
#     Log class
#     """
#     class __impl(object):
#         """ Implementation of singleton interface"""
#         def __init__(self):
#             """
#             """
#             self.levMap = {"ERROR":(0, "\033[31m%s%s\033[0m"), "WARNING":(1, "\033[33m%s%s\033[0m"), \
#                             "INFO":(2, "\033[34m%s%s\033[0m"), "MSG":(3, "\033[32m%s%s\033[0m"), \
#                             "UNKNOW":(4, "\033[31m%s%s\033[0m")}
#             self.config_level = 5
# 
#         def setLogLevel(self, lev):
#             """
#             Description : set log file level.The log message lower than this level will not be printed.
#             Args : lev : log level will be set.Value is [0-5],0 will not print any log message, 5 will print all log message
#             Return : NULL 
#             """
#             self.config_level = lev
# 
#         def LevPrint(self, level, msg, prefix=True):
#             """
#             Description :  print log message which has higher level than config level
#             Args: 
#                 level : ["ERROR", "WARNING", "INFO", "MSG", "UNKNOW"]
#                 msg : string, log message
#                 prefix : True , print time information
#                          False , don't print time information
#             Return : None
#             """
#             if level in self.levMap and self.levMap[level][0] > self.config_level:
#                 return 
# 
#             now = ""
#             pmsg = ""
#             if prefix:
#                 now = "[" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) \
#                     + " " + str(threading.current_thread().ident) + "] "
#             if (level in self.levMap):
#                     pmsg = self.levMap[level][1] % (now, msg)
#             else:
#                     pmsg = "UNKNOW log level(%s)" % level 
#                     pmsg += self.levMap["UNKNOW"][1] % (now, msg)
#                 
#             print(pmsg)
#             sys.stdout.flush()
# 
#     
#     # class Log
#     __instance = None
#     def __init__(self):
#         """ Create singleton instance """
#         # Check whether we already have an instance
#         if Log.__instance is None:
#             # Create and remember instance
#             Log.__instance = Log.__impl()
# 
#         # Store instance reference as the only member in the handle
#         self.__dict__['_Log__instance'] = Log.__instance
# 
#     def __getattr__(self, attr):
#         """ Delegate access to implementation """
#         return getattr(self.__instance, attr)
# 
#     def __setattr__(self, attr, value):
#         """ Delegate access to implementation """
#         return setattr(self.__instance, attr, value)


# Path: dependency/Builder.py
import os
import sys
from util import Function
from util import Log

            workspace : the abs path of workspace
        """ 
        Builder.__init__(self, obj, compiler, workspace)
        self.workspace = workspace
        self._includes = ""
        self._opts = None
        self._infile = infile
        self._header_cmd = None
        if includes:
            self._includes += "\t".join(map(lambda x: "-I%s \\\n" % os.path.normpath(x), includes))
        if opts: 
            self._opts = " \\\n\t".join(map(lambda x: x, opts))

        self.build_cmd = "mkdir -p %s && %s \\\n\t-c \\\n\t%s \\\n\t%s\t-o \\\n\t%s \\\n\t%s" % \
                         (self.obj_dir, self.compiler, self._opts, self._includes, self.obj, infile)

        self._header_cmd = "%s \\\n\t-MM -MG\\\n\t%s\t%s" % \
                            (self.compiler, self._includes, self._infile)

    def CalcHeaderFiles(self):
        """
        calculate the header files that source file dependends
        Returns:
            { ret : True | False, headers : set(), msg : 'error message' }
            calculate successfully ret is True; otherwise ret is False and msg contains error message
        """
        result = dict()
        result['ret'] = False
        result['headers'] = set()
        result['msg'] = ''

        retcode, msg = Function.RunCommand(self._header_cmd, ignore_stderr_when_ok=True)