// 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.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(RuleClass.Builder builder, RuleDefinitionEnvironment environment) { return builder /* A path on the local filesystem.

This must be an absolute path to an existing file or a directory.

*/ .add(attr("path", STRING).mandatory()) /* A file to use as a BUILD file for this directory.

Either build_file or build_file_content must be specified.

This attribute is a label relative to the main 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.)

*/ .add(attr("build_file", STRING)) /* The content for the BUILD file for this repository.

Either build_file or build_file_content must be specified.

*/ .add(attr("build_file_content", STRING)) /* The file to use as the WORKSPACE file for this repository.

Either workspace_file or workspace_file_content can be specified, but not both.

This attribute is a label relative to the main workspace. The file does not need to be named WORKSPACE, but can be. (Something like WORKSPACE.new-repo-name may work well for distinguishing it from the repository's actual WORKSPACE files.)

*/ .add(attr("workspace_file", STRING)) /* The content for the WORKSPACE file for this repository.

Either workspace_file or workspace_file_content can be specified, but not both.

*/ .add(attr("workspace_file_content", STRING)) .setWorkspaceOnly() .build(); } @Override public Metadata getMetadata() { return RuleDefinition.Metadata.builder() .name(NewLocalRepositoryRule.NAME) .type(RuleClassType.WORKSPACE) .ancestors(WorkspaceBaseRule.class) .factoryClass(WorkspaceConfiguredTargetFactory.class) .build(); } } /*

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.

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 path.

Examples

Suppose the current repository is a chat client, rooted at the directory ~/chat-app. It would like to use an SSL library which is defined in a different directory: ~/ssl.

The user can add a dependency by creating a BUILD file for the SSL library (~/chat-app/BUILD.my-ssl) containing:

java_library(
    name = "openssl",
    srcs = glob(['*.java'])
    visibility = ["//visibility:public"],
)

Then they can add the following lines to ~/chat-app/WORKSPACE:

new_local_repository(
    name = "my-ssl",
    path = "/home/user/ssl",
    build_file = "BUILD.my-ssl",
)

This will create a @my-ssl repository that symlinks to /home/user/ssl. Targets can depend on this library by adding @my-ssl//:openssl to a target's dependencies.

You can also use new_local_repository 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:

new_local_repository(
    name = "piano",
    path = "/home/username/Downloads/piano.jar",
    build_file = "BUILD.piano",
)

And creating the following BUILD.piano file:

java_import(
    name = "play-music",
    jars = ["piano.jar"],
    visibility = ["//visibility:public"],
)
Then targets can depend on @piano//:play-music to use piano.jar. */