1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 """Repository rules to configure the zlib used by LLVM.
7 Most users should pick one of the explicit rules to configure their use of zlib
9 - `llvm_zlib_external` will link against an external Bazel zlib repository.
10 - `llvm_zlib_system` will link against the system zlib (non-hermetically).
11 - 'llvm_zlib_disable` will disable zlib completely.
13 If you would like to make your build configurable, you can use
14 `llvm_zlib_from_env`. By default, this will disable zlib, but will inspect
15 the environment variable (most easily set with a `--repo_env` flag to the
16 Bazel invocation) `BAZEL_LLVM_ZLIB_STRATEGY`. If it is set to `external`,
17 then it will behave the same as `llvm_zlib_external`. If it is set to
18 `system` then it will behave the same as `llvm_zlib_system`. Any other
19 setting will disable zlib the same as not setting it at all.
22 def _llvm_zlib_external_impl(repository_ctx):
23 repository_ctx.template(
25 repository_ctx.attr._external_build_template,
27 "@external_zlib_repo//:zlib_rule": repository_ctx.attr.external_zlib,
32 llvm_zlib_external = repository_rule(
33 implementation = _llvm_zlib_external_impl,
35 "_external_build_template": attr.label(
36 default = Label("//utils/bazel/deps_impl:zlib_external.BUILD"),
37 allow_single_file = True,
39 "external_zlib": attr.string(
40 doc = "The dependency that should be used for the external zlib library.",
46 def _llvm_zlib_system_impl(repository_ctx):
47 repository_ctx.template(
49 repository_ctx.attr._system_build_template,
53 # While it may seem like this needs to be local, it doesn't actually inspect
54 # any local state, it just configures to build against that local state.
55 llvm_zlib_system = repository_rule(
56 implementation = _llvm_zlib_system_impl,
58 "_system_build_template": attr.label(
59 default = Label("//utils/bazel/deps_impl:zlib_system.BUILD"),
60 allow_single_file = True,
65 def _llvm_zlib_disable_impl(repository_ctx):
66 repository_ctx.template(
68 repository_ctx.attr._disable_build_template,
72 llvm_zlib_disable = repository_rule(
73 implementation = _llvm_zlib_disable_impl,
75 "_disable_build_template": attr.label(
76 default = Label("//utils/bazel/deps_impl:zlib_disable.BUILD"),
77 allow_single_file = True,
82 def _llvm_zlib_from_env_impl(repository_ctx):
83 zlib_strategy = repository_ctx.os.environ.get("BAZEL_LLVM_ZLIB_STRATEGY")
84 if zlib_strategy == "external":
85 _llvm_zlib_external_impl(repository_ctx)
86 elif zlib_strategy == "system":
87 _llvm_zlib_system_impl(repository_ctx)
89 _llvm_zlib_disable_impl(repository_ctx)
91 llvm_zlib_from_env = repository_rule(
92 implementation = _llvm_zlib_from_env_impl,
94 "_disable_build_template": attr.label(
95 default = Label("//utils/bazel/deps_impl:zlib_disable.BUILD"),
96 allow_single_file = True,
98 "_external_build_template": attr.label(
99 default = Label("//utils/bazel/deps_impl:zlib_external.BUILD"),
100 allow_single_file = True,
102 "_system_build_template": attr.label(
103 default = Label("//utils/bazel/deps_impl:zlib_system.BUILD"),
104 allow_single_file = True,
106 "external_zlib": attr.label(
107 doc = "The dependency that should be used for the external zlib library.",
111 environ = ["BAZEL_LLVM_ZLIB_STRATEGY"],