blob: 617ab7db58b303d4e3e614d25a080e1b94ec8d73 [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Primiano Tucci8e627442019-08-28 07:58:38 +02002# 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
Primiano Tucci94c47f02019-12-05 03:13:11 +000031COPYRIGHT_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
Primiano Tucci834fdc72019-10-04 11:33:44 +010049
Primiano Tucci8e627442019-08-28 07:58:38 +020050def 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('.', '_')
Primiano Tucci572dc942019-08-28 20:39:21 +020069 lines = []
Florian Mayer5ed03d12021-04-19 18:07:52 +010070 lines.append('// Generated by %s' % os.path.basename(__file__))
Primiano Tucci572dc942019-08-28 20:39:21 +020071 lines.append('')
72 lines.append('// fix_include_guards: off')
73 lines.append('#ifndef %s' % guard)
74 lines.append('#define %s' % guard)
75 lines.append('')
Primiano Tucci18c0cff2019-09-09 17:09:03 -070076 lines.append('// clang-format off')
Primiano Tucci572dc942019-08-28 20:39:21 +020077 for kv in flags:
78 lines.append('#define PERFETTO_BUILDFLAG_DEFINE_%s() (%s)' % kv)
79 lines.append('')
Primiano Tucci18c0cff2019-09-09 17:09:03 -070080 lines.append('// clang-format on')
Primiano Tucci572dc942019-08-28 20:39:21 +020081 lines.append('#endif // %s' % guard)
82 lines.append('')
83
84 with open(args.out, 'w') as out:
Primiano Tucci94c47f02019-12-05 03:13:11 +000085 out.write(COPYRIGHT_HEADER)
Primiano Tucci572dc942019-08-28 20:39:21 +020086 out.write('\n'.join(lines))
Primiano Tucci8e627442019-08-28 07:58:38 +020087
Primiano Tucci834fdc72019-10-04 11:33:44 +010088
Primiano Tucci8e627442019-08-28 07:58:38 +020089if __name__ == '__main__':
90 sys.exit(main())