Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / config / esp32 / components / chip / create_args_gn.py
1 #
2 #    Copyright (c) 2021 Project CHIP Authors
3 #    Copyright (c) 2018 Nest Labs, Inc.
4 #    All rights reserved.
5 #
6 #    Licensed under the Apache License, Version 2.0 (the "License");
7 #    you may not use this file except in compliance with the License.
8 #    You may obtain a copy of the License at
9 #
10 #        http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #    Unless required by applicable law or agreed to in writing, software
13 #    distributed under the License is distributed on an "AS IS" BASIS,
14 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #    See the License for the specific language governing permissions and
16 #    limitations under the License.
17 #
18 #    Description:
19 #      Component makefile for building CHIP within the ESP32 ESP-IDF environment.
20 #
21
22 import json
23 import os
24 import argparse
25 import re
26
27 # Parse the build's compile_commands.json to generate
28 # final args file for CHIP build.
29
30 argparser = argparse.ArgumentParser()
31
32 argparser.add_argument("build_dir")
33 argparser.add_argument("idf_path")
34 argparser.add_argument("c_file")
35 argparser.add_argument("cpp_file")
36 argparser.add_argument("input")
37 argparser.add_argument("output")
38 argparser.add_argument("--filter-out")
39
40 args = argparser.parse_args()
41
42 compile_commands_path = os.path.join(args.build_dir, "compile_commands.json")
43
44 with open(compile_commands_path) as compile_commands_json:
45     compile_commands = json.load(compile_commands_json)
46
47     def get_compile_flags(src_file):
48         compile_command = list(map(lambda res: res["command"],
49                                filter(lambda cmd: cmd["file"] == src_file, compile_commands)))
50
51         if len(compile_command) != 1:
52             raise Exception("Failed to resolve compile flags for %s", src_file)
53
54         compile_command = compile_command[0]
55         # Trim compiler, input and output
56         compile_flags = compile_command.split()[1:-4]
57
58         replace = "-I%s" % args.idf_path
59         replace_with = "-isystem%s" % args.idf_path
60
61         compile_flags = list(map(lambda f: ('"%s"' % f).replace(replace, replace_with), compile_flags))
62
63         if args.filter_out:
64             filter_out = list(map(lambda f: ('"%s"' % f), args.filter_out.split(';')))
65             compile_flags = [c for c in compile_flags if c not in filter_out]
66
67         return compile_flags
68
69     c_flags = get_compile_flags(args.c_file)
70     cpp_flags = get_compile_flags(args.cpp_file)
71
72     with open(args.input) as args_input:
73         with open(args.output, "w") as args_output:
74             args_output.write(args_input.read())
75
76             args_output.write("target_cflags_c = [%s]" % ', '.join(c_flags))
77             args_output.write("\n")
78             args_output.write("target_cflags_cc = [%s]" % ', '.join(cpp_flags))