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 4 5"""Repository rules to configure the zlib used by LLVM. 6 7Most users should pick one of the explicit rules to configure their use of zlib 8with LLVM: 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. 12 13If 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 15the environment variable (most easily set with a `--repo_env` flag to the 16Bazel invocation) `BAZEL_LLVM_ZLIB_STRATEGY`. If it is set to `external`, 17then 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 19setting will disable zlib the same as not setting it at all. 20""" 21 22def _llvm_zlib_external_impl(repository_ctx): 23 repository_ctx.template( 24 "BUILD", 25 repository_ctx.attr._external_build_template, 26 substitutions = { 27 "@external_zlib_repo//:zlib_rule": repository_ctx.attr.external_zlib, 28 }, 29 executable = False, 30 ) 31 32llvm_zlib_external = repository_rule( 33 implementation = _llvm_zlib_external_impl, 34 attrs = { 35 "_external_build_template": attr.label( 36 default = Label("//utils/bazel/deps_impl:zlib_external.BUILD"), 37 allow_single_file = True, 38 ), 39 "external_zlib": attr.string( 40 doc = "The dependency that should be used for the external zlib library.", 41 mandatory = True, 42 ), 43 }, 44) 45 46def _llvm_zlib_system_impl(repository_ctx): 47 repository_ctx.template( 48 "BUILD", 49 repository_ctx.attr._system_build_template, 50 executable = False, 51 ) 52 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. 55llvm_zlib_system = repository_rule( 56 implementation = _llvm_zlib_system_impl, 57 attrs = { 58 "_system_build_template": attr.label( 59 default = Label("//utils/bazel/deps_impl:zlib_system.BUILD"), 60 allow_single_file = True, 61 ), 62 }, 63) 64 65def _llvm_zlib_disable_impl(repository_ctx): 66 repository_ctx.template( 67 "BUILD", 68 repository_ctx.attr._disable_build_template, 69 executable = False, 70 ) 71 72llvm_zlib_disable = repository_rule( 73 implementation = _llvm_zlib_disable_impl, 74 attrs = { 75 "_disable_build_template": attr.label( 76 default = Label("//utils/bazel/deps_impl:zlib_disable.BUILD"), 77 allow_single_file = True, 78 ), 79 }, 80) 81 82def _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) 88 else: 89 _llvm_zlib_disable_impl(repository_ctx) 90 91llvm_zlib_from_env = repository_rule( 92 implementation = _llvm_zlib_from_env_impl, 93 attrs = { 94 "_disable_build_template": attr.label( 95 default = Label("//utils/bazel/deps_impl:zlib_disable.BUILD"), 96 allow_single_file = True, 97 ), 98 "_external_build_template": attr.label( 99 default = Label("//utils/bazel/deps_impl:zlib_external.BUILD"), 100 allow_single_file = True, 101 ), 102 "_system_build_template": attr.label( 103 default = Label("//utils/bazel/deps_impl:zlib_system.BUILD"), 104 allow_single_file = True, 105 ), 106 "external_zlib": attr.label( 107 doc = "The dependency that should be used for the external zlib library.", 108 mandatory = True, 109 ), 110 }, 111 environ = ["BAZEL_LLVM_ZLIB_STRATEGY"], 112) 113