fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
7edda6f1
main
1{# 2To keep line numbers consistent with the rendered version, empty space is 3intentionally placed here which should match the line length of `partials/header.j2`. 4 5Expected length = 6 lines 6#} {%- include "partials/header.j2" %} 7""" 8# `crates_repository` API 9 10- [aliases](#aliases) 11- [crate_deps](#crate_deps) 12- [all_crate_deps](#all_crate_deps) 13- [crate_repositories](#crate_repositories) 14 15""" 16 17load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") 18load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 19load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") 20load("@bazel_skylib//lib:selects.bzl", "selects") 21load("@rules_rust//crate_universe/private:local_crate_mirror.bzl", "local_crate_mirror") 22 23############################################################################### 24# MACROS API 25############################################################################### 26 27# An identifier that represent common dependencies (unconditional). 28_COMMON_CONDITION = "" 29 30def _flatten_dependency_maps(all_dependency_maps): 31 """Flatten a list of dependency maps into one dictionary. 32 33 Dependency maps have the following structure: 34 35 ```python 36 DEPENDENCIES_MAP = { 37 # The first key in the map is a Bazel package 38 # name of the workspace this file is defined in. 39 "workspace_member_package": { 40 41 # Not all dependencies are supported for all platforms. 42 # the condition key is the condition required to be true 43 # on the host platform. 44 "condition": { 45 46 # An alias to a crate target. # The label of the crate target the 47 # Aliases are only crate names. # package name refers to. 48 "package_name": "@full//:label", 49 } 50 } 51 } 52 ``` 53 54 Args: 55 all_dependency_maps (list): A list of dicts as described above 56 57 Returns: 58 dict: A dictionary as described above 59 """ 60 dependencies = {} 61 62 for workspace_deps_map in all_dependency_maps: 63 for pkg_name, conditional_deps_map in workspace_deps_map.items(): 64 if pkg_name not in dependencies: 65 non_frozen_map = dict() 66 for key, values in conditional_deps_map.items(): 67 non_frozen_map.update({key: dict(values.items())}) 68 dependencies.setdefault(pkg_name, non_frozen_map) 69 continue 70 71 for condition, deps_map in conditional_deps_map.items(): 72 # If the condition has not been recorded, do so and continue 73 if condition not in dependencies[pkg_name]: 74 dependencies[pkg_name].setdefault(condition, dict(deps_map.items())) 75 continue 76 77 # Alert on any miss-matched dependencies 78 inconsistent_entries = [] 79 for crate_name, crate_label in deps_map.items(): 80 existing = dependencies[pkg_name][condition].get(crate_name) 81 if existing and existing != crate_label: 82 inconsistent_entries.append((crate_name, existing, crate_label)) 83 dependencies[pkg_name][condition].update({crate_name: crate_label}) 84 85 return dependencies 86 87def crate_deps(deps, package_name ={{ default_package_name }} ): 88 """Finds the fully qualified label of the requested crates for the package where this macro is called. 89 90 Args: 91 deps (list): The desired list of crate targets. 92 package_name (str, optional): The package name of the set of dependencies to look up. 93 Defaults to `native.package_name()`. 94 95 Returns: 96 list: A list of labels to generated rust targets (str) 97 """ 98 99 if not deps: 100 return [] 101 102 if package_name == None: 103 package_name = native.package_name() 104 105 # Join both sets of dependencies 106 dependencies = _flatten_dependency_maps([ 107 _NORMAL_DEPENDENCIES, 108 _NORMAL_DEV_DEPENDENCIES, 109 _PROC_MACRO_DEPENDENCIES, 110 _PROC_MACRO_DEV_DEPENDENCIES, 111 _BUILD_DEPENDENCIES, 112 _BUILD_PROC_MACRO_DEPENDENCIES, 113 ]).pop(package_name, {}) 114 115 # Combine all conditional packages so we can easily index over a flat list 116 # TODO: Perhaps this should actually return select statements and maintain 117 # the conditionals of the dependencies 118 flat_deps = {} 119 for deps_set in dependencies.values(): 120 for crate_name, crate_label in deps_set.items(): 121 flat_deps.update({crate_name: crate_label}) 122 123 missing_crates = [] 124 crate_targets = [] 125 for crate_target in deps: 126 if crate_target not in flat_deps: 127 missing_crates.append(crate_target) 128 else: 129 crate_targets.append(flat_deps[crate_target]) 130 131 if missing_crates: 132 fail("Could not find crates `{}` among dependencies of `{}`. Available dependencies were `{}`".format( 133 missing_crates, 134 package_name, 135 dependencies, 136 )) 137 138 return crate_targets 139 140def all_crate_deps( 141 normal = False, 142 normal_dev = False, 143 proc_macro = False, 144 proc_macro_dev = False, 145 build = False, 146 build_proc_macro = False, 147 package_name ={{ default_package_name }} ): 148 """Finds the fully qualified label of all requested direct crate dependencies \ 149 for the package where this macro is called. 150 151 If no parameters are set, all normal dependencies are returned. Setting any one flag will 152 otherwise impact the contents of the returned list. 153 154 Args: 155 normal (bool, optional): If True, normal dependencies are included in the 156 output list. 157 normal_dev (bool, optional): If True, normal dev dependencies will be 158 included in the output list.. 159 proc_macro (bool, optional): If True, proc_macro dependencies are included 160 in the output list. 161 proc_macro_dev (bool, optional): If True, dev proc_macro dependencies are 162 included in the output list. 163 build (bool, optional): If True, build dependencies are included 164 in the output list. 165 build_proc_macro (bool, optional): If True, build proc_macro dependencies are 166 included in the output list. 167 package_name (str, optional): The package name of the set of dependencies to look up. 168 Defaults to `native.package_name()` when unset. 169 170 Returns: 171 list: A list of labels to generated rust targets (str) 172 """ 173 174 if package_name == None: 175 package_name = native.package_name() 176 177 # Determine the relevant maps to use 178 all_dependency_maps = [] 179 if normal: 180 all_dependency_maps.append(_NORMAL_DEPENDENCIES) 181 if normal_dev: 182 all_dependency_maps.append(_NORMAL_DEV_DEPENDENCIES) 183 if proc_macro: 184 all_dependency_maps.append(_PROC_MACRO_DEPENDENCIES) 185 if proc_macro_dev: 186 all_dependency_maps.append(_PROC_MACRO_DEV_DEPENDENCIES) 187 if build: 188 all_dependency_maps.append(_BUILD_DEPENDENCIES) 189 if build_proc_macro: 190 all_dependency_maps.append(_BUILD_PROC_MACRO_DEPENDENCIES) 191 192 # Default to always using normal dependencies 193 if not all_dependency_maps: 194 all_dependency_maps.append(_NORMAL_DEPENDENCIES) 195 196 dependencies = _flatten_dependency_maps(all_dependency_maps).pop(package_name, None) 197 198 if not dependencies: 199 if dependencies == None: 200 fail("Tried to get all_crate_deps for package " + package_name + " but that package had no Cargo.toml file") 201 else: 202 return [] 203 204 crate_deps = list(dependencies.pop(_COMMON_CONDITION, {}).values()) 205 for condition, deps in dependencies.items(): 206 crate_deps += selects.with_or({ 207 tuple(_CONDITIONS[condition]): deps.values(), 208 "//conditions:default": [], 209 }) 210 211 return crate_deps 212 213def aliases( 214 normal = False, 215 normal_dev = False, 216 proc_macro = False, 217 proc_macro_dev = False, 218 build = False, 219 build_proc_macro = False, 220 package_name ={{ default_package_name }} ): 221 """Produces a map of Crate alias names to their original label 222 223 If no dependency kinds are specified, `normal` and `proc_macro` are used by default. 224 Setting any one flag will otherwise determine the contents of the returned dict. 225 226 Args: 227 normal (bool, optional): If True, normal dependencies are included in the 228 output list. 229 normal_dev (bool, optional): If True, normal dev dependencies will be 230 included in the output list.. 231 proc_macro (bool, optional): If True, proc_macro dependencies are included 232 in the output list. 233 proc_macro_dev (bool, optional): If True, dev proc_macro dependencies are 234 included in the output list. 235 build (bool, optional): If True, build dependencies are included 236 in the output list. 237 build_proc_macro (bool, optional): If True, build proc_macro dependencies are 238 included in the output list. 239 package_name (str, optional): The package name of the set of dependencies to look up. 240 Defaults to `native.package_name()` when unset. 241 242 Returns: 243 dict: The aliases of all associated packages 244 """ 245 if package_name == None: 246 package_name = native.package_name() 247 248 # Determine the relevant maps to use 249 all_aliases_maps = [] 250 if normal: 251 all_aliases_maps.append(_NORMAL_ALIASES) 252 if normal_dev: 253 all_aliases_maps.append(_NORMAL_DEV_ALIASES) 254 if proc_macro: 255 all_aliases_maps.append(_PROC_MACRO_ALIASES) 256 if proc_macro_dev: 257 all_aliases_maps.append(_PROC_MACRO_DEV_ALIASES) 258 if build: 259 all_aliases_maps.append(_BUILD_ALIASES) 260 if build_proc_macro: 261 all_aliases_maps.append(_BUILD_PROC_MACRO_ALIASES) 262 263 # Default to always using normal aliases 264 if not all_aliases_maps: 265 all_aliases_maps.append(_NORMAL_ALIASES) 266 all_aliases_maps.append(_PROC_MACRO_ALIASES) 267 268 aliases = _flatten_dependency_maps(all_aliases_maps).pop(package_name, None) 269 270 if not aliases: 271 return dict() 272 273 common_items = aliases.pop(_COMMON_CONDITION, {}).items() 274 275 # If there are only common items in the dictionary, immediately return them 276 if not len(aliases.keys()) == 1: 277 return dict(common_items) 278 279 # Build a single select statement where each conditional has accounted for the 280 # common set of aliases. 281 crate_aliases = {"//conditions:default": dict(common_items)} 282 for condition, deps in aliases.items(): 283 condition_triples = _CONDITIONS[condition] 284 for triple in condition_triples: 285 if triple in crate_aliases: 286 crate_aliases[triple].update(deps) 287 else: 288 crate_aliases.update({triple: dict(deps.items() + common_items)}) 289 290 return select(crate_aliases) 291 292############################################################################### 293# WORKSPACE MEMBER DEPS AND ALIASES 294############################################################################### 295 296_NORMAL_DEPENDENCIES ={% set deps_type = "normal" %}{% include "partials/module/deps_map.j2" %} 297 298_NORMAL_ALIASES ={% set deps_type = "normal" %}{% include "partials/module/aliases_map.j2" %} 299 300_NORMAL_DEV_DEPENDENCIES ={% set deps_type = "normal-dev" %}{% include "partials/module/deps_map.j2" %} 301 302_NORMAL_DEV_ALIASES ={% set deps_type = "normal-dev" %}{% include "partials/module/aliases_map.j2" %} 303 304_PROC_MACRO_DEPENDENCIES ={% set deps_type = "proc-macro" %}{% include "partials/module/deps_map.j2" %} 305 306_PROC_MACRO_ALIASES ={% set deps_type = "proc-macro-dev" %}{% include "partials/module/aliases_map.j2" %} 307 308_PROC_MACRO_DEV_DEPENDENCIES ={% set deps_type = "proc-macro-dev" %}{% include "partials/module/deps_map.j2" %} 309 310_PROC_MACRO_DEV_ALIASES ={% set deps_type = "normal-dev" %}{% include "partials/module/aliases_map.j2" %} 311 312_BUILD_DEPENDENCIES ={% set deps_type = "build" %}{% include "partials/module/deps_map.j2" %} 313 314_BUILD_ALIASES ={% set deps_type = "build" %}{% include "partials/module/aliases_map.j2" %} 315 316_BUILD_PROC_MACRO_DEPENDENCIES ={% set deps_type = "build-proc-macro" %}{% include "partials/module/deps_map.j2" %} 317 318_BUILD_PROC_MACRO_ALIASES ={% set deps_type = "build-proc-macro" %}{% include "partials/module/aliases_map.j2" %} 319 320_CONDITIONS = { 321{%- for condition , triples in platforms %} 322 "{{ condition | addslashes }} ":{{ triples | sort | json_encode | safe }} , 323{%- endfor %} 324} 325{% set current_vendor_mode = vendor_mode | default ( value = "remote" ) %}{% if current_vendor_mode == "remote" %} 326############################################################################### 327 328def crate_repositories(): 329 """A macro for defining repositories for all generated crates. 330 331 Returns: 332 A list of repos visible to the module through the module extension. 333 """ 334{%- for id , crate in context . crates %} 335{%- if not crate . repository %}{% continue %} {% endif %} 336{%- for repository_type , attrs in crate . repository %} 337{%- if repository_type in [ "Http" ] %} 338{% include "partials/module/repo_http.j2" %} 339{%- elif repository_type in [ "Git" ] %} 340{% include "partials/module/repo_git.j2" %} 341{%- elif repository_type in [ "Path" ] %} 342 343local_crate_mirror( 344name = " {{ crate_repository ( name = crate . name , version = crate . version ) }} ", 345{%- if generator %} 346generator = " {{ generator }} ", 347{%- endif %} 348options_json = " {{ local_crate_mirror_options_json ( crate_context = crate ) | replace ( from = '\' , to = '\\' ) | replace ( from = '"', to=' \ "' ) }} ", 349path = " {{ attrs . path }} ", 350) 351 352{%- else %} 353{{ throw ( message = "Unsupported checksum type: " ~ repository_type ) }} 354{%- endif %} 355{%- endfor %} 356{%- endfor %} 357return [ 358{%- for id in context . direct_deps %} 359{%- set crate = context . crates | get ( key = id ) %} 360{%- if crate . repository %} 361struct(repo=" {{ crate_repository ( name = crate . name , version = crate . version ) }} ", is_dev_dep = False), 362{%- endif %} 363{%- endfor %} 364{%- for id in context . direct_dev_deps %} 365{%- set crate = context . crates | get ( key = id ) %} 366{%- if crate . repository %} 367struct(repo = " {{ crate_repository ( name = crate . name , version = crate . version ) }} ", is_dev_dep = True), 368{%- endif %} 369{%- endfor %} 370] 371{%- endif %}