xref: /aosp_15_r20/external/bazelbuild-rules_go/go/private/rules/wrappers.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1*9bb1b549SSpandan Das# Copyright 2014 The Bazel Authors. All rights reserved.
2*9bb1b549SSpandan Das#
3*9bb1b549SSpandan Das# Licensed under the Apache License, Version 2.0 (the "License");
4*9bb1b549SSpandan Das# you may not use this file except in compliance with the License.
5*9bb1b549SSpandan Das# You may obtain a copy of the License at
6*9bb1b549SSpandan Das#
7*9bb1b549SSpandan Das#    http://www.apache.org/licenses/LICENSE-2.0
8*9bb1b549SSpandan Das#
9*9bb1b549SSpandan Das# Unless required by applicable law or agreed to in writing, software
10*9bb1b549SSpandan Das# distributed under the License is distributed on an "AS IS" BASIS,
11*9bb1b549SSpandan Das# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9bb1b549SSpandan Das# See the License for the specific language governing permissions and
13*9bb1b549SSpandan Das# limitations under the License.
14*9bb1b549SSpandan Das
15*9bb1b549SSpandan Dasload(
16*9bb1b549SSpandan Das    "//go/private:mode.bzl",
17*9bb1b549SSpandan Das    "LINKMODES_EXECUTABLE",
18*9bb1b549SSpandan Das    "LINKMODE_C_ARCHIVE",
19*9bb1b549SSpandan Das    "LINKMODE_C_SHARED",
20*9bb1b549SSpandan Das    "LINKMODE_NORMAL",
21*9bb1b549SSpandan Das)
22*9bb1b549SSpandan Dasload(
23*9bb1b549SSpandan Das    "//go/private/rules:library.bzl",
24*9bb1b549SSpandan Das    "go_library",
25*9bb1b549SSpandan Das)
26*9bb1b549SSpandan Dasload(
27*9bb1b549SSpandan Das    "//go/private/rules:binary.bzl",
28*9bb1b549SSpandan Das    "go_binary",
29*9bb1b549SSpandan Das    "go_non_executable_binary",
30*9bb1b549SSpandan Das)
31*9bb1b549SSpandan Dasload(
32*9bb1b549SSpandan Das    "//go/private/rules:test.bzl",
33*9bb1b549SSpandan Das    "go_test",
34*9bb1b549SSpandan Das)
35*9bb1b549SSpandan Das
36*9bb1b549SSpandan Das_SELECT_TYPE = type(select({"//conditions:default": ""}))
37*9bb1b549SSpandan Das
38*9bb1b549SSpandan Dasdef _cgo(name, kwargs):
39*9bb1b549SSpandan Das    if "objc" in kwargs:
40*9bb1b549SSpandan Das        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*9bb1b549SSpandan Das
42*9bb1b549SSpandan Dasdef go_library_macro(name, **kwargs):
43*9bb1b549SSpandan Das    """See docs/go/core/rules.md#go_library for full documentation."""
44*9bb1b549SSpandan Das    _cgo(name, kwargs)
45*9bb1b549SSpandan Das    go_library(name = name, **kwargs)
46*9bb1b549SSpandan Das
47*9bb1b549SSpandan Dasdef go_binary_macro(name, **kwargs):
48*9bb1b549SSpandan Das    """See docs/go/core/rules.md#go_binary for full documentation."""
49*9bb1b549SSpandan Das    _cgo(name, kwargs)
50*9bb1b549SSpandan Das
51*9bb1b549SSpandan Das    if kwargs.get("goos") != None or kwargs.get("goarch") != None:
52*9bb1b549SSpandan Das        for key, value in kwargs.items():
53*9bb1b549SSpandan Das            if type(value) == _SELECT_TYPE:
54*9bb1b549SSpandan Das                # In the long term, we should replace goos/goarch with Bazel-native platform
55*9bb1b549SSpandan Das                # support, but while we have the mechanisms, we try to avoid people trying to use
56*9bb1b549SSpandan Das                # _both_ goos/goarch _and_ native platform support.
57*9bb1b549SSpandan Das                #
58*9bb1b549SSpandan Das                # It's unclear to users whether the select should happen before or after the
59*9bb1b549SSpandan Das                # goos/goarch is reconfigured, and we can't interpose code to force either
60*9bb1b549SSpandan Das                # behaviour, so we forbid this.
61*9bb1b549SSpandan Das                fail("Cannot use select for go_binary with goos/goarch set, but {} was a select".format(key))
62*9bb1b549SSpandan Das
63*9bb1b549SSpandan Das    if kwargs.get("linkmode", default = LINKMODE_NORMAL) in LINKMODES_EXECUTABLE:
64*9bb1b549SSpandan Das        go_binary(name = name, **kwargs)
65*9bb1b549SSpandan Das    else:
66*9bb1b549SSpandan Das        go_non_executable_binary(name = name, **kwargs)
67*9bb1b549SSpandan Das
68*9bb1b549SSpandan Das    if kwargs.get("linkmode") in (LINKMODE_C_ARCHIVE, LINKMODE_C_SHARED):
69*9bb1b549SSpandan Das        # Create an alias to tell users of the `.cc` rule that it is deprecated.
70*9bb1b549SSpandan Das        native.alias(
71*9bb1b549SSpandan Das            name = "{}.cc".format(name),
72*9bb1b549SSpandan Das            actual = name,
73*9bb1b549SSpandan Das            visibility = ["//visibility:public"],
74*9bb1b549SSpandan Das            tags = ["manual"],
75*9bb1b549SSpandan Das            deprecation = "This target is deprecated and will be removed in the near future. Please depend on ':{}' directly.".format(name),
76*9bb1b549SSpandan Das        )
77*9bb1b549SSpandan Das
78*9bb1b549SSpandan Dasdef go_test_macro(name, **kwargs):
79*9bb1b549SSpandan Das    """See docs/go/core/rules.md#go_test for full documentation."""
80*9bb1b549SSpandan Das    _cgo(name, kwargs)
81*9bb1b549SSpandan Das    go_test(name = name, **kwargs)
82