| # Copyright 2021 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| # Defines a Python binary. |
| # |
| # Example |
| # |
| # ``` |
| # python_binary("main") { |
| # main_source = "main.py" |
| # main_callable = "main" |
| # sources = [ |
| # "foo.py", |
| # "bar.py", |
| # ] |
| # output_name = "main.pyz" |
| # deps = [ "//path/to/lib" ] |
| # } |
| # ``` |
| # |
| # Parameters |
| # |
| # main_source (required) |
| # Source file including the entry callable for this binary. |
| # This file will typically contain |
| # ``` |
| # if __name__ == "__main__": |
| # main() |
| # ``` |
| # Type: path |
| # |
| # main_callable (optional) |
| # Main callable, which serves as the entry point of the output zip archive. |
| # In the example above, this is "main". |
| # Type: string |
| # Default: main |
| # |
| # output_name (optional) |
| # Name of the output Python zip archive, must have .pyz as extension. |
| # Type: string |
| # Default: ${target_name}.pyz |
| # |
| # sources |
| # deps |
| # visibility |
| # testonly |
| template("python_binary") { |
| assert(defined(invoker.main_source), "main_source is required") |
| |
| _library_infos_target = "${target_name}_library_infos" |
| _library_infos_json = "${target_gen_dir}/${target_name}_library_infos.json" |
| generated_file(_library_infos_target) { |
| forward_variables_from(invoker, |
| [ |
| "testonly", |
| "deps", |
| ]) |
| visibility = [ ":*" ] |
| |
| outputs = [ _library_infos_json ] |
| output_conversion = "json" |
| data_keys = [ "library_info" ] |
| walk_keys = [ "library_info_barrier" ] |
| } |
| |
| action(target_name) { |
| forward_variables_from(invoker, |
| [ |
| "testonly", |
| "visibility", |
| ]) |
| |
| sources = [ invoker.main_source ] |
| if (defined(invoker.sources)) { |
| sources += invoker.sources |
| } |
| inputs = [ _library_infos_json ] |
| deps = [ ":${_library_infos_target}" ] |
| |
| # Output must be a .pyz, so our build knows to use a vendored Python |
| # interpreter to run them. |
| # |
| # Output a single .pyz file makes the output deterministic, otherwise we'd |
| # have to list out all the library sources that will be copied to output |
| # directory, which is not possible because they are not known until the |
| # generated JSON file is parsed at build time. |
| _output = "${target_out_dir}/${target_name}.pyz" |
| if (defined(invoker.output_name)) { |
| assert(get_path_info(invoker.output_name, "extension") == "pyz", |
| "output_name must have .pyz as extension") |
| _output = "${target_out_dir}/${invoker.output_name}" |
| } |
| outputs = [ _output ] |
| |
| _main_callable = "main" |
| if (defined(invoker.main_callable)) { |
| _main_callable = invoker.main_callable |
| } |
| |
| script = "//build/python/package_python_binary.py" |
| depfile = "${target_out_dir}/${target_name}.d" |
| args = [ "--sources" ] + rebase_path(sources, root_build_dir) + [ |
| "--target_name", |
| target_name, |
| "--main_source", |
| rebase_path(invoker.main_source, root_build_dir), |
| "--main_callable", |
| _main_callable, |
| "--library_infos", |
| rebase_path(_library_infos_json, root_build_dir), |
| "--depfile", |
| rebase_path(depfile, root_build_dir), |
| "--gen_dir", |
| rebase_path(target_gen_dir, root_build_dir), |
| "--output", |
| rebase_path(_output, root_build_dir), |
| ] |
| } |
| } |