Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / build / toolchain / flashable_executable.gni
1 # Copyright (c) 2020 Project CHIP Authors
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import("//build_overrides/build.gni")
16
17 # Convert a binary to a target format using objcopy.
18
19 template("objcopy_convert") {
20   forward_variables_from(invoker,
21                          [
22                            "conversion_input",
23                            "conversion_output",
24                            "conversion_target_format",
25                            "deps",
26                            "objcopy",
27                          ])
28
29   action(target_name) {
30     inputs = [ conversion_input ]
31     outputs = [ conversion_output ]
32
33     args = [
34       objcopy,
35       "-O",
36       conversion_target_format,
37       rebase_path(conversion_input, root_build_dir),
38       rebase_path(conversion_output, root_build_dir),
39     ]
40     script = "${build_root}/gn_run_binary.py"
41   }
42 }
43
44 # Build a script to perform a device flashing operation.
45 #
46 # This requires a Python script, given by flashing_script_generator,
47 # to construct the resulting flashing script, given by flashing_script_name.
48 #
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.
54
55 template("gen_flashing_script") {
56   forward_variables_from(invoker,
57                          [
58                            "flashing_script_generator",
59                            "flashing_script_name",
60                            "flashing_options",
61                            "deps",
62                            "data_deps",
63                          ])
64
65   action(target_name) {
66     outputs = [ flashing_script_name ]
67
68     args = flashing_options
69     args += [
70       "--output",
71       rebase_path(flashing_script_name, root_build_dir),
72     ]
73
74     script = flashing_script_generator
75   }
76 }
77
78 # Build target for an executable, optionally converted to the preferred form
79 # for flashing, plus a script that performs the flashing operation.
80 #
81 # The intent is that every flashable (or testable) build target in CHIP will
82 # ultimately be flashable/runnable in a consistent way.
83
84 template("flashable_executable") {
85   executable_target = "$target_name.executable"
86
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"
93   } else {
94     # The executable is the final target.
95     final_target = executable_target
96   }
97   group(target_name) {
98     data_deps = [ ":$final_target" ]
99
100     if (defined(invoker.data_deps)) {
101       data_deps += invoker.data_deps
102     }
103
104     write_runtime_deps = "${root_out_dir}/${target_name}.flashbundle.txt"
105   }
106
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))
111
112     image_target = "$target_name.image"
113     image_name = invoker.objcopy_image_name
114     image_format = invoker.objcopy_image_format
115     objcopy = invoker.objcopy
116
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" ]
122     }
123   }
124
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
130     }
131
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
137       } else {
138         flashing_options = []
139       }
140       flashing_options += [
141         "--application",
142         rebase_path(image_name, root_out_dir, root_out_dir),
143       ]
144       data_deps = [ ":$image_target" ]
145     }
146   }
147
148   executable(executable_target) {
149     forward_variables_from(invoker, "*")
150   }
151 }