3 # GObject-Introspection - a framework for introspecting GObject libraries
4 # Copyright (C) 2008 Johan Dahlin
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # This only works on unix systems
28 currentdir = os.path.dirname(os.path.abspath(sys.argv[0]))
29 basedir = os.path.abspath(os.path.join(currentdir, '..'))
30 if (os.path.exists(os.path.join(basedir, '.svn')) or
31 os.path.exists(os.path.join(basedir, '.git'))):
40 path = os.path.join(basedir, libdir, 'python%d.%d' % sys.version_info[:2],
42 sys.path.insert(0, path)
44 from giscanner.annotationparser import AnnotationParser, InvalidAnnotationError
45 from giscanner.ast import Include
46 from giscanner.cachestore import CacheStore
47 from giscanner.dumper import compile_introspection_binary
48 from giscanner.glibtransformer import GLibTransformer, IntrospectionBinary
49 from giscanner.minixpath import myxpath, xpath_assert
50 from giscanner.sourcescanner import SourceScanner
51 from giscanner.transformer import Transformer
53 def _get_option_parser():
54 parser = optparse.OptionParser('%prog [options] sources')
55 parser.add_option("", "--format",
56 action="store", dest="format",
58 help="format to use, one of gidl, gir")
59 parser.add_option("-i", "--include",
60 action="append", dest="includes", default=[],
61 help="include types for other gidls")
62 parser.add_option("", "--add-include-path",
63 action="append", dest="include_paths", default=[],
64 help="include paths for other GIR files")
65 parser.add_option("", "--program",
66 action="store", dest="program", default=None,
67 help="program to execute")
68 parser.add_option("", "--program-arg",
69 action="append", dest="program_args", default=[],
70 help="extra arguments to program")
71 parser.add_option("", "--libtool",
72 action="store", dest="libtool_path", default=None,
73 help="full path to libtool")
74 parser.add_option("", "--no-libtool",
75 action="store_true", dest="nolibtool", default=False,
77 parser.add_option("-l", "--library",
78 action="append", dest="libraries", default=[],
79 help="libraries of this unit")
80 parser.add_option("-L", "--library-path",
81 action="append", dest="library_paths", default=[],
82 help="directories to search for libraries")
83 parser.add_option("-n", "--namespace",
84 action="store", dest="namespace_name",
85 help=("name of namespace for this unit, also "
86 "used as --strip-prefix default"))
87 parser.add_option("", "--nsversion",
88 action="store", dest="namespace_version",
89 help="version of namespace for this unit")
90 parser.add_option("", "--strip-prefix",
91 action="store", dest="strip_prefix", default=None,
92 help="remove this prefix from objects and functions")
93 parser.add_option("-o", "--output",
94 action="store", dest="output",
95 help="output to writeout, defaults to stdout")
96 parser.add_option("", "--pkg",
97 action="append", dest="packages", default=[],
98 help="pkg-config packages to get cflags from")
99 parser.add_option("-v", "--verbose",
100 action="store_true", dest="verbose",
102 parser.add_option("", "--noclosure",
103 action="store_true", dest="noclosure",
104 help="do not delete unknown types")
105 parser.add_option("", "--typelib-xml",
106 action="store_true", dest="typelib_xml",
107 help="Just convert GIR to typelib XML")
108 parser.add_option("", "--inject",
109 action="store_true", dest="inject",
110 help="Inject additional components into GIR XML")
111 parser.add_option("", "--xpath-assertions",
112 action="store", dest="xpath_assertions",
113 help="Use given file to create assertions on GIR content")
115 group = optparse.OptionGroup(parser, "Preprocessor options")
116 group.add_option("-I", help="Pre-processor include file",
117 action="append", dest="cpp_includes",
119 group.add_option("-D", help="Pre-processor define",
120 action="append", dest="cpp_defines",
122 group.add_option("-U", help="Pre-processor undefine",
123 action="append", dest="cpp_undefines",
125 group.add_option("-p", dest="", help="Ignored")
126 parser.add_option_group(group)
132 raise SystemExit('ERROR: %s' % (msg, ))
134 def typelib_xml_strip(path):
135 from giscanner.girparser import GIRParser
136 from giscanner.girwriter import GIRWriter
137 from giscanner.girparser import C_NS
138 from xml.etree.cElementTree import parse
140 c_ns_key = '{%s}' % (C_NS, )
143 root = tree.getroot()
144 for node in root.getiterator():
145 for attrib in list(node.attrib):
146 if attrib.startswith(c_ns_key):
147 del node.attrib[attrib]
149 parser.parse_tree(tree)
151 writer = GIRWriter(parser.get_namespace(),
152 parser.get_shared_libraries(),
153 parser.get_includes())
154 sys.stdout.write(writer.get_xml())
157 def inject(path, additions, outpath):
158 from giscanner.girparser import GIRParser
159 from giscanner.girwriter import GIRWriter
160 from xml.etree.cElementTree import parse
163 root = tree.getroot()
164 injectDoc = parse(open(additions))
165 for node in injectDoc.getroot():
166 injectPath = node.attrib['path']
167 target = myxpath(root, injectPath)
169 raise ValueError("Couldn't find path %r" % (injectPath, ))
174 parser.parse_tree(tree)
175 writer = GIRWriter(parser.get_namespace(),
176 parser.get_shared_libraries(),
177 parser.get_includes())
178 outf = open(outpath, 'w')
179 outf.write(writer.get_xml())
183 def validate(assertions, path):
184 from xml.etree.cElementTree import parse
185 doc = parse(open(path))
188 assertions_list = f.readlines()
189 print "=== CHECKING %s (%d assertions) ===" % (assertions,
190 len(assertions_list))
191 for assertion in assertions_list:
192 assertion = assertion.strip()
193 xpath_assert(root, assertion)
195 print "=== PASSED %s ===" % (assertions, )
198 def process_options(output, allowed_flags):
199 for option in output.split():
200 for flag in allowed_flags:
201 if not option.startswith(flag):
206 def process_packages(parser, options, packages):
207 args = ['pkg-config', '--cflags']
208 args.extend(packages)
209 output = subprocess.Popen(args,
210 stdout=subprocess.PIPE).communicate()[0]
212 # the error output should have already appeared on our stderr,
215 # Some pkg-config files on Windows have options we don't understand,
216 # so we explicitly filter to only the ones we need.
217 options_whitelist = ['-I', '-D', '-U', '-l', '-L']
218 filtered_output = list(process_options(output, options_whitelist))
219 pkg_options, unused = parser.parse_args(filtered_output)
220 options.cpp_includes.extend(pkg_options.cpp_includes)
221 options.cpp_defines.extend(pkg_options.cpp_defines)
222 options.cpp_undefines.extend(pkg_options.cpp_undefines)
224 args = ['pkg-config', '--libs-only-L']
225 args.extend(packages)
226 output = subprocess.Popen(args,
227 stdout=subprocess.PIPE).communicate()[0]
230 filtered_output = list(process_options(output, options_whitelist))
231 pkg_options, unused = parser.parse_args(filtered_output)
232 options.library_paths.extend(pkg_options.library_paths)
235 parser = _get_option_parser()
236 (options, args) = parser.parse_args(args)
239 _error('Need at least one filename')
241 if options.typelib_xml:
242 return typelib_xml_strip(args[1])
246 _error('Need three filenames; e.g. g-ir-scanner '
247 '--inject Source.gir Additions.xml SourceOut.gir')
248 return inject(*args[1:4])
250 if options.xpath_assertions:
251 return validate(options.xpath_assertions, args[1])
253 if not options.namespace_name:
254 _error('Namespace name missing')
256 if options.format == 'gir':
257 from giscanner.girwriter import GIRWriter as Writer
259 _error("Unknown format: %s" % (options.format, ))
261 if not (options.libraries or options.program):
262 _error("Must specify --program or --library")
263 libraries = options.libraries
265 # FIXME: using LPATH is definitely not portable enough. Using Python's
266 # find_library for finding our shared libraries is not a portable enough
267 # anyway as it behaves differently depending on the OS
268 lpath = os.environ.get('LPATH')
269 library_path = ':'.join(options.library_paths)
271 ld_library_path = os.environ.get('LD_LIBRARY_PATH')
273 library_path = ':'.join([ld_library_path, library_path])
276 os.environ['LPATH'] = ':'.join([lpath, library_path])
278 os.environ['LPATH'] = library_path
281 if (arg.endswith('.c') or
283 if not os.path.exists(arg):
284 _error('%s: no such a file or directory' % (arg, ))
285 filenames.append(arg)
287 cachestore = CacheStore()
288 transformer = Transformer(cachestore,
289 options.namespace_name,
290 options.namespace_version)
291 if options.strip_prefix:
292 transformer.set_strip_prefix(options.strip_prefix)
294 transformer.set_strip_prefix(options.namespace_name)
295 transformer.set_include_paths(options.include_paths)
296 shown_include_warning = False
297 for include in options.includes:
298 if os.sep in include:
299 raise ValueError("Invalid include path %r" % (include, ))
300 include_obj = Include.from_string(include)
301 transformer.register_include(include_obj)
303 packages = set(options.packages)
304 packages.update(transformer.get_pkgconfig_packages())
305 process_packages(parser, options, packages)
307 # Run the preprocessor, tokenize and construct simple
308 # objects representing the raw C symbols
310 ss.set_cpp_options(options.cpp_includes,
312 options.cpp_undefines)
313 ss.parse_files(filenames)
314 ss.parse_macros(filenames)
316 # Transform the C symbols into AST nodes
317 transformer.set_source_ast(ss)
319 # Transform the C AST nodes into higher level
321 glibtransformer = GLibTransformer(transformer,
322 noclosure=options.noclosure)
324 # Do enough parsing that we have the get_type() functions to reference
325 # when creating the introspection binary
326 glibtransformer.init_parse()
329 args=[options.program]
330 args.extend(options.program_args)
331 binary = IntrospectionBinary(args)
333 binary = compile_introspection_binary(options,
334 glibtransformer.get_get_type_functions())
336 glibtransformer.set_introspection_binary(binary)
338 namespace = glibtransformer.parse()
340 ap = AnnotationParser(namespace, ss, transformer)
343 except InvalidAnnotationError, e:
344 raise SystemExit("ERROR in annotation: %s" % (str(e), ))
347 writer = Writer(namespace, libraries, transformer.get_includes(),
349 data = writer.get_xml()
351 fd = open(options.output, "w")
358 sys.exit(main(sys.argv))