[build] Use python_binary in mesa template

... for mesa_python_stdout_to_file_action

This allows us to set python libraries as dependencies, instead of
adding their sources as inputs directly.

python_binary requires scripts to have a main function, so a few
scripts are updated for this.

Change-Id: I49ae526befb79f38bcf441b4449406f13560321a
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/mesa/+/516200
Reviewed-by: John Rosasco <rosasco@google.com>
Reviewed-by: Shai Barack <shayba@google.com>
diff --git a/mesa.gni b/mesa.gni
index a9ac811..c0b9301 100644
--- a/mesa.gni
+++ b/mesa.gni
@@ -19,6 +19,7 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 # IN THE SOFTWARE.
 
+import("//build/python/python_binary.gni")
 import("//src/graphics/lib/magma/gnbuild/magma.gni")
 
 mesa_build_root = "//third_party/mesa"
@@ -40,75 +41,94 @@
 
 # Executes a python script and writes its output to a file.
 #
+# Example
+#
+#   mesa_python_stdout_to_file_action("opcodes.c") {
+#     output = "opcodes.c"
+#     script = "opcodes_c.py"
+#     sources = [ "opcodes_lib.py" ]
+#     libraries = [ "//third_party/mako" ]
+#   }
+#
 # Parameters
-# * script
-#   - Required: The .py file that will be interpreted.
-#   - Type: path.
 #
-# * args
-#   - Optional: Arguments to pass to the script.
-#   - Type: list(str).
+#   script (required)
+#     The .py file that will be interpreted.
+#     Type: path
 #
-#  * output
-#   - Required: Path to the output file. Assumed to be relative to ${target_gen_dir}.
-#   - Type: path.
+#   output (required)
+#     Path to the output file. Assumed to be relative to ${target_gen_dir}.
+#     Type: path
 #
-#  * pythonpath
-#    - Optional: Used as PYTHONPATH environment variable. Defaults to ${magma_python_path}.
-#    - Type: str.
+#   sources (optional)
+#     Extra .py source files script imports.
+#     Type: list(path)
+#     Default: empty list
 #
