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