aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/python/site-packages/longbow/Language_C.py
blob: 1d97a67652e31d03312cab027e37e56b285f2893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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()