-# Other parameters have the same meaning as for action().
-
+#   libraries (optional)
+#     Paths to python_libraries script imports.
+#     Type: list(string)
+#     Default: empty list
+#
+#   args (optional)
+#     Arguments to pass to the script.
+#     Type: list(str)
+#     Default: empty list
+#
+#   deps
+#   inputs
+#   testonly
+#   visibility
 template("mesa_python_stdout_to_file_action") {
   assert(defined(invoker.script), "script is required")
   assert(defined(invoker.output), "output is required")
+
+  py_binary_target = "${target_name}_py_binary"
+  python_binary(py_binary_target) {
+    forward_variables_from(invoker,
+                           [
+                             "sources",
+                             "testonly",
+                           ])
+    main_source = invoker.script
+    if (defined(invoker.libraries)) {
+      deps = invoker.libraries
+    }
+    visibility = [ ":*" ]
+  }
+
   action(target_name) {
     forward_variables_from(invoker,
-                           "*",
                            [
-                             "args",
+                             "testonly",
+                             "visibility",
                              "inputs",
-                             "output",
-                             "outputs",
-                             "pythonpath",
-                             "script",
                            ])
 
-    hermetic_deps = false
-
     script = "${mesa_build_root}/scripts/gn_script_wrapper.sh"
+    outputs = [ "${target_gen_dir}/${invoker.output}" ]
 
-    output = "${target_gen_dir}/${invoker.output}"
-
-    if (defined(invoker.pythonpath)) {
-      pythonpath = invoker.pythonpath
-      inputs = []
-    } else {
-      pythonpath = magma_python_path
-
-      # Some file that we can use as a signal that the other files
-      # in magma_python_path have changed.
-      inputs = [ "${magma_python_path}/doc/build/changelog.rst" ]
-    }
+    py_binary = get_target_outputs(":${py_binary_target}")
+    assert(py_binary == [ py_binary[0] ],
+           "${py_binary_target} should only have one output")
+    py_binary = py_binary[0]
+    sources = [ py_binary ]
 
     args = [
       rebase_path(python_exe_src, root_build_dir),
-      pythonpath,
-      rebase_path(output, root_build_dir),
-      rebase_path(invoker.script, root_build_dir),
+
+      # TODO(jayzhuang): remove this arg after migrating mesa_python_action.
+      magma_python_path,
+      rebase_path(outputs[0], root_build_dir),
+      rebase_path(py_binary, root_build_dir),
     ]
     if (defined(invoker.args)) {
       args += invoker.args
     }
 
-    inputs += [ invoker.script ]
-    if (defined(invoker.inputs)) {
-      inputs += invoker.inputs
-    }
-
-    outputs = [ output ]
-    if (defined(invoker.outputs)) {
-      outputs += invoker.outputs
+    deps = [ ":${py_binary_target}" ]
+    if (defined(invoker.deps)) {
+      deps += invoker.deps
     }
   }
 }
diff --git a/src/broadcom/cle/gen_pack_header.py b/src/broadcom/cle/gen_pack_header.py
index cc9e4a0..df54520 100644
--- a/src/broadcom/cle/gen_pack_header.py
+++ b/src/broadcom/cle/gen_pack_header.py
@@ -616,11 +616,15 @@
         self.parser.ParseFile(file)
         file.close()
 
-if len(sys.argv) < 2:
-    print("No input xml file specified")
-    sys.exit(1)
+def main():
+    if len(sys.argv) < 2:
+        print("No input xml file specified")
+        sys.exit(1)
 
-input_file = sys.argv[1]
+    input_file = sys.argv[1]
 
-p = Parser(sys.argv[2])
-p.parse(input_file)
+    p = Parser(sys.argv[2])
+    p.parse(input_file)
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/compiler/glsl/BUILD.gn b/src/compiler/glsl/BUILD.gn
index b1d73c0..f69adf9 100644
--- a/src/compiler/glsl/BUILD.gn
+++ b/src/compiler/glsl/BUILD.gn
@@ -19,13 +19,12 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 # IN THE SOFTWARE.
 
-import("//third_party/mako/py_srcs.gni")
 import("../../../mesa.gni")
 
 mesa_python_stdout_to_file_action("gen_ir_expression_operation") {
   output = "ir_expression_operation.h"
   script = "ir_expression_operation.py"
-  inputs = third_party_mako_srcs
+  libraries = [ "//third_party/mako" ]
   args = [ "enum" ]
 }
 
diff --git a/src/compiler/glsl/ir_expression_operation.py b/src/compiler/glsl/ir_expression_operation.py
index d5c43e6..7cc1da8 100644
--- a/src/compiler/glsl/ir_expression_operation.py
+++ b/src/compiler/glsl/ir_expression_operation.py
@@ -730,7 +730,7 @@
 ]
 
 
-if __name__ == "__main__":
+def main():
    copyright = """/*
  * Copyright (C) 2010 Intel Corporation
  *
@@ -807,3 +807,6 @@
       print(strings_template.render(values=ir_expression_operation))
    elif sys.argv[1] == "constant":
       print(constant_template.render(values=ir_expression_operation))
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/compiler/nir/BUILD.gn b/src/compiler/nir/BUILD.gn
index 7155295..1107cea 100644
--- a/src/compiler/nir/BUILD.gn
+++ b/src/compiler/nir/BUILD.gn
@@ -178,32 +178,42 @@
 mesa_python_stdout_to_file_action("nir_opcodes") {
   output = "nir_opcodes.h"
   script = "nir_opcodes_h.py"
-  inputs = [ "nir_opcodes.py" ] + third_party_mako_srcs
+  sources = [ "nir_opcodes.py" ]
+  libraries = [ "//third_party/mako" ]
 }
 
 mesa_python_stdout_to_file_action("nir_builder_opcodes") {
   output = "nir_builder_opcodes.h"
   script = "nir_builder_opcodes_h.py"
-  inputs = [ "nir_opcodes.py" ] + third_party_mako_srcs
+  sources = [
+    "nir_intrinsics.py",
+    "nir_opcodes.py",
+  ]
+  libraries = [ "//third_party/mako" ]
 }
 
 mesa_python_stdout_to_file_action("nir_constant_expressions") {
   output = "nir_constant_expressions.c"
   script = "nir_constant_expressions.py"
-  inputs = [ "nir_opcodes.py" ] + third_party_mako_srcs
+  sources = [ "nir_opcodes.py" ]
+  libraries = [ "//third_party/mako" ]
 }
 
 mesa_python_stdout_to_file_action("nir_opcodes_c") {
   output = "nir_opcodes.c"
   script = "nir_opcodes_c.py"
-
-  inputs = [ "nir_opcodes.py" ] + third_party_mako_srcs
+  sources = [ "nir_opcodes.py" ]
+  libraries = [ "//third_party/mako" ]
 }
 
 mesa_python_stdout_to_file_action("nir_opt_algebraic") {
   output = "nir_opt_algebraic.c"
   script = "nir_opt_algebraic.py"
-  inputs = [ "nir_algebraic.py" ] + third_party_mako_srcs
+  sources = [
+    "nir_algebraic.py",
+    "nir_opcodes.py",
+  ]
+  libraries = [ "//third_party/mako" ]
 }
 
 mesa_python_action("nir_intrinsics_h") {
diff --git a/src/compiler/nir/nir_builder_opcodes_h.py b/src/compiler/nir/nir_builder_opcodes_h.py
index f0d8cf1..6edaae3 100644
--- a/src/compiler/nir/nir_builder_opcodes_h.py
+++ b/src/compiler/nir/nir_builder_opcodes_h.py
@@ -101,8 +101,14 @@
 
 #endif /* _NIR_BUILDER_OPCODES_ */"""
 
