-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-pc.py
executable file
·97 lines (81 loc) · 3.12 KB
/
generate-pc.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Python script to generate a .pc file from gn.
This scripts fills the fields of the .pc file with the given values of
the flags.
For detailed meaning of each field, please find it in
https://people.freedesktop.org/~dbn/pkg-config-guide.html#concepts
Example:
python generate-pc.py \
--output="${target}.pc" \
--name="${libname}" \
--description="${description}" \
--version="${version}" \
--requires="${requires}"
...
"""
from __future__ import print_function
# TODO(crbug.com/868820): chromite.lib.commandline may be a good alternate
# but is concerned the loading performance. Considering this is a part of
# configuration, use argparse in standard library. Replace it when the loading
# gets enough fast.
import argparse
import sys
_TEMPLATE = """
Name: %(name)s
Description: %(description)s
Version: %(version)s
Requires: %(requires)s
Requires.private: %(requires_private)s
Libs: %(libs)s
Libs.private: %(libs_private)s
Cflags: %(cflags)s
"""
def _generate(output, params):
"""Generates a .pc file.
Args:
output: Path to the output file.
params: an object containing the .pc file data. It should have the
following members: name, description, version, requires,
requies_private, libs, libs_private and cflags.
"""
with open(output, 'w') as f:
f.write(_TEMPLATE.lstrip('\n') % {
'name': params.name,
'description': params.description,
'version': params.version,
'requires': ' '.join(params.requires),
'requires_private': ' '.join(params.requires_private),
'libs': ' '.join(params.libs),
'libs_private': ' '.join(params.libs_private),
'cflags': ' '.join(params.cflags)
})
def _get_parser():
"""Returns an argument parser for this script."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output', required=True, help='The output file name')
parser.add_argument('--name', required=True, help='The library name')
parser.add_argument('--description', default='',
help='The description of the library')
parser.add_argument('--version', required=True,
help='The version of the library')
parser.add_argument('--requires', action='append', default=[],
help='Packages for Required')
parser.add_argument('--requires-private', action='append', default=[],
help='Packages for Required.private')
parser.add_argument('--libs', action='append', default=[],
help='Libraries for Libs')
parser.add_argument('--libs-private', action='append', default=[],
help='Libraries for Libs.private')
parser.add_argument('--cflags', action='append', default=[],
help='Compiler flags')
return parser
def main(argv):
parser = _get_parser()
options = parser.parse_args(argv)
_generate(options.output, options)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))