summaryrefslogtreecommitdiffstats
path: root/source-builder/pkg-config
blob: fa251e5b00ec4ee65e24161ca64240765f0d6d63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#! /usr/bin/env python
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2014-2016 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

from __future__ import print_function

import os
import sys

base = os.path.dirname(sys.argv[0])
sys.path.insert(0, base + '/sb')

try:
    import argparse
except:
    sys.path.insert(0, base + '/sb/imports')
    try:
        import argparse
    except:
        print("Incorrect Source Builder installation", file = sys.stderr)
        sys.exit(1)

try:
    import pkgconfig
except ImportError:
    print("Incorrect Source Builder installation", file = sys.stderr)
    sys.exit(1)

#
# Make trace true to get a file of what happens and what is being asked.
#
trace = True
trace_stdout = False
logfile = 'pkg-config.log'
out = None
srcfd = None

#
# Write all the package source parsed to a single file.
#
trace_src = True
if trace_src:
    srcfd = open('pkg-src.txt', 'w')

def src(text):
    if srcfd:
        srcfd.writelines(text)

def log(s, lf = True):
    global trace, logfile, out
    if trace:
        if out is None:
            if logfile:
                out = open(logfile, 'a')
            else:
                out = sys.stdout
        if lf:
            if out != sys.stdout and trace_stdout:
                print(s)
            print(s, file = out)
        else:
            if out != sys.stdout and trace_stdout:
                print(s, end = '')
                sys.stdout.flush()
            print(s, end = '', file = out)

def run(argv):

    class version_action(argparse.Action):
        def __call__(self, parser, namespace, values, option_string = None):
            parts = values[0].strip().split('.')
            for p in parts:
                if not p.isdigit():
                    raise error('invalid version value: %s' % (values))
            setattr(namespace, self.dest, '.'.join(parts))

    ec = 0

    opts = argparse.ArgumentParser(prog = 'pkg-config', description = 'Package Configuration.')
    opts.add_argument('libraries', metavar='lib', type = str,  help = 'a library', nargs = '*')
    opts.add_argument('--modversion', dest = 'modversion', action = 'store', default = None,
                      help = 'Requests that the version information of the libraries.')
    opts.add_argument('--print-errors', dest = 'print_errors', action = 'store_true',
                      default = False,
                      help = 'Print any errors.')
    opts.add_argument('--short-errors', dest = 'short_errors', action = 'store_true',
                      default = False,
                      help = 'Make error messages short.')
    opts.add_argument('--silence-errors', dest = 'silence_errors', action = 'store_true',
                      default = False,
                      help = 'Do not print any errors.')
    opts.add_argument('--errors-to-stdout', dest = 'errors_to_stdout', action = 'store_true',
                      default = False,
                      help = 'Print errors to stdout rather than stderr.')
    opts.add_argument('--cflags', dest = 'cflags', action = 'store_true',
                      default = False,
                      help = 'This prints pre-processor and compile flags required to' \
                             ' compile the package(s)')
    opts.add_argument('--libs', dest = 'libs', action = 'store_true',
                      default = False,
                      help = 'This option is identical to "--cflags", only it prints the' \
                             ' link flags.')
    opts.add_argument('--libs-only-L', dest = 'libs_only_L', action = 'store_true',
                      default = False,
                      help = 'This prints the -L/-R part of "--libs".')
    opts.add_argument('--libs-only-l', dest = 'libs_only_l', action = 'store_true',
                      default = False,
                      help = 'This prints the -l part of "--libs".')
    opts.add_argument('--variable', dest = 'variable', action = 'store',
                      nargs = 1, default = None,
                      help = 'This returns the value of a variable.')
    opts.add_argument('--define-variable', dest = 'define_variable', action = 'store',
                      nargs = 1, default = None,
                      help = 'This sets a global value for a variable')
    opts.add_argument('--uninstalled', dest = 'uninstalled', action = 'store_true',
                      default = False,
                      help = 'Ignored')
    opts.add_argument('--atleast-pkgconfig-version', dest = 'atleast_pkgconfig_version',
                      action = 'store', nargs = 1, default = None,
                      help = 'Check the version of package config. Always ok.')
    opts.add_argument('--exists', dest = 'exists', action = 'store_true',
                      default = False,
                      help = 'Test if a library is present')
    opts.add_argument('--atleast-version', dest = 'atleast_version',
                      action = version_action, nargs = 1, default = None,
                      help = 'The package is at least this version.')
    opts.add_argument('--exact-version', dest = 'exact_version', action = version_action,
                       nargs = 1, default = None,
                        help = 'The package is the exact version.')
    opts.add_argument('--max-version', dest = 'max_version', action = version_action,
                      nargs = 1, default = None,
                      help = 'The package is no later than this version.')
    opts.add_argument('--msvc-syntax', dest = 'msvc_syntax', action = 'store_true',
                      default = False,
                      help = 'Ignored')
    opts.add_argument('--dont-define-prefix', dest = 'dont_define_prefix', action = 'store_true',
                      default = False,
                      help = 'Ignored')
    opts.add_argument('--prefix-variable', dest = 'prefix', action = 'store',
                      nargs = 1, default = pkgconfig.default_prefix(),
                      help = 'Define the prefix.')
    opts.add_argument('--static', dest = 'static', action = 'store_true',
                      default = False,
                      help = 'Output libraries suitable for static linking')
    opts.add_argument('--dump', dest = 'dump', action = 'store_true',
                      default = False,
                      help = 'Dump the package if one is found.')

    args = opts.parse_args(argv[1:])

    if (args.exists and (args.exact_version or args.max_version)) or \
            (args.exact_version and (args.exists or args.max_version)) or \
            (args.max_version and (args.exists or args.exact_version)):
        raise error('only one of --exists, --exact-version, or --max-version')

    if args.dont_define_prefix:
        args.prefix = pkgconfig.default_prefix(False)

    exists = False

    ec = 1

    if args.atleast_pkgconfig_version:
        ec = 0
    else:
        ec, pkg, flags = pkgconfig.check_package(args.libraries, args, log, src)
        if ec == 0:
            if args.cflags:
                if len(flags['cflags']):
                    print(flags['cflags'])
                    log('cflags: %s' % (flags['cflags']))
                else:
                    log('cflags: empty')
            if args.libs:
                if len(flags['libs']):
                    print(flags['libs'])
                    log('libs: %s' % (flags['libs']))
                else:
                    log('libs: empty')

    #pkgconfig.package.dump_loaded()

    return ec

try:
    log('-' * 40)
    log('pkg-config', lf = False)
    for a in sys.argv[1:]:
        log(' "%s"' % (a), lf = False)
    log('')
    ec = run(sys.argv)
    log('ec = %d' % (ec))
except ImportError:
    print("incorrect package config installation", file = sys.stderr)
    sys.exit(1)
except pkgconfig.error as e:
    print('error: %s' % (e), file = sys.stderr)
    sys.exit(1)
sys.exit(ec)