flashable_executable.gni 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. import("//build_overrides/build.gni")
  15. # Convert a binary to a target format using objcopy.
  16. template("objcopy_convert") {
  17. forward_variables_from(invoker,
  18. [
  19. "conversion_input",
  20. "conversion_output",
  21. "conversion_target_format",
  22. "deps",
  23. "objcopy",
  24. ])
  25. action(target_name) {
  26. inputs = [ conversion_input ]
  27. outputs = [ conversion_output ]
  28. args = [
  29. objcopy,
  30. "-O",
  31. conversion_target_format,
  32. rebase_path(conversion_input, root_build_dir),
  33. rebase_path(conversion_output, root_build_dir),
  34. ]
  35. script = "${build_root}/gn_run_binary.py"
  36. }
  37. }
  38. # Build a script to perform a device flashing operation.
  39. #
  40. # This requires a Python script, given by flashing_script_generator,
  41. # to construct the resulting flashing script, given by flashing_script_name.
  42. #
  43. # As used by flashable_executable(), the generator script requires two options,
  44. # --output SCRIPT - The generated script
  45. # --application IMAGE - The file to be flashed
  46. # plus any platform- or target-specific options as passed in by
  47. # flashable_executable()'s flashing_options.
  48. template("gen_flashing_script") {
  49. forward_variables_from(invoker,
  50. [
  51. "flashing_script_generator",
  52. "flashing_script_name",
  53. "flashing_script_inputs",
  54. "flashing_options",
  55. "deps",
  56. "data_deps",
  57. ])
  58. action(target_name) {
  59. outputs = [ flashing_script_name ]
  60. args = flashing_options
  61. args += [
  62. "--output",
  63. rebase_path(flashing_script_name, root_build_dir),
  64. ]
  65. script = flashing_script_generator
  66. inputs = flashing_script_inputs
  67. }
  68. }
  69. # Build target for an executable, optionally converted to the preferred form
  70. # for flashing, plus a script that performs the flashing operation.
  71. #
  72. # The intent is that every flashable (or testable) build target in CHIP will
  73. # ultimately be flashable/runnable in a consistent way.
  74. template("flashable_executable") {
  75. executable_target = "$target_name.executable"
  76. if (defined(invoker.flashing_script_name)) {
  77. # Generating the flashing script is the final target.
  78. final_target = "$target_name.flashing"
  79. } else if (defined(invoker.objcopy_image_name)) {
  80. # Converted image is the final target.
  81. final_target = "$target_name.image"
  82. } else {
  83. # The executable is the final target.
  84. final_target = executable_target
  85. }
  86. if (defined(invoker.flashbundle_name)) {
  87. flashbundle_name = invoker.flashbundle_name
  88. } else {
  89. flashbundle_name = "${target_name}.flashbundle.txt"
  90. }
  91. group(target_name) {
  92. data_deps = [ ":$final_target" ]
  93. if (defined(invoker.data_deps)) {
  94. data_deps += invoker.data_deps
  95. }
  96. write_runtime_deps = "${root_out_dir}/${flashbundle_name}"
  97. }
  98. if (defined(invoker.objcopy_image_name)) {
  99. # Executable target must be converted for flashing.
  100. assert(defined(invoker.objcopy_image_format))
  101. assert(defined(invoker.objcopy))
  102. image_target = "$target_name.image"
  103. image_name = invoker.objcopy_image_name
  104. image_format = invoker.objcopy_image_format
  105. objcopy = invoker.objcopy
  106. objcopy_convert(image_target) {
  107. conversion_input = "${root_out_dir}/${invoker.output_name}"
  108. conversion_output = "${root_out_dir}/${image_name}"
  109. conversion_target_format = image_format
  110. deps = [ ":$executable_target" ]
  111. }
  112. }
  113. if (defined(invoker.flashing_script_name)) {
  114. if (!defined(image_target)) {
  115. # The executable can be flashed directly.
  116. image_target = executable_target
  117. image_name = invoker.output_name
  118. }
  119. gen_flashing_script("$target_name.flashing") {
  120. flashing_script_generator = invoker.flashing_script_generator
  121. flashing_script_inputs = invoker.flashing_script_inputs
  122. flashing_script_name = "$root_out_dir/${invoker.flashing_script_name}"
  123. if (defined(invoker.flashing_options)) {
  124. flashing_options = invoker.flashing_options
  125. } else {
  126. flashing_options = []
  127. }
  128. flashing_options += [
  129. "--application",
  130. rebase_path(image_name, root_out_dir, root_out_dir),
  131. ]
  132. data_deps = [ ":$image_target" ]
  133. }
  134. }
  135. executable(executable_target) {
  136. forward_variables_from(invoker, "*")
  137. }
  138. }