Add native.rule(NAME), which returns the attributes of a previously defined rule.
Add native.rules(), which returns all previously defined rules.
These primitives can be used to write Skylark extensions that aggregate over the contents of a BUILD file, eg.
def instantiate_if_needed(name):
n = name + "_wrapped"
if not native.rule(n):
py_test(name = n , ... )
def archive_cc_src_files(tag):
all_src = []
for r in native.rules().values():
if tag in r["tags"] and r["kind"] == "cc_library":
all_src.append(r["srcs"])
native.genrule(cmd = "zip $@ $^", srcs = all_src, outs = ["out.zip"])
RELNOTES: Support aggregation over existing rules in Skylark extensions
through native.rules and native.rule.
--
MOS_MIGRATED_REVID=112249050
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java
index bc78044..ce49383 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Package.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java
@@ -49,6 +49,8 @@
import java.util.Map;
import java.util.Set;
+import javax.annotation.Nullable;
+
/**
* A package, which is a container of {@link Rule}s, each of
* which contains a dictionary of named attributes.
@@ -1045,6 +1047,11 @@
return Package.getTargets(targets);
}
+ @Nullable
+ public Target getTarget(String name) {
+ return targets.get(name);
+ }
+
/**
* Returns an (immutable, unordered) view of all the targets belonging to
* this package which are instances of the specified class.