fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
fed7e8f4
patching
1"""Utilities directly related to bootstrapping `cargo-bazel`""" 2 3_SRCS_TEMPLATE = """\ 4\"\"\"A generate file containing all source files used to produce `cargo-bazel`\"\"\" 5 6# Each source file is tracked as a target so the `cargo_bootstrap_repository` 7# rule will know to automatically rebuild if any of the sources changed. 8 9# Run 'bazel run //crate_universe/private:srcs_module.install' to regenerate. 10 11CARGO_BAZEL_SRCS = [ 12{ srcs } 13] 14""" 15 16def _format_src_label(label): 17 if label.workspace_name != "": 18 fail("`srcs` must be from the rules_rust repository") 19 return "Label(\"{}\")," . format ( str ( label ). lstrip ( "@" )) 20 21def _srcs_module_impl ( ctx ): 22srcs = [ _format_src_label ( src . owner ) for src in ctx . files . srcs ] 23if not srcs : 24fail ( "`srcs` cannot be empty" ) 25output = ctx . actions . declare_file ( ctx . label . name ) 26 27ctx . actions . write ( 28output = output , 29content = _SRCS_TEMPLATE . format ( 30srcs = "\n ". join ( srcs ), 31), 32) 33 34return DefaultInfo ( 35files = depset ([ output ]), 36) 37 38_srcs_module = rule ( 39doc = "A rule for writing a list of sources to a templated file" , 40implementation = _srcs_module_impl , 41attrs = { 42"srcs" : attr . label ( 43doc = "A filegroup of source files" , 44allow_files = True , 45), 46}, 47) 48 49_INSTALLER_TEMPLATE = """\ 50#!/usr/bin/env bash 51set -euo pipefail 52cp -f "{path}" "${{BUILD_WORKSPACE_DIRECTORY}}/{dest}" 53""" 54 55def _srcs_installer_impl ( ctx ): 56output = ctx . actions . declare_file ( ctx . label . name + ".sh" ) 57target_file = ctx . file . input 58dest = ctx . file . dest . short_path 59 60ctx . actions . write ( 61output = output , 62content = _INSTALLER_TEMPLATE . format ( 63path = target_file . short_path , 64dest = dest , 65), 66is_executable = True , 67) 68 69return DefaultInfo ( 70files = depset ([ output ]), 71runfiles = ctx . runfiles ( files = [ target_file ]), 72executable = output , 73) 74 75_srcs_installer = rule ( 76doc = "A rule for writing a file back to the repository" , 77implementation = _srcs_installer_impl , 78attrs = { 79"dest" : attr . label ( 80doc = "the file name to use for installation" , 81allow_single_file = True , 82mandatory = True , 83), 84"input" : attr . label ( 85doc = "The file to write back to the repository" , 86allow_single_file = True , 87mandatory = True , 88), 89}, 90executable = True , 91) 92 93def srcs_module ( name , dest , ** kwargs ): 94"""A helper rule to ensure the bootstrapping functionality of `cargo-bazel` is always up to date 95 96Args: 97name (str): The name of the sources module 98dest (str): The filename the module should be written as in the current package. 99**kwargs (dict): Additional keyword arguments 100""" 101tags = kwargs . pop ( "tags" , []) 102 103_srcs_module ( 104name = name , 105tags = tags , 106** kwargs 107) 108 109_srcs_installer ( 110name = name + ".install" , 111input = name , 112dest = dest , 113tags = tags , 114)