• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Example showing how to create a rule that rules_cc can depend on."""
16
17load("@rules_cc//cc:action_names.bzl", "CPP_LINK_STATIC_LIBRARY_ACTION_NAME")
18load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cpp_toolchain", "use_cc_toolchain")
19load("//examples/my_c_compile:my_c_compile.bzl", "MyCCompileInfo")
20
21def _my_c_archive_impl(ctx):
22    cc_toolchain = find_cpp_toolchain(ctx)
23    object_file = ctx.attr.object[MyCCompileInfo].object
24    output_file = ctx.actions.declare_file(ctx.label.name + ".a")
25
26    feature_configuration = cc_common.configure_features(
27        ctx = ctx,
28        cc_toolchain = cc_toolchain,
29        requested_features = ctx.features,
30        unsupported_features = ctx.disabled_features,
31    )
32
33    linker_input = cc_common.create_linker_input(
34        owner = ctx.label,
35        libraries = depset(direct = [
36            cc_common.create_library_to_link(
37                actions = ctx.actions,
38                feature_configuration = feature_configuration,
39                cc_toolchain = cc_toolchain,
40                static_library = output_file,
41            ),
42        ]),
43    )
44    compilation_context = cc_common.create_compilation_context()
45    linking_context = cc_common.create_linking_context(linker_inputs = depset(direct = [linker_input]))
46
47    archiver_path = cc_common.get_tool_for_action(
48        feature_configuration = feature_configuration,
49        action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
50    )
51    archiver_variables = cc_common.create_link_variables(
52        feature_configuration = feature_configuration,
53        cc_toolchain = cc_toolchain,
54        output_file = output_file.path,
55        is_using_linker = False,
56    )
57    command_line = cc_common.get_memory_inefficient_command_line(
58        feature_configuration = feature_configuration,
59        action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
60        variables = archiver_variables,
61    )
62    args = ctx.actions.args()
63    args.add_all(command_line)
64    args.add(object_file)
65
66    env = cc_common.get_environment_variables(
67        feature_configuration = feature_configuration,
68        action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
69        variables = archiver_variables,
70    )
71
72    ctx.actions.run(
73        executable = archiver_path,
74        arguments = [args],
75        env = env,
76        inputs = depset(
77            direct = [object_file],
78            transitive = [
79                cc_toolchain.all_files,
80            ],
81        ),
82        outputs = [output_file],
83    )
84
85    cc_info = cc_common.merge_cc_infos(cc_infos = [
86        CcInfo(compilation_context = compilation_context, linking_context = linking_context),
87    ] + [dep[CcInfo] for dep in ctx.attr.deps])
88    return [cc_info]
89
90my_c_archive = rule(
91    implementation = _my_c_archive_impl,
92    attrs = {
93        "deps": attr.label_list(providers = [CcInfo]),
94        "object": attr.label(mandatory = True, providers = [MyCCompileInfo]),
95        "_cc_toolchain": attr.label(default = Label("@rules_cc//cc:current_cc_toolchain")),
96    },
97    fragments = ["cpp"],
98    toolchains = use_cc_toolchain(),
99)
100