blob: 933c00bff4a05451efaeb0ad953f7ef46c106094 [file] [log] [blame]
Geoffrey Martin-Noble1e20c602019-11-11 12:06:19 -08001# Copyright 2019 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Stella Laurenzoe7d9e882019-10-10 14:32:38 -070015# TODO(laurenzo): This is available upstream as of 0.28. Remove when ready.
16# See: https://docs.bazel.build/versions/master/repo/utils.html#maybe
17def maybe(repo_rule, name, **kwargs):
18 """Utility function for only adding a repository if it's not already present.
19 This is to implement safe repositories.bzl macro documented in
20 https://docs.bazel.build/versions/master/skylark/deploying.html#dependencies.
21 Args:
22 repo_rule: repository rule function.
23 name: name of the repository to create.
24 **kwargs: remaining arguments that are passed to the repo_rule function.
25 Returns:
26 Nothing, defines the repository when needed as a side-effect.
27 """
28 if not native.existing_rule(name):
29 repo_rule(name = name, **kwargs)