xref: /third_party/node/deps/v8/bazel/defs.bzl (revision 1cb0ef41)
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_ciFlagInfo = provider(fields = ["value"])
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cidef _options_impl(ctx):
81cb0ef41Sopenharmony_ci    return FlagInfo(value = ctx.build_setting_value)
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci_create_option_flag = rule(
111cb0ef41Sopenharmony_ci    implementation = _options_impl,
121cb0ef41Sopenharmony_ci    build_setting = config.bool(flag = True),
131cb0ef41Sopenharmony_ci)
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci_create_option_string = rule(
161cb0ef41Sopenharmony_ci    implementation = _options_impl,
171cb0ef41Sopenharmony_ci    build_setting = config.string(flag = True),
181cb0ef41Sopenharmony_ci)
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci_create_option_int = rule(
211cb0ef41Sopenharmony_ci    implementation = _options_impl,
221cb0ef41Sopenharmony_ci    build_setting = config.int(flag = True),
231cb0ef41Sopenharmony_ci)
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cidef v8_flag(name, default = False):
261cb0ef41Sopenharmony_ci    _create_option_flag(name = name, build_setting_default = default)
271cb0ef41Sopenharmony_ci    native.config_setting(name = "is_" + name, flag_values = {name: "True"})
281cb0ef41Sopenharmony_ci    native.config_setting(name = "is_not_" + name, flag_values = {name: "False"})
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cidef v8_string(name, default = ""):
311cb0ef41Sopenharmony_ci    _create_option_string(name = name, build_setting_default = default)
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cidef v8_int(name, default = 0):
341cb0ef41Sopenharmony_ci    _create_option_int(name = name, build_setting_default = default)
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cidef _custom_config_impl(ctx):
371cb0ef41Sopenharmony_ci    defs = []
381cb0ef41Sopenharmony_ci    defs.append("V8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=" +
391cb0ef41Sopenharmony_ci                str(ctx.attr._v8_typed_array_max_size_in_heap[FlagInfo].value))
401cb0ef41Sopenharmony_ci    context = cc_common.create_compilation_context(defines = depset(defs))
411cb0ef41Sopenharmony_ci    return [CcInfo(compilation_context = context)]
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_civ8_custom_config = rule(
441cb0ef41Sopenharmony_ci    implementation = _custom_config_impl,
451cb0ef41Sopenharmony_ci    attrs = {
461cb0ef41Sopenharmony_ci        "_v8_typed_array_max_size_in_heap": attr.label(default = ":v8_typed_array_max_size_in_heap"),
471cb0ef41Sopenharmony_ci    },
481cb0ef41Sopenharmony_ci)
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cidef _config_impl(ctx):
511cb0ef41Sopenharmony_ci    hdrs = []
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    # Add headers
541cb0ef41Sopenharmony_ci    for h in ctx.attr.hdrs:
551cb0ef41Sopenharmony_ci        hdrs += h[DefaultInfo].files.to_list()
561cb0ef41Sopenharmony_ci    defs = []
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    # Add conditional_defines
591cb0ef41Sopenharmony_ci    for f, d in ctx.attr.conditional_defines.items():
601cb0ef41Sopenharmony_ci        if f[FlagInfo].value:
611cb0ef41Sopenharmony_ci            defs.append(d)
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    # Add defines
641cb0ef41Sopenharmony_ci    for d in ctx.attr.defines:
651cb0ef41Sopenharmony_ci        defs.append(d)
661cb0ef41Sopenharmony_ci    context = cc_common.create_compilation_context(
671cb0ef41Sopenharmony_ci        defines = depset(
681cb0ef41Sopenharmony_ci            defs,
691cb0ef41Sopenharmony_ci            transitive = [dep[CcInfo].compilation_context.defines for dep in ctx.attr.deps],
701cb0ef41Sopenharmony_ci        ),
711cb0ef41Sopenharmony_ci        headers = depset(
721cb0ef41Sopenharmony_ci            hdrs,
731cb0ef41Sopenharmony_ci            transitive = [dep[CcInfo].compilation_context.headers for dep in ctx.attr.deps],
741cb0ef41Sopenharmony_ci        ),
751cb0ef41Sopenharmony_ci    )
761cb0ef41Sopenharmony_ci    return [CcInfo(compilation_context = context)]
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_civ8_config = rule(
791cb0ef41Sopenharmony_ci    implementation = _config_impl,
801cb0ef41Sopenharmony_ci    attrs = {
811cb0ef41Sopenharmony_ci        "conditional_defines": attr.label_keyed_string_dict(),
821cb0ef41Sopenharmony_ci        "defines": attr.string_list(),
831cb0ef41Sopenharmony_ci        "deps": attr.label_list(),
841cb0ef41Sopenharmony_ci        "hdrs": attr.label_list(allow_files = True),
851cb0ef41Sopenharmony_ci    },
861cb0ef41Sopenharmony_ci)
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cidef _default_args():
891cb0ef41Sopenharmony_ci    return struct(
901cb0ef41Sopenharmony_ci        deps = [":define_flags"],
911cb0ef41Sopenharmony_ci        defines = select({
921cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_windows": [
931cb0ef41Sopenharmony_ci                "UNICODE",
941cb0ef41Sopenharmony_ci                "_UNICODE",
951cb0ef41Sopenharmony_ci                "_CRT_RAND_S",
961cb0ef41Sopenharmony_ci                "_WIN32_WINNT=0x0602",  # Override bazel default to Windows 8
971cb0ef41Sopenharmony_ci            ],
981cb0ef41Sopenharmony_ci            "//conditions:default": [],
991cb0ef41Sopenharmony_ci        }),
1001cb0ef41Sopenharmony_ci        copts = select({
1011cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_posix": [
1021cb0ef41Sopenharmony_ci                "-fPIC",
1031cb0ef41Sopenharmony_ci                "-fno-strict-aliasing",
1041cb0ef41Sopenharmony_ci                "-Werror",
1051cb0ef41Sopenharmony_ci                "-Wextra",
1061cb0ef41Sopenharmony_ci                "-Wno-unknown-warning-option",
1071cb0ef41Sopenharmony_ci                "-Wno-bitwise-instead-of-logical",
1081cb0ef41Sopenharmony_ci                "-Wno-builtin-assume-aligned-alignment",
1091cb0ef41Sopenharmony_ci                "-Wno-unused-parameter",
1101cb0ef41Sopenharmony_ci                "-Wno-implicit-int-float-conversion",
1111cb0ef41Sopenharmony_ci                "-Wno-deprecated-copy",
1121cb0ef41Sopenharmony_ci                "-Wno-non-virtual-dtor",
1131cb0ef41Sopenharmony_ci                "-isystem .",
1141cb0ef41Sopenharmony_ci            ],
1151cb0ef41Sopenharmony_ci            "//conditions:default": [],
1161cb0ef41Sopenharmony_ci        }) + select({
1171cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_clang": [
1181cb0ef41Sopenharmony_ci                "-Wno-invalid-offsetof",
1191cb0ef41Sopenharmony_ci                "-std=c++17",
1201cb0ef41Sopenharmony_ci            ],
1211cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_gcc": [
1221cb0ef41Sopenharmony_ci                "-Wno-extra",
1231cb0ef41Sopenharmony_ci                "-Wno-array-bounds",
1241cb0ef41Sopenharmony_ci                "-Wno-class-memaccess",
1251cb0ef41Sopenharmony_ci                "-Wno-comments",
1261cb0ef41Sopenharmony_ci                "-Wno-deprecated-declarations",
1271cb0ef41Sopenharmony_ci                "-Wno-implicit-fallthrough",
1281cb0ef41Sopenharmony_ci                "-Wno-int-in-bool-context",
1291cb0ef41Sopenharmony_ci                "-Wno-maybe-uninitialized",
1301cb0ef41Sopenharmony_ci                "-Wno-mismatched-new-delete",
1311cb0ef41Sopenharmony_ci                "-Wno-redundant-move",
1321cb0ef41Sopenharmony_ci                "-Wno-return-type",
1331cb0ef41Sopenharmony_ci                "-Wno-stringop-overflow",
1341cb0ef41Sopenharmony_ci                # Use GNU dialect, because GCC doesn't allow using
1351cb0ef41Sopenharmony_ci                # ##__VA_ARGS__ when in standards-conforming mode.
1361cb0ef41Sopenharmony_ci                "-std=gnu++17",
1371cb0ef41Sopenharmony_ci            ],
1381cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_windows": [
1391cb0ef41Sopenharmony_ci                "/std:c++17",
1401cb0ef41Sopenharmony_ci            ],
1411cb0ef41Sopenharmony_ci            "//conditions:default": [],
1421cb0ef41Sopenharmony_ci        }) + select({
1431cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_gcc_fastbuild": [
1441cb0ef41Sopenharmony_ci                # Non-debug builds without optimizations fail because
1451cb0ef41Sopenharmony_ci                # of recursive inlining of "always_inline" functions.
1461cb0ef41Sopenharmony_ci                "-O1",
1471cb0ef41Sopenharmony_ci            ],
1481cb0ef41Sopenharmony_ci            "//conditions:default": [],
1491cb0ef41Sopenharmony_ci        }) + select({
1501cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_clang_s390x": [
1511cb0ef41Sopenharmony_ci                "-fno-integrated-as",
1521cb0ef41Sopenharmony_ci            ],
1531cb0ef41Sopenharmony_ci            "//conditions:default": [],
1541cb0ef41Sopenharmony_ci        }),
1551cb0ef41Sopenharmony_ci        includes = ["include"],
1561cb0ef41Sopenharmony_ci        linkopts = select({
1571cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_windows": [
1581cb0ef41Sopenharmony_ci                "Winmm.lib",
1591cb0ef41Sopenharmony_ci                "DbgHelp.lib",
1601cb0ef41Sopenharmony_ci                "Advapi32.lib",
1611cb0ef41Sopenharmony_ci            ],
1621cb0ef41Sopenharmony_ci            "@v8//bazel/config:is_macos": ["-pthread"],
1631cb0ef41Sopenharmony_ci            "//conditions:default": ["-Wl,--no-as-needed -ldl -pthread"],
1641cb0ef41Sopenharmony_ci        }) + select({
1651cb0ef41Sopenharmony_ci            ":should_add_rdynamic": ["-rdynamic"],
1661cb0ef41Sopenharmony_ci            "//conditions:default": [],
1671cb0ef41Sopenharmony_ci        }),
1681cb0ef41Sopenharmony_ci    )
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ciENABLE_I18N_SUPPORT_DEFINES = [
1711cb0ef41Sopenharmony_ci    "-DV8_INTL_SUPPORT",
1721cb0ef41Sopenharmony_ci    "-DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_STATIC",
1731cb0ef41Sopenharmony_ci    # src/regexp/regexp-compiler-tonode.cc uses an unsafe ICU method and
1741cb0ef41Sopenharmony_ci    # access a character implicitly.
1751cb0ef41Sopenharmony_ci    "-DUNISTR_FROM_CHAR_EXPLICIT=",
1761cb0ef41Sopenharmony_ci]
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_cidef _should_emit_noicu_and_icu(noicu_srcs, noicu_deps, icu_srcs, icu_deps):
1791cb0ef41Sopenharmony_ci    return noicu_srcs != [] or noicu_deps != [] or icu_srcs != [] or icu_deps != []
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci# buildifier: disable=function-docstring
1821cb0ef41Sopenharmony_cidef v8_binary(
1831cb0ef41Sopenharmony_ci        name,
1841cb0ef41Sopenharmony_ci        srcs,
1851cb0ef41Sopenharmony_ci        deps = [],
1861cb0ef41Sopenharmony_ci        includes = [],
1871cb0ef41Sopenharmony_ci        copts = [],
1881cb0ef41Sopenharmony_ci        linkopts = [],
1891cb0ef41Sopenharmony_ci        noicu_srcs = [],
1901cb0ef41Sopenharmony_ci        noicu_deps = [],
1911cb0ef41Sopenharmony_ci        icu_srcs = [],
1921cb0ef41Sopenharmony_ci        icu_deps = [],
1931cb0ef41Sopenharmony_ci        **kwargs):
1941cb0ef41Sopenharmony_ci    default = _default_args()
1951cb0ef41Sopenharmony_ci    if _should_emit_noicu_and_icu(noicu_srcs, noicu_deps, icu_srcs, icu_deps):
1961cb0ef41Sopenharmony_ci        native.cc_binary(
1971cb0ef41Sopenharmony_ci            name = "noicu/" + name,
1981cb0ef41Sopenharmony_ci            srcs = srcs + noicu_srcs,
1991cb0ef41Sopenharmony_ci            deps = deps + noicu_deps + default.deps,
2001cb0ef41Sopenharmony_ci            includes = includes + default.includes,
2011cb0ef41Sopenharmony_ci            copts = copts + default.copts,
2021cb0ef41Sopenharmony_ci            linkopts = linkopts + default.linkopts,
2031cb0ef41Sopenharmony_ci            **kwargs
2041cb0ef41Sopenharmony_ci        )
2051cb0ef41Sopenharmony_ci        native.cc_binary(
2061cb0ef41Sopenharmony_ci            name = "icu/" + name,
2071cb0ef41Sopenharmony_ci            srcs = srcs + icu_srcs,
2081cb0ef41Sopenharmony_ci            deps = deps + icu_deps + default.deps,
2091cb0ef41Sopenharmony_ci            includes = includes + default.includes,
2101cb0ef41Sopenharmony_ci            copts = copts + default.copts + ENABLE_I18N_SUPPORT_DEFINES,
2111cb0ef41Sopenharmony_ci            linkopts = linkopts + default.linkopts,
2121cb0ef41Sopenharmony_ci            **kwargs
2131cb0ef41Sopenharmony_ci        )
2141cb0ef41Sopenharmony_ci    else:
2151cb0ef41Sopenharmony_ci        native.cc_binary(
2161cb0ef41Sopenharmony_ci            name = name,
2171cb0ef41Sopenharmony_ci            srcs = srcs,
2181cb0ef41Sopenharmony_ci            deps = deps + default.deps,
2191cb0ef41Sopenharmony_ci            includes = includes + default.includes,
2201cb0ef41Sopenharmony_ci            copts = copts + default.copts,
2211cb0ef41Sopenharmony_ci            linkopts = linkopts + default.linkopts,
2221cb0ef41Sopenharmony_ci            **kwargs
2231cb0ef41Sopenharmony_ci        )
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci# buildifier: disable=function-docstring
2261cb0ef41Sopenharmony_cidef v8_library(
2271cb0ef41Sopenharmony_ci        name,
2281cb0ef41Sopenharmony_ci        srcs,
2291cb0ef41Sopenharmony_ci        deps = [],
2301cb0ef41Sopenharmony_ci        includes = [],
2311cb0ef41Sopenharmony_ci        copts = [],
2321cb0ef41Sopenharmony_ci        linkopts = [],
2331cb0ef41Sopenharmony_ci        noicu_srcs = [],
2341cb0ef41Sopenharmony_ci        noicu_deps = [],
2351cb0ef41Sopenharmony_ci        icu_srcs = [],
2361cb0ef41Sopenharmony_ci        icu_deps = [],
2371cb0ef41Sopenharmony_ci        **kwargs):
2381cb0ef41Sopenharmony_ci    default = _default_args()
2391cb0ef41Sopenharmony_ci    if _should_emit_noicu_and_icu(noicu_srcs, noicu_deps, icu_srcs, icu_deps):
2401cb0ef41Sopenharmony_ci        native.cc_library(
2411cb0ef41Sopenharmony_ci            name = name + "_noicu",
2421cb0ef41Sopenharmony_ci            srcs = srcs + noicu_srcs,
2431cb0ef41Sopenharmony_ci            deps = deps + noicu_deps + default.deps,
2441cb0ef41Sopenharmony_ci            includes = includes + default.includes,
2451cb0ef41Sopenharmony_ci            copts = copts + default.copts,
2461cb0ef41Sopenharmony_ci            linkopts = linkopts + default.linkopts,
2471cb0ef41Sopenharmony_ci            alwayslink = 1,
2481cb0ef41Sopenharmony_ci            linkstatic = 1,
2491cb0ef41Sopenharmony_ci            **kwargs
2501cb0ef41Sopenharmony_ci        )
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci        # Alias target used because of cc_library bug in bazel on windows
2531cb0ef41Sopenharmony_ci        # https://github.com/bazelbuild/bazel/issues/14237
2541cb0ef41Sopenharmony_ci        # TODO(victorgomes): Remove alias once bug is fixed
2551cb0ef41Sopenharmony_ci        native.alias(
2561cb0ef41Sopenharmony_ci            name = "noicu/" + name,
2571cb0ef41Sopenharmony_ci            actual = name + "_noicu",
2581cb0ef41Sopenharmony_ci        )
2591cb0ef41Sopenharmony_ci        native.cc_library(
2601cb0ef41Sopenharmony_ci            name = name + "_icu",
2611cb0ef41Sopenharmony_ci            srcs = srcs + icu_srcs,
2621cb0ef41Sopenharmony_ci            deps = deps + icu_deps + default.deps,
2631cb0ef41Sopenharmony_ci            includes = includes + default.includes,
2641cb0ef41Sopenharmony_ci            copts = copts + default.copts + ENABLE_I18N_SUPPORT_DEFINES,
2651cb0ef41Sopenharmony_ci            linkopts = linkopts + default.linkopts,
2661cb0ef41Sopenharmony_ci            alwayslink = 1,
2671cb0ef41Sopenharmony_ci            linkstatic = 1,
2681cb0ef41Sopenharmony_ci            **kwargs
2691cb0ef41Sopenharmony_ci        )
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ci        # Alias target used because of cc_library bug in bazel on windows
2721cb0ef41Sopenharmony_ci        # https://github.com/bazelbuild/bazel/issues/14237
2731cb0ef41Sopenharmony_ci        # TODO(victorgomes): Remove alias once bug is fixed
2741cb0ef41Sopenharmony_ci        native.alias(
2751cb0ef41Sopenharmony_ci            name = "icu/" + name,
2761cb0ef41Sopenharmony_ci            actual = name + "_icu",
2771cb0ef41Sopenharmony_ci        )
2781cb0ef41Sopenharmony_ci    else:
2791cb0ef41Sopenharmony_ci        native.cc_library(
2801cb0ef41Sopenharmony_ci            name = name,
2811cb0ef41Sopenharmony_ci            srcs = srcs,
2821cb0ef41Sopenharmony_ci            deps = deps + default.deps,
2831cb0ef41Sopenharmony_ci            includes = includes + default.includes,
2841cb0ef41Sopenharmony_ci            copts = copts + default.copts,
2851cb0ef41Sopenharmony_ci            linkopts = linkopts + default.linkopts,
2861cb0ef41Sopenharmony_ci            alwayslink = 1,
2871cb0ef41Sopenharmony_ci            linkstatic = 1,
2881cb0ef41Sopenharmony_ci            **kwargs
2891cb0ef41Sopenharmony_ci        )
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_cidef _torque_impl(ctx):
2921cb0ef41Sopenharmony_ci    if ctx.workspace_name == "v8":
2931cb0ef41Sopenharmony_ci        v8root = "."
2941cb0ef41Sopenharmony_ci    else:
2951cb0ef41Sopenharmony_ci        v8root = "external/v8"
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci    # Arguments
2981cb0ef41Sopenharmony_ci    args = []
2991cb0ef41Sopenharmony_ci    args += ctx.attr.args
3001cb0ef41Sopenharmony_ci    args.append("-o")
3011cb0ef41Sopenharmony_ci    args.append(ctx.bin_dir.path + "/" + v8root + "/" + ctx.attr.prefix + "/torque-generated")
3021cb0ef41Sopenharmony_ci    args.append("-strip-v8-root")
3031cb0ef41Sopenharmony_ci    args.append("-v8-root")
3041cb0ef41Sopenharmony_ci    args.append(v8root)
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ci    # Sources
3071cb0ef41Sopenharmony_ci    args += [f.path for f in ctx.files.srcs]
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci    # Generate/declare output files
3101cb0ef41Sopenharmony_ci    outs = []
3111cb0ef41Sopenharmony_ci    for src in ctx.files.srcs:
3121cb0ef41Sopenharmony_ci        root, period, ext = src.path.rpartition(".")
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci        # Strip v8root
3151cb0ef41Sopenharmony_ci        if root[:len(v8root)] == v8root:
3161cb0ef41Sopenharmony_ci            root = root[len(v8root):]
3171cb0ef41Sopenharmony_ci        file = ctx.attr.prefix + "/torque-generated/" + root
3181cb0ef41Sopenharmony_ci        outs.append(ctx.actions.declare_file(file + "-tq-csa.cc"))
3191cb0ef41Sopenharmony_ci        outs.append(ctx.actions.declare_file(file + "-tq-csa.h"))
3201cb0ef41Sopenharmony_ci        outs.append(ctx.actions.declare_file(file + "-tq-inl.inc"))
3211cb0ef41Sopenharmony_ci        outs.append(ctx.actions.declare_file(file + "-tq.inc"))
3221cb0ef41Sopenharmony_ci        outs.append(ctx.actions.declare_file(file + "-tq.cc"))
3231cb0ef41Sopenharmony_ci    outs += [ctx.actions.declare_file(ctx.attr.prefix + "/torque-generated/" + f) for f in ctx.attr.extras]
3241cb0ef41Sopenharmony_ci    ctx.actions.run(
3251cb0ef41Sopenharmony_ci        outputs = outs,
3261cb0ef41Sopenharmony_ci        inputs = ctx.files.srcs,
3271cb0ef41Sopenharmony_ci        arguments = args,
3281cb0ef41Sopenharmony_ci        executable = ctx.executable.tool,
3291cb0ef41Sopenharmony_ci        mnemonic = "GenTorque",
3301cb0ef41Sopenharmony_ci        progress_message = "Generating Torque files",
3311cb0ef41Sopenharmony_ci    )
3321cb0ef41Sopenharmony_ci    return [DefaultInfo(files = depset(outs))]
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci_v8_torque = rule(
3351cb0ef41Sopenharmony_ci    implementation = _torque_impl,
3361cb0ef41Sopenharmony_ci    # cfg = v8_target_cpu_transition,
3371cb0ef41Sopenharmony_ci    attrs = {
3381cb0ef41Sopenharmony_ci        "prefix": attr.string(mandatory = True),
3391cb0ef41Sopenharmony_ci        "srcs": attr.label_list(allow_files = True, mandatory = True),
3401cb0ef41Sopenharmony_ci        "extras": attr.string_list(),
3411cb0ef41Sopenharmony_ci        "tool": attr.label(
3421cb0ef41Sopenharmony_ci            allow_files = True,
3431cb0ef41Sopenharmony_ci            executable = True,
3441cb0ef41Sopenharmony_ci            cfg = "exec",
3451cb0ef41Sopenharmony_ci        ),
3461cb0ef41Sopenharmony_ci        "args": attr.string_list(),
3471cb0ef41Sopenharmony_ci    },
3481cb0ef41Sopenharmony_ci)
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_cidef v8_torque(name, noicu_srcs, icu_srcs, args, extras):
3511cb0ef41Sopenharmony_ci    _v8_torque(
3521cb0ef41Sopenharmony_ci        name = "noicu/" + name,
3531cb0ef41Sopenharmony_ci        prefix = "noicu",
3541cb0ef41Sopenharmony_ci        srcs = noicu_srcs,
3551cb0ef41Sopenharmony_ci        args = args,
3561cb0ef41Sopenharmony_ci        extras = extras,
3571cb0ef41Sopenharmony_ci        tool = select({
3581cb0ef41Sopenharmony_ci            "@v8//bazel/config:v8_target_is_32_bits": ":torque_non_pointer_compression",
3591cb0ef41Sopenharmony_ci            "//conditions:default": ":torque",
3601cb0ef41Sopenharmony_ci        }),
3611cb0ef41Sopenharmony_ci    )
3621cb0ef41Sopenharmony_ci    _v8_torque(
3631cb0ef41Sopenharmony_ci        name = "icu/" + name,
3641cb0ef41Sopenharmony_ci        prefix = "icu",
3651cb0ef41Sopenharmony_ci        srcs = icu_srcs,
3661cb0ef41Sopenharmony_ci        args = args,
3671cb0ef41Sopenharmony_ci        extras = extras,
3681cb0ef41Sopenharmony_ci        tool = select({
3691cb0ef41Sopenharmony_ci            "@v8//bazel/config:v8_target_is_32_bits": ":torque_non_pointer_compression",
3701cb0ef41Sopenharmony_ci            "//conditions:default": ":torque",
3711cb0ef41Sopenharmony_ci        }),
3721cb0ef41Sopenharmony_ci    )
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_cidef _v8_target_cpu_transition_impl(settings, attr):
3751cb0ef41Sopenharmony_ci    # Check for an existing v8_target_cpu flag.
3761cb0ef41Sopenharmony_ci    if "@v8//bazel/config:v8_target_cpu" in settings:
3771cb0ef41Sopenharmony_ci        if settings["@v8//bazel/config:v8_target_cpu"] != "none":
3781cb0ef41Sopenharmony_ci            return
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci    # Auto-detect target architecture based on the --cpu flag.
3811cb0ef41Sopenharmony_ci    mapping = {
3821cb0ef41Sopenharmony_ci        "haswell": "x64",
3831cb0ef41Sopenharmony_ci        "k8": "x64",
3841cb0ef41Sopenharmony_ci        "x86_64": "x64",
3851cb0ef41Sopenharmony_ci        "darwin": "x64",
3861cb0ef41Sopenharmony_ci        "darwin_x86_64": "x64",
3871cb0ef41Sopenharmony_ci        "x64_windows": "x64",
3881cb0ef41Sopenharmony_ci        "x86": "ia32",
3891cb0ef41Sopenharmony_ci        "aarch64": "arm64",
3901cb0ef41Sopenharmony_ci        "arm64-v8a": "arm64",
3911cb0ef41Sopenharmony_ci        "arm": "arm64",
3921cb0ef41Sopenharmony_ci        "darwin_arm64": "arm64",
3931cb0ef41Sopenharmony_ci        "armeabi-v7a": "arm32",
3941cb0ef41Sopenharmony_ci        "s390x": "s390x",
3951cb0ef41Sopenharmony_ci        "riscv64": "riscv64",
3961cb0ef41Sopenharmony_ci        "ppc": "ppc64le",
3971cb0ef41Sopenharmony_ci    }
3981cb0ef41Sopenharmony_ci    v8_target_cpu = mapping[settings["//command_line_option:cpu"]]
3991cb0ef41Sopenharmony_ci    return {"@v8//bazel/config:v8_target_cpu": v8_target_cpu}
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci# Set the v8_target_cpu to be the correct architecture given the cpu specified
4021cb0ef41Sopenharmony_ci# on the command line.
4031cb0ef41Sopenharmony_civ8_target_cpu_transition = transition(
4041cb0ef41Sopenharmony_ci    implementation = _v8_target_cpu_transition_impl,
4051cb0ef41Sopenharmony_ci    inputs = ["@v8//bazel/config:v8_target_cpu", "//command_line_option:cpu"],
4061cb0ef41Sopenharmony_ci    outputs = ["@v8//bazel/config:v8_target_cpu"],
4071cb0ef41Sopenharmony_ci)
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_cidef _mksnapshot(ctx):
4101cb0ef41Sopenharmony_ci    outs = [
4111cb0ef41Sopenharmony_ci        ctx.actions.declare_file(ctx.attr.prefix + "/snapshot.cc"),
4121cb0ef41Sopenharmony_ci        ctx.actions.declare_file(ctx.attr.prefix + "/embedded.S"),
4131cb0ef41Sopenharmony_ci    ]
4141cb0ef41Sopenharmony_ci    ctx.actions.run(
4151cb0ef41Sopenharmony_ci        outputs = outs,
4161cb0ef41Sopenharmony_ci        inputs = [],
4171cb0ef41Sopenharmony_ci        arguments = [
4181cb0ef41Sopenharmony_ci            "--embedded_variant=Default",
4191cb0ef41Sopenharmony_ci            "--startup_src",
4201cb0ef41Sopenharmony_ci            outs[0].path,
4211cb0ef41Sopenharmony_ci            "--embedded_src",
4221cb0ef41Sopenharmony_ci            outs[1].path,
4231cb0ef41Sopenharmony_ci        ] + ctx.attr.args,
4241cb0ef41Sopenharmony_ci        executable = ctx.executable.tool,
4251cb0ef41Sopenharmony_ci        progress_message = "Running mksnapshot",
4261cb0ef41Sopenharmony_ci    )
4271cb0ef41Sopenharmony_ci    return [DefaultInfo(files = depset(outs))]
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ci_v8_mksnapshot = rule(
4301cb0ef41Sopenharmony_ci    implementation = _mksnapshot,
4311cb0ef41Sopenharmony_ci    attrs = {
4321cb0ef41Sopenharmony_ci        "args": attr.string_list(),
4331cb0ef41Sopenharmony_ci        "tool": attr.label(
4341cb0ef41Sopenharmony_ci            mandatory = True,
4351cb0ef41Sopenharmony_ci            allow_files = True,
4361cb0ef41Sopenharmony_ci            executable = True,
4371cb0ef41Sopenharmony_ci            cfg = "exec",
4381cb0ef41Sopenharmony_ci        ),
4391cb0ef41Sopenharmony_ci        "_allowlist_function_transition": attr.label(
4401cb0ef41Sopenharmony_ci            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
4411cb0ef41Sopenharmony_ci        ),
4421cb0ef41Sopenharmony_ci        "prefix": attr.string(mandatory = True),
4431cb0ef41Sopenharmony_ci    },
4441cb0ef41Sopenharmony_ci    cfg = v8_target_cpu_transition,
4451cb0ef41Sopenharmony_ci)
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_cidef v8_mksnapshot(name, args):
4481cb0ef41Sopenharmony_ci    _v8_mksnapshot(
4491cb0ef41Sopenharmony_ci        name = "noicu/" + name,
4501cb0ef41Sopenharmony_ci        args = args,
4511cb0ef41Sopenharmony_ci        prefix = "noicu",
4521cb0ef41Sopenharmony_ci        tool = ":noicu/mksnapshot",
4531cb0ef41Sopenharmony_ci    )
4541cb0ef41Sopenharmony_ci    _v8_mksnapshot(
4551cb0ef41Sopenharmony_ci        name = "icu/" + name,
4561cb0ef41Sopenharmony_ci        args = args,
4571cb0ef41Sopenharmony_ci        prefix = "icu",
4581cb0ef41Sopenharmony_ci        tool = ":icu/mksnapshot",
4591cb0ef41Sopenharmony_ci    )
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_cidef _quote(val):
4621cb0ef41Sopenharmony_ci    if val[0] == '"' and val[-1] == '"':
4631cb0ef41Sopenharmony_ci        fail("String", val, "already quoted")
4641cb0ef41Sopenharmony_ci    return '"' + val + '"'
4651cb0ef41Sopenharmony_ci
4661cb0ef41Sopenharmony_cidef _kv_bool_pair(k, v):
4671cb0ef41Sopenharmony_ci    return _quote(k) + ": " + v
4681cb0ef41Sopenharmony_ci
4691cb0ef41Sopenharmony_cidef _json(kv_pairs):
4701cb0ef41Sopenharmony_ci    content = "{"
4711cb0ef41Sopenharmony_ci    for (k, v) in kv_pairs[:-1]:
4721cb0ef41Sopenharmony_ci        content += _kv_bool_pair(k, v) + ", "
4731cb0ef41Sopenharmony_ci    (k, v) = kv_pairs[-1]
4741cb0ef41Sopenharmony_ci    content += _kv_bool_pair(k, v)
4751cb0ef41Sopenharmony_ci    content += "}\n"
4761cb0ef41Sopenharmony_ci    return content
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_cidef build_config_content(cpu, icu):
4791cb0ef41Sopenharmony_ci    return _json([
4801cb0ef41Sopenharmony_ci        ("current_cpu", cpu),
4811cb0ef41Sopenharmony_ci        ("dcheck_always_on", "false"),
4821cb0ef41Sopenharmony_ci        ("is_android", "false"),
4831cb0ef41Sopenharmony_ci        ("is_asan", "false"),
4841cb0ef41Sopenharmony_ci        ("is_cfi", "false"),
4851cb0ef41Sopenharmony_ci        ("is_clang", "true"),
4861cb0ef41Sopenharmony_ci        ("is_component_build", "false"),
4871cb0ef41Sopenharmony_ci        ("is_debug", "false"),
4881cb0ef41Sopenharmony_ci        ("is_full_debug", "false"),
4891cb0ef41Sopenharmony_ci        ("is_gcov_coverage", "false"),
4901cb0ef41Sopenharmony_ci        ("is_msan", "false"),
4911cb0ef41Sopenharmony_ci        ("is_tsan", "false"),
4921cb0ef41Sopenharmony_ci        ("is_ubsan_vptr", "false"),
4931cb0ef41Sopenharmony_ci        ("target_cpu", cpu),
4941cb0ef41Sopenharmony_ci        ("v8_current_cpu", cpu),
4951cb0ef41Sopenharmony_ci        ("v8_dict_property_const_tracking", "false"),
4961cb0ef41Sopenharmony_ci        ("v8_enable_atomic_marking_state", "false"),
4971cb0ef41Sopenharmony_ci        ("v8_enable_atomic_object_field_writes", "false"),
4981cb0ef41Sopenharmony_ci        ("v8_enable_concurrent_marking", "false"),
4991cb0ef41Sopenharmony_ci        ("v8_enable_i18n_support", icu),
5001cb0ef41Sopenharmony_ci        ("v8_enable_verify_predictable", "false"),
5011cb0ef41Sopenharmony_ci        ("v8_enable_verify_csa", "false"),
5021cb0ef41Sopenharmony_ci        ("v8_enable_lite_mode", "false"),
5031cb0ef41Sopenharmony_ci        ("v8_enable_runtime_call_stats", "false"),
5041cb0ef41Sopenharmony_ci        ("v8_enable_pointer_compression", "true"),
5051cb0ef41Sopenharmony_ci        ("v8_enable_pointer_compression_shared_cage", "false"),
5061cb0ef41Sopenharmony_ci        ("v8_enable_third_party_heap", "false"),
5071cb0ef41Sopenharmony_ci        ("v8_enable_webassembly", "false"),
5081cb0ef41Sopenharmony_ci        ("v8_control_flow_integrity", "false"),
5091cb0ef41Sopenharmony_ci        ("v8_enable_single_generation", "false"),
5101cb0ef41Sopenharmony_ci        ("v8_enable_sandbox", "false"),
5111cb0ef41Sopenharmony_ci        ("v8_enable_shared_ro_heap", "false"),
5121cb0ef41Sopenharmony_ci        ("v8_target_cpu", cpu),
5131cb0ef41Sopenharmony_ci    ])
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci# TODO(victorgomes): Create a rule (instead of a macro), that can
5161cb0ef41Sopenharmony_ci# dynamically populate the build config.
5171cb0ef41Sopenharmony_cidef v8_build_config(name):
5181cb0ef41Sopenharmony_ci    cpu = _quote("x64")
5191cb0ef41Sopenharmony_ci    native.genrule(
5201cb0ef41Sopenharmony_ci        name = "noicu/" + name,
5211cb0ef41Sopenharmony_ci        outs = ["noicu/" + name + ".json"],
5221cb0ef41Sopenharmony_ci        cmd = "echo '" + build_config_content(cpu, "false") + "' > \"$@\"",
5231cb0ef41Sopenharmony_ci    )
5241cb0ef41Sopenharmony_ci    native.genrule(
5251cb0ef41Sopenharmony_ci        name = "icu/" + name,
5261cb0ef41Sopenharmony_ci        outs = ["icu/" + name + ".json"],
5271cb0ef41Sopenharmony_ci        cmd = "echo '" + build_config_content(cpu, "true") + "' > \"$@\"",
5281cb0ef41Sopenharmony_ci    )
529