github.com/tiagovtristao/plz@v13.4.0+incompatible/src/parse/rules/subrepo_rules.build_defs (about) 1 """Contains rules for working with sub-repositories. 2 3 These allow downloading targets from another repo and making them available here. 4 Each repository has a name, which must be unique (including packages in the 5 current repository). 6 7 Rules in subrepos can be accessed in one of two ways: 8 - by prefixing them with the name of the repo, for example //my_repo/pkg:target 9 to access what would be //pkg:target within it. 10 - by using Bazel-style @my_repo//pkg:target syntax. 11 12 These are still fairly experimental. 13 """ 14 15 def workspace(name:str): 16 """Defines the name of the current workspace. 17 18 Args: 19 name (str): Name to bind this workspace as. 20 """ 21 subrepo( 22 name = name, 23 path = '', 24 ) 25 26 27 def http_archive(name:str, urls:list, strip_prefix:str=None, hashes:str|list=None, 28 sha256:str=None, config:str=None, bazel_compat:bool=False, 29 visibility:list=None): 30 """Fetches a remote file over HTTP and expands its contents. 31 32 The archive should be either a zipfile or a tarball. Which one to use will be autodetected 33 based on the file extension in the URL given. 34 35 This is still experimental and known not to work in many cases. 36 37 Args: 38 name: Name of the rule. 39 urls: List of URLs to fetch from. These are assumed to be mirrors and will be 40 tried in sequence. 41 strip_prefix: Prefix to strip from the expanded archive. 42 hashes: List of hashes to verify the rule with. 43 sha256: Used for Bazel compat but currently has no effect. 44 config: Configuration file to apply to this subrepo. 45 bazel_compat: Shorthand to turn on Bazel compatibility. This is equivalent to 46 specifying a config file with `compatibility = true` in the `[bazel]` 47 section. 48 visibility: Deprecated, has no effect. 49 """ 50 new_http_archive( 51 name = name, 52 urls = urls, 53 strip_prefix = strip_prefix, 54 hashes = hashes, 55 sha256 = sha256, 56 config = config, 57 bazel_compat = bazel_compat, 58 ) 59 60 61 def new_http_archive(name:str, urls:list, build_file:str=None, build_file_content:str=None, 62 strip_prefix:str=None, hashes:str|list=None, sha256:str=None, 63 config:str=None, bazel_compat:bool=False, visibility:list=None): 64 """Fetches a remote file over HTTP and expands its contents, combined with a BUILD file. 65 66 The archive should be either a zipfile or a tarball. Which one to use will be autodetected 67 based on the file extension in the URL given. 68 69 The given build file (via build_file or build_file_contents) will replace any existing file 70 and will therefore be used to build the contents of the subrepo. 71 72 This is still experimental and known not to work in many cases. 73 74 Args: 75 name: Name of the rule. 76 urls: List of URLs to fetch from. These are assumed to be mirrors and will be 77 tried in sequence. 78 build_file: The file to use as a BUILD file for this subrepository. 79 build_file_content: Text content to use for the BUILD file for the subrepository. 80 We suggest using build_file instead, but this is provided for Bazel compatibility. 81 strip_prefix: Prefix to strip from the expanded archive. 82 hashes: List of hashes to verify the rule with. 83 sha256: Used for Bazel compat but currently has no effect. 84 config: Configuration file to apply to this subrepo. 85 bazel_compat: Shorthand to turn on Bazel compatibility. This is equivalent to 86 specifying a config file with `compatibility = true` in the `[bazel]` 87 section. 88 visibility: Deprecated, has no effect. 89 """ 90 remote_rule = remote_file( 91 name = name, 92 _tag = 'download', 93 url = urls, 94 out = name + '_' + basename(urls[0]), 95 hashes = hashes, 96 ) 97 if any([u.endswith('.jar') or u.endswith('.zip') for u in urls]): 98 if strip_prefix: 99 cmd = '$TOOL x $SRCS_REMOTE -o $OUT -s ' + strip_prefix 100 else: 101 cmd = '$TOOL x $SRCS_REMOTE -o $OUT' 102 else: 103 if strip_prefix: 104 cmd = f'tar -axf $SRCS_REMOTE && mv {strip_prefix} $OUT' 105 else: 106 cmd = 'mkdir $OUT && tar -axf $SRCS_REMOTE -C $OUT' 107 if build_file: 108 cmd += ' && mv $SRCS_BUILD $OUT/' + CONFIG.BUILD_FILE_NAMES[0] 109 elif build_file_content: 110 cmd += ' && cat > $OUT/%s << EOF\n%s\nEOF' % (CONFIG.BUILD_FILE_NAMES[0], build_file_content) 111 112 extract_rule = build_rule( 113 name = name, 114 srcs = { 115 'remote': [remote_rule], 116 'build': [build_file], 117 }, 118 tools = [CONFIG.JARCAT_TOOL], 119 outs = [name], 120 cmd = cmd, 121 ) 122 subrepo( 123 name = name, 124 dep = extract_rule, 125 config = config, 126 bazel_compat = bazel_compat, 127 ) 128 129 130 def new_local_repository(name:str, path:str): 131 """Defines a new subrepo corresponding to a directory on the local disk. 132 133 It's up to the user to make sure this repo is where this rule says it is. Typically this 134 would be managed by e.g. a CI system that checks out side-by-side git repos. 135 136 Right now this does not support specifying BUILD files as the others do. The BUILD file 137 must exist in the target directory. 138 139 Args: 140 name: The name of the subrepo to use. 141 path: The path (absolute or relative to this repo's root) to the local repository. 142 """ 143 # Bit of a hack; subrepo() doesn't generate a rule, but the subrepo code internally 144 # expects there to be one by this name. Maybe we should use something like system_library 145 # to pull it in as a dependency, but that seems pretty heavyweight. 146 subrepo( 147 name = name, 148 path = path, 149 dep = filegroup( 150 name = name, 151 ), 152 ) 153 154 155 def github_repo(name:str, repo:str, revision:str, build_file:str=None, hashes:str|list=None, 156 config:str=None, bazel_compat:bool=False): 157 """Defines a new subrepo corresponding to a Github project. 158 159 This is a convenience alias to the more general new_http_archive. It knows how to 160 construct Github URLs appropriately which allows a more concise description. 161 162 Args: 163 name: Name of the rule. 164 repo: Github repo to fetch from (e.g. "thought-machine/please"). 165 revision: Revision to download. 166 build_file: The file to use as a BUILD file for this subrepository. 167 hashes: List of hashes to verify the rule with. 168 config: Configuration file to apply to this subrepo. 169 bazel_compat: Shorthand to turn on Bazel compatibility. This is equivalent to 170 specifying a config file with `compatibility = true` in the `[bazel]` 171 section. 172 """ 173 org, _, repo = repo.partition('/') 174 assert repo, "Must pass a valid Github repo argument, e.g. thought-machine/please" 175 return new_http_archive( 176 name = name, 177 urls = [f'https://github.com/{org}/{repo}/archive/{revision}.zip'], 178 strip_prefix = f'{repo}-{revision}', 179 build_file = build_file, 180 hashes = hashes, 181 config = config, 182 bazel_compat = bazel_compat, 183 )