2 # Copyright (c) 2021 Project CHIP Authors
3 # Copyright (c) 2018 Nest Labs, Inc.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 # Component makefile for building CHIP within the ESP32 ESP-IDF environment.
27 # Parse the build's compile_commands.json to generate
28 # final args file for CHIP build.
30 argparser = argparse.ArgumentParser()
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")
40 args = argparser.parse_args()
42 compile_commands_path = os.path.join(args.build_dir, "compile_commands.json")
44 with open(compile_commands_path) as compile_commands_json:
45 compile_commands = json.load(compile_commands_json)
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)))
51 if len(compile_command) != 1:
52 raise Exception("Failed to resolve compile flags for %s", src_file)
54 compile_command = compile_command[0]
55 # Trim compiler, input and output
56 compile_flags = compile_command.split()[1:-4]
58 replace = "-I%s" % args.idf_path
59 replace_with = "-isystem%s" % args.idf_path
61 compile_flags = list(map(lambda f: ('"%s"' % f).replace(replace, replace_with), compile_flags))
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]
69 c_flags = get_compile_flags(args.c_file)
70 cpp_flags = get_compile_flags(args.cpp_file)
72 with open(args.input) as args_input:
73 with open(args.output, "w") as args_output:
74 args_output.write(args_input.read())
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))