|  | # Copyright 2024 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. | 
|  |  | 
|  | import("//build/host.gni") | 
|  |  | 
|  | # Publishes a Python binary as a host tool for use with `fx`. | 
|  | # | 
|  | # Example: | 
|  | # | 
|  | # This installs `fx my-tool`: | 
|  | # ``` | 
|  | # python_binary("main") { | 
|  | #   ... | 
|  | # } | 
|  | # | 
|  | # install_python_tool("install") { | 
|  | #   name = "my-tool" | 
|  | #   binary = ":main" | 
|  | # } | 
|  | # ``` | 
|  | # | 
|  | # Parameters | 
|  | # | 
|  | #   name (required) | 
|  | #     Name of the resulting host tool. This is the name used to invoke the tool | 
|  | #     using `fx`. | 
|  | #     Type: string | 
|  | # | 
|  | #   binary (required) | 
|  | #     The python_binary target to install. | 
|  | #     Type: target_name | 
|  | # | 
|  | #   emit_tool_path (optional) | 
|  | #     If false, disable emitting a tool path for this binary. | 
|  | #     Type: boolean | 
|  | #     Default: true | 
|  | template("install_python_tool") { | 
|  | assert(defined(invoker.name), "`name` must be set to the desired output name") | 
|  | assert(defined(invoker.binary), | 
|  | "`binary` must be set to a python_binary to install") | 
|  | outs = get_target_outputs(invoker.binary) | 
|  |  | 
|  | name = invoker.name | 
|  | binary = invoker.binary | 
|  |  | 
|  | # Rename the resulting .pyz for the host tool | 
|  | copy("${target_name}-copy") { | 
|  | deps = [ binary ] | 
|  | sources = [ outs[0] ] | 
|  | outputs = [ "${root_out_dir}/${name}" ] | 
|  | } | 
|  |  | 
|  | install_host_tools(target_name) { | 
|  | deps = [ ":${target_name}-copy" ] | 
|  | outputs = [ name ] | 
|  |  | 
|  | if (!defined(invoker.emit_tool_path)) { | 
|  | emit_tool_path = true | 
|  | } else { | 
|  | emit_tool_path = invoker.emit_tool_path | 
|  | } | 
|  |  | 
|  | _dep_outputs = get_target_outputs(deps[0]) | 
|  | rebased_output_file = rebase_path(_dep_outputs[0], root_build_dir) | 
|  |  | 
|  | metadata = { | 
|  | if (emit_tool_path) { | 
|  | tool_paths = [ | 
|  | { | 
|  | cpu = current_cpu | 
|  | label = get_label_info(":$target_name", "label_with_toolchain") | 
|  | name = outputs[0] | 
|  | os = current_os | 
|  | path = rebased_output_file | 
|  | }, | 
|  | ] | 
|  | } | 
|  | } | 
|  | } | 
|  | } |