compiled_action.gni 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2014 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # This file introduces two related templates that act like action and
  5. # action_foreach but instead of running a Python script, it will compile a
  6. # given tool in the host toolchain and run that (either once or over the list
  7. # of inputs, depending on the variant).
  8. #
  9. # Parameters
  10. #
  11. # tool (required)
  12. # [label] Label of the tool to run. This should be an executable, and
  13. # this label should not include a toolchain (anything in parens). The
  14. # host compile of this tool will be used.
  15. #
  16. # outputs (required)
  17. # [list of files] Like the outputs of action (if using "compiled_action",
  18. # this would be just the list of outputs), or action_foreach (if using
  19. # "compiled_action_foreach", this would contain source expansions mapping
  20. # input to output files).
  21. #
  22. # args (required)
  23. # [list of strings] Same meaning as action/action_foreach.
  24. #
  25. # inputs (optional)
  26. # Files the binary takes as input. The step will be re-run whenever any
  27. # of these change. If inputs is empty, the step will run only when the
  28. # binary itself changes.
  29. #
  30. # depfile
  31. # deps
  32. # visibility (all optional)
  33. # Same meaning as action/action_foreach.
  34. #
  35. #
  36. # Example of usage:
  37. #
  38. # compiled_action("run_my_tool") {
  39. # tool = "//tools/something:mytool"
  40. # outputs = [
  41. # "$target_gen_dir/mysource.cc",
  42. # "$target_gen_dir/mysource.h",
  43. # ]
  44. #
  45. # # The tool takes this input.
  46. # inputs = [ "my_input_file.idl" ]
  47. #
  48. # # In this case, the tool takes as arguments the input file and the output
  49. # # build dir (both relative to the "cd" that the script will be run in)
  50. # # and will produce the output files listed above.
  51. # args = [
  52. # rebase_path("my_input_file.idl", root_build_dir),
  53. # "--output-dir", rebase_path(target_gen_dir, root_build_dir),
  54. # ]
  55. # }
  56. #
  57. # You would typically declare your tool like this:
  58. # if (host_toolchain == current_toolchain) {
  59. # executable("mytool") {
  60. # ...
  61. # }
  62. # }
  63. # The if statement around the executable is optional. That says "I only care
  64. # about this target in the host toolchain". Usually this is what you want, and
  65. # saves unnecessarily compiling your tool for the target platform. But if you
  66. # need a target build of your tool as well, just leave off the if statement.
  67. if (host_os == "win") {
  68. _host_executable_suffix = ".exe"
  69. } else {
  70. _host_executable_suffix = ""
  71. }
  72. template("compiled_action") {
  73. assert(defined(invoker.tool), "tool must be defined for $target_name")
  74. assert(defined(invoker.outputs), "outputs must be defined for $target_name")
  75. assert(defined(invoker.args), "args must be defined for $target_name")
  76. assert(!defined(invoker.sources),
  77. "compiled_action doesn't take a sources arg. Use inputs instead.")
  78. action(target_name) {
  79. forward_variables_from(invoker,
  80. [
  81. "data_deps",
  82. "deps",
  83. "depfile",
  84. "inputs",
  85. "outputs",
  86. "testonly",
  87. "visibility",
  88. ])
  89. if (!defined(deps)) {
  90. deps = []
  91. }
  92. if (!defined(inputs)) {
  93. inputs = []
  94. }
  95. script = "//build/gn_run_binary.py"
  96. # Constuct the host toolchain version of the tool.
  97. host_tool = invoker.tool + "($host_toolchain)"
  98. # Get the path to the executable. Currently, this assumes that the tool
  99. # does not specify output_name so that the target name is the name to use.
  100. # If that's not the case, we'll need another argument to the script to
  101. # specify this, since we can't know what the output name is (it might be in
  102. # another file not processed yet).
  103. host_executable =
  104. get_label_info(host_tool, "root_out_dir") + "/" +
  105. get_label_info(host_tool, "name") + _host_executable_suffix
  106. deps += [ host_tool ]
  107. # The script takes as arguments the binary to run, and then the arguments
  108. # to pass it.
  109. args = [ rebase_path(host_executable, root_build_dir) ] + invoker.args
  110. }
  111. }
  112. template("compiled_action_foreach") {
  113. assert(defined(invoker.sources), "sources must be defined for $target_name")
  114. assert(defined(invoker.tool), "tool must be defined for $target_name")
  115. assert(defined(invoker.outputs), "outputs must be defined for $target_name")
  116. assert(defined(invoker.args), "args must be defined for $target_name")
  117. action_foreach(target_name) {
  118. forward_variables_from(invoker,
  119. [
  120. "deps",
  121. "depfile",
  122. "inputs",
  123. "outputs",
  124. "sources",
  125. "testonly",
  126. "visibility",
  127. ])
  128. if (!defined(deps)) {
  129. deps = []
  130. }
  131. if (!defined(inputs)) {
  132. inputs = []
  133. }
  134. script = "//build/gn_run_binary.py"
  135. # Constuct the host toolchain version of the tool.
  136. host_tool = invoker.tool + "($host_toolchain)"
  137. # Get the path to the executable. Currently, this assumes that the tool
  138. # does not specify output_name so that the target name is the name to use.
  139. # If that's not the case, we'll need another argument to the script to
  140. # specify this, since we can't know what the output name is (it might be in
  141. # another file not processed yet).
  142. host_executable =
  143. get_label_info(host_tool, "root_out_dir") + "/" +
  144. get_label_info(host_tool, "name") + _host_executable_suffix
  145. deps += [ host_tool ]
  146. # The script takes as arguments the binary to run, and then the arguments
  147. # to pass it.
  148. args = [ rebase_path(host_executable, root_build_dir) ] + invoker.args
  149. }
  150. }