1#!/usr/bin/env python3
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This writes headers for build flags. See the gen_buildflags target in
17# /gn/BUILD.gn for usage.
18#
19# The parameters are passed in a response file so we don't have to worry
20# about command line lengths. The name of the response file is passed on the
21# command line.
22#
23# The format of the response file is:
24#    [--flags <list of one or more flag values>]
25
26import argparse
27import os
28import shlex
29import sys
30
31COPYRIGHT_HEADER = '''/*
32 * Copyright (C) 2019 The Android Open Source Project
33 *
34 * Licensed under the Apache License, Version 2.0 (the "License");
35 * you may not use this file except in compliance with the License.
36 * You may obtain a copy of the License at
37 *
38 *      http://www.apache.org/licenses/LICENSE-2.0
39 *
40 * Unless required by applicable law or agreed to in writing, software
41 * distributed under the License is distributed on an "AS IS" BASIS,
42 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43 * See the License for the specific language governing permissions and
44 * limitations under the License.
45 */
46
47'''
48
49
50def main():
51  parser = argparse.ArgumentParser()
52  parser.add_argument('--rsp', help='Input response file containing the flags.')
53  parser.add_argument('--out', help='Output path of the generated header file.')
54  args = parser.parse_args()
55
56  flags = []
57  with open(args.rsp, 'r') as def_file:
58    marker_seen = False
59    for flag in shlex.split(def_file.read()):
60      if not marker_seen:
61        marker_seen = flag == '--flags'
62        continue
63      key, value = flag.split('=', 1)
64      value = '1' if value == 'true' else '0' if value == 'false' else value
65      flags.append((key, value))
66
67  guard = '%s_' % args.out.upper()
68  guard = guard.replace('/', '_').replace('\\', '_').replace('.', '_')
69  lines = []
70  lines.append('// Generated by %s' % os.path.basename(__file__))
71  lines.append('')
72  lines.append('// fix_include_guards: off')
73  lines.append('#ifndef %s' % guard)
74  lines.append('#define %s' % guard)
75  lines.append('')
76  lines.append('// clang-format off')
77  for kv in flags:
78    lines.append('#define PERFETTO_BUILDFLAG_DEFINE_%s() (%s)' % kv)
79  lines.append('')
80  lines.append('// clang-format on')
81  lines.append('#endif  // %s' % guard)
82  lines.append('')
83
84  with open(args.out, 'w') as out:
85    out.write(COPYRIGHT_HEADER)
86    out.write('\n'.join(lines))
87
88
89if __name__ == '__main__':
90  sys.exit(main())
91