github.com/bazelbuild/rules_go@v0.47.2-0.20240515105122-e7ddb9ea474e/go/private/rules/go_bin_for_host.bzl (about) 1 # Copyright 2023 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 load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") 16 load("//go/private:common.bzl", "GO_TOOLCHAIN") 17 18 def _ensure_target_cfg(ctx): 19 # A target is assumed to be built in the target configuration if it is neither in the exec nor 20 # the host configuration (the latter has been removed in Bazel 6). Since there is no API for 21 # this, use the output directory to determine the configuration, which is a common pattern. 22 if "-exec" in ctx.bin_dir.path or "/host/" in ctx.bin_dir.path: 23 fail("//go is only meant to be used with 'bazel run', not as a tool. " + 24 "If you need to use it as a tool (e.g. in a genrule), please " + 25 "open an issue at " + 26 "https://github.com/bazelbuild/rules_go/issues/new explaining " + 27 "your use case.") 28 29 def _go_bin_for_host_impl(ctx): 30 """Exposes the go binary of the current Go toolchain for the host.""" 31 _ensure_target_cfg(ctx) 32 33 sdk = ctx.toolchains[GO_TOOLCHAIN].sdk 34 sdk_files = ctx.runfiles([sdk.go] + sdk.headers + sdk.libs + sdk.srcs + sdk.tools) 35 36 return [ 37 DefaultInfo( 38 files = depset([sdk.go]), 39 runfiles = sdk_files, 40 ), 41 ] 42 43 go_bin_for_host = rule( 44 implementation = _go_bin_for_host_impl, 45 toolchains = [GO_TOOLCHAIN], 46 # Resolve a toolchain that runs on the host platform. 47 exec_compatible_with = HOST_CONSTRAINTS, 48 )