1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Impl of `embedded_exec`.""" 16 17load("@bazel_skylib//lib:shell.bzl", "shell") 18load(":exec_aspect.bzl", "ExecAspectInfo", "exec_aspect") 19 20visibility([ 21 "//build/bazel_common_rules/exec/...", 22 "//build/bazel_common_rules/dist/...", 23]) 24 25def _impl(ctx): 26 # buildifier: disable=print 27 print(("\nWARNING: {}: embedded_exec is deprecated. Consider writing a custom rule with " + 28 "arguments specified at the wrapper rule instead.").format(ctx.label)) 29 30 target = ctx.attr.actual 31 files_to_run = target[DefaultInfo].files_to_run 32 if not files_to_run or not files_to_run.executable: 33 fail("{}: {} is not an executable".format(ctx.label, target)) 34 35 out_file = ctx.actions.declare_file(ctx.label.name) 36 37 content = "#!{}\n".format(ctx.attr.hashbang) 38 39 expand_location_targets = [] 40 for dependant_attr in ("data", "srcs", "deps"): 41 dependants = getattr(target[ExecAspectInfo], dependant_attr) 42 if dependants: 43 expand_location_targets += dependants 44 45 args = target[ExecAspectInfo].args 46 if not args: 47 args = [] 48 quoted_args = " ".join([shell.quote(ctx.expand_location(arg, expand_location_targets)) for arg in args]) 49 50 env = target[ExecAspectInfo].env 51 if not env: 52 env = {} 53 54 quoted_env = " ".join(["{}={}".format(k, shell.quote(ctx.expand_location(v, expand_location_targets))) for k, v in env.items()]) 55 56 content += '{} {} {} "$@"'.format(quoted_env, target[DefaultInfo].files_to_run.executable.short_path, quoted_args) 57 58 ctx.actions.write(out_file, content, is_executable = True) 59 60 runfiles = ctx.runfiles(files = ctx.files.actual) 61 runfiles = runfiles.merge_all([target[DefaultInfo].default_runfiles]) 62 63 return DefaultInfo( 64 files = depset([out_file]), 65 executable = out_file, 66 runfiles = runfiles, 67 ) 68 69embedded_exec = rule( 70 implementation = _impl, 71 attrs = { 72 "actual": attr.label(doc = "The actual executable.", aspects = [exec_aspect]), 73 "hashbang": attr.string(doc = "The hashbang of the script", default = "/bin/bash -e"), 74 }, 75 executable = True, 76) 77