Skip to content

Commit

Permalink
Add link-group-mappings subtarget for shared library file to generate…
Browse files Browse the repository at this point in the history
… the summary for the specified link group.

Summary:
Currently we have a subtarget that prints dependencies of link groups:

```
christylee@devbig394:~/fbsource/fbcode(47c0c0319)$ buck2 build @//mode/opt-lg tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:main_bin[link-group-mappings] --show-output
Buck UI: https://www.internalfb.com/buck2/be198590-6945-4e63-8faa-bfe68322ec9a
Network: Up: 0B  Down: 0B
Jobs completed: 4. Time elapsed: 0.0s.
BUILD SUCCEEDED
fbcode//tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:main_bin[link-group-mappings] buck-out/v2/gen/fbcode/c6f87ea505222064/tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test/__main_bin__/main_bin.link_group_map.json

christylee@devbig394:~/fbsource(87742e131|remote/master)$ cat buck-out/v2/gen/fbcode/c6f87ea505222064/tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test/__main_bin__/main_bin.link_group_map.json | jq
{
  "NO_MATCH": [
    "fbcode//common/init:kill"
  ],
  "shared_lib_2": [
    "fbcode//tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:D"
  ],
  "shared_lib_1": [
    "fbcode//tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:A",
    "fbcode//third-party-buck/platform010/build/libgcc:stdc++-legacy",
    "fbcode//third-party-buck/platform010/build/libgcc:stdc++",
    "fbcode//third-party-buck/platform010/build/libgcc:gcc_s",
    "fbcode//third-party-buck/platform010/build/libgcc:atomic",
    "fbcode//third-party-buck/platform010/build/openmp:headers",
    "fbcode//third-party-buck/platform010/build/glibc:pthread",
    "fbcode//third-party-buck/platform010/build/glibc:m",
    "fbcode//third-party-buck/platform010/build/glibc:mvec",
    "fbcode//third-party-buck/platform010/build/glibc:c",
    "fbcode//tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:C"
  ]
}
```

This gets unwieldy when we have a sufficiently large number of link groups. This diff creates a subtarget for each link group shared library that prints the dependencies that compose of that shared library. We want something that looks like this:

```
buck2 build @//mode/opt-lg tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:main_bin[shared-libraries][libshared_lib_2.so][link-group-mappings]

{
  "shared_lib_2": [
    "fbcode//tools/build/test/cpp/link_groups/cpp_rule_link_group_basic_test:D"
  ],
}
```

Reviewed By: christycylee

Differential Revision: D61422475

fbshipit-source-id: 4bebf87b304b398177adfe5e473874c706c12315
  • Loading branch information
Zhixuan Huan authored and facebook-github-bot committed Aug 24, 2024
1 parent 749c7ea commit 9beff40
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions prelude/cxx/cxx_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ load(
"PDB_SUB_TARGET",
"get_dumpbin_providers",
"get_pdb_providers",
"get_shared_library_name",
)
load(
":preprocessor.bzl",
Expand Down Expand Up @@ -580,34 +581,13 @@ def cxx_executable(ctx: AnalysisContext, impl_params: CxxRuleConstructorParams,
for shlib in shared_libs
if shlib.soname.is_str()
}
sub_targets["shared-libraries"] = [DefaultInfo(
default_output = ctx.actions.write_json(
binary.output.basename + ".shared-libraries.json",
{
"libraries": [
"{}:{}[shared-libraries][{}]".format(ctx.label.path, ctx.label.name, soname)
for soname in str_soname_shlibs
],
"librariesdwp": [
"{}:{}[shared-libraries][{}][dwp]".format(ctx.label.path, ctx.label.name, soname)
for soname, shlib in str_soname_shlibs.items()
if shlib.lib.dwp
],
"rpathtree": ["{}:{}[rpath-tree]".format(ctx.label.path, ctx.label.name)] if shared_libs_symlink_tree else [],
},
),
sub_targets = {
soname: [DefaultInfo(
default_output = shlib.lib.output,
sub_targets = {"dwp": [DefaultInfo(default_output = shlib.lib.dwp)]} if shlib.lib.dwp else {},
)]
for soname, shlib in str_soname_shlibs.items()
},
)]

readable_mappings = {}
soname_to_group_mappings = {}
if link_group_mappings:
readable_mappings = {}
for node, group in link_group_mappings.items():
soname = get_shared_library_name(linker_info, group, True)
soname_to_group_mappings[soname] = group
readable_mappings[group] = readable_mappings.get(group, []) + ["{}//{}:{}".format(node.cell, node.package, node.name)]

sub_targets[LINK_GROUP_MAPPINGS_SUB_TARGET] = [DefaultInfo(
Expand All @@ -627,6 +607,43 @@ def cxx_executable(ctx: AnalysisContext, impl_params: CxxRuleConstructorParams,
),
)]

shared_libraries_sub_targets = {}
for soname, shlib in str_soname_shlibs.items():
targets = {"dwp": [DefaultInfo(default_output = shlib.lib.dwp)]} if shlib.lib.dwp else {}

group = soname_to_group_mappings.get(soname)
if group in readable_mappings:
output_json_file = binary.output.basename + "." + group + LINK_GROUP_MAPPINGS_FILENAME_SUFFIX
targets[LINK_GROUP_MAPPINGS_SUB_TARGET] = [DefaultInfo(
default_output = ctx.actions.write_json(
output_json_file,
{group: readable_mappings[group]},
),
)]
shared_libraries_sub_targets[soname] = [DefaultInfo(
default_output = shlib.lib.output,
sub_targets = targets,
)]

sub_targets["shared-libraries"] = [DefaultInfo(
default_output = ctx.actions.write_json(
binary.output.basename + ".shared-libraries.json",
{
"libraries": [
"{}:{}[shared-libraries][{}]".format(ctx.label.path, ctx.label.name, soname)
for soname in str_soname_shlibs
],
"librariesdwp": [
"{}:{}[shared-libraries][{}][dwp]".format(ctx.label.path, ctx.label.name, soname)
for soname, shlib in str_soname_shlibs.items()
if shlib.lib.dwp
],
"rpathtree": ["{}:{}[rpath-tree]".format(ctx.label.path, ctx.label.name)] if shared_libs_symlink_tree else [],
},
),
sub_targets = shared_libraries_sub_targets,
)]

# If we have some resources, write it to the resources JSON file and add
# it and all resources to "runtime_files" so that we make to materialize
# them with the final binary.
Expand Down

0 comments on commit 9beff40

Please sign in to comment.