Skip to content

Commit

Permalink
cquery --show_config_fragments: report --define requirements more pre…
Browse files Browse the repository at this point in the history
…cisely.

Before this change, any reliance on --define a=foo reports a dependency on the
"CoreOptions" fragment. While true, that's too broad: it includes *all*
--defines, including ones having nothing to do with "a".

This change reports --define like user-defined flags (which it essentially
is a special-case version of): return the specific --define instead. In the
above example that'd be --define:a (using a syntax that's easy enough to read
while being reasonably parseable as a no-spaces token).

This only works for select(). It doesn't include MAKE variables, e.g.:
https://github.com/bazelbuild/bazel/blob/6bd6cc4435e722393127c72189dbb646149d844b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java#L610-L621. That
requires its own
analysis.

Prereq work for #10613.

PiperOrigin-RevId: 292350812
  • Loading branch information
gregestren authored and copybara-github committed Jan 30, 2020
1 parent c89dc1c commit 1e4b997
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,23 @@ private static boolean matchesConfig(
foundMismatch = true;
continue;
}
requiredFragmentOptions.add(ClassName.getSimpleNameWithOuter(optionClass));

requiredFragmentOptions.add(
optionName.equals("define")
// --define is more like user-defined build flags than traditional native flags.
// Report it
// like user-defined flags: the dependency is directly on the flag vs. the fragment
// that
// contains the flag. This frees a rule that depends on "--define a=1" from preserving
// another rule's dependency on "--define b=2". In other words, if both rules simply
// said
// "I require CoreOptions" (which is the FragmentOptions --define belongs to), that
// would
// hide the reality that they really have orthogonal dependencies: removing
// "--define b=2" is perfectly safe for the rule that needs "--define a=1".
? "--define:" + expectedRawValue.substring(0, expectedRawValue.indexOf('='))
// For other native flags, it's reasonable to report the fragment they belong to.
: ClassName.getSimpleNameWithOuter(optionClass));

SelectRestriction selectRestriction = options.getSelectRestriction(optionName);
if (selectRestriction != null) {
Expand Down
26 changes: 26 additions & 0 deletions src/test/shell/integration/configured_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,32 @@ EOF
assert_not_contains "//$pkg:foo_feature .*//$pkg:foo_feature" output
}

function test_show_config_fragments_on_define() {
local -r pkg=$FUNCNAME
mkdir -p $pkg
cat > $pkg/BUILD <<EOF
config_setting(
name = "is_a_on",
define_values = {"a": "on"}
)
cc_library(
name = "cclib_with_select",
srcs = select({
":is_a_on": ["version1.cc"],
"//conditions:default": ["version2.cc"],
})
)
EOF

bazel cquery "//$pkg:all" --show_config_fragments=direct --define a=on \
--define b=on > output 2>"$TEST_log" || fail "Expected success"

assert_contains "//$pkg:cclib_with_select .*CppConfiguration" output
assert_contains "//$pkg:cclib_with_select .*--define:a" output
assert_not_contains "//$pkg:cclib_with_select .*--define:b" output
}

function test_manual_tagged_targets_always_included_for_queries() {
local -r pkg=$FUNCNAME
mkdir -p $pkg
Expand Down

0 comments on commit 1e4b997

Please sign in to comment.