-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sometimes users don't want to follow the DWYU rules for all targets or have to work with external dependencies not following the DWYU principles. For such cases DWYU allows now creating a mapping which for a chosen target makes more headers available as the target actually provides. In other words, one can combine virtually for the DWYU analysis multiple targets. Doing so allows using headers from transitive dependencies without DWYU raising an error for select cases.
- Loading branch information
Showing
21 changed files
with
418 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
load("@depend_on_what_you_use//src/cc_info_mapping/private:direct_deps.bzl", "mapping_to_direct_deps") | ||
load("@depend_on_what_you_use//src/cc_info_mapping/private:explicit.bzl", "explicit_mapping") | ||
load("@depend_on_what_you_use//src/cc_info_mapping/private:providers.bzl", "DwyuCcInfoRemapInfo") | ||
load("@depend_on_what_you_use//src/cc_info_mapping/private:transitive_deps.bzl", "mapping_to_transitive_deps") | ||
load("@depend_on_what_you_use//src/utils:utils.bzl", "label_to_name") | ||
|
||
MAP_DIRECT_DEPS = "__DWYU_MAP_DIRECT_DEPS__" | ||
MAP_TRANSITIVE_DEPS = "__DWYU_MAP_TRANSITIVE_DEPS__" | ||
|
||
DwyuCcInfoRemappingsInfo = provider( | ||
"Dictionary of targets labels wnd which CcInfo provider DWYU should use for analysing them.", | ||
fields = { | ||
"mapping": "Dictionary with structure {'target label': CcInfo provider which should be used by DWYU}.", | ||
}, | ||
) | ||
|
||
def _make_remapping_info_impl(ctx): | ||
return DwyuCcInfoRemappingsInfo(mapping = { | ||
remap[DwyuCcInfoRemapInfo].target: remap[DwyuCcInfoRemapInfo].cc_info | ||
for remap in ctx.attr.remappings | ||
}) | ||
|
||
_make_remapping_info = rule( | ||
implementation = _make_remapping_info_impl, | ||
provides = [DwyuCcInfoRemappingsInfo], | ||
attrs = { | ||
"remappings": attr.label_list(providers = [DwyuCcInfoRemapInfo]), | ||
}, | ||
) | ||
|
||
def dwyu_make_cc_info_mapping(name, mapping): | ||
""" | ||
Create a mapping which allows treating targets as if they themselves would offer header files, which in fact are | ||
coming from their dependencies. This enables the DWYU analysis to skip over some usage of headers provided by | ||
transitive dependencies without raising an error. | ||
Args: | ||
name: Unique name for this target. Will be the prefix for all private intermediate targets. | ||
mapping: Dictionary containing various targets and how they should be mapped. Possible mappings are: | ||
- An explicit list of targets which are mapped to the main target. Be careful only to choose targets | ||
which are dependencies of the main target! | ||
- The MAP_DIRECT_DEPS token which tells the rule to map all direct dependencies to the main target. | ||
- The MAP_TRANSITIVE_DEPS token which tells the rule to map recursively all transitive dependencies to | ||
the main target. | ||
""" | ||
mappings = [] | ||
for target, map_to in mapping.items(): | ||
mapping_action = "{}_mapping_{}".format(name, label_to_name(target)) | ||
if map_to == MAP_DIRECT_DEPS: | ||
mapping_to_direct_deps( | ||
name = mapping_action, | ||
target = target, | ||
) | ||
elif map_to == MAP_TRANSITIVE_DEPS: | ||
mapping_to_transitive_deps( | ||
name = mapping_action, | ||
target = target, | ||
) | ||
else: | ||
explicit_mapping( | ||
name = mapping_action, | ||
target = target, | ||
map_to = map_to, | ||
) | ||
mappings.append(mapping_action) | ||
|
||
_make_remapping_info( | ||
name = name, | ||
remappings = mappings, | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
load(":providers.bzl", "DwyuCcInfoRemapInfo") | ||
|
||
def _aggregate_direct_deps_aspect_impl(target, ctx): | ||
aggregated_compilation_context = cc_common.merge_compilation_contexts( | ||
compilation_contexts = | ||
[tgt[CcInfo].compilation_context for tgt in [target] + ctx.rule.attr.deps], | ||
) | ||
|
||
return DwyuCcInfoRemapInfo(target = target.label, cc_info = CcInfo( | ||
compilation_context = aggregated_compilation_context, | ||
linking_context = target[CcInfo].linking_context, | ||
)) | ||
|
||
_aggregate_direct_deps_aspect = aspect( | ||
implementation = _aggregate_direct_deps_aspect_impl, | ||
provides = [DwyuCcInfoRemapInfo], | ||
attr_aspects = [], | ||
) | ||
|
||
def _mapping_to_direct_deps_impl(ctx): | ||
return ctx.attr.target[DwyuCcInfoRemapInfo] | ||
|
||
mapping_to_direct_deps = rule( | ||
implementation = _mapping_to_direct_deps_impl, | ||
provides = [DwyuCcInfoRemapInfo], | ||
attrs = { | ||
"target": attr.label(aspects = [_aggregate_direct_deps_aspect], providers = [CcInfo]), | ||
}, | ||
doc = """ | ||
Make headers from all direct dependencies available as if they where provided by the main target itself. | ||
We do so by merging the compilation_context information from the direct dependencies into the main target's CcInfo. | ||
We explicitly ignore implementation_deps, as allowing to map them would break their design of not being visible to users of the target. | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
load(":providers.bzl", "DwyuCcInfoRemapInfo") | ||
|
||
def _explicit_mapping_impl(ctx): | ||
aggregated_compilation_context = cc_common.merge_compilation_contexts( | ||
compilation_contexts = | ||
[tgt[CcInfo].compilation_context for tgt in [ctx.attr.target] + ctx.attr.map_to], | ||
) | ||
|
||
return DwyuCcInfoRemapInfo(target = ctx.attr.target.label, cc_info = CcInfo( | ||
compilation_context = aggregated_compilation_context, | ||
linking_context = ctx.attr.target[CcInfo].linking_context, | ||
)) | ||
|
||
explicit_mapping = rule( | ||
implementation = _explicit_mapping_impl, | ||
provides = [DwyuCcInfoRemapInfo], | ||
attrs = { | ||
"map_to": attr.label_list(providers = [CcInfo]), | ||
"target": attr.label(providers = [CcInfo]), | ||
}, | ||
doc = """ | ||
Make headers from all explicitly listed targets available as if they where provided by the main target itself. | ||
We do so by merging the compilation_context information from the listed targets into the main target's CcInfo. | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DwyuCcInfoRemapInfo = provider( | ||
"An alternative CcInfo object for a target which can be used by DWYU during the analysis.", | ||
fields = { | ||
"cc_info": "CcInfo provider.", | ||
"target": "Label of target which should use the cc_info object.", | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
load(":providers.bzl", "DwyuCcInfoRemapInfo") | ||
|
||
def _aggregate_transitive_deps_aspect_impl(target, ctx): | ||
all_cc_info = [target[CcInfo]] | ||
all_cc_info.extend([dep[DwyuCcInfoRemapInfo].cc_info for dep in ctx.rule.attr.deps]) | ||
aggregated_compilation_context = cc_common.merge_compilation_contexts( | ||
compilation_contexts = [cci.compilation_context for cci in all_cc_info], | ||
) | ||
|
||
return DwyuCcInfoRemapInfo(target = target.label, cc_info = CcInfo( | ||
compilation_context = aggregated_compilation_context, | ||
linking_context = target[CcInfo].linking_context, | ||
)) | ||
|
||
_aggregate_transitive_deps_aspect = aspect( | ||
implementation = _aggregate_transitive_deps_aspect_impl, | ||
provides = [DwyuCcInfoRemapInfo], | ||
attr_aspects = ["deps"], | ||
) | ||
|
||
def _mapping_to_transitive_deps_impl(ctx): | ||
return ctx.attr.target[DwyuCcInfoRemapInfo] | ||
|
||
mapping_to_transitive_deps = rule( | ||
implementation = _mapping_to_transitive_deps_impl, | ||
provides = [DwyuCcInfoRemapInfo], | ||
attrs = { | ||
"target": attr.label(aspects = [_aggregate_transitive_deps_aspect], providers = [CcInfo]), | ||
}, | ||
doc = """ | ||
Make headers from all transitive dependencies available as if they where provided by the main target itself. | ||
We do so by recursively merging the compilation_context information from the dependencies into the main target's CcInfo. | ||
We explicitly ignore implementation_deps, as allowing to map them would break their design of not being visible to users of the target. | ||
""", | ||
) |
Oops, something went wrong.