diff options
Diffstat (limited to 'longbow/src/python/site-packages/longbow/Language_C.py')
-rwxr-xr-x | longbow/src/python/site-packages/longbow/Language_C.py | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/longbow/src/python/site-packages/longbow/Language_C.py b/longbow/src/python/site-packages/longbow/Language_C.py new file mode 100755 index 00000000..1d97a676 --- /dev/null +++ b/longbow/src/python/site-packages/longbow/Language_C.py @@ -0,0 +1,204 @@ +#! /usr/bin/env python +# Copyright (c) 2017 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# +import os +import glob + +import fnmatch +import subprocess + +def findFiles(startDir, pattern): + matches = [] + for root, dirnames, filenames in os.walk(startDir): + for filename in fnmatch.filter(filenames, pattern): + matches.append(os.path.join(root, filename)) + + return matches + +def getLibPathForObject(libraryPath, filename): + ''' + Returns a path to an object file suitable for nm + ''' + result = '' + + command = ['/usr/bin/ar', '-t', libraryPath] + output = subprocess.check_output(command) + lines = output.splitlines() + + for line in lines: + tokens = line.split('.') + if tokens[0] == filename: + result = libraryPath + '(' + line + ')' + break + + return result + +class Module: + ''' + Represent a C language module. + A module consists of the file names of the C source, C header file, object file, and an executable file + ''' + def __init__(self, srcPath, objectDirs=[]): + self.path = self.initialzePath(srcPath) + if not objectDirs: + objectDirs = [self.path] + split = srcPath.split("/") + self.originalName = split[len(split) - 1] + self.originalBaseName = os.path.basename(srcPath) + tokens = self.originalBaseName.split('.') + self.fileName = tokens[0].replace("/","") + + if self.fileName.startswith("test_"): + self.fileName = self.fileName[5:] + + # Search for an appropriate object + self.objectPath = ""; + for objectDir in objectDirs: + if objectDir.endswith(".a"): + self.objectPath = getLibPathForObject(objectDir, self.fileName) + if self.objectPath: + break + else: + objectSearchPath = os.path.join(objectDir, self.fileName) + "*.o*" + ofiles = glob.glob(objectSearchPath); + if ofiles: + # if we've found some matches, assume we want the first. + self.objectPath = ofiles[0]; + break + return + + def isTestExecutableName(self): + return self.getTestExecutableName() == self.originalBaseName + + def isTestSourceName(self): + return self.getTestSourceName() == self.originalBaseName + + def isCSourceName(self): + return self.getCSourceName() == self.originalBaseName + + def isCHeaderName(self): + return self.getCHeaderName() == self.originalBaseName + + def getCSourceName(self): + return self.fileName + ".c" + + def getCHeaderName(self): + return self.fileName + ".h" + + def getPathToObjectFile(self): + return self.objectPath; + + def getExecutableName(self): + return self.fileName + + def getTestExecutableName(self): + return "test_" + self.fileName + + def getTestSourceName(self): + return self.getTestExecutableName() + ".c" + + def getNamespace(self): + sourceFileName = self.getCSourceName() + if (sourceFileName.find("_") >= 0): + stripped = sourceFileName[0:sourceFileName.index("_")] + return stripped + else: + return None + + def getModuleName(self): + sourceFileName = self.getCSourceName() + split = sourceFileName.split("/") + sourceFileName = split[len(split) - 1] + if (sourceFileName.find(".") >= 0): + stripped = sourceFileName[0:sourceFileName.index(".")] + return stripped + else: + return None + + def getModulePrefix(self): + sourceFileName = self.getCSourceName() + squashed = sourceFileName.replace("_", "") + if (squashed.find(".") >= 0): + return squashed[0:squashed.index(".")] + else: + return None + + def getTypeName(self): + sourceFileName = self.getCSourceName() + if (sourceFileName.find(".") >= 0 and sourceFileName.find("_") >= 0): + stripped = sourceFileName[(sourceFileName.index("_") + 1):sourceFileName.index(".")] + return stripped + else: + return None + + def getModulePath(self): + return self.path + + def initialzePath(self, sourceFileName): + parts = sourceFileName.split("/") + parts = parts[0:len(parts) - 1] + return '/'.join(map(str, parts)) + +if __name__ == '__main__': + cFile = Module("file.c.gcov") + if cFile.getCSourceName() != "file.c": + print "getCSourceName failed", cFile.getCSourceName() + + if cFile.getCHeaderName() != "file.h": + print "getCHeaderName failed", cFile.getCHeaderName() + + if cFile.getTestSourceName() != "test_file.c": + print "getTestSourceName failed", cFile.getTestSourceName() + + if cFile.getTestExecutableName() != "test_file": + print "getTestExecutableName failed", cFile.getTestExecutableName() + + if cFile.getNamespace() != None: + print "getNamespace failed", cFile.getNamespace() + + if cFile.getModuleName() != "file": + print "getModuleName failed", cFile.getModuleName() + + if cFile.getModulePrefix() != "file": + print "getModulePrefix failed", cFile.getModulePrefix() + + if cFile.getTypeName() != None: + print "getTypeName failed", cFile.getTypeName() + + cFile = Module("parc_Object.c.gcov") + if cFile.getCSourceName() != "parc_Object.c": + print "getCSourceName failed", cFile.getCSourceName() + + if cFile.getCHeaderName() != "parc_Object.h": + print "getCHeaderName failed", cFile.getCHeaderName() + + if cFile.getTestSourceName() != "test_parc_Object.c": + print "getTestSourceName failed", cFile.getTestSourceName() + + if cFile.getTestExecutableName() != "test_parc_Object": + print "getTestExecutableName failed", cFile.getTestExecutableName() + + if cFile.getNamespace() != "parc": + print "getNamespace failed", cFile.getNamespace() + + if cFile.getModuleName() != "parc_Object": + print "getModuleName failed", cFile.getModuleName() + + if cFile.getModulePrefix() != "parcObject": + print "getModulePrefix failed", cFile.getModulePrefix() + + if cFile.getTypeName() != "Object": + print "getTypeName failed", cFile.getTypeName() |