-
Notifications
You must be signed in to change notification settings - Fork 471
/
build.rs
123 lines (104 loc) · 3.99 KB
/
build.rs
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
#[macro_use]
extern crate cfg_if;
cfg_if! {
if #[cfg(feature = "edition-2015")] {
extern crate env_logger;
extern crate prost_build;
}
}
use std::env;
use std::fs;
use std::path::PathBuf;
fn main() {
let _ = env_logger::init();
// The source directory. The indirection is necessary in order to support the tests-2015 crate,
// which sets the current directory to tests-2015 during build script evaluation.
let src = PathBuf::from("../tests/src");
let includes = &[src.clone()];
let mut config = prost_build::Config::new();
// Generate BTreeMap fields for all messages. This forces encoded output to be consistent, so
// that encode/decode roundtrips can use encoded output for comparison. Otherwise trying to
// compare based on the Rust PartialEq implementations is difficult, due to presence of NaN
// values.
//
// Note nostd collections implies Btree everywhere anyways
cfg_if! {
if #[cfg(feature = "nostd-collections")] {
config.use_alloc_collections_lib();
} else {
config.btree_map(&["."]);
}
}
// Tests for custom attributes
config.type_attribute("Foo.Bar_Baz.Foo_barBaz", "#[derive(Eq, PartialOrd, Ord)]");
config.type_attribute(
"Foo.Bar_Baz.Foo_barBaz.fuzz_buster",
"#[derive(Eq, PartialOrd, Ord)]",
);
config.type_attribute("Foo.Custom.Attrs.Msg", "#[allow(missing_docs)]");
config.type_attribute("Foo.Custom.Attrs.Msg.field", "/// Oneof docs");
config.type_attribute("Foo.Custom.Attrs.AnEnum", "#[allow(missing_docs)]");
config.type_attribute("Foo.Custom.Attrs.AnotherEnum", "/// Oneof docs");
config.type_attribute(
"Foo.Custom.OneOfAttrs.Msg.field",
"#[derive(Eq, PartialOrd, Ord)]",
);
config.field_attribute("Foo.Custom.Attrs.AnotherEnum.C", "/// The C docs");
config.field_attribute("Foo.Custom.Attrs.AnotherEnum.D", "/// The D docs");
config.field_attribute("Foo.Custom.Attrs.Msg.field.a", "/// Oneof A docs");
config.field_attribute("Foo.Custom.Attrs.Msg.field.b", "/// Oneof B docs");
config
.compile_protos(&[src.join("ident_conversion.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("nesting.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("recursive_oneof.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("custom_attributes.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("oneof_attributes.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("no_unused_results.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("default_enum_value.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("groups.proto")], includes)
.unwrap();
config
.compile_protos(&[src.join("well_known_types.proto")], includes)
.unwrap();
config
.compile_protos(
&[src.join("packages/widget_factory.proto")],
&[src.join("packages")],
)
.unwrap();
let out_dir =
&PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"))
.join("extern_paths");
fs::create_dir_all(out_dir).expect("failed to create prefix directory");
config.out_dir(out_dir);
// Compile some of the module examples as an extern path. The extern path syntax is edition
// specific, since the way crate-internal fully qualified paths has changed.
cfg_if! {
if #[cfg(feature = "edition-2015")] {
const EXTERN_PATH: &str = "::packages::gizmo";
} else {
const EXTERN_PATH: &str = "crate::packages::gizmo";
}
};
config.extern_path(".packages.gizmo", EXTERN_PATH);
config
.compile_protos(
&[src.join("packages").join("widget_factory.proto")],
&[src.join("packages")],
)
.unwrap();
}