1# Copyright 2022 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"""Rule implementation of py_binary for Bazel.""" 15 16load("@bazel_skylib//lib:dicts.bzl", "dicts") 17load(":attributes.bzl", "AGNOSTIC_BINARY_ATTRS") 18load( 19 ":py_executable_bazel.bzl", 20 "create_executable_rule", 21 "py_executable_bazel_impl", 22) 23 24_PY_TEST_ATTRS = { 25 # Magic attribute to help C++ coverage work. There's no 26 # docs about this; see TestActionBuilder.java 27 "_collect_cc_coverage": attr.label( 28 default = "@bazel_tools//tools/test:collect_cc_coverage", 29 executable = True, 30 cfg = "exec", 31 ), 32 # Magic attribute to make coverage work. There's no 33 # docs about this; see TestActionBuilder.java 34 "_lcov_merger": attr.label( 35 default = configuration_field(fragment = "coverage", name = "output_generator"), 36 executable = True, 37 cfg = "exec", 38 ), 39} 40 41def _py_binary_impl(ctx): 42 return py_executable_bazel_impl( 43 ctx = ctx, 44 is_test = False, 45 inherited_environment = [], 46 ) 47 48py_binary = create_executable_rule( 49 implementation = _py_binary_impl, 50 attrs = dicts.add(AGNOSTIC_BINARY_ATTRS, _PY_TEST_ATTRS), 51 executable = True, 52) 53