Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 """
borlanic 0:02dd72d1d465 2 mbed SDK
borlanic 0:02dd72d1d465 3 Copyright (c) 2011-2013 ARM Limited
borlanic 0:02dd72d1d465 4
borlanic 0:02dd72d1d465 5 Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 6 you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 7 You may obtain a copy of the License at
borlanic 0:02dd72d1d465 8
borlanic 0:02dd72d1d465 9 http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 10
borlanic 0:02dd72d1d465 11 Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 12 distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 14 See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 15 limitations under the License.
borlanic 0:02dd72d1d465 16
borlanic 0:02dd72d1d465 17
borlanic 0:02dd72d1d465 18 Utility to find which libraries could define a given symbol
borlanic 0:02dd72d1d465 19 """
borlanic 0:02dd72d1d465 20 from argparse import ArgumentParser
borlanic 0:02dd72d1d465 21 from os.path import join, splitext
borlanic 0:02dd72d1d465 22 from os import walk
borlanic 0:02dd72d1d465 23 from subprocess import Popen, PIPE
borlanic 0:02dd72d1d465 24
borlanic 0:02dd72d1d465 25
borlanic 0:02dd72d1d465 26 OBJ_EXT = ['.o', '.a', '.ar']
borlanic 0:02dd72d1d465 27
borlanic 0:02dd72d1d465 28
borlanic 0:02dd72d1d465 29 def find_sym_in_lib(sym, obj_path):
borlanic 0:02dd72d1d465 30 contain_symbol = False
borlanic 0:02dd72d1d465 31
borlanic 0:02dd72d1d465 32 out = Popen(["nm", "-C", obj_path], stdout=PIPE, stderr=PIPE).communicate()[0]
borlanic 0:02dd72d1d465 33 for line in out.splitlines():
borlanic 0:02dd72d1d465 34 tokens = line.split()
borlanic 0:02dd72d1d465 35 n = len(tokens)
borlanic 0:02dd72d1d465 36 if n == 2:
borlanic 0:02dd72d1d465 37 sym_type = tokens[0]
borlanic 0:02dd72d1d465 38 sym_name = tokens[1]
borlanic 0:02dd72d1d465 39 elif n == 3:
borlanic 0:02dd72d1d465 40 sym_type = tokens[1]
borlanic 0:02dd72d1d465 41 sym_name = tokens[2]
borlanic 0:02dd72d1d465 42 else:
borlanic 0:02dd72d1d465 43 continue
borlanic 0:02dd72d1d465 44
borlanic 0:02dd72d1d465 45 if sym_type == "U":
borlanic 0:02dd72d1d465 46 # This object is using this symbol, not defining it
borlanic 0:02dd72d1d465 47 continue
borlanic 0:02dd72d1d465 48
borlanic 0:02dd72d1d465 49 if sym_name == sym:
borlanic 0:02dd72d1d465 50 contain_symbol = True
borlanic 0:02dd72d1d465 51
borlanic 0:02dd72d1d465 52 return contain_symbol
borlanic 0:02dd72d1d465 53
borlanic 0:02dd72d1d465 54
borlanic 0:02dd72d1d465 55 def find_sym_in_path(sym, dir_path):
borlanic 0:02dd72d1d465 56 for root, _, files in walk(dir_path):
borlanic 0:02dd72d1d465 57 for file in files:
borlanic 0:02dd72d1d465 58
borlanic 0:02dd72d1d465 59 _, ext = splitext(file)
borlanic 0:02dd72d1d465 60 if ext not in OBJ_EXT: continue
borlanic 0:02dd72d1d465 61
borlanic 0:02dd72d1d465 62 path = join(root, file)
borlanic 0:02dd72d1d465 63 if find_sym_in_lib(sym, path):
borlanic 0:02dd72d1d465 64 print path
borlanic 0:02dd72d1d465 65
borlanic 0:02dd72d1d465 66
borlanic 0:02dd72d1d465 67 if __name__ == '__main__':
borlanic 0:02dd72d1d465 68 parser = ArgumentParser(description='Find Symbol')
borlanic 0:02dd72d1d465 69 parser.add_argument('-s', '--sym', required=True,
borlanic 0:02dd72d1d465 70 help='The symbol to be searched')
borlanic 0:02dd72d1d465 71 parser.add_argument('-p', '--path', required=True,
borlanic 0:02dd72d1d465 72 help='The path where to search')
borlanic 0:02dd72d1d465 73 args = parser.parse_args()
borlanic 0:02dd72d1d465 74
borlanic 0:02dd72d1d465 75 find_sym_in_path(args.sym, args.path)