github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/rules/private/execution.bzl (about) 1 # Copyright 2021 The Bazel Authors. All rights reserved. 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 16 def env_execute(ctx, arguments, environment = {}, **kwargs): 17 """Executes a command in for a repository rule. 18 19 Args: 20 ctx: the context object 21 arguments: list of arguments to execute 22 environment: environment variables for the process, defaults to empty 23 **kwargs: rest 24 25 Returns: 26 the return value of ctx.execute 27 28 It prepends "env -i" to "arguments" before calling "ctx.execute". 29 Variables that aren't explicitly mentioned in "environment" 30 are removed from the environment. This should be preferred to "ctx.execute" 31 in most situations. 32 """ 33 if ctx.os.name.startswith("windows"): 34 return ctx.execute(arguments, environment = environment, **kwargs) 35 env_args = ["env", "-i"] 36 environment = dict(environment) 37 for var in ["TMP", "TMPDIR"]: 38 if var in ctx.os.environ and not var in environment: 39 environment[var] = ctx.os.environ[var] 40 for k, v in environment.items(): 41 env_args.append("%s=%s" % (k, v)) 42 arguments = env_args + arguments 43 return ctx.execute(arguments, **kwargs) 44 45 def executable_extension(ctx): 46 extension = "" 47 if ctx.os.name.startswith("windows"): 48 extension = ".exe" 49 return extension