-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathrust.bzl
126 lines (108 loc) · 3.79 KB
/
rust.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0
"""Rules for writing per-crate build files for tock and libtock."""
BUILD_FILE = """###############################################################################
# @generated
# DO NOT MODIFY: This file is auto-generated by the crate_build function in
# //rules:rust.bzl.
###############################################################################
load("@rules_rust//rust:defs.bzl", "rust_library")
package(default_visibility = ["//visibility:public"])
rust_library(
name = "{name}",
srcs = {srcs},
compile_data = {compile_data},
crate_features = {crate_features},
crate_name = "{crate_name}",
crate_root = "{crate_root}",
edition = "{edition}",
rustc_flags = {rustc_flags},
tags = {tags},
version = "{version}",
deps = {deps},
)
{additional_build_file_content}
"""
COMPILE_DATA = """glob(
include = ["**"],
exclude = [
"**/* *",
"BUILD",
"BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
],
)"""
RUSTC_FLAGS = ["--cap-lints=allow"]
TAGS = """[
"cargo-bazel",
"crate-name={crate_name}",
"manual",
"noclippy",
"norustfmt",
]"""
def _format_list(items):
items = ["\"{}\",".format(i) for i in items]
return "[\n " + "\n ".join(items) + "\n ]"
def crate_build(
name,
srcs = [],
compile_data = [],
crate_features = [],
crate_name = None,
crate_root = "src/lib.rs",
edition = "2021",
rustc_flags = [],
tags = [],
version = "0.1.0",
deps = [],
additional_build_file_content = ""):
"""Create a BUILD file for a simple rust crate.
Args:
name: str; The name of the `rust_library` rule to generate.
srcs: list[str]; A list of sources for library (optional).
compile_data: list[str]; As list of files need at compile time (optional).
crate_features: list[str]; Features to enable in the crate.
crate_name: str; The name of the create (defaults to `name`).
crate_root: str; The root source of the crate (defaults to `src/lib.rs`).
edition: str; The rust edition to use (defaults to 2021).
rustc_flags: list[str]; Flags to pass to the compiler (optional).
tags: list[str]; Tags to for the library rule (optional).
version: str; The crate version (defaults to "0.1.0").
deps: list[str]; The dependencies needed by this library (optional).
additional_build_file_content: str; Additional content to place in the build
file after the automatic rules.
Returns:
str: The BUILD file content.
"""
subst = {
"name": name,
"crate_features": _format_list(crate_features),
"crate_root": crate_root,
"edition": edition,
"version": version,
"deps": _format_list(deps),
"additional_build_file_content": additional_build_file_content,
}
if srcs:
subst["srcs"] = _format_list(srcs)
else:
subst["srcs"] = "glob([\"**/*.rs\"])"
if compile_data:
subst["compile_data"] = _format_list(compile_data)
else:
subst["compile_data"] = COMPILE_DATA
if rustc_flags:
subst["rustc_flags"] = _format_list(rustc_flags)
else:
subst["rustc_flags"] = _format_list(RUSTC_FLAGS)
if crate_name:
subst["crate_name"] = crate_name.format(**subst).replace("-", "_")
else:
subst["crate_name"] = name.replace("-", "_")
if tags:
subst["tags"] = _format_list(tags)
else:
subst["tags"] = TAGS.format(**subst)
return BUILD_FILE.format(**subst)