1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright © 2013 Intel Corporation
5#
6# Permission is hereby granted, free of charge, to any person obtaining a
7# copy of this software and associated documentation files (the "Software"),
8# to deal in the Software without restriction, including without limitation
9# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10# and/or sell copies of the Software, and to permit persons to whom the
11# Software is furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice (including the next
14# paragraph) shall be included in all copies or substantial portions of the
15# Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23# IN THE SOFTWARE.
24
25import sys
26import argparse
27import re
28import subprocess
29
30# Example usages:
31# ./gen-symbol-redefs.py i915/.libs/libi915_dri.a old_ i915 i830
32# ./gen-symbol-redefs.py r200/.libs/libr200_dri.a r200_ r200
33
34argparser = argparse.ArgumentParser(description="Generates #defines to hide driver global symbols outside of a driver's namespace.")
35argparser.add_argument("file",
36                    metavar = 'file',
37                    help='libdrivername.a file to read')
38argparser.add_argument("newprefix",
39                    metavar = 'newprefix',
40                    help='New prefix to give non-driver global symbols')
41argparser.add_argument('prefixes',
42                       metavar='prefix',
43                       nargs='*',
44                       help='driver-specific prefixes')
45args = argparser.parse_args()
46
47stdout = subprocess.check_output(['nm', args.file])
48
49for line in stdout.splitlines():
50    m = re.match("[0-9a-z]+ [BT] (.*)", line)
51    if not m:
52        continue
53
54    symbol = m.group(1)
55
56    has_good_prefix = re.match(args.newprefix, symbol) != None
57    for prefix in args.prefixes:
58        if re.match(prefix, symbol):
59            has_good_prefix = True
60            break
61    if has_good_prefix:
62        continue
63
64    # This is the single public entrypoint.
65    if re.match("__driDriverGetExtensions", symbol):
66        continue
67
68    print '#define {0:35} {1}{0}'.format(symbol, args.newprefix)
69