1b8e80941Smrg#!/usr/bin/env python2
2848b8605Smrg# -*- coding: utf-8 -*-
3848b8605Smrg
4848b8605Smrg# Copyright © 2013 Intel Corporation
5848b8605Smrg#
6848b8605Smrg# Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg# copy of this software and associated documentation files (the "Software"),
8848b8605Smrg# to deal in the Software without restriction, including without limitation
9848b8605Smrg# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg# and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg# Software is furnished to do so, subject to the following conditions:
12848b8605Smrg#
13848b8605Smrg# The above copyright notice and this permission notice (including the next
14848b8605Smrg# paragraph) shall be included in all copies or substantial portions of the
15848b8605Smrg# Software.
16848b8605Smrg#
17848b8605Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18848b8605Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19848b8605Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20848b8605Smrg# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21848b8605Smrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22848b8605Smrg# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23848b8605Smrg# IN THE SOFTWARE.
24848b8605Smrg
25848b8605Smrgimport sys
26848b8605Smrgimport argparse
27848b8605Smrgimport re
28848b8605Smrgimport subprocess
29848b8605Smrg
30848b8605Smrg# Example usages:
31848b8605Smrg# ./gen-symbol-redefs.py i915/.libs/libi915_dri.a old_ i915 i830
32848b8605Smrg# ./gen-symbol-redefs.py r200/.libs/libr200_dri.a r200_ r200
33848b8605Smrg
34848b8605Smrgargparser = argparse.ArgumentParser(description="Generates #defines to hide driver global symbols outside of a driver's namespace.")
35848b8605Smrgargparser.add_argument("file",
36848b8605Smrg                    metavar = 'file',
37848b8605Smrg                    help='libdrivername.a file to read')
38848b8605Smrgargparser.add_argument("newprefix",
39848b8605Smrg                    metavar = 'newprefix',
40848b8605Smrg                    help='New prefix to give non-driver global symbols')
41848b8605Smrgargparser.add_argument('prefixes',
42848b8605Smrg                       metavar='prefix',
43848b8605Smrg                       nargs='*',
44848b8605Smrg                       help='driver-specific prefixes')
45848b8605Smrgargs = argparser.parse_args()
46848b8605Smrg
47848b8605Smrgstdout = subprocess.check_output(['nm', args.file])
48848b8605Smrg
49848b8605Smrgfor line in stdout.splitlines():
50848b8605Smrg    m = re.match("[0-9a-z]+ [BT] (.*)", line)
51848b8605Smrg    if not m:
52848b8605Smrg        continue
53848b8605Smrg
54848b8605Smrg    symbol = m.group(1)
55848b8605Smrg
56848b8605Smrg    has_good_prefix = re.match(args.newprefix, symbol) != None
57848b8605Smrg    for prefix in args.prefixes:
58848b8605Smrg        if re.match(prefix, symbol):
59848b8605Smrg            has_good_prefix = True
60848b8605Smrg            break
61848b8605Smrg    if has_good_prefix:
62848b8605Smrg        continue
63848b8605Smrg
64848b8605Smrg    # This is the single public entrypoint.
65848b8605Smrg    if re.match("__driDriverGetExtensions", symbol):
66848b8605Smrg        continue
67848b8605Smrg
68848b8605Smrg    print '#define {0:35} {1}{0}'.format(symbol, args.newprefix)
69