From 9efab86a54a253addde7dc2c4f3b0eafe6a4841a Mon Sep 17 00:00:00 2001 From: jonmeow Date: Wed, 30 Oct 2024 15:23:48 -0700 Subject: [PATCH] Refactor run_tool --- toolchain/install/run_tool.bzl | 40 ++++++++++++++++++++++++---------- toolchain/install/run_tool.py | 22 ------------------- 2 files changed, 29 insertions(+), 33 deletions(-) delete mode 100644 toolchain/install/run_tool.py diff --git a/toolchain/install/run_tool.bzl b/toolchain/install/run_tool.bzl index b95f570dbc27a..1f74f60ebd9e5 100644 --- a/toolchain/install/run_tool.bzl +++ b/toolchain/install/run_tool.bzl @@ -4,15 +4,33 @@ """Supports running a tool from the install filegroup.""" -load("@rules_python//python:defs.bzl", "py_binary") - -def run_tool(name, tool, data, env): - # TODO: Fix the driver file discovery in order to allow symlinks. - py_binary( - name = name, - main = "run_tool.py", - srcs = ["run_tool.py"], - args = ["$(location {})".format(tool)], - data = [tool] + data, - env = env, +def _run_tool_impl(ctx): + tool_files = ctx.attr.tool.files.to_list() + if len(tool_files) != 1: + fail("Expected 1 tool file, found {0}".format(len(tool_files))) + ctx.actions.symlink( + output = ctx.outputs.executable, + target_file = tool_files[0], + is_executable = True, ) + return [ + DefaultInfo( + runfiles = ctx.runfiles(files = ctx.files.data), + ), + RunEnvironmentInfo(environment = ctx.attr.env), + ] + +run_tool = rule( + doc = "Helper for running a tool in a filegroup.", + implementation = _run_tool_impl, + attrs = { + "data": attr.label_list(allow_files = True), + "env": attr.string_dict(), + "tool": attr.label( + allow_single_file = True, + executable = True, + cfg = "target", + ), + }, + executable = True, +) diff --git a/toolchain/install/run_tool.py b/toolchain/install/run_tool.py deleted file mode 100644 index f99b0d0d6977f..0000000000000 --- a/toolchain/install/run_tool.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python3 - -"""Runs the tool specified in argv. - -This script is essentially just a bounce-through to get an appropriate arg0. -See the TODO in run_tool.bzl. -""" - -__copyright__ = """ -Part of the Carbon Language project, under the Apache License v2.0 with LLVM -Exceptions. See /LICENSE for license information. -SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -""" - -import os -import sys -from pathlib import Path - -if __name__ == "__main__": - symbolizer = str(Path(os.environ["LLVM_SYMBOLIZER_PATH"]).absolute()) - os.environ["LLVM_SYMBOLIZER_PATH"] = symbolizer - os.execv(sys.argv[1], sys.argv[1:])