+import sys
+
 from nir_opcodes import opcodes
 from nir_intrinsics import INTR_OPCODES
 from mako.template import Template
 
-print(Template(template).render(opcodes=opcodes, INTR_OPCODES=INTR_OPCODES))
+def main():
+    print(Template(template).render(opcodes=opcodes, INTR_OPCODES=INTR_OPCODES))
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/compiler/nir/nir_constant_expressions.py b/src/compiler/nir/nir_constant_expressions.py
index 8b8cd5f..898b573 100644
--- a/src/compiler/nir/nir_constant_expressions.py
+++ b/src/compiler/nir/nir_constant_expressions.py
@@ -1,5 +1,6 @@
 from __future__ import print_function
 
+import sys
 import re
 from nir_opcodes import opcodes
 from nir_opcodes import type_has_size, type_size, type_sizes, type_base_type
@@ -504,10 +505,14 @@
 
 from mako.template import Template
 
-print(Template(template).render(opcodes=opcodes, type_sizes=type_sizes,
-                                type_base_type=type_base_type,
-                                type_size=type_size,
-                                type_has_size=type_has_size,
-                                type_add_size=type_add_size,
-                                op_bit_sizes=op_bit_sizes,
-                                get_const_field=get_const_field))
+def main():
+    print(Template(template).render(opcodes=opcodes, type_sizes=type_sizes,
+                                    type_base_type=type_base_type,
+                                    type_size=type_size,
+                                    type_has_size=type_has_size,
+                                    type_add_size=type_add_size,
+                                    op_bit_sizes=op_bit_sizes,
+                                    get_const_field=get_const_field))
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/compiler/nir/nir_opcodes_c.py b/src/compiler/nir/nir_opcodes_c.py
index c6e5bb3..da2f2181 100644
--- a/src/compiler/nir/nir_opcodes_c.py
+++ b/src/compiler/nir/nir_opcodes_c.py
@@ -25,6 +25,8 @@
 
 from __future__ import print_function
 
+import sys
+
 from nir_opcodes import opcodes, type_sizes
 from mako.template import Template
 
@@ -127,4 +129,8 @@
 };
 """)
 
-print(template.render(opcodes=opcodes, type_sizes=type_sizes))
+def main():
+    print(template.render(opcodes=opcodes, type_sizes=type_sizes))
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/compiler/nir/nir_opcodes_h.py b/src/compiler/nir/nir_opcodes_h.py
index 6b4e2fe..4057091 100644
--- a/src/compiler/nir/nir_opcodes_h.py
+++ b/src/compiler/nir/nir_opcodes_h.py
@@ -41,7 +41,13 @@
 
 #endif /* _NIR_OPCODES_ */"""
 
+import sys
+
 from nir_opcodes import opcodes
 from mako.template import Template
 
-print(Template(template).render(opcodes=opcodes))
+def main():
+    print(Template(template).render(opcodes=opcodes))
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index e870bff..bc87b4d 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -30,6 +30,7 @@
 from nir_opcodes import type_sizes
 import itertools
 import struct
+import sys
 from math import pi
 
 # Convenience variables
@@ -1754,8 +1755,12 @@
         (('bcsel', a, (op, b, c, d), (op + '(is_used_once)', b, e, d)), (op, b, ('bcsel', a, c, e), d)),
     ]
 
