Imported Upstream version 1.39.3
[platform/upstream/gobject-introspection.git] / giscanner / utils.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008  Johan Dahlin
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 #
20
21 import re
22 import os
23 import subprocess
24
25
26 _debugflags = None
27
28
29 def have_debug_flag(flag):
30     """Check for whether a specific debugging feature is enabled.
31 Well-known flags:
32  * start: Drop into debugger just after processing arguments
33  * exception: Drop into debugger on fatalexception
34  * warning: Drop into debugger on warning
35  * posttrans: Drop into debugger just before introspectable pass
36 """
37     global _debugflags
38     if _debugflags is None:
39         _debugflags = os.environ.get('GI_SCANNER_DEBUG', '').split(',')
40         if '' in _debugflags:
41             _debugflags.remove('')
42     return flag in _debugflags
43
44
45 def break_on_debug_flag(flag):
46     if have_debug_flag(flag):
47         import pdb
48         pdb.set_trace()
49
50 # Copied from h2defs.py
51 _upperstr_pat1 = re.compile(r'([^A-Z])([A-Z])')
52 _upperstr_pat2 = re.compile(r'([A-Z][A-Z])([A-Z][0-9a-z])')
53 _upperstr_pat3 = re.compile(r'^([A-Z])([A-Z])')
54
55
56 def to_underscores(name):
57     """Converts a typename to the equivalent underscores name.
58     This is used to form the type conversion macros and enum/flag
59     name variables.
60     In particular, and differently from to_underscores_noprefix(),
61     this function treats the first character differently if it is
62     uppercase and followed by another uppercase letter."""
63     name = _upperstr_pat1.sub(r'\1_\2', name)
64     name = _upperstr_pat2.sub(r'\1_\2', name)
65     name = _upperstr_pat3.sub(r'\1_\2', name, count=1)
66     return name
67
68
69 def to_underscores_noprefix(name):
70     """Like to_underscores, but designed for "unprefixed" names.
71     to_underscores("DBusFoo") => dbus_foo, not d_bus_foo."""
72     name = _upperstr_pat1.sub(r'\1_\2', name)
73     name = _upperstr_pat2.sub(r'\1_\2', name)
74     return name
75
76
77 _libtool_pat = re.compile("dlname='([A-z0-9\.\-\+]+)'\n")
78
79
80 def _extract_dlname_field(la_file):
81     f = open(la_file)
82     data = f.read()
83     f.close()
84     m = _libtool_pat.search(data)
85     if m:
86         return m.groups()[0]
87     else:
88         return None
89
90
91 # Returns the name that we would pass to dlopen() the library
92 # corresponding to this .la file
93 def extract_libtool_shlib(la_file):
94     dlname = _extract_dlname_field(la_file)
95     if dlname is None:
96         return None
97
98     # From the comments in extract_libtool(), older libtools had
99     # a path rather than the raw dlname
100     return os.path.basename(dlname)
101
102
103 def extract_libtool(la_file):
104     dlname = _extract_dlname_field(la_file)
105     if dlname is None:
106         raise ValueError("%s has no dlname. Not a shared library?" % la_file)
107     libname = os.path.join(os.path.dirname(la_file),
108                            '.libs', dlname)
109     # FIXME: This hackish, but I'm not sure how to do this
110     #        in a way which is compatible with both libtool 2.2
111     #        and pre-2.2. Johan 2008-10-21
112     libname = libname.replace('.libs/.libs', '.libs')
113     return libname
114
115
116 # Returns arguments for invoking libtool, if applicable, otherwise None
117 def get_libtool_command(options):
118     libtool_infection = not options.nolibtool
119     if not libtool_infection:
120         return None
121
122     libtool_path = options.libtool_path
123     if libtool_path:
124         # Automake by default sets:
125         # LIBTOOL = $(SHELL) $(top_builddir)/libtool
126         # To be strictly correct we would have to parse shell.  For now
127         # we simply split().
128         return libtool_path.split(' ')
129
130     try:
131         subprocess.check_call(['libtool', '--version'],
132                               stdout=open(os.devnull))
133     except (subprocess.CalledProcessError, OSError):
134         # If libtool's not installed, assume we don't need it
135         return None
136
137     return ['libtool']
138
139
140 def files_are_identical(path1, path2):
141     f1 = open(path1)
142     f2 = open(path2)
143     buf1 = f1.read(8192)
144     buf2 = f2.read(8192)
145     while buf1 == buf2 and buf1 != '':
146         buf1 = f1.read(8192)
147         buf2 = f2.read(8192)
148     f1.close()
149     f2.close()
150     return buf1 == buf2
151
152
153 def cflag_real_include_path(cflag):
154     if not cflag.startswith("-I"):
155         return cflag
156
157     return "-I" + os.path.realpath(cflag[2:])