1 # Copyright (c) 2020 Project CHIP Authors
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 import("//build_overrides/build.gni")
17 # Convert a binary to a target format using objcopy.
19 template("objcopy_convert") {
20 forward_variables_from(invoker,
24 "conversion_target_format",
30 inputs = [ conversion_input ]
31 outputs = [ conversion_output ]
36 conversion_target_format,
37 rebase_path(conversion_input, root_build_dir),
38 rebase_path(conversion_output, root_build_dir),
40 script = "${build_root}/gn_run_binary.py"
44 # Build a script to perform a device flashing operation.
46 # This requires a Python script, given by flashing_script_generator,
47 # to construct the resulting flashing script, given by flashing_script_name.
49 # As used by flashable_executable(), the generator script requires two options,
50 # --output SCRIPT - The generated script
51 # --application IMAGE - The file to be flashed
52 # plus any platform- or target-specific options as passed in by
53 # flashable_executable()'s flashing_options.
55 template("gen_flashing_script") {
56 forward_variables_from(invoker,
58 "flashing_script_generator",
59 "flashing_script_name",
66 outputs = [ flashing_script_name ]
68 args = flashing_options
71 rebase_path(flashing_script_name, root_build_dir),
74 script = flashing_script_generator
78 # Build target for an executable, optionally converted to the preferred form
79 # for flashing, plus a script that performs the flashing operation.
81 # The intent is that every flashable (or testable) build target in CHIP will
82 # ultimately be flashable/runnable in a consistent way.
84 template("flashable_executable") {
85 executable_target = "$target_name.executable"
87 if (defined(invoker.flashing_script_name)) {
88 # Generating the flashing script is the final target.
89 final_target = "$target_name.flashing"
90 } else if (defined(invoker.objcopy_image_name)) {
91 # Converted image is the final target.
92 final_target = "$target_name.image"
94 # The executable is the final target.
95 final_target = executable_target
98 data_deps = [ ":$final_target" ]
100 if (defined(invoker.data_deps)) {
101 data_deps += invoker.data_deps
104 write_runtime_deps = "${root_out_dir}/${target_name}.flashbundle.txt"
107 if (defined(invoker.objcopy_image_name)) {
108 # Executable target must be converted for flashing.
109 assert(defined(invoker.objcopy_image_format))
110 assert(defined(invoker.objcopy))
112 image_target = "$target_name.image"
113 image_name = invoker.objcopy_image_name
114 image_format = invoker.objcopy_image_format
115 objcopy = invoker.objcopy
117 objcopy_convert(image_target) {
118 conversion_input = "${root_out_dir}/${invoker.output_name}"
119 conversion_output = "${root_out_dir}/${image_name}"
120 conversion_target_format = image_format
121 deps = [ ":$executable_target" ]
125 if (defined(invoker.flashing_script_name)) {
126 if (!defined(image_target)) {
127 # The executable can be flashed directly.
128 image_target = executable_target
129 image_name = invoker.output_name
132 gen_flashing_script("$target_name.flashing") {
133 flashing_script_generator = invoker.flashing_script_generator
134 flashing_script_name = "$root_out_dir/${invoker.flashing_script_name}"
135 if (defined(invoker.flashing_options)) {
136 flashing_options = invoker.flashing_options
138 flashing_options = []
140 flashing_options += [
142 rebase_path(image_name, root_out_dir, root_out_dir),
144 data_deps = [ ":$image_target" ]
148 executable(executable_target) {
149 forward_variables_from(invoker, "*")