-print(nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render())
-print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_before_ffma",
-                                  before_ffma_optimizations).render())
-print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_late",
-                                  late_optimizations).render())
+def main():
+    print(nir_algebraic.AlgebraicPass("nir_opt_algebraic", optimizations).render())
+    print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_before_ffma",
+                                      before_ffma_optimizations).render())
+    print(nir_algebraic.AlgebraicPass("nir_opt_algebraic_late",
+                                      late_optimizations).render())
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/src/intel/compiler/BUILD.gn b/src/intel/compiler/BUILD.gn
index d5752a3..cc5c5e5 100644
--- a/src/intel/compiler/BUILD.gn
+++ b/src/intel/compiler/BUILD.gn
@@ -19,7 +19,6 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 # IN THE SOFTWARE.
 
-import("//third_party/mako/py_srcs.gni")
 import("../../../mesa.gni")
 
 config("compiler_config") {
@@ -149,14 +148,13 @@
 mesa_python_stdout_to_file_action("gen_brw_nir_trig_workarounds") {
   output = "brw_nir_trig_workarounds.c"
   script = "brw_nir_trig_workarounds.py"
-
-  inputs = [ "$mesa_build_root/src/compiler/nir/nir_algebraic.py" ] +
-           third_party_mako_srcs
-
-  pythonpath = "$magma_python_path:" + rebase_path("$mesa_build_root") +
-               "/src/compiler/nir"
+  sources = [
+    "$mesa_build_root/src/compiler/nir/nir_algebraic.py",
+    "$mesa_build_root/src/compiler/nir/nir_opcodes.py",
+  ]
+  libraries = [ "//third_party/mako" ]
   args = [
     "--import-path",
-    pythonpath,
+    "$magma_python_path:" + rebase_path("$mesa_build_root/src/compiler/nir"),
   ]
 }
diff --git a/src/intel/genxml/BUILD.gn b/src/intel/genxml/BUILD.gn
index 186ab21..dcc3000 100644
--- a/src/intel/genxml/BUILD.gn
+++ b/src/intel/genxml/BUILD.gn
@@ -25,72 +25,64 @@
 mesa_python_stdout_to_file_action("gen_pack7_header") {
   output = "gen7_pack.h"
   script = "gen_pack_header.py"
-
   inputs = [ "gen7.xml" ]
-  args = [ rebase_path(".") + "/gen7.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_pack75_header") {
   output = "gen75_pack.h"
   script = "gen_pack_header.py"
   inputs = [ "gen75.xml" ]
-  args = [ rebase_path(".") + "/gen75.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_pack8_header") {
   output = "gen8_pack.h"
   script = "gen_pack_header.py"
   inputs = [ "gen8.xml" ]
-  args = [ rebase_path(".") + "/gen8.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_pack9_header") {
   output = "gen9_pack.h"
   script = "gen_pack_header.py"
   inputs = [ "gen9.xml" ]
-  args = [ rebase_path(".") + "/gen9.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_pack10_header") {
   output = "gen10_pack.h"
   script = "gen_pack_header.py"
   inputs = [ "gen10.xml" ]
-  args = [ rebase_path(".") + "/gen10.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_pack11_header") {
   output = "gen11_pack.h"
   script = "gen_pack_header.py"
   inputs = [ "gen11.xml" ]
-  args = [ rebase_path(".") + "/gen11.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_pack12_header") {
   output = "gen12_pack.h"
   script = "gen_pack_header.py"
   inputs = [ "gen12.xml" ]
-  args = [ rebase_path(".") + "/gen12.xml" ]
+  args = rebase_path(inputs, root_build_dir)
 }
 
 mesa_python_stdout_to_file_action("gen_bits_header") {
   output = "genX_bits.h"
   script = "gen_bits_header.py"
   inputs = [
-             "gen10.xml",
-             "gen11.xml",
-             "gen12.xml",
-             "gen7.xml",
-             "gen75.xml",
-             "gen8.xml",
-             "gen9.xml",
-           ] + third_party_mako_srcs
-  args = [
-    rebase_path(".") + "/gen7.xml",
-    rebase_path(".") + "/gen75.xml",
-    rebase_path(".") + "/gen8.xml",
-    rebase_path(".") + "/gen9.xml",
-    rebase_path(".") + "/gen10.xml",
-    rebase_path(".") + "/gen11.xml",
-    rebase_path(".") + "/gen12.xml",
+    "gen10.xml",
+    "gen11.xml",
+    "gen12.xml",
+    "gen7.xml",
+    "gen75.xml",
+    "gen8.xml",
+    "gen9.xml",
   ]
+  libraries = [ "//third_party/mako" ]
+  args = rebase_path(inputs, root_build_dir)
 }