source: rtems-source-builder/source-builder/pkg-config @ 9797bd1

4.104.114.95
Last change on this file since 9797bd1 was 9797bd1, checked in by Chris Johns <chrisj@…>, on 02/14/14 at 02:26:11

sb: Clean up using argparse. It is not available on CentOS.

Include the argparse package in the source and use if not available.

  • Property mode set to 100755
File size: 10.2 KB
Line 
1#! /usr/bin/env python
2#
3# RTEMS Tools Project (http://www.rtems.org/)
4# Copyright 2014 Chris Johns (chrisj@rtems.org)
5# All rights reserved.
6#
7# This file is part of the RTEMS Tools package in 'rtems-tools'.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions are met:
11#
12# 1. Redistributions of source code must retain the above copyright notice,
13# this list of conditions and the following disclaimer.
14#
15# 2. Redistributions in binary form must reproduce the above copyright notice,
16# this list of conditions and the following disclaimer in the documentation
17# and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32import os
33import sys
34
35base = os.path.dirname(sys.argv[0])
36sys.path.insert(0, base + '/sb')
37
38try:
39    import argparse
40except:
41    sys.path.insert(0, base + '/sb/imports')
42    try:
43        import argparse
44    except:
45        print >> sys.stderr, "Incorrect Source Builder installation"
46        sys.exit(1)
47
48try:
49    import pkgconfig
50except ImportError:
51    print >> sys.stderr, "Incorrect Source Builder installation"
52    sys.exit(1)
53
54#
55# Make trace true to get a file of what happens and what is being asked.
56#
57trace = True
58trace_stdout = False
59logfile = 'pkg-config.log'
60out = None
61
62#
63# Write all the package source parsed to a single file.
64#
65trace_src = True
66if trace_src:
67    src = open('pkg-src.txt', 'w')
68
69def log(s, lf = True):
70    global trace, logfile, out
71    if trace:
72        if out is None:
73            if logfile:
74                out = open(logfile, 'a')
75            else:
76                out = sys.stdout
77        if lf:
78            if out != sys.stdout and trace_stdout:
79                print s
80            print >> out, s
81        else:
82            if out != sys.stdout and trace_stdout:
83                print s,
84            print >> out, s,
85
86def _check_package(lib_check, args):
87    ec = 1
88    pkg = None
89    flags = { 'cflags': '',
90              'libs': '' }
91    libs = pkgconfig.package.splitter(lib_check)
92    for lib in libs:
93        log('pkg: %s' % (lib))
94        pkg = pkgconfig.package(lib[0], prefix = args.prefix, output = log, src = src)
95        if args.dump:
96            log(pkg)
97        if pkg.exists():
98            if len(lib) == 1:
99                if args.exact_version:
100                    if pkg.check('=', args.exact_version):
101                        ec = 0
102                elif args.atleast_version:
103                    if pkg.check('>=', args.atleast_version):
104                        ec = 0
105                elif args.max_version:
106                    if pkg.check('<=', args.max_version):
107                        ec = 0
108                else:
109                    ec = 0
110            else:
111                if len(lib) != 3:
112                    raise error('invalid package check: %s' % (' '.join(lib)))
113                if pkg.check(lib[1], lib[2]):
114                    ec = 0
115            if ec == 0:
116                cflags = pkg.get('cflags')
117                if cflags:
118                    flags['cflags'] += cflags
119                libs = pkg.get('libs', private = False)
120                if libs:
121                    flags['libs'] += libs
122                break
123        if ec > 0:
124            break
125    return ec, pkg, flags
126
127def run(argv):
128
129    class version_action(argparse.Action):
130        def __call__(self, parser, namespace, values, option_string = None):
131            parts = values[0].strip().split('.')
132            for p in parts:
133                if not p.isdigit():
134                    raise error('invalid version value: %s' % (values))
135            setattr(namespace, self.dest, '.'.join(parts))
136
137    ec = 0
138
139    opts = argparse.ArgumentParser(prog = 'pkg-config', description = 'Package Configuration.')
140    opts.add_argument('libraries', metavar='lib', type = str,  help = 'a library', nargs = '*')
141    opts.add_argument('--modversion', dest = 'modversion', action = 'store', default = None,
142                      help = 'Requests that the version information of the libraries.')
143    opts.add_argument('--print-errors', dest = 'print_errors', action = 'store_true',
144                      default = False,
145                      help = 'Print any errors.')
146    opts.add_argument('--short-errors', dest = 'short_errors', action = 'store_true',
147                      default = False,
148                      help = 'Make error messages short.')
149    opts.add_argument('--silence-errors', dest = 'silence_errors', action = 'store_true',
150                      default = False,
151                      help = 'Do not print any errors.')
152    opts.add_argument('--errors-to-stdout', dest = 'errors_to_stdout', action = 'store_true',
153                      default = False,
154                      help = 'Print errors to stdout rather than stderr.')
155    opts.add_argument('--cflags', dest = 'cflags', action = 'store_true',
156                      default = False,
157                      help = 'This prints pre-processor and compile flags required to' \
158                             ' compile the package(s)')
159    opts.add_argument('--libs', dest = 'libs', action = 'store_true',
160                      default = False,
161                      help = 'This option is identical to "--cflags", only it prints the' \
162                             ' link flags.')
163    opts.add_argument('--libs-only-L', dest = 'libs_only_L', action = 'store_true',
164                      default = False,
165                      help = 'This prints the -L/-R part of "--libs".')
166    opts.add_argument('--libs-only-l', dest = 'libs_only_l', action = 'store_true',
167                      default = False,
168                      help = 'This prints the -l part of "--libs".')
169    opts.add_argument('--variable', dest = 'variable', action = 'store',
170                      nargs = 1, default = None,
171                      help = 'This returns the value of a variable.')
172    opts.add_argument('--define-variable', dest = 'define_variable', action = 'store',
173                      nargs = 1, default = None,
174                      help = 'This sets a global value for a variable')
175    opts.add_argument('--uninstalled', dest = 'uninstalled', action = 'store_true',
176                      default = False,
177                      help = 'Ignored')
178    opts.add_argument('--atleast-pkgconfig-version', dest = 'atleast_pkgconfig_version',
179                      action = 'store', nargs = 1, default = None,
180                      help = 'Check the version of package config. Always ok.')
181    opts.add_argument('--exists', dest = 'exists', action = 'store_true',
182                      default = False,
183                      help = 'Test if a library is present')
184    opts.add_argument('--atleast-version', dest = 'atleast_version',
185                      action = version_action, nargs = 1, default = None,
186                      help = 'The package is at least this version.')
187    opts.add_argument('--exact-version', dest = 'exact_version', action = version_action,
188                       nargs = 1, default = None,
189                        help = 'The package is the exact version.')
190    opts.add_argument('--max-version', dest = 'max_version', action = version_action,
191                      nargs = 1, default = None,
192                      help = 'The package is no later than this version.')
193    opts.add_argument('--msvc-syntax', dest = 'msvc_syntax', action = 'store_true',
194                      default = False,
195                      help = 'Ignored')
196    opts.add_argument('--dont-define-prefix', dest = 'dont_define_prefix', action = 'store_true',
197                      default = False,
198                      help = 'Ignored')
199    opts.add_argument('--prefix-variable', dest = 'prefix', action = 'store',
200                      nargs = 1, default = pkgconfig.default_prefix(),
201                      help = 'Define the prefix.')
202    opts.add_argument('--static', dest = 'static', action = 'store_true',
203                      default = False,
204                      help = 'Output libraries suitable for static linking')
205    opts.add_argument('--dump', dest = 'dump', action = 'store_true',
206                      default = False,
207                      help = 'Dump the package if one is found.')
208
209    args = opts.parse_args(argv[1:])
210
211    if (args.exists and (args.exact_version or args.max_version)) or \
212            (args.exact_version and (args.exists or args.max_version)) or \
213            (args.max_version and (args.exists or args.exact_version)):
214        raise error('only one of --exists, --exact-version, or --max-version')
215
216    exists = False
217
218    ec = 1
219
220    if args.atleast_pkgconfig_version:
221        ec = 0
222    else:
223        for lib in args.libraries:
224            ec, pkg, flags = _check_package(lib, args)
225            if ec == 0:
226                if args.cflags:
227                    if len(flags['cflags']):
228                        print flags['cflags']
229                        log('cflags: %s' % (flags['cflags']))
230                    else:
231                        log('cflags: empty')
232                if args.libs:
233                    if len(flags['libs']):
234                        print flags['libs']
235                        log('libs: %s' % (flags['libs']))
236                    else:
237                        log('libs: empty')
238
239    #pkgconfig.package.dump_loaded()
240
241    return ec
242
243try:
244    log('-' * 40)
245    log('pkg-config', lf = False)
246    for a in sys.argv[1:]:
247        log(' "%s"' % (a), lf = False)
248    log('')
249    ec = run(sys.argv)
250    log('ec = %d' % (ec))
251except ImportError:
252    print >> sys.stderr, "incorrect package config installation"
253    sys.exit(1)
254except pkgconfig.error, e:
255    print >> sys.stderr, 'error: %s' % (e)
256    sys.exit(1)
257sys.exit(ec)
Note: See TracBrowser for help on using the repository browser.