Skip to content

Commit dc41a20

Browse files
katrekkressGoogler
authored
[5.1.0] cherrypick subpackages support (bazelbuild#14780)
* Split ConfiguredAttributeMapper into a new build target. PiperOrigin-RevId: 411055066 * Part 1 of the Implementation for new 'subpackages()` built-in helper function. Design proposal: https://docs.google.com/document/d/13UOT0GoQofxDW40ILzH2sWpUOmuYy6QZ7CUmhej9vgk/edit# This CL modifies the globber infrastructure to support an additional mode of listing sub-directories. * Add new Globber Operation enum allowing, Globber implementations to discriminate between glob, glob w/directories and the future sub-packages use-case. * Modify UnixGlob to replace Predicate and bools with UnixGlobPathDiscriminator interface for: a) Determining whether to traverse a sub-directory (previously was lambda) b) function for determing what entries to include in the List<Path> produced by UnixGlob.globAsync. These allow relatively simple re-use of the same logic for both subpackages and glob 4) Add a few tests for UnixGlob to ensure both cases continue to work as expected. PiperOrigin-RevId: 421125424 * Part 2 Implementation for new 'subpackages()` built-in helper function. Design proposal: https://docs.google.com/document/d/13UOT0GoQofxDW40ILzH2sWpUOmuYy6QZ7CUmhej9vgk/edit# Overview: Add StarlarkNativeModule 'subpackages' function with parameters that mirror glob() PiperOrigin-RevId: 422652954 * Fix some typographical errors in the 'subpackages' docs. PiperOrigin-RevId: 425942284 Co-authored-by: kkress <[email protected]> Co-authored-by: Googler <[email protected]>
1 parent a58ddea commit dc41a20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1674
-471
lines changed

src/main/java/com/google/devtools/build/docgen/templates/be/functions.vm

+42-35
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ title: Functions
1919
<li><a href="#exports_files">exports_files</a></li>
2020
<li><a href="#glob">glob</a></li>
2121
<li><a href="#select">select</a></li>
22+
<li><a href="#subpackages">subpackages</a></li>
2223
</ul>
2324
</div>
2425
#end
@@ -636,48 +637,54 @@ sh_binary(
636637
<li><code>select</code> works with most, but not all, attributes. Incompatible
637638
attributes are marked <code>nonconfigurable</code> in their documentation.
638639

639-
</li>
640-
</ul>
641-
642-
By default, Bazel produces the following error when no conditions match:
643-
<pre class="code">
644-
Configurable attribute "foo" doesn't match this configuration (would a default
645-
condition help?).
646-
Conditions checked:
647-
//pkg:conditionA.
648-
//pkg:conditionB.
649-
</pre>
640+
<!-- =================================================================
641+
subpackages()
642+
=================================================================
643+
-->
650644

651-
You can signal more precise errors with <code>no_match_error</code>.
645+
<h2 id="subpackages">subpackages</h2>
652646

653-
<h3 id="select_example">Examples</h3>
647+
<pre>subpackages(include, exclude=[], allow_empty=True)</pre>
654648

655-
<pre class="code">
656-
config_setting(
657-
name = "windows",
658-
values = {
659-
"crosstool_top": "//crosstools/windows",
660-
},
661-
)
649+
<p>
650+
<code>subpackages()</code> is a helper function, similar to <code>glob()</code>
651+
that lists subpackages instead of files and directories. It uses the same
652+
path patterns as <code>glob()</code> and can match any subpackage that is a
653+
direct decendant of the currently loading BUILD file. See <a
654+
href="#glob">glob</a> for a detailed explanation and examples of include and
655+
exclude patterns.
656+
</p>
662657

663-
cc_binary(
664-
name = "multiplatform_app",
665-
...
666-
linkopts = select({
667-
":windows": [
668-
"-Wl,windows_support1.lib",
669-
"-Wl,windows_support2.lib",
670-
],
671-
"//conditions:default": [],
672-
...
673-
)
674-
</pre>
658+
<p>
659+
The resulting list of subpackages returned is in sorted order and contains
660+
paths relative to the current loading package that match the given patterns in
661+
<code>include</code> and not those in <code>exclude</code>.
675662

676-
<p>In the above example, <code>multiplatform_app</code> links with additional
677-
options when invoked with <code>bazel build //pkg:multiplatform_app
678-
--crosstool_top=//crosstools/windows </code>.
663+
<h3 id=subpackages_example">Example</h3>
679664

680665
<p>
666+
The following example lists all the direct subpackages for the package <code>foo/BUILD</code>
667+
668+
<pre class="code">
669+
# The following BUILD files exist:
670+
# foo/BUILD
671+
# foo/bar/baz/BUILD
672+
# foo/sub/BUILD
673+
# foo/sub/deeper/BUILD
674+
#
675+
# In foo/BUILD a call to
676+
subs = subpackages(include = ["**"])
677+
678+
# results in subs == ["sub", "bar/baz"]
679+
#
680+
# 'sub/deeper' is not included because it is a subpackage of 'foo/sub' not of
681+
# 'foo'
682+
</pre>
683+
684+
<p>
685+
In general it is preferred that instead of calling this function directly
686+
that users use the 'subpackages' module of
687+
<a href="https://github.com/bazelbuild/bazel-skylib">skylib</a>.
681688

682689
#if (!$singlePage)
683690
#parse("com/google/devtools/build/docgen/templates/be/footer.vm")

src/main/java/com/google/devtools/build/lib/analysis/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ java_library(
400400
"//src/main/java/com/google/devtools/build/lib/exec:test_xml_output_parser_exception",
401401
"//src/main/java/com/google/devtools/build/lib/graph",
402402
"//src/main/java/com/google/devtools/build/lib/packages",
403+
"//src/main/java/com/google/devtools/build/lib/packages:configured_attribute_mapper",
403404
"//src/main/java/com/google/devtools/build/lib/packages:exec_group",
404405
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
405406
"//src/main/java/com/google/devtools/build/lib/profiler",

src/main/java/com/google/devtools/build/lib/includescanning/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ java_library(
3333
"//src/main/java/com/google/devtools/build/lib/exec:module_action_context_registry",
3434
"//src/main/java/com/google/devtools/build/lib/exec:spawn_strategy_resolver",
3535
"//src/main/java/com/google/devtools/build/lib/packages",
36+
"//src/main/java/com/google/devtools/build/lib/packages:globber",
3637
"//src/main/java/com/google/devtools/build/lib/profiler",
3738
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
3839
"//src/main/java/com/google/devtools/build/lib/skyframe:containing_package_lookup_value",

src/main/java/com/google/devtools/build/lib/includescanning/IncludeParser.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.devtools.build.lib.concurrent.BlazeInterners;
3939
import com.google.devtools.build.lib.events.Event;
4040
import com.google.devtools.build.lib.includescanning.IncludeParser.Inclusion.Kind;
41+
import com.google.devtools.build.lib.packages.Globber;
4142
import com.google.devtools.build.lib.packages.NoSuchPackageException;
4243
import com.google.devtools.build.lib.profiler.Profiler;
4344
import com.google.devtools.build.lib.profiler.ProfilerTask;
@@ -347,7 +348,7 @@ ImmutableSet<Artifact> getPathLevelHintedInclusions(
347348
containingPackageLookupValue.getContainingPackageName(),
348349
containingPackageLookupValue.getContainingPackageRoot(),
349350
pattern,
350-
/*excludeDirs=*/ true,
351+
Globber.Operation.FILES,
351352
relativePath.relativeTo(packageFragment)));
352353
} catch (InvalidGlobPatternException e) {
353354
env.getListener()

src/main/java/com/google/devtools/build/lib/packages/BUILD

+31-1
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,51 @@ filegroup(
1010
visibility = ["//src:__subpackages__"],
1111
)
1212

13+
java_library(
14+
name = "configured_attribute_mapper",
15+
srcs = ["ConfiguredAttributeMapper.java"],
16+
deps = [
17+
":packages",
18+
"//src/main/java/com/google/devtools/build/lib/analysis:config/config_matching_provider",
19+
"//src/main/java/com/google/devtools/build/lib/cmdline",
20+
"//third_party:guava",
21+
"//third_party:jsr305",
22+
],
23+
)
24+
25+
java_library(
26+
name = "globber",
27+
srcs = ["Globber.java"],
28+
)
29+
30+
java_library(
31+
name = "globber_utils",
32+
srcs = ["GlobberUtils.java"],
33+
deps = [
34+
":globber",
35+
"//third_party:error_prone_annotations",
36+
],
37+
)
38+
1339
java_library(
1440
name = "packages",
1541
srcs = glob(
1642
["*.java"],
1743
exclude = [
1844
"BuilderFactoryForTesting.java", # see builder_factory_for_testing
45+
"Globber.java",
46+
"GlobberUtils.java",
1947
"ExecGroup.java",
48+
"ConfiguredAttributeMapper.java",
2049
],
2150
),
2251
deps = [
2352
":exec_group",
53+
":globber",
54+
":globber_utils",
2455
"//src/main/java/com/google/devtools/build/docgen/annot",
2556
"//src/main/java/com/google/devtools/build/lib/actions:execution_requirements",
2657
"//src/main/java/com/google/devtools/build/lib/actions:thread_state_receiver",
27-
"//src/main/java/com/google/devtools/build/lib/analysis:config/config_matching_provider",
2858
"//src/main/java/com/google/devtools/build/lib/analysis:config/fragment",
2959
"//src/main/java/com/google/devtools/build/lib/analysis:config/fragment_class_set",
3060
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/configuration_transition",

0 commit comments

Comments
 (0)