xref: /aosp_15_r20/external/bazelbuild-rules_go/go/private/rules/wrappers.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2014 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
15load(
16    "//go/private:mode.bzl",
17    "LINKMODES_EXECUTABLE",
18    "LINKMODE_C_ARCHIVE",
19    "LINKMODE_C_SHARED",
20    "LINKMODE_NORMAL",
21)
22load(
23    "//go/private/rules:library.bzl",
24    "go_library",
25)
26load(
27    "//go/private/rules:binary.bzl",
28    "go_binary",
29    "go_non_executable_binary",
30)
31load(
32    "//go/private/rules:test.bzl",
33    "go_test",
34)
35
36_SELECT_TYPE = type(select({"//conditions:default": ""}))
37
38def _cgo(name, kwargs):
39    if "objc" in kwargs:
40        fail("//{}:{}: the objc attribute has been removed. .m sources may be included in srcs or may be extracted into a separated objc_library listed in cdeps.".format(native.package_name(), name))
41
42def go_library_macro(name, **kwargs):
43    """See docs/go/core/rules.md#go_library for full documentation."""
44    _cgo(name, kwargs)
45    go_library(name = name, **kwargs)
46
47def go_binary_macro(name, **kwargs):
48    """See docs/go/core/rules.md#go_binary for full documentation."""
49    _cgo(name, kwargs)
50
51    if kwargs.get("goos") != None or kwargs.get("goarch") != None:
52        for key, value in kwargs.items():
53            if type(value) == _SELECT_TYPE:
54                # In the long term, we should replace goos/goarch with Bazel-native platform
55                # support, but while we have the mechanisms, we try to avoid people trying to use
56                # _both_ goos/goarch _and_ native platform support.
57                #
58                # It's unclear to users whether the select should happen before or after the
59                # goos/goarch is reconfigured, and we can't interpose code to force either
60                # behaviour, so we forbid this.
61                fail("Cannot use select for go_binary with goos/goarch set, but {} was a select".format(key))
62
63    if kwargs.get("linkmode", default = LINKMODE_NORMAL) in LINKMODES_EXECUTABLE:
64        go_binary(name = name, **kwargs)
65    else:
66        go_non_executable_binary(name = name, **kwargs)
67
68    if kwargs.get("linkmode") in (LINKMODE_C_ARCHIVE, LINKMODE_C_SHARED):
69        # Create an alias to tell users of the `.cc` rule that it is deprecated.
70        native.alias(
71            name = "{}.cc".format(name),
72            actual = name,
73            visibility = ["//visibility:public"],
74            tags = ["manual"],
75            deprecation = "This target is deprecated and will be removed in the near future. Please depend on ':{}' directly.".format(name),
76        )
77
78def go_test_macro(name, **kwargs):
79    """See docs/go/core/rules.md#go_test for full documentation."""
80    _cgo(name, kwargs)
81    go_test(name = name, **kwargs)
82