• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 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"""Helper macros for declaring library link arguments."""
15
16load("//cc/toolchains:nested_args.bzl", "cc_nested_args")
17
18def macos_force_load_library_args(name, variable):
19    """A helper for declaring -force_load argument expansion for a library.
20
21    This creates an argument expansion that will expand to -Wl,-force_load,<library>
22    if the library should be linked as a whole archive.
23
24    Args:
25      name: The name of the rule.
26      variable: The variable to expand.
27    """
28    cc_nested_args(
29        name = name,
30        nested = [
31            ":{}_force_load_library".format(name),
32            ":{}_no_force_load_library".format(name),
33        ],
34    )
35    cc_nested_args(
36        name = name + "_no_force_load_library",
37        requires_false = "//cc/toolchains/variables:libraries_to_link.is_whole_archive",
38        args = ["{library}"],
39        format = {
40            "library": variable,
41        },
42    )
43    cc_nested_args(
44        name = name + "_force_load_library",
45        requires_true = "//cc/toolchains/variables:libraries_to_link.is_whole_archive",
46        args = ["-Wl,-force_load,{library}"],
47        format = {
48            "library": variable,
49        },
50    )
51
52def library_link_args(name, library_type, from_variable, iterate_over_variable = False):
53    """A helper for declaring a library to link.
54
55    For most platforms, this expands something akin to the following:
56
57        cc_nested_args(
58            name = "foo",
59            requires_equal = "//cc/toolchains/variables:libraries_to_link.type",
60            requires_equal_value = "interface_library",
61            iterate_over = None,
62            args = ["{library}"],
63            format = {
64                "library": //cc/toolchains/variables:libraries_to_link.name,
65            },
66        )
67
68    For macos, this expands to a more complex cc_nested_args structure that
69    handles the -force_load flag.
70
71    Args:
72      name: The name of the rule.
73      library_type: The type of the library.
74      from_variable: The variable to expand.
75      iterate_over_variable: Whether to iterate over the variable.
76    """
77    native.alias(
78        name = name,
79        actual = select({
80            "@platforms//os:macos": ":macos_{}".format(name),
81            "//conditions:default": ":generic_{}".format(name),
82        }),
83    )
84    cc_nested_args(
85        name = "generic_{}".format(name),
86        requires_equal = "//cc/toolchains/variables:libraries_to_link.type",
87        requires_equal_value = library_type,
88        iterate_over = from_variable if iterate_over_variable else None,
89        args = ["{library}"],
90        format = {
91            "library": from_variable,
92        },
93    )
94    cc_nested_args(
95        name = "macos_{}".format(name),
96        requires_equal = "//cc/toolchains/variables:libraries_to_link.type",
97        requires_equal_value = library_type,
98        iterate_over = from_variable if iterate_over_variable else None,
99        nested = [":{}_maybe_force_load".format(name)],
100    )
101    macos_force_load_library_args(
102        name = "{}_maybe_force_load".format(name),
103        variable = from_variable,
104    )
105