# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""Definitions of optimization `cc_toolchain_config` features."""

load(
    "@rules_cc//cc:cc_toolchain_config_lib.bzl",
    "feature",
    "feature_set",
    "flag_group",
    "flag_set",
    "with_feature_set",
)
load(
    ":cc_toolchain_actions.bzl",
    "all_compile_actions",
    "all_link_actions",
    "codegen_compile_actions",
)

# Handle different levels of optimization with individual features so that
# they can be ordered and the defaults can override the minimal settings if
# both are enabled.
minimal_optimization_flags = feature(
    name = "minimal_optimization_flags",
    flag_sets = [flag_set(
        actions = codegen_compile_actions,
        flag_groups = [flag_group(flags = ["-O1"])],
    )],
)
default_optimization_flags = feature(
    name = "default_optimization_flags",
    enabled = True,
    requires = [feature_set(["opt"])],
    flag_sets = [
        flag_set(
            actions = all_compile_actions,
            flag_groups = [flag_group(flags = ["-DNDEBUG"])],
        ),
        flag_set(
            actions = codegen_compile_actions,
            flag_groups = [flag_group(flags = ["-O3"])],
        ),
    ],
)

cpu_flags = feature(
    name = "aarch64_cpu_flags",
    enabled = True,
    flag_sets = [
        flag_set(
            actions = all_compile_actions + all_link_actions,
            flag_groups = [flag_group(flags = ["-march=armv8.2-a"])],
            with_features = [with_feature_set(["aarch64_target"])],
        ),
        flag_set(
            actions = all_compile_actions + all_link_actions,
            flag_groups = [flag_group(flags = ["-march=x86-64-v2"])],
            with_features = [with_feature_set(["x86_64_target"])],
        ),
    ],
)

# Note that the order of features is significant in this list and determines the
# relative order of flags from the features listed.
optimization_features = [
    minimal_optimization_flags,
    default_optimization_flags,
    cpu_flags,
]
