Primiano Tucci | 34bc559 | 2021-02-19 17:53:36 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Primiano Tucci | 8e62744 | 2019-08-28 07:58:38 +0200 | [diff] [blame] | 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 | |
| 26 | import argparse |
| 27 | import os |
| 28 | import shlex |
| 29 | import sys |
| 30 | |
Primiano Tucci | 94c47f0 | 2019-12-05 03:13:11 +0000 | [diff] [blame] | 31 | COPYRIGHT_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 Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 49 | |
Primiano Tucci | 8e62744 | 2019-08-28 07:58:38 +0200 | [diff] [blame] | 50 | def 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 Tucci | 572dc94 | 2019-08-28 20:39:21 +0200 | [diff] [blame] | 69 | lines = [] |
Florian Mayer | 5ed03d1 | 2021-04-19 18:07:52 +0100 | [diff] [blame] | 70 | lines.append('// Generated by %s' % os.path.basename(__file__)) |
Primiano Tucci | 572dc94 | 2019-08-28 20:39:21 +0200 | [diff] [blame] | 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('') |
Primiano Tucci | 18c0cff | 2019-09-09 17:09:03 -0700 | [diff] [blame] | 76 | lines.append('// clang-format off') |
Primiano Tucci | 572dc94 | 2019-08-28 20:39:21 +0200 | [diff] [blame] | 77 | for kv in flags: |
| 78 | lines.append('#define PERFETTO_BUILDFLAG_DEFINE_%s() (%s)' % kv) |
| 79 | lines.append('') |
Primiano Tucci | 18c0cff | 2019-09-09 17:09:03 -0700 | [diff] [blame] | 80 | lines.append('// clang-format on') |
Primiano Tucci | 572dc94 | 2019-08-28 20:39:21 +0200 | [diff] [blame] | 81 | lines.append('#endif // %s' % guard) |
| 82 | lines.append('') |
| 83 | |
| 84 | with open(args.out, 'w') as out: |
Primiano Tucci | 94c47f0 | 2019-12-05 03:13:11 +0000 | [diff] [blame] | 85 | out.write(COPYRIGHT_HEADER) |
Primiano Tucci | 572dc94 | 2019-08-28 20:39:21 +0200 | [diff] [blame] | 86 | out.write('\n'.join(lines)) |
Primiano Tucci | 8e62744 | 2019-08-28 07:58:38 +0200 | [diff] [blame] | 87 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 88 | |
Primiano Tucci | 8e62744 | 2019-08-28 07:58:38 +0200 | [diff] [blame] | 89 | if __name__ == '__main__': |
| 90 | sys.exit(main()) |