11cb0ef41Sopenharmony_ci# Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci# found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci"""Build rules to choose the v8 target architecture."""
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciload("@bazel_skylib//lib:selects.bzl", "selects")
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciV8CpuTypeInfo = provider(
101cb0ef41Sopenharmony_ci    doc = "A singleton provider that specifies the V8 target CPU type",
111cb0ef41Sopenharmony_ci    fields = {
121cb0ef41Sopenharmony_ci        "value": "The V8 Target CPU selected.",
131cb0ef41Sopenharmony_ci    },
141cb0ef41Sopenharmony_ci)
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cidef _host_target_cpu_impl(ctx):
171cb0ef41Sopenharmony_ci    allowed_values = ["arm", "arm64", "ia32", "ppc64le", "riscv64", "s390x", "x64", "none"]
181cb0ef41Sopenharmony_ci    cpu_type = ctx.build_setting_value
191cb0ef41Sopenharmony_ci    if cpu_type in allowed_values:
201cb0ef41Sopenharmony_ci        return V8CpuTypeInfo(value = cpu_type)
211cb0ef41Sopenharmony_ci    else:
221cb0ef41Sopenharmony_ci        fail("Error setting " + str(ctx.label) + ": invalid v8 target cpu '" +
231cb0ef41Sopenharmony_ci             cpu_type + "'. Allowed values are " + str(allowed_values))
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_civ8_target_cpu = rule(
261cb0ef41Sopenharmony_ci    implementation = _host_target_cpu_impl,
271cb0ef41Sopenharmony_ci    build_setting = config.string(flag = True),
281cb0ef41Sopenharmony_ci    doc = "CPU that V8 will generate code for.",
291cb0ef41Sopenharmony_ci)
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cidef v8_configure_target_cpu(name, matching_configs):
321cb0ef41Sopenharmony_ci    selects.config_setting_group(
331cb0ef41Sopenharmony_ci        name = "is_" + name,
341cb0ef41Sopenharmony_ci        match_any = matching_configs,
351cb0ef41Sopenharmony_ci    )
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    # If v8_target_cpu flag is set to 'name'
381cb0ef41Sopenharmony_ci    native.config_setting(
391cb0ef41Sopenharmony_ci        name = "v8_host_target_is_" + name,
401cb0ef41Sopenharmony_ci        flag_values = {
411cb0ef41Sopenharmony_ci            ":v8_target_cpu": name,
421cb0ef41Sopenharmony_ci        },
431cb0ef41Sopenharmony_ci    )
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    # Default target if no v8_host_target flag is set.
461cb0ef41Sopenharmony_ci    selects.config_setting_group(
471cb0ef41Sopenharmony_ci        name = "v8_target_is_" + name,
481cb0ef41Sopenharmony_ci        match_all = [
491cb0ef41Sopenharmony_ci            ":v8_host_target_is_none",
501cb0ef41Sopenharmony_ci            ":is_" + name,
511cb0ef41Sopenharmony_ci        ],
521cb0ef41Sopenharmony_ci    )
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    # Select either the default target or the flag.
551cb0ef41Sopenharmony_ci    selects.config_setting_group(
561cb0ef41Sopenharmony_ci        name = "v8_target_" + name,
571cb0ef41Sopenharmony_ci        match_any = [
581cb0ef41Sopenharmony_ci            ":v8_host_target_is_" + name,
591cb0ef41Sopenharmony_ci            ":v8_target_is_" + name,
601cb0ef41Sopenharmony_ci        ],
611cb0ef41Sopenharmony_ci    )
62