aboutsummaryrefslogtreecommitdiff
path: root/product_config/utils.bzl
blob: 3a6c92493492485cacb1d34072611ec91bef07f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
load("@//build/bazel/platforms:product_variables/product_platform.bzl", "determine_target_arches_from_config", "product_variables_providing_rule")
load("@//build/bazel/platforms/arch/variants:constants.bzl", _variant_constants = "constants")
load("//build/bazel/product_variables:constants.bzl", "constants")
load(
    "//prebuilts/clang/host/linux-x86:cc_toolchain_constants.bzl",
    "arch_to_variants",
    "variant_constraints",
    "variant_name",
)

def product_variable_constraint_settings(variables):
    constraints = []

    local_vars = dict(variables)

    # Native_coverage is not set within soong.variables, but is hardcoded
    # within config.go NewConfig
    local_vars["Native_coverage"] = (
        local_vars.get("ClangCoverage", False) or
        local_vars.get("GcovCoverage", False)
    )

    # Some attributes on rules are able to access the values of product
    # variables via make-style expansion (like $(foo)). We collect the values
    # of the relevant product variables here so that it can be passed to
    # product_variables_providing_rule, which exports a
    # platform_common.TemplateVariableInfo provider to allow the substitution.
    attribute_vars = {}

    def add_attribute_var(typ, var, value):
        if typ == "bool":
            attribute_vars[var] = "1" if value else "0"
        elif typ == "list":
            attribute_vars[var] = ",".join(value)
        elif typ == "int":
            attribute_vars[var] = str(value)
        elif typ == "string":
            attribute_vars[var] = value

    # Generate constraints for Soong config variables (bool, value, string typed).
    vendor_vars = local_vars.pop("VendorVars", default = {})
    for (namespace, variables) in vendor_vars.items():
        for (var, value) in variables.items():
            # All vendor vars are Starlark string-typed, even though they may be
            # boxed bools/strings/arbitrary printf'd values, like numbers, so
            # we'll need to do some translation work here by referring to
            # soong_injection's generated data.

            if value == "":
                # Variable is not set so skip adding this as a constraint.
                continue

            # Create the identifier for the constraint var (or select key)
            config_var = namespace + "__" + var

            # List of all soong_config_module_type variables.
            if not config_var in constants.SoongConfigVariables:
                continue

            # Normalize all constraint vars (i.e. select keys) to be lowercased.
            constraint_var = config_var.lower()

            if config_var in constants.SoongConfigBoolVariables:
                constraints.append("@//build/bazel/product_variables:" + constraint_var)
            elif config_var in constants.SoongConfigStringVariables:
                # The string value is part of the the select key.
                constraints.append("@//build/bazel/product_variables:" + constraint_var + "__" + value.lower())
            elif config_var in constants.SoongConfigValueVariables:
                # For value variables, providing_vars add support for substituting
                # the value using TemplateVariableInfo.
                constraints.append("@//build/bazel/product_variables:" + constraint_var)
                add_attribute_var("string", constraint_var, value)

    for (var, value) in local_vars.items():
        # TODO(b/187323817): determine how to handle remaining product
        # variables not used in product_variables
        constraint_var = var.lower()
        if not constants.ProductVariables.get(constraint_var):
            continue

        # variable.go excludes nil values
        add_constraint = (value != None)
        add_attribute_var(type(value), var, value)
        if type(value) == "bool":
            # variable.go special cases bools
            add_constraint = value

        if add_constraint:
            constraints.append("@//build/bazel/product_variables:" + constraint_var)

    return constraints, attribute_vars

