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