Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / config / nrfconnect / chip-module / make_gn_args.py
1 #!/usr/bin/env python3
2
3 #
4 #    Copyright (c) 2021 Project CHIP Authors
5 #    All rights reserved.
6 #
7 #    Licensed under the Apache License, Version 2.0 (the "License");
8 #    you may not use this file except in compliance with the License.
9 #    You may obtain a copy of the License at
10 #
11 #        http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #    Unless required by applicable law or agreed to in writing, software
14 #    distributed under the License is distributed on an "AS IS" BASIS,
15 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #    See the License for the specific language governing permissions and
17 #    limitations under the License.
18 #
19
20 import argparse
21 import re
22 import sys
23
24 GN_SPECIAL_CHARACTERS = r'(["$\\])'
25 GN_CFLAG_EXCLUDES = [
26     '-fno-asynchronous-unwind-tables',
27     '-fno-common',
28     '-fno-defer-pop',
29     '-fno-reorder-functions',
30     '-ffunction-sections',
31     '-fdata-sections',
32     '-g*',
33     '-O*',
34     '-W*',
35 ]
36
37 def escape_strings(gn_args):
38     return [[key, re.sub(GN_SPECIAL_CHARACTERS, r'\\\1', value)] for key, value in gn_args]
39
40 def write_gn_args(args):
41     if args.module:
42         sys.stdout.write('import("{}")\n'.format(args.module))
43
44     for key, value in args.arg:
45         sys.stdout.write('{} = {}\n'.format(key, value))
46
47     for key, value in args.arg_string:
48         sys.stdout.write('{} = "{}"\n'.format(key, value))
49
50     cflag_excludes = ', '.join(['"{}"'.format(exclude) for exclude in GN_CFLAG_EXCLUDES])
51
52     for key, value in args.arg_cflags:
53         sys.stdout.write('{} = filter_exclude(string_split("{}"), [{}])\n'.format(key, value, cflag_excludes))
54
55 def main():
56     parser = argparse.ArgumentParser()
57     parser.add_argument('--module', action='store')
58     parser.add_argument('--arg', action='append', nargs=2, default=[])
59     parser.add_argument('--arg-string', action='append', nargs=2, default=[])
60     parser.add_argument('--arg-cflags', action='append', nargs=2, default=[])
61     args = parser.parse_args()
62     args.arg_string = escape_strings(args.arg_string)
63     args.arg_cflags = escape_strings(args.arg_cflags)
64     write_gn_args(args)
65
66 if __name__ == "__main__":
67     main()