v8-target-cpu.bzl 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright 2021 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Build rules to choose the v8 target architecture."""

load("@bazel_skylib//lib:selects.bzl", "selects")

V8CpuTypeInfo = provider(
    doc = "A singleton provider that specifies the V8 target CPU type",
    fields = {
        "value": "The V8 Target CPU selected.",
    },
)

def _host_target_cpu_impl(ctx):
17
    allowed_values = ["arm", "arm64", "ia32", "ppc64le", "riscv64", "s390x", "x64", "none"]
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
    cpu_type = ctx.build_setting_value
    if cpu_type in allowed_values:
        return V8CpuTypeInfo(value = cpu_type)
    else:
        fail("Error setting " + str(ctx.label) + ": invalid v8 target cpu '" +
             cpu_type + "'. Allowed values are " + str(allowed_values))

v8_target_cpu = rule(
    implementation = _host_target_cpu_impl,
    build_setting = config.string(flag = True),
    doc = "CPU that V8 will generate code for.",
)

def v8_configure_target_cpu(name, matching_configs):
    selects.config_setting_group(
        name = "is_" + name,
        match_any = matching_configs,
    )

    # If v8_target_cpu flag is set to 'name'
    native.config_setting(
        name = "v8_host_target_is_" + name,
        flag_values = {
            ":v8_target_cpu": name,
        },
    )

    # Default target if no v8_host_target flag is set.
    selects.config_setting_group(
        name = "v8_target_is_" + name,
        match_all = [
            ":v8_host_target_is_none",
            ":is_" + name,
        ],
    )

    # Select either the default target or the flag.
    selects.config_setting_group(
        name = "v8_target_" + name,
        match_any = [
            ":v8_host_target_is_" + name,
            ":v8_target_is_" + name,
        ],
    )