aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/repository
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/repository')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/Bind.java135
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/BindRule.java135
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryFunction.java96
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryRule.java87
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryFunction.java71
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryRule.java132
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java112
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java384
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceBaseRule.java41
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceConfiguredTargetFactory.java37
10 files changed, 1230 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/Bind.java b/src/main/java/com/google/devtools/build/lib/rules/repository/Bind.java
new file mode 100644
index 0000000000..db927bc746
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/Bind.java
@@ -0,0 +1,135 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.common.collect.ImmutableCollection;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.UnmodifiableIterator;
+import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.FileProvider;
+import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.SkylarkProviders;
+import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
+import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
+import com.google.devtools.build.lib.collect.nestedset.Order;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
+import com.google.devtools.build.lib.syntax.ClassObject;
+import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
+
+/**
+ * Implementation for the bind rule.
+ */
+public class Bind implements RuleConfiguredTargetFactory {
+
+ /**
+ * This configured target pretends to be whatever type of target "actual" is, returning its
+ * transitive info providers and target, but returning the label for the //external target.
+ */
+ private static class BindConfiguredTarget implements ConfiguredTarget, ClassObject {
+
+ private Label label;
+ private ConfiguredTarget configuredTarget;
+ private BuildConfiguration config;
+
+ BindConfiguredTarget(RuleContext ruleContext) {
+ label = ruleContext.getRule().getLabel();
+ config = ruleContext.getConfiguration();
+ // TODO(bazel-team): we should special case ConfiguredTargetFactory.createConfiguredTarget,
+ // not cast down here.
+ configuredTarget = (ConfiguredTarget) ruleContext.getPrerequisite("actual", Mode.TARGET);
+ }
+
+ @Override
+ public <P extends TransitiveInfoProvider> P getProvider(Class<P> provider) {
+ return configuredTarget == null ? null : configuredTarget.getProvider(provider);
+ }
+
+ @Override
+ public Label getLabel() {
+ return label;
+ }
+
+ @Override
+ public Object get(String providerKey) {
+ return configuredTarget == null ? null : configuredTarget.get(providerKey);
+ }
+
+ @Override
+ public UnmodifiableIterator<TransitiveInfoProvider> iterator() {
+ return configuredTarget == null
+ ? ImmutableList.<TransitiveInfoProvider>of().iterator()
+ : configuredTarget.iterator();
+ }
+
+ @Override
+ public Target getTarget() {
+ return configuredTarget == null ? null : configuredTarget.getTarget();
+ }
+
+ @Override
+ public BuildConfiguration getConfiguration() {
+ return config;
+ }
+
+ /* ClassObject methods */
+
+ @Override
+ public Object getValue(String name) {
+ if (name.equals("label")) {
+ return getLabel();
+ } else if (name.equals("files")) {
+ // A shortcut for files to build in Skylark. FileConfiguredTarget and RunleConfiguredTarget
+ // always has FileProvider and Error- and PackageGroupConfiguredTarget-s shouldn't be
+ // accessible in Skylark.
+ return SkylarkNestedSet.of(Artifact.class, configuredTarget == null
+ ? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER)
+ : getProvider(FileProvider.class).getFilesToBuild());
+ }
+ return configuredTarget == null ? null : configuredTarget.get(name);
+ }
+
+ @SuppressWarnings("cast")
+ @Override
+ public ImmutableCollection<String> getKeys() {
+ ImmutableList.Builder<String> result = ImmutableList.<String>builder().add("label", "files");
+ if (configuredTarget != null) {
+ result.addAll(
+ configuredTarget.getProvider(SkylarkProviders.class).getKeys());
+ }
+ return result.build();
+ }
+
+ @Override
+ public String errorMessage(String name) {
+ // Use the default error message.
+ return null;
+ }
+ }
+
+ @Override
+ public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException {
+ if (ruleContext.getPrerequisite("actual", Mode.TARGET) == null) {
+ ruleContext.ruleError(String.format("The external label '%s' is not bound to anything",
+ ruleContext.getLabel()));
+ return null;
+ }
+ return new BindConfiguredTarget(ruleContext);
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/BindRule.java b/src/main/java/com/google/devtools/build/lib/rules/repository/BindRule.java
new file mode 100644
index 0000000000..ff47bc06f7
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/BindRule.java
@@ -0,0 +1,135 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import static com.google.devtools.build.lib.packages.Attribute.attr;
+import static com.google.devtools.build.lib.packages.BuildType.LABEL;
+
+import com.google.devtools.build.lib.analysis.BaseRuleClasses.BaseRule;
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
+import com.google.devtools.build.lib.util.FileTypeSet;
+
+/**
+ * Binds an existing target to a target in the virtual //external package.
+ */
+public final class BindRule implements RuleDefinition {
+
+ @Override
+ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
+ return builder
+ /* <!-- #BLAZE_RULE(bind).ATTRIBUTE(actual) -->
+ The target to be aliased.
+ ${SYNOPSIS}
+
+ <p>This target must exist, but can be any type of rule (including bind).</p>
+
+ <p>If this attribute is omitted, rules referring to this target in <code>//external</code>
+ will simply not see this dependency edge. Note that this is different from omitting the
+ <code>bind</code> rule completely: it is an error if an <code>//external</code> dependency
+ does not have an associated <code>bind</code> rule.
+ </p>
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+ .add(attr("actual", LABEL).allowedFileTypes(FileTypeSet.ANY_FILE))
+ .setWorkspaceOnly()
+ .build();
+ }
+
+ @Override
+ public Metadata getMetadata() {
+ return RuleDefinition.Metadata.builder()
+ .name("bind")
+ .type(RuleClassType.WORKSPACE)
+ .ancestors(BaseRule.class)
+ .factoryClass(Bind.class)
+ .build();
+ }
+}
+/*<!-- #BLAZE_RULE (NAME = bind, TYPE = OTHER, FAMILY = Workspace)[GENERIC_RULE] -->
+
+${ATTRIBUTE_SIGNATURE}
+
+<p>Gives a target an alias in the <code>//external</code> package.</p>
+
+${ATTRIBUTE_DEFINITION}
+
+<p>The <code>//external</code> package is not a "normal" package: there is no external/ directory,
+ so it can be thought of as a "virtual package" that contains all bound targets.</p>
+
+<h4 id="bind_examples">Examples</h4>
+
+<p>To give a target an alias, <code>bind</code> it in the <i>WORKSPACE</i> file. For example,
+ suppose there is a <code>java_library</code> target called
+ <code>//third_party/javacc-v2</code>. This can be aliased by adding the following to the
+ <i>WORKSPACE</i> file:</p>
+
+<pre class="code">
+bind(
+ name = "javacc-latest",
+ actual = "//third_party/javacc-v2",
+)
+</pre>
+
+<p>Now targets can depend on <code>//external:javacc-latest</code> instead of
+ <code>//third_party/javacc-v2</code>. If javacc-v3 is released, the <code>bind</code> rule can be
+ updated and all of the BUILD files depending on <code>//external:javacc-latest</code> will now
+ depend on javacc-v3 without needing to be edited.</p>
+
+<p>Bind can also be used to make targets in external repositories available to your workspace.
+ For example, if there is a remote repository named <code>@my-ssl</code> imported in the
+ <i>WORKSPACE</i> file and it has a cc_library target <code>//src:openssl-lib</code>, you can
+ create an alias for this target using <code>bind</code>:</p>
+
+<pre class="code">
+bind(
+ name = "openssl",
+ actual = "@my-ssl//src:openssl-lib",
+)
+</pre>
+
+<p>Then, in a BUILD file in your workspace, the bound target can be used as follows:</p>
+
+<pre class="code">
+cc_library(
+ name = "sign-in",
+ srcs = ["sign_in.cc"],
+ hdrs = ["sign_in.h"],
+ deps = ["//external:openssl"],
+)
+</pre>
+
+<p>Within <code>sign_in.cc</code> and <code>sign_in.h</code>, the header files exposed by
+ <code>//external:openssl</code> can be referred to using their path relative to their repository
+ root. For example, if the rule definition for <code>@my-ssl//src:openssl-lib</code> looks like
+ this:</p>
+
+<pre class="code">
+cc_library(
+ name = "openssl-lib",
+ srcs = ["openssl.cc"],
+ hdrs = ["openssl.h"],
+)
+</pre>
+
+<p>Then <code>sign_in.cc</code>'s includes might look like this:</p>
+
+<pre class="code">
+#include "sign_in.h"
+#include "src/openssl.h"
+</pre>
+
+<!-- #END_BLAZE_RULE -->*/
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryFunction.java
new file mode 100644
index 0000000000..4d259a41d8
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryFunction.java
@@ -0,0 +1,96 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.cmdline.PackageIdentifier.RepositoryName;
+import com.google.devtools.build.lib.packages.AggregatingAttributeMapper;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.skyframe.FileValue;
+import com.google.devtools.build.lib.skyframe.RepositoryValue;
+import com.google.devtools.build.lib.syntax.EvalException;
+import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.vfs.FileSystemUtils;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import com.google.devtools.build.lib.vfs.Symlinks;
+import com.google.devtools.build.skyframe.SkyFunctionException;
+import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
+import com.google.devtools.build.skyframe.SkyFunctionName;
+import com.google.devtools.build.skyframe.SkyKey;
+import com.google.devtools.build.skyframe.SkyValue;
+
+import java.io.IOException;
+
+/**
+ * Access a repository on the local filesystem.
+ */
+public class LocalRepositoryFunction extends RepositoryFunction {
+
+ @Override
+ public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException {
+ RepositoryName repositoryName = (RepositoryName) skyKey.argument();
+ Rule rule = RepositoryFunction.getRule(repositoryName, LocalRepositoryRule.NAME, env);
+ if (rule == null) {
+ return null;
+ }
+
+ AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
+ String path = mapper.get("path", Type.STRING);
+ PathFragment pathFragment = new PathFragment(path);
+ if (!pathFragment.isAbsolute()) {
+ throw new RepositoryFunctionException(
+ new EvalException(
+ rule.getLocation(),
+ "In " + rule + " the 'path' attribute must specify an absolute path"),
+ Transience.PERSISTENT);
+ }
+ Path repositoryPath = getExternalRepositoryDirectory().getRelative(rule.getName());
+ try {
+ FileSystemUtils.createDirectoryAndParents(repositoryPath.getParentDirectory());
+ if (repositoryPath.exists(Symlinks.NOFOLLOW)) {
+ repositoryPath.delete();
+ }
+ repositoryPath.createSymbolicLink(pathFragment);
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(
+ new IOException("Could not create symlink to repository " + pathFragment + ": "
+ + e.getMessage()), Transience.TRANSIENT);
+ }
+ FileValue repositoryValue = getRepositoryDirectory(repositoryPath, env);
+ if (repositoryValue == null) {
+ // TODO(bazel-team): If this returns null, we unnecessarily recreate the symlink above on the
+ // second execution.
+ return null;
+ }
+
+ if (!repositoryValue.isDirectory()) {
+ throw new RepositoryFunctionException(
+ new IOException(rule + " must specify an existing directory"), Transience.TRANSIENT);
+ }
+
+ return RepositoryValue.create(repositoryPath, repositoryValue);
+ }
+
+ @Override
+ public SkyFunctionName getSkyFunctionName() {
+ return SkyFunctionName.create(LocalRepositoryRule.NAME.toUpperCase());
+ }
+
+ @Override
+ public Class<? extends RuleDefinition> getRuleDefinition() {
+ return LocalRepositoryRule.class;
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryRule.java b/src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryRule.java
new file mode 100644
index 0000000000..fcfb9b0a55
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/LocalRepositoryRule.java
@@ -0,0 +1,87 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import static com.google.devtools.build.lib.packages.Attribute.attr;
+import static com.google.devtools.build.lib.syntax.Type.STRING;
+
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.packages.RuleClass.Builder;
+import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
+
+/**
+ * Rule definition for the local_repository rule.
+ */
+public class LocalRepositoryRule implements RuleDefinition {
+
+ public static final String NAME = "local_repository";
+
+ @Override
+ public RuleClass build(Builder builder, RuleDefinitionEnvironment environment) {
+ return builder
+ /* <!-- #BLAZE_RULE(local_repository).ATTRIBUTE(path) -->
+ The path to the local repository's directory.
+ ${SYNOPSIS}
+
+ <p>This must be an absolute path to the directory containing the repository's
+ <i>WORKSPACE</i> file.</p>
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+ .add(attr("path", STRING).mandatory())
+ .setWorkspaceOnly()
+ .build();
+ }
+
+ @Override
+ public Metadata getMetadata() {
+ return RuleDefinition.Metadata.builder()
+ .name(LocalRepositoryRule.NAME)
+ .type(RuleClassType.WORKSPACE)
+ .ancestors(WorkspaceBaseRule.class)
+ .factoryClass(WorkspaceConfiguredTargetFactory.class)
+ .build();
+ }
+}
+/*<!-- #BLAZE_RULE (NAME = local_repository, TYPE = OTHER, FAMILY = Workspace)[GENERIC_RULE] -->
+
+${ATTRIBUTE_SIGNATURE}
+
+<p>Allows targets from a local directory to be bound. This means that the current repository can
+ use targets defined in this other directory. See the <a href="#bind_examples">bind section</a>
+ for more details.</p>
+
+${ATTRIBUTE_DEFINITION}
+
+<h4 id="local_repository_examples">Examples</h4>
+
+<p>Suppose the current repository is a chat client, rooted at the directory <i>~/chat-app</i>. It
+ would like to use an SSL library which is defined in a different repository: <i>~/ssl</i>. The
+ SSL library has a target <code>//src:openssl-lib</code>.</p>
+
+<p>The user can add a dependency on this target by adding the following lines to
+ <i>~/chat-app/WORKSPACE</i>:</p>
+
+<pre class="code">
+local_repository(
+ name = "my-ssl",
+ path = "/home/user/ssl",
+)
+</pre>
+
+<p>Targets would specify <code>@my-ssl//src:openssl-lib</code> as a dependency to depend on this
+library.</p>
+
+<!-- #END_BLAZE_RULE -->*/
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryFunction.java
new file mode 100644
index 0000000000..356173e660
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryFunction.java
@@ -0,0 +1,71 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.cmdline.PackageIdentifier.RepositoryName;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.skyframe.FileValue;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import com.google.devtools.build.skyframe.SkyFunctionException;
+import com.google.devtools.build.skyframe.SkyFunctionName;
+import com.google.devtools.build.skyframe.SkyKey;
+import com.google.devtools.build.skyframe.SkyValue;
+
+/**
+ * Create a repository from a directory on the local filesystem.
+ */
+public class NewLocalRepositoryFunction extends RepositoryFunction {
+
+ @Override
+ public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException {
+ RepositoryName repositoryName = (RepositoryName) skyKey.argument();
+ Rule rule = getRule(repositoryName, NewLocalRepositoryRule.NAME, env);
+ if (rule == null) {
+ return null;
+ }
+
+ FileValue directoryValue = prepareLocalRepositorySymlinkTree(rule, env);
+ if (directoryValue == null) {
+ return null;
+ }
+
+ PathFragment pathFragment = getTargetPath(rule);
+
+ // Link x/y/z to /some/path/to/y/z.
+ if (!symlinkLocalRepositoryContents(
+ directoryValue.realRootedPath().asPath(),
+ getOutputBase().getFileSystem().getPath(pathFragment),
+ env)) {
+ return null;
+ }
+
+ // Link x/BUILD to <build_root>/x.BUILD.
+ return symlinkBuildFile(rule, getWorkspace(), directoryValue, env);
+ }
+
+ /**
+ * @see RepositoryFunction#getRule(RepositoryName, String, Environment)
+ */
+ @Override
+ public SkyFunctionName getSkyFunctionName() {
+ return SkyFunctionName.create(NewLocalRepositoryRule.NAME.toUpperCase());
+ }
+
+ @Override
+ public Class<? extends RuleDefinition> getRuleDefinition() {
+ return NewLocalRepositoryRule.class;
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryRule.java b/src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryRule.java
new file mode 100644
index 0000000000..f2a83b846e
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/NewLocalRepositoryRule.java
@@ -0,0 +1,132 @@
+// Copyright 2015 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import static com.google.devtools.build.lib.packages.Attribute.attr;
+import static com.google.devtools.build.lib.syntax.Type.STRING;
+
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.packages.RuleClass.Builder;
+import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
+
+/**
+ * Rule definition for the new_repository rule.
+ */
+public class NewLocalRepositoryRule implements RuleDefinition {
+ public static final String NAME = "new_local_repository";
+
+ @Override
+ public RuleClass build(Builder builder, RuleDefinitionEnvironment environment) {
+ return builder
+ /* <!-- #BLAZE_RULE(new_local_repository).ATTRIBUTE(path) -->
+ A path on the local filesystem.
+ ${SYNOPSIS}
+
+ <p>This must be an absolute path to an existing file or a directory.</p>
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+ .add(attr("path", STRING).mandatory())
+ /* <!-- #BLAZE_RULE(new_local_repository).ATTRIBUTE(build_file) -->
+ A file to use as a BUILD file for this directory.
+ ${SYNOPSIS}
+
+ <p>This path is relative to the build's workspace. The file does not need to be named
+ BUILD, but can be (something like BUILD.new-repo-name may work well for distinguishing it
+ from the repository's actual BUILD files.</p>
+ <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
+ .add(attr("build_file", STRING).mandatory())
+ .setWorkspaceOnly()
+ .build();
+ }
+
+ @Override
+ public Metadata getMetadata() {
+ return RuleDefinition.Metadata.builder()
+ .name(NewLocalRepositoryRule.NAME)
+ .type(RuleClassType.WORKSPACE)
+ .ancestors(WorkspaceBaseRule.class)
+ .factoryClass(WorkspaceConfiguredTargetFactory.class)
+ .build();
+ }
+}
+/*<!-- #BLAZE_RULE (NAME = new_local_repository, TYPE = OTHER, FAMILY = Workspace)[GENERIC_RULE] -->
+
+${ATTRIBUTE_SIGNATURE}
+
+<p>Allows a local directory to be turned into a Bazel repository. This means that the current
+ repository can define and use targets from anywhere on the filesystem.</p>
+
+<p>This rule creates a Bazel repository by creating a WORKSPACE file and subdirectory containing
+symlinks to the BUILD file and path given. The build file should create targets relative to the
+<code>path</code>.
+
+${ATTRIBUTE_DEFINITION}
+
+<h4 id="new_local_repository_examples">Examples</h4>
+
+<p>Suppose the current repository is a chat client, rooted at the directory <i>~/chat-app</i>. It
+ would like to use an SSL library which is defined in a different directory: <i>~/ssl</i>.</p>
+
+<p>The user can add a dependency by creating a BUILD file for the SSL library
+(~/chat-app/BUILD.my-ssl) containing:
+
+<pre class="code">
+java_library(
+ name = "openssl",
+ srcs = glob(['*.java'])
+ visibility = ["//visibility:public"],
+)
+</pre>
+
+<p>Then they can add the following lines to <i>~/chat-app/WORKSPACE</i>:</p>
+
+<pre class="code">
+new_local_repository(
+ name = "my-ssl",
+ path = "/home/user/ssl",
+ build_file = "BUILD.my-ssl",
+)
+</pre>
+
+<p>This will create a <code>@my-ssl</code> repository that symlinks to <i>/home/user/ssl</i>.
+Targets can depend on this library by adding <code>@my-ssl//:openssl</code> to a target's
+dependencies.</p>
+
+<p>You can also use <code>new_local_repository</code> to include single files, not just
+directories. For example, suppose you had a jar file at /home/username/Downloads/piano.jar. You
+could add just that file to your build by adding the following to your WORKSPACE file:
+
+<pre class="code">
+new_local_repository(
+ name = "piano",
+ path = "/home/username/Downloads/piano.jar",
+ build_file = "BUILD.piano",
+)
+</pre>
+
+<p>And creating the following BUILD.piano file:</p>
+
+<pre class="code">
+java_import(
+ name = "play-music",
+ jars = ["piano.jar"],
+ visibility = ["//visibility:public"],
+)
+</pre>
+
+Then targets can depend on <code>@piano//:play-music</code> to use piano.jar.
+
+<!-- #END_BLAZE_RULE -->*/
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java
new file mode 100644
index 0000000000..beb7381349
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java
@@ -0,0 +1,112 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.analysis.BlazeDirectories;
+import com.google.devtools.build.lib.cmdline.PackageIdentifier.RepositoryName;
+import com.google.devtools.build.lib.events.Location;
+import com.google.devtools.build.lib.packages.NoSuchPackageException;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException;
+import com.google.devtools.build.lib.skyframe.FileValue;
+import com.google.devtools.build.lib.skyframe.RepositoryValue;
+import com.google.devtools.build.lib.syntax.EvalException;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.skyframe.SkyFunction;
+import com.google.devtools.build.skyframe.SkyFunctionException;
+import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
+import com.google.devtools.build.skyframe.SkyKey;
+import com.google.devtools.build.skyframe.SkyValue;
+
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Implements delegation to the correct repository fetcher.
+ */
+public class RepositoryDelegatorFunction implements SkyFunction {
+
+ // Mapping of rule class name to SkyFunction.
+ private final ImmutableMap<String, RepositoryFunction> handlers;
+
+ // This is a reference to isFetch in BazelRepositoryModule, which tracks whether the current
+ // command is a fetch. Remote repository lookups are only allowed during fetches.
+ private final AtomicBoolean isFetch;
+ private final BlazeDirectories directories;
+
+ public RepositoryDelegatorFunction(
+ BlazeDirectories directories, ImmutableMap<String, RepositoryFunction> handlers,
+ AtomicBoolean isFetch) {
+ this.directories = directories;
+ this.handlers = handlers;
+ this.isFetch = isFetch;
+ }
+
+ @Override
+ public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException {
+ RepositoryName repositoryName = (RepositoryName) skyKey.argument();
+ Rule rule = RepositoryFunction
+ .getRule(repositoryName, null, env);
+ if (rule == null) {
+ return null;
+ }
+
+ // If Bazel isn't running a fetch command, we shouldn't be able to download anything. To
+ // prevent having to rerun fetch on server restart, we check if the external repository
+ // directory already exists and, if it does, just use that.
+ if (!isFetch.get()) {
+ FileValue repoRoot = RepositoryFunction.getRepositoryDirectory(
+ RepositoryFunction
+ .getExternalRepositoryDirectory(directories)
+ .getRelative(rule.getName()), env);
+ if (repoRoot == null) {
+ return null;
+ }
+ Path repoPath = repoRoot.realRootedPath().asPath();
+ if (!repoPath.exists()) {
+ throw new RepositoryFunctionException(new IOException(
+ "to fix, run\n\tbazel fetch //...\nExternal repository " + repositoryName
+ + " not found"),
+ Transience.TRANSIENT);
+ }
+ return RepositoryValue.create(repoRoot);
+ }
+
+ RepositoryFunction handler = handlers.get(rule.getRuleClass());
+ if (handler == null) {
+ throw new RepositoryFunctionException(new EvalException(
+ Location.fromFile(directories.getWorkspace().getRelative("WORKSPACE")),
+ "Could not find handler for " + rule), Transience.PERSISTENT);
+ }
+ SkyKey key = new SkyKey(handler.getSkyFunctionName(), repositoryName);
+
+ try {
+ return env.getValueOrThrow(
+ key, NoSuchPackageException.class, IOException.class, EvalException.class);
+ } catch (NoSuchPackageException e) {
+ throw new RepositoryFunctionException(e, Transience.PERSISTENT);
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(e, Transience.PERSISTENT);
+ } catch (EvalException e) {
+ throw new RepositoryFunctionException(e, Transience.PERSISTENT);
+ }
+ }
+
+ @Override
+ public String extractTag(SkyKey skyKey) {
+ return null;
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java
new file mode 100644
index 0000000000..93d3cf6c7e
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java
@@ -0,0 +1,384 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.common.base.Preconditions;
+import com.google.devtools.build.lib.analysis.BlazeDirectories;
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
+import com.google.devtools.build.lib.cmdline.PackageIdentifier.RepositoryName;
+import com.google.devtools.build.lib.packages.AggregatingAttributeMapper;
+import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException;
+import com.google.devtools.build.lib.packages.BuildFileNotFoundException;
+import com.google.devtools.build.lib.packages.NoSuchPackageException;
+import com.google.devtools.build.lib.packages.Package;
+import com.google.devtools.build.lib.packages.Rule;
+import com.google.devtools.build.lib.skyframe.FileSymlinkException;
+import com.google.devtools.build.lib.skyframe.FileValue;
+import com.google.devtools.build.lib.skyframe.InconsistentFilesystemException;
+import com.google.devtools.build.lib.skyframe.PackageValue;
+import com.google.devtools.build.lib.skyframe.RepositoryValue;
+import com.google.devtools.build.lib.syntax.EvalException;
+import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.vfs.FileSystemUtils;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import com.google.devtools.build.lib.vfs.RootedPath;
+import com.google.devtools.build.skyframe.SkyFunction;
+import com.google.devtools.build.skyframe.SkyFunctionException;
+import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
+import com.google.devtools.build.skyframe.SkyFunctionName;
+import com.google.devtools.build.skyframe.SkyKey;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import javax.annotation.Nullable;
+
+/**
+ * Parent class for repository-related Skyframe functions.
+ */
+public abstract class RepositoryFunction implements SkyFunction {
+ /**
+ * Exception thrown when something goes wrong accessing a remote repository.
+ *
+ * <p>This exception should be used by child classes to limit the types of exceptions
+ * {@link RepositoryDelegatorFunction} has to know how to catch.</p>
+ */
+ public static final class RepositoryFunctionException extends SkyFunctionException {
+ public RepositoryFunctionException(NoSuchPackageException cause, Transience transience) {
+ super(cause, transience);
+ }
+
+ /**
+ * Error reading or writing to the filesystem.
+ */
+ public RepositoryFunctionException(IOException cause, Transience transience) {
+ super(cause, transience);
+ }
+
+ /**
+ * For errors in WORKSPACE file rules (e.g., malformed paths or URLs).
+ */
+ public RepositoryFunctionException(EvalException cause, Transience transience) {
+ super(cause, transience);
+ }
+ }
+
+ private BlazeDirectories directories;
+
+ protected FileValue prepareLocalRepositorySymlinkTree(Rule rule, Environment env)
+ throws RepositoryFunctionException {
+ Path repositoryDirectory = getExternalRepositoryDirectory().getRelative(rule.getName());
+ try {
+ FileSystemUtils.deleteTree(repositoryDirectory);
+ FileSystemUtils.createDirectoryAndParents(repositoryDirectory);
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(e, Transience.TRANSIENT);
+ }
+ FileValue directoryValue = getRepositoryDirectory(repositoryDirectory, env);
+
+ if (directoryValue == null) {
+ return null;
+ }
+
+ // Add x/WORKSPACE.
+ createWorkspaceFile(repositoryDirectory, rule);
+ return directoryValue;
+ }
+
+ protected void createWorkspaceFile(Path repositoryDirectory, Rule rule)
+ throws RepositoryFunctionException {
+ try {
+ Path workspaceFile = repositoryDirectory.getRelative("WORKSPACE");
+ FileSystemUtils.writeContent(workspaceFile, Charset.forName("UTF-8"),
+ String.format("# DO NOT EDIT: automatically generated WORKSPACE file for %s\n", rule));
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(e, Transience.TRANSIENT);
+ }
+ }
+
+ protected RepositoryValue writeBuildFile(FileValue directoryValue, String contents)
+ throws RepositoryFunctionException {
+ Path buildFilePath = directoryValue.realRootedPath().asPath().getRelative("BUILD");
+ try {
+ FileSystemUtils.writeContentAsLatin1(buildFilePath, contents);
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(e, Transience.TRANSIENT);
+ }
+
+ return RepositoryValue.create(directoryValue);
+ }
+
+ /**
+ * Symlinks a BUILD file from the local filesystem into the external repository's root.
+ * @param rule the rule that declares the build_file path.
+ * @param workspaceDirectory the workspace root for the build.
+ * @param directoryValue the FileValue corresponding to the external repository's root directory.
+ * @param env the Skyframe environment.
+ * @return the file value of the symlink created.
+ * @throws RepositoryFunctionException if the BUILD file specified does not exist or cannot be
+ * linked.
+ */
+ protected RepositoryValue symlinkBuildFile(
+ Rule rule, Path workspaceDirectory, FileValue directoryValue, Environment env)
+ throws RepositoryFunctionException {
+ AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
+ PathFragment buildFile = new PathFragment(mapper.get("build_file", Type.STRING));
+ Path buildFileTarget = workspaceDirectory.getRelative(buildFile);
+ if (!buildFileTarget.exists()) {
+ throw new RepositoryFunctionException(
+ new EvalException(rule.getLocation(),
+ String.format("In %s the 'build_file' attribute does not specify an existing file "
+ + "(%s does not exist)", rule, buildFileTarget)),
+ Transience.PERSISTENT);
+ }
+
+ RootedPath rootedBuild;
+ if (buildFile.isAbsolute()) {
+ rootedBuild = RootedPath.toRootedPath(
+ buildFileTarget.getParentDirectory(), new PathFragment(buildFileTarget.getBaseName()));
+ } else {
+ rootedBuild = RootedPath.toRootedPath(workspaceDirectory, buildFile);
+ }
+ SkyKey buildFileKey = FileValue.key(rootedBuild);
+ FileValue buildFileValue;
+ try {
+ buildFileValue = (FileValue) env.getValueOrThrow(buildFileKey, IOException.class,
+ FileSymlinkException.class, InconsistentFilesystemException.class);
+ if (buildFileValue == null) {
+ return null;
+ }
+ } catch (IOException | FileSymlinkException | InconsistentFilesystemException e) {
+ throw new RepositoryFunctionException(
+ new IOException("Cannot lookup " + buildFile + ": " + e.getMessage()),
+ Transience.TRANSIENT);
+ }
+
+ Path buildFilePath = directoryValue.realRootedPath().asPath().getRelative("BUILD");
+ if (createSymbolicLink(buildFilePath, buildFileTarget, env) == null) {
+ return null;
+ }
+ return RepositoryValue.createNew(directoryValue, buildFileValue);
+ }
+
+ protected static PathFragment getTargetPath(Rule rule) throws RepositoryFunctionException {
+ AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
+ String path = mapper.get("path", Type.STRING);
+ PathFragment pathFragment = new PathFragment(path);
+ if (!pathFragment.isAbsolute()) {
+ throw new RepositoryFunctionException(
+ new EvalException(
+ rule.getLocation(),
+ "In " + rule + " the 'path' attribute must specify an absolute path"),
+ Transience.PERSISTENT);
+ }
+
+ return pathFragment;
+ }
+
+ /**
+ * Given a targetDirectory /some/path/to/y that contains files z, w, and v, create the following
+ * directory structure:
+ * <pre>
+ * .external-repository/
+ * x/
+ * WORKSPACE
+ * BUILD -> &lt;build_root&gt;/x.BUILD
+ * z -> /some/path/to/y/z
+ * w -> /some/path/to/y/w
+ * v -> /some/path/to/y/v
+ * </pre>
+ */
+ public static boolean symlinkLocalRepositoryContents(
+ Path repositoryDirectory, Path targetDirectory, Environment env)
+ throws RepositoryFunctionException {
+ try {
+ for (Path target : targetDirectory.getDirectoryEntries()) {
+ Path symlinkPath =
+ repositoryDirectory.getRelative(target.getBaseName());
+ if (createSymbolicLink(symlinkPath, target, env) == null) {
+ return false;
+ }
+ }
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(e, Transience.TRANSIENT);
+ }
+
+ return true;
+ }
+
+ private static FileValue createSymbolicLink(Path from, Path to, Environment env)
+ throws RepositoryFunctionException {
+ try {
+ // Remove not-symlinks that are already there.
+ if (from.exists()) {
+ from.delete();
+ }
+ FileSystemUtils.ensureSymbolicLink(from, to);
+ } catch (IOException e) {
+ throw new RepositoryFunctionException(
+ new IOException(String.format("Error creating symbolic link from %s to %s: %s",
+ from, to, e.getMessage())), Transience.TRANSIENT);
+ }
+
+ SkyKey outputDirectoryKey = FileValue.key(RootedPath.toRootedPath(
+ from, PathFragment.EMPTY_FRAGMENT));
+ try {
+ return (FileValue) env.getValueOrThrow(outputDirectoryKey, IOException.class,
+ FileSymlinkException.class, InconsistentFilesystemException.class);
+ } catch (IOException | FileSymlinkException | InconsistentFilesystemException e) {
+ throw new RepositoryFunctionException(
+ new IOException(String.format("Could not access %s: %s", from, e.getMessage())),
+ Transience.PERSISTENT);
+ }
+ }
+
+ @Nullable
+ public static Package getExternalPackage(Environment env)
+ throws RepositoryFunctionException {
+ SkyKey packageKey = PackageValue.key(Package.EXTERNAL_PACKAGE_IDENTIFIER);
+ PackageValue packageValue;
+ try {
+ packageValue = (PackageValue) env.getValueOrThrow(packageKey,
+ NoSuchPackageException.class);
+ } catch (NoSuchPackageException e) {
+ throw new RepositoryFunctionException(
+ new BuildFileNotFoundException(
+ Package.EXTERNAL_PACKAGE_IDENTIFIER, "Could not load //external package"),
+ Transience.PERSISTENT);
+ }
+ if (packageValue == null) {
+ return null;
+ }
+
+ Package externalPackage = packageValue.getPackage();
+ if (externalPackage.containsErrors()) {
+ throw new RepositoryFunctionException(
+ new BuildFileContainsErrorsException(
+ Package.EXTERNAL_PACKAGE_IDENTIFIER, "Could not load //external package"),
+ Transience.PERSISTENT);
+ }
+ return externalPackage;
+ }
+
+ @Nullable
+ public static Rule getRule(
+ String ruleName, @Nullable String ruleClassName, Environment env)
+ throws RepositoryFunctionException {
+ try {
+ return getRule(RepositoryName.create("@" + ruleName), ruleClassName, env);
+ } catch (LabelSyntaxException e) {
+ throw new RepositoryFunctionException(
+ new IOException("Invalid rule name " + ruleName), Transience.PERSISTENT);
+ }
+ }
+
+ /**
+ * Uses a remote repository name to fetch the corresponding Rule describing how to get it.
+ * This should be called from {@link SkyFunction#compute} functions, which should return null if
+ * this returns null. If {@code ruleClassName} is set, the rule found must have a matching rule
+ * class name.
+ */
+ @Nullable
+ public static Rule getRule(
+ RepositoryName repositoryName, @Nullable String ruleClassName, Environment env)
+ throws RepositoryFunctionException {
+ Package externalPackage = getExternalPackage(env);
+ if (externalPackage == null) {
+ return null;
+ }
+
+ Rule rule = externalPackage.getRule(repositoryName.strippedName());
+ if (rule == null) {
+ throw new RepositoryFunctionException(
+ new BuildFileContainsErrorsException(
+ Package.EXTERNAL_PACKAGE_IDENTIFIER,
+ "The repository named '" + repositoryName + "' could not be resolved"),
+ Transience.PERSISTENT);
+ }
+ Preconditions.checkState(ruleClassName == null || rule.getRuleClass().equals(ruleClassName),
+ "Got %s, was expecting a %s", rule, ruleClassName);
+ return rule;
+ }
+
+ /**
+ * Adds the repository's directory to the graph and, if it's a symlink, resolves it to an
+ * actual directory.
+ */
+ @Nullable
+ public static FileValue getRepositoryDirectory(Path repositoryDirectory, Environment env)
+ throws RepositoryFunctionException {
+ SkyKey outputDirectoryKey = FileValue.key(RootedPath.toRootedPath(
+ repositoryDirectory, PathFragment.EMPTY_FRAGMENT));
+ FileValue value;
+ try {
+ value = (FileValue) env.getValueOrThrow(outputDirectoryKey, IOException.class,
+ FileSymlinkException.class, InconsistentFilesystemException.class);
+ } catch (IOException | FileSymlinkException | InconsistentFilesystemException e) {
+ throw new RepositoryFunctionException(
+ new IOException("Could not access " + repositoryDirectory + ": " + e.getMessage()),
+ Transience.PERSISTENT);
+ }
+ return value;
+ }
+
+ @Override
+ public String extractTag(SkyKey skyKey) {
+ return null;
+ }
+
+ /**
+ * Gets Skyframe's name for this.
+ */
+ public abstract SkyFunctionName getSkyFunctionName();
+
+ /**
+ * Sets up output path information.
+ */
+ public void setDirectories(BlazeDirectories directories) {
+ this.directories = directories;
+ }
+
+ protected Path getExternalRepositoryDirectory() {
+ return RepositoryFunction.getExternalRepositoryDirectory(directories);
+ }
+
+ public static Path getExternalRepositoryDirectory(BlazeDirectories directories) {
+ return directories
+ .getOutputBase()
+ .getRelative(Package.EXTERNAL_PACKAGE_IDENTIFIER.getPackageFragment());
+ }
+
+ /**
+ * Gets the base directory repositories should be stored in locally.
+ */
+ protected Path getOutputBase() {
+ return directories.getOutputBase();
+ }
+
+ /**
+ * Gets the directory the WORKSPACE file for the build is in.
+ */
+ protected Path getWorkspace() {
+ return directories.getWorkspace();
+ }
+
+
+ /**
+ * Returns the RuleDefinition class for this type of repository.
+ */
+ public abstract Class<? extends RuleDefinition> getRuleDefinition();
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceBaseRule.java b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceBaseRule.java
new file mode 100644
index 0000000000..64bb1d526e
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceBaseRule.java
@@ -0,0 +1,41 @@
+// Copyright 2015 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.devtools.build.lib.analysis.RuleDefinition;
+import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
+import com.google.devtools.build.lib.packages.RuleClass;
+import com.google.devtools.build.lib.packages.RuleClass.Builder;
+import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
+
+/**
+ * Base rule for rules in the WORKSPACE file.
+ */
+public class WorkspaceBaseRule implements RuleDefinition {
+ @Override
+ public RuleClass build(Builder builder, RuleDefinitionEnvironment environment) {
+ return builder
+ .exemptFromConstraintChecking("workspace rules aren't built for target environments")
+ .build();
+ }
+
+ @Override
+ public Metadata getMetadata() {
+ return RuleDefinition.Metadata.builder()
+ .name("$workspace_base_rule")
+ .type(RuleClassType.ABSTRACT)
+ .build();
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceConfiguredTargetFactory.java b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceConfiguredTargetFactory.java
new file mode 100644
index 0000000000..cf7a37dc4f
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/WorkspaceConfiguredTargetFactory.java
@@ -0,0 +1,37 @@
+// Copyright 2014 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.rules.repository;
+
+import com.google.devtools.build.lib.analysis.ConfiguredTarget;
+import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
+import com.google.devtools.build.lib.analysis.RuleContext;
+import com.google.devtools.build.lib.analysis.RunfilesProvider;
+import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
+
+/**
+ * Implementation of workspace rules. Generally, these don't have any providers, since they
+ * "forward" to the SkyFunctions which actually create the repositories and then are accessed via
+ * "normal" rules.
+ */
+public class WorkspaceConfiguredTargetFactory implements RuleConfiguredTargetFactory {
+
+ @Override
+ public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException {
+ return new RuleConfiguredTargetBuilder(ruleContext)
+ .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
+ .build();
+ }
+
+}