How to use libs built with cc_common.compile(...) from cc_binary?

587 views
Skip to first unread message

magnus.a...@gmail.com

unread,
Oct 6, 2021, 9:26:10 AM10/6/21
to bazel-discuss
Hi,

We have a custom Starlark rule that builds some static C libraries with cc_common.compile(...) and cc_common.create_linking_context_from_compilation_outputs(...). (There are reasons why we cannot build them with normal cc_library rules, but I think that these reasons are not important for this question.)

We want to add these libraries as deps to a cc_binary rule. How do we do this?

custom_rule_that_builds_two_libs_with_cc_common_compile(
    name ="libs",
    srcs = ["file1.c", "file2.c", ...],
    ...
)

cc_binary(
    name = "program",
    deps = ? # How do we add the libs from the custom rule above as deps?
)

BR,
Magnus

Keith Smiley

unread,
Oct 6, 2021, 12:10:06 PM10/6/21
to magnus.a...@gmail.com, bazel-discuss
If your rule propagates the output of those actions you can add the target name directly to the `deps` like normal.
--
Keith Smiley


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/6ac782c2-d395-4142-9de2-069cb8afd000n%40googlegroups.com.

magnus.a...@gmail.com

unread,
Oct 6, 2021, 1:09:22 PM10/6/21
to bazel-discuss
Thanks Keith! May I ask you to provide more details?

Let us say that the custom rule is implemented as below. What should we return, to be able to feed it to cc_binary(deps)?

def _custom_rule_that_builds_two_libs_with_cc_common_compile_impl(...):
    # Build library1
    (compilation_context1, compilation_outputs1) = cc_common.compile(...)
    (linking_context1, linking_outputs1) = cc_common.create_linking_context_from_compilation_outputs(...)

    # Build library2
    (compilation_context2, compilation_outputs2) = cc_common.compile(...)
    (linking_context2, linking_outputs2) = cc_common.create_linking_context_from_compilation_outputs(...)

    return ??? # What to return here?

Keith Smiley

unread,
Oct 6, 2021, 1:14:35 PM10/6/21
to magnus.a...@gmail.com, bazel-discuss

magnus.a...@gmail.com

unread,
Oct 7, 2021, 3:25:33 AM10/7/21
to bazel-discuss
Thanks, I see. The solution in this example is to return a CcInfo with the merged output of the libraries.

My example was a bit too simplified. Our rule builds some libraries but also links some binaries. The libraries will be used by some rules, but the binaries will be used by other rules. I suppose that we cannot return one single CcInfo. Is is possible to return multiple CcInfo instances and later filter out which CcInfo to use where?

def _custom_rule_that_builds_two_libs_with_cc_common_compile_impl(...):
    ...
    return [CcInfo(library_output), CcInfo(binary_output)]

cc_binary(
    name = "rule_that_needs_libs",
    deps = ??? # Use the library CcInfo from above rule
)

some_other_rule(
     name = "rule_that_needs_bins",
     deps = ??? # Use the binary CcInfo from above rule
)

BR,
Magnus

Keith Smiley

unread,
Oct 7, 2021, 12:13:22 PM10/7/21
to magnus.a...@gmail.com, bazel-discuss
At the worst case you could use OutputGroupInfo and throw your individual providers in some different fields there, but you'd probably still need to propagate _some_ CcInfo by default if you want inclusion in cc_library to "just work"
--
Keith Smiley


Message has been deleted

magnus.a...@gmail.com

unread,
Oct 19, 2021, 12:55:25 PM10/19/21
to bazel-discuss
Hmm, I got stuck on this problem. May I ask for some more details on how to use OutputGroupInfo (or something else) to solve the problem in the my previous post?

Konstantin

unread,
Nov 5, 2021, 2:28:37 PM11/5/21
to bazel-discuss
    output_group_1= OutputGroupInfo(
        cc_info = CcInfo(...),
    )
    output_group_2= OutputGroupInfo(
        cc_info = CcInfo(...),
    )
...
return [
            output_group_1,
            output_group_2,
            CcInfo(), # default case.
        ]

After that you can use filegroup rule to filter "output_group_1" or "output_group_2" and make subsequent rules depend on those filtering filegroup rules.

Konstantin
Reply all
Reply to author
Forward
0 new messages