1""" CC Archive ActionConfigs for llvm-ar """
2
3load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
4load(
5    "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
6    "action_config",
7    "flag_group",
8    "flag_set",
9    "tool",
10    "variable_with_value",
11)
12
13BASE_ARCHIVER_FLAG_SET = flag_set(
14    flag_groups = [
15        flag_group(
16            flags = ["-rcsD", "%{output_execpath}"],
17            expand_if_available = "output_execpath",
18        ),
19        flag_group(
20            iterate_over = "libraries_to_link",
21            flag_groups = [
22                flag_group(
23                    flags = ["%{libraries_to_link.name}"],
24                    expand_if_equal = variable_with_value(
25                        name = "libraries_to_link.type",
26                        value = "object_file",
27                    ),
28                ),
29                flag_group(
30                    flags = ["%{libraries_to_link.object_files}"],
31                    iterate_over = "libraries_to_link.object_files",
32                    expand_if_equal = variable_with_value(
33                        name = "libraries_to_link.type",
34                        value = "object_file_group",
35                    ),
36                ),
37            ],
38            expand_if_available = "libraries_to_link",
39        ),
40    ],
41)
42
43def archive_action_configs(llvm, archive_flags):
44    """
45    Generates CC Archive ActionConfigs
46
47    Args:
48        llvm (string): Path to LLVM binaries.
49        archive_flags (List): List of flags to always be passed to the archiver.
50
51    Returns:
52        List of CC Archive ActionConfigs
53    """
54
55    archive_flag_set = flag_set(
56        flag_groups = ([
57            flag_group(flags = archive_flags),
58        ] if archive_flags else []),
59    )
60
61    return [action_config(
62        action_name = ACTION_NAMES.cpp_link_static_library,
63        tools = [tool(path = "{}/bin/llvm-ar".format(llvm))],
64        flag_sets = [
65            # Mandatory, no link flags come through on command line.
66            flag_set(
67                flag_groups = [
68                    flag_group(
69                        flags = ["@%{linker_param_file}"],
70                        expand_if_available = "linker_param_file",
71                    ),
72                ],
73            ),
74            archive_flag_set,
75            BASE_ARCHIVER_FLAG_SET,
76        ],
77    )]
78