# android_product integrates product variables into Bazel platforms.
#
# This uses soong.variables to create constraints, and platforms used by the
# bazel build. The soong.variables file itself contains a post-processed list of
# variables derived from Make variables, through soong_config.mk, generated
# during the product config step.
#
# Some constraints used here are handcrafted in
# //build/bazel/platforms/{arch,os}. The rest are dynamically generated.
#
# If you're looking for what --config=android, --config=linux_x86_64 or most
# select statements in the BUILD files (ultimately) refer to, they're all
# created here.
def android_product(name, soong_variables):
    product_var_constraints, attribute_vars = product_variable_constraint_settings(soong_variables)
    arch_configs = determine_target_arches_from_config(soong_variables)

    product_variables_providing_rule(
        name = name + "_product_vars",
        product_vars = attribute_vars,
    )

    native.constraint_value(
        name = name + "_constraint_value",
        constraint_setting = "@//build/bazel/product_config:current_product",
    )

    common_constraints = product_var_constraints + [name + "_constraint_value"]

    # TODO(b/258802089): figure out how to deal with multiple arches for target
    if len(arch_configs) > 0:
        arch = arch_configs[0]
        native.platform(
            name = name,
            constraint_values = common_constraints + [
                "@//build/bazel/platforms/arch:" + arch.arch,
                "@//build/bazel/platforms/os:android",
            ] + ["@" + v for v in variant_constraints(
                arch,
                _variant_constants.AndroidArchToVariantToFeatures[arch.arch],
            )],
        )
        if len(arch_configs) > 1:
            secondary_arch = arch_configs[1]
            native.platform(
                name = name + "_secondary",
                constraint_values = common_constraints + [
                    "@//build/bazel/platforms/arch:" + secondary_arch.arch,
                    "@//build/bazel/platforms/os:android",
                ] + ["@" + v for v in variant_constraints(
                    secondary_arch,
                    _variant_constants.AndroidArchToVariantToFeatures[secondary_arch.arch],
                )],
            )
        else:
            native.alias(
                name = name + "_secondary",
                actual = ":" + name,
            )

        if arch.arch == "arm64" or arch.arch == "x86_64":
            # Apexes need to transition their native_shared_libs to 32 bit.
            # Bazel currently cannot transition on arch directly, and instead
            # requires transitioning on a command line option like --platforms instead.
            # Create a 32 bit variant of the product so that apexes can transition on it.
            if arch == "arm64":
                newarch = struct(
                    arch = "arm",
                    arch_variant = "armv7-a-neon",
                    cpu_variant = "",
                )
            else:
                newarch = struct(
                    arch = "x86",
                    arch_variant = "",
                    cpu_variant = "",
                )
            native.platform(
                name = name + "__internal_32_bit",
                constraint_values = common_constraints + [
                    "@//build/bazel/platforms/arch:" + newarch.arch,
                    "@//build/bazel/platforms/os:android",
                ] + ["@" + v for v in variant_constraints(
                    newarch,
                    _variant_constants.AndroidArchToVariantToFeatures[newarch.arch],
                )],
            )
        else:
            native.alias(
                name = name + "__internal_32_bit",
                actual = ":" + name,
            )

        # These variants are mostly for mixed builds, which may request a
        # module with a certain arch
        for arch, variants in arch_to_variants.items():
            for variant in variants:
                native.platform(
                    name = name + "_android_" + arch + variant_name(variant),
                    constraint_values = common_constraints + [
                        "@//build/bazel/platforms/arch:" + arch,
                        "@//build/bazel/platforms/os:android",
                    ] + ["@" + v for v in variant_constraints(
                        variant,
                        _variant_constants.AndroidArchToVariantToFeatures[arch],
                    )],
                )

        arch_transitions = [
            struct(
                arch = "x86",
                arch_variant = "",
                cpu_variant = "",
            ),
            struct(
                arch = "x86_64",
                arch_variant = "",
                cpu_variant = "",
            ),
            struct(
                arch = "arm",
                arch_variant = "armv7-a-neon",
                cpu_variant = "",
            ),
            struct(
                arch = "arm64",
                arch_variant = "armv8-a",
                cpu_variant = "",
            ),
        ]

        # TODO(b/249685973): Remove this, this is currently just for aabs
        # to build each architecture
        for arch in arch_transitions:
            native.platform(
                name = name + "__internal_" + arch.arch,
                constraint_values = common_constraints + [
                    "@//build/bazel/platforms/arch:" + arch.arch,
                    "@//build/bazel/platforms/os:android",
                ] + ["@" + v for v in variant_constraints(
                    arch,
                    _variant_constants.AndroidArchToVariantToFeatures[arch.arch],
                )],
            )

    # Now define the host platforms. We need a host platform per product because
    # the host platforms still use the product variables.
    # TODO(b/262753134): Investigate making the host platforms product-independant
    native.platform(
        name = name + "_linux_x86",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86",
            "@//build/bazel/platforms/os:linux",
        ],
    )

    native.platform(
        name = name + "_linux_x86_64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86_64",
            "@//build/bazel/platforms/os:linux",
        ],
    )

    native.platform(
        name = name + "_linux_musl_x86",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86",
            "@//build/bazel/platforms/os:linux_musl",
        ],
    )

    native.platform(
        name = name + "_linux_musl_x86_64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86_64",
            "@//build/bazel/platforms/os:linux_musl",
        ],
    )

    # linux_bionic is the OS for the Linux kernel plus the Bionic libc runtime, but
    # without the rest of Android.
    native.platform(
        name = name + "_linux_bionic_arm64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:arm64",
            "@//build/bazel/platforms/os:linux_bionic",
        ],
    )

    native.platform(
        name = name + "_linux_bionic_x86_64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86_64",
            "@//build/bazel/platforms/os:linux_bionic",
        ],
    )

    native.platform(
        name = name + "_darwin_arm64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:arm64",
            "@//build/bazel/platforms/os:darwin",
        ],
    )

    native.platform(
        name = name + "_darwin_x86_64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86_64",
            "@//build/bazel/platforms/os:darwin",
        ],
    )

    native.platform(
        name = name + "_windows_x86",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86",
            "@//build/bazel/platforms/os:windows",
        ],
    )

    native.platform(
        name = name + "_windows_x86_64",
        constraint_values = common_constraints + [
            "@//build/bazel/platforms/arch:x86_64",
            "@//build/bazel/platforms/os:windows",
        ],
    )