source: rtems-source-builder/source-builder/pkg-config @ ecda605

4.104.114.95
Last change on this file since ecda605 was ecda605, checked in by Chris Johns <chrisj@…>, on 04/08/14 at 06:16:50

sb: Fix pkg-config to handle quoted libraries.

  • 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(libraries, args):
87    ec = 1
88    pkg = None
89    flags = { 'cflags': '',
90              'libs': '' }
91    log('libraries: %s' % (libraries))
92    libs = pkgconfig.package.splitter(libraries)
93    for lib in libs:
94        log('pkg: %s' % (lib))
95        pkg = pkgconfig.package(lib[0], prefix = args.prefix, output = log, src = src)
96        if args.dump:
97            log(pkg)
98        if pkg.exists():
99            if len(lib) == 1:
100                if args.exact_version:
101                    if pkg.check('=', args.exact_version):
102                        ec = 0
103                elif args.atleast_version:
104                    if pkg.check('>=', args.atleast_version):
105                        ec = 0
106                elif args.max_version:
107                    if pkg.check('<=', args.max_version):
108                        ec = 0
109                else:
110                    ec = 0
111            else:
112                if len(lib) != 3:
113                    raise error('invalid package check: %s' % (' '.join(lib)))
114                if pkg.check(lib[1], lib[2]):
115                    ec = 0
116            if ec == 0:
117                cflags = pkg.get('cflags')
118                if cflags:
119                    flags['cflags'] += cflags
120                libs = pkg.get('libs', private = False)
121                if libs:
122                    flags['libs'] += libs
123                break
124        if ec > 0:
125            break
126    return ec, pkg, flags
127
128def run(argv):
129
130    class version_action(argparse.Action):
131        def __call__(self, parser, namespace, values, option_string = None):
132            parts = values[0].strip().split('.')
133            for p in parts:
134                if not p.isdigit():
135                    raise error('invalid version value: %s' % (values))
136            setattr(namespace, self.dest, '.'.join(parts))
137
138    ec = 0
139
140    opts = argparse.ArgumentParser(prog = 'pkg-config', description = 'Package Configuration.')
141    opts.add_argument('libraries', metavar='lib', type = str,  help = 'a library', nargs = '*')
142    opts.add_argument('--modversion', dest = 'modversion', action = 'store', default = None,
143                      help = 'Requests that the version information of the libraries.')
144    opts.add_argument('--print-errors', dest = 'print_errors', action = 'store_true',
145                      default = False,
146                      help = 'Print any errors.')
147    opts.add_argument('--short-errors', dest = 'short_errors', action = 'store_true',
148                      default = False,
149                      help = 'Make error messages short.')
150    opts.add_argument('--silence-errors', dest = 'silence_errors', action = 'store_true',
151                      default = False,
152                      help = 'Do not print any errors.')
153    opts.add_argument('--errors-to-stdout', dest = 'errors_to_stdout', action = 'store_true',
154                      default = False,
155                      help = 'Print errors to stdout rather than stderr.')
156    opts.add_argument('--cflags', dest = 'cflags', action = 'store_true',
157                      default = False,
158                      help = 'This prints pre-processor and compile flags required to' \
159                             ' compile the package(s)')
160    opts.add_argument('--libs', dest = 'libs', action = 'store_true',
161                      default = False,
162                      help = 'This option is identical to "--cflags", only it prints the' \
163                             ' link flags.')
164    opts.add_argument('--libs-only-L', dest = 'libs_only_L', action = 'store_true',
165                      default = False,
166                      help = 'This prints the -L/-R part of "--libs".')
167    opts.add_argument('--libs-only-l', dest = 'libs_only_l', action = 'store_true',
168                      default = False,
169                      help = 'This prints the -l part of "--libs".')
170    opts.add_argument('--variable', dest = 'variable', action = 'store',
171                      nargs = 1, default = None,
172                      help = 'This returns the value of a variable.')
173    opts.add_argument('--define-variable', dest = 'define_variable', action = 'store',
174                      nargs = 1, default = None,
175                      help = 'This sets a global value for a variable')
176    opts.add_argument('--uninstalled', dest = 'uninstalled', action = 'store_true',
177                      default = False,
178                      help = 'Ignored')
179    opts.add_argument('--atleast-pkgconfig-version', dest = 'atleast_pkgconfig_version',
180                      action = 'store', nargs = 1, default = None,
181                      help = 'Check the version of package config. Always ok.')
182    opts.add_argument('--exists', dest = 'exists', action = 'store_true',
183                      default = False,
184                      help = 'Test if a library is present')
185    opts.add_argument('--atleast-version', dest = 'atleast_version',
186                      action = version_action, nargs = 1, default = None,
187                      help = 'The package is at least this version.')
188    opts.add_argument('--exact-version', dest = 'exact_version', action = version_action,
189                       nargs = 1, default = None,
190                        help = 'The package is the exact version.')
191    opts.add_argument('--max-version', dest = 'max_version', action = version_action,
192                      nargs = 1, default = None,
193                      help = 'The package is no later than this version.')
194    opts.add_argument('--msvc-syntax', dest = 'msvc_syntax', action = 'store_true',
195                      default = False,
196                      help = 'Ignored')
197    opts.add_argument('--dont-define-prefix', dest = 'dont_define_prefix', action = 'store_true',
198                      default = False,
199                      help = 'Ignored')
200    opts.add_argument('--prefix-variable', dest = 'prefix', action = 'store',
201                      nargs = 1, default = pkgconfig.default_prefix(),
202                      help = 'Define the prefix.')
203    opts.add_argument('--static', dest = 'static', action = 'store_true',
204                      default = False,
205                      help = 'Output libraries suitable for static linking')
206    opts.add_argument('--dump', dest = 'dump', action = 'store_true',
207                      default = False,
208                      help = 'Dump the package if one is found.')
209
210    args = opts.parse_args(argv[1:])
211
212    if (args.exists and (args.exact_version or args.max_version)) or \
213            (args.exact_version and (args.exists or args.max_version)) or \
214            (args.max_version and (args.exists or args.exact_version)):
215        raise error('only one of --exists, --exact-version, or --max-version')
216
217    if args.dont_define_prefix:
218        args.prefix = pkgconfig.default_prefix(False)
219
220    exists = False
221
222    ec = 1
223
224    if args.atleast_pkgconfig_version:
225        ec = 0
226    else:
227        ec, pkg, flags = _check_package(args.libraries, args)
228        if ec == 0:
229            if args.cflags:
230                if len(flags['cflags']):
231                    print flags['cflags']
232                    log('cflags: %s' % (flags['cflags']))
233                else:
234                    log('cflags: empty')
235            if args.libs:
236                if len(flags['libs']):
237                    print flags['libs']
238                    log('libs: %s' % (flags['libs']))
239                else:
240                    log('libs: empty')
241
242    #pkgconfig.package.dump_loaded()
243
244    return ec
245
246try:
247    log('-' * 40)
248    log('pkg-config', lf = False)
249    for a in sys.argv[1:]:
250        log(' "%s"' % (a), lf = False)
251    log('')
252    ec = run(sys.argv)
253    log('ec = %d' % (ec))
254except ImportError:
255    print >> sys.stderr, "incorrect package config installation"
256    sys.exit(1)
257except pkgconfig.error, e:
258    print >> sys.stderr, 'error: %s' % (e)
259    sys.exit(1)
260sys.exit(ec)
Note: See TracBrowser for help on using the repository browser.