diff options
Diffstat (limited to 'src')
3 files changed, 109 insertions, 51 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/FilesetEntry.java b/src/main/java/com/google/devtools/build/lib/packages/FilesetEntry.java index 210fa1b537..eba5aba3b2 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/FilesetEntry.java +++ b/src/main/java/com/google/devtools/build/lib/packages/FilesetEntry.java @@ -14,15 +14,16 @@ package com.google.devtools.build.lib.packages; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; import com.google.devtools.build.lib.syntax.Printer; +import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.Collection; @@ -40,10 +41,18 @@ import javax.annotation.Nullable; name = "FilesetEntry", doc = "", documented = false) +@Immutable +@ThreadSafe public final class FilesetEntry implements SkylarkValue { + public static final SymlinkBehavior DEFAULT_SYMLINK_BEHAVIOR = SymlinkBehavior.COPY; + public static final String DEFAULT_STRIP_PREFIX = "."; + @Override public boolean isImmutable() { + // TODO(laszlocsomor): set this to true. I think we could do this right now, but am not sure. + // Maybe we have to verify that Skylark recognizes every member's type to be recursively + // immutable; as of 15/01/2016 this is not true for enum types in general, to name an example. return false; } @@ -64,24 +73,26 @@ public final class FilesetEntry implements SkylarkValue { @Override public void write(Appendable buffer, char quotationMark) { - Printer.append(buffer, "FilesetEntry(srcdir = "); - Printer.write(buffer, getSrcLabel().toString(), quotationMark); - Printer.append(buffer, ", files = "); - Printer.write(buffer, makeStringList(getFiles()), quotationMark); - Printer.append(buffer, ", excludes = "); - Printer.write(buffer, makeList(getExcludes()), quotationMark); - Printer.append(buffer, ", destdir = "); - Printer.write(buffer, getDestDir().getPathString(), quotationMark); - Printer.append(buffer, ", strip_prefix = "); - Printer.write(buffer, getStripPrefix(), quotationMark); - Printer.append(buffer, ", symlinks = "); - Printer.append(buffer, quotationMark); - Printer.append(buffer, getSymlinkBehavior().toString()); - Printer.append(buffer, quotationMark); - Printer.append(buffer, ")"); + Printer.append(buffer, "FilesetEntry(srcdir = "); + Printer.write(buffer, getSrcLabel().toString(), quotationMark); + Printer.append(buffer, ", files = "); + Printer.write(buffer, makeStringList(getFiles()), quotationMark); + Printer.append(buffer, ", excludes = "); + Printer.write(buffer, makeList(getExcludes()), quotationMark); + Printer.append(buffer, ", destdir = "); + Printer.write(buffer, getDestDir().getPathString(), quotationMark); + Printer.append(buffer, ", strip_prefix = "); + Printer.write(buffer, getStripPrefix(), quotationMark); + Printer.append(buffer, ", symlinks = "); + Printer.append(buffer, quotationMark); + Printer.append(buffer, getSymlinkBehavior().toString()); + Printer.append(buffer, quotationMark); + Printer.append(buffer, ")"); } /** SymlinkBehavior decides what to do when a source file of a FilesetEntry is a symlink. */ + @Immutable + @ThreadSafe public enum SymlinkBehavior { /** Just copies the symlink as-is. May result in dangling links. */ COPY, @@ -117,18 +128,19 @@ public final class FilesetEntry implements SkylarkValue { * @param stripPrefix the prefix to strip from the package-relative path. If ".", keep only the * basename. */ - public FilesetEntry(Label srcLabel, + public FilesetEntry( + Label srcLabel, @Nullable List<Label> files, - @Nullable List<String> excludes, - String destDir, - SymlinkBehavior symlinkBehavior, - String stripPrefix) { - this.srcLabel = checkNotNull(srcLabel); - this.destDir = new PathFragment((destDir == null) ? "" : destDir); + @Nullable Collection<String> excludes, + @Nullable String destDir, + @Nullable SymlinkBehavior symlinkBehavior, + @Nullable String stripPrefix) { + this.srcLabel = Preconditions.checkNotNull(srcLabel); this.files = files == null ? null : ImmutableList.copyOf(files); this.excludes = (excludes == null || excludes.isEmpty()) ? null : ImmutableSet.copyOf(excludes); - this.symlinkBehavior = symlinkBehavior; - this.stripPrefix = stripPrefix; + this.destDir = new PathFragment((destDir == null) ? "" : destDir); + this.symlinkBehavior = symlinkBehavior == null ? DEFAULT_SYMLINK_BEHAVIOR : symlinkBehavior; + this.stripPrefix = stripPrefix == null ? DEFAULT_STRIP_PREFIX : stripPrefix; } /** @@ -206,8 +218,8 @@ public final class FilesetEntry implements SkylarkValue { return "Cannot specify files with Fileset label '" + srcLabel + "'"; } else if (destDir.isAbsolute()) { return "Cannot specify absolute destdir '" + destDir + "'"; - } else if (!stripPrefix.equals(".") && files == null) { - return "If the strip prefix is not '.', files must be specified"; + } else if (!stripPrefix.equals(DEFAULT_STRIP_PREFIX) && files == null) { + return "If the strip prefix is not \"" + DEFAULT_STRIP_PREFIX + "\", files must be specified"; } else if (new PathFragment(stripPrefix).containsUplevelReferences()) { return "Strip prefix must not contain uplevel references"; } else { @@ -217,8 +229,13 @@ public final class FilesetEntry implements SkylarkValue { @Override public String toString() { - return String.format("FilesetEntry(srcdir=%s, destdir=%s, strip_prefix=%s, symlinks=%s, " - + "%d file(s) and %d excluded)", srcLabel, destDir, stripPrefix, symlinkBehavior, + return String.format( + "FilesetEntry(srcdir=%s, destdir=%s, strip_prefix=%s, symlinks=%s, " + + "%d file(s) and %d excluded)", + srcLabel, + destDir, + stripPrefix, + symlinkBehavior, files != null ? files.size() : 0, excludes != null ? excludes.size() : 0); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java index 14fdfa604d..9f735e5903 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/query2/output/XmlOutputFormatter.java @@ -127,7 +127,7 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { * - 'name' attribute is target's label. * - 'location' attribute is consistent with output of --output location. * - rule attributes are represented in the DOM structure. - * @throws InterruptedException + * @throws InterruptedException */ private Element createTargetElement(Document doc, Target target) throws InterruptedException { @@ -346,8 +346,8 @@ class XmlOutputFormatter extends AbstractUnorderedFormatter { // Fileset entries: not configurable. FilesetEntry filesetEntry = (FilesetEntry) Iterables.getOnlyElement(values); elem = doc.createElement("fileset-entry"); - elem.setAttribute("srcdir", filesetEntry.getSrcLabel().toString()); - elem.setAttribute("destdir", filesetEntry.getDestDir().toString()); + elem.setAttribute("srcdir", filesetEntry.getSrcLabel().toString()); + elem.setAttribute("destdir", filesetEntry.getDestDir().toString()); elem.setAttribute("symlinks", filesetEntry.getSymlinkBehavior().toString()); elem.setAttribute("strip_prefix", filesetEntry.getStripPrefix()); diff --git a/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java b/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java index 067fc9276f..ad3df44b6f 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java +++ b/src/test/java/com/google/devtools/build/lib/packages/BuildTypeTest.java @@ -19,10 +19,10 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; import com.google.devtools.build.lib.packages.BuildType.Selector; @@ -58,7 +58,13 @@ public class BuildTypeTest { Label srcDir = Label.create("foo", "src"); Label entryLabel = Label.create("foo", "entry"); FilesetEntry input = - new FilesetEntry(srcDir, ImmutableList.of(entryLabel), null, null, null, null); + new FilesetEntry( + /* srcLabel */ srcDir, + /* files */ ImmutableList.of(entryLabel), + /* excludes */ null, + /* destDir */ null, + /* symlinkBehavior */ null, + /* stripPrefix */ null); assertEquals(input, BuildType.FILESET_ENTRY.convert(input, null, currentRule)); assertThat(BuildType.FILESET_ENTRY.flatten(input)).containsExactly(entryLabel); } @@ -69,8 +75,20 @@ public class BuildTypeTest { Label entry1Label = Label.create("foo", "entry1"); Label entry2Label = Label.create("foo", "entry"); List<FilesetEntry> input = ImmutableList.of( - new FilesetEntry(srcDir, ImmutableList.of(entry1Label), null, null, null, null), - new FilesetEntry(srcDir, ImmutableList.of(entry2Label), null, null, null, null)); + new FilesetEntry( + /* srcLabel */ srcDir, + /* files */ ImmutableList.of(entry1Label), + /* excludes */ null, + /* destDir */ null, + /* symlinkBehavior */ null, + /* stripPrefix */ null), + new FilesetEntry( + /* srcLabel */ srcDir, + /* files */ ImmutableList.of(entry2Label), + /* excludes */ null, + /* destDir */ null, + /* symlinkBehavior */ null, + /* stripPrefix */ null)); assertEquals(input, BuildType.FILESET_ENTRY_LIST.convert(input, null, currentRule)); assertThat(BuildType.FILESET_ENTRY_LIST.flatten(input)).containsExactly(entry1Label, entry2Label); } @@ -249,9 +267,13 @@ public class BuildTypeTest { private static FilesetEntry makeFilesetEntry() { try { - return new FilesetEntry(Label.parseAbsolute("//foo:bar"), - Lists.<Label>newArrayList(), Lists.newArrayList("xyz"), "", - FilesetEntry.SymlinkBehavior.COPY, "."); + return new FilesetEntry( + /* srcLabel */ Label.parseAbsolute("//foo:bar"), + /* files */ ImmutableList.<Label>of(), + /* excludes */ ImmutableSet.of("xyz"), + /* destDir */ null, + /* symlinkBehavior */ null, + /* stripPrefix */ null); } catch (LabelSyntaxException e) { throw new RuntimeException("Bad label: ", e); } @@ -278,7 +300,12 @@ public class BuildTypeTest { throws LabelSyntaxException { Label label = Label.parseAbsolute("//x"); return new FilesetEntry( - label, Arrays.asList(label), Arrays.<String>asList(), "", symlinkBehavior, "."); + /* srcLabel */ label, + /* files */ Arrays.asList(label), + /* excludes */ null, + /* destDir */ null, + /* symlinkBehavior */ symlinkBehavior, + /* stripPrefix */ null); } private FilesetEntry createTestFilesetEntry() throws LabelSyntaxException { @@ -316,12 +343,12 @@ public class BuildTypeTest { private FilesetEntry createStripPrefixFilesetEntry(String stripPrefix) throws Exception { Label label = Label.parseAbsolute("//x"); return new FilesetEntry( - label, - Arrays.asList(label), - Arrays.<String>asList(), - "", - FilesetEntry.SymlinkBehavior.DEREFERENCE, - stripPrefix); + /* srcLabel */ label, + /* files */ Arrays.asList(label), + /* excludes */ null, + /* destDir */ null, + /* symlinkBehavior */ FilesetEntry.SymlinkBehavior.DEREFERENCE, + /* stripPrefix */ stripPrefix); } @Test @@ -338,10 +365,24 @@ public class BuildTypeTest { @Test public void testPrintFilesetEntry() throws Exception { - assertEquals("FilesetEntry(srcdir = \"//foo:bar\", files = [], " - + "excludes = [\"xyz\"], destdir = \"\", " - + "strip_prefix = \".\", symlinks = \"copy\")", - Printer.repr(makeFilesetEntry())); + assertThat( + Printer.repr( + new FilesetEntry( + /* srcLabel */ Label.parseAbsolute("//foo:BUILD"), + /* files */ ImmutableList.of(Label.parseAbsolute("//foo:bar")), + /* excludes */ ImmutableSet.of("baz"), + /* destDir */ "qux", + /* symlinkBehavior */ FilesetEntry.SymlinkBehavior.DEREFERENCE, + /* stripPrefix */ "blah"))) + .isEqualTo( + Joiner.on(" ").join( + ImmutableList.of( + "FilesetEntry(srcdir = \"//foo:BUILD\",", + "files = [\"//foo:bar\"],", + "excludes = [\"baz\"],", + "destdir = \"qux\",", + "strip_prefix = \"blah\",", + "symlinks = \"dereference\")"))); } @Test |