forked from ctron/openshift-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
173 lines (161 loc) · 7.83 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const MIN: usize = 2;
const MAX: usize = 6;
fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::io::Write;
let enabled_version = {
let mut enabled_versions =
(MIN..=MAX).filter(|v| std::env::var(format!("CARGO_FEATURE_V4_{}", v)).is_ok());
let v1 = enabled_versions.next().expect("\n\
None of the v4_* features are enabled on the openshift-openapi crate.\n\
\n\
The openshift-openapi crate requires a feature to be enabled to indicate which version of OpenShift it should support.\n\
\n\
If you're using openshift-openapi in a binary crate, enable the feature corresponding to the minimum version of API server that you want to support. \
It may be possible that your binary crate does not directly depend on openshift-openapi. In this case, add a dependency on openshift-openapi, then enable \
the corresponding feature.\n\
\n\
If you're using openshift-openapi in a library crate, add a dev-dependency on openshift-openapi and enable one of the features there. This way \
the feature will be enabled when buildings tests and examples of your library, but not when building the library itself. \
It may be possible that your library crate does not directly depend on openshift-openapi. In this case, add a dev-dependency on openshift-openapi, \
then enable the corresponding feature.\n\
\n\
Library crates *must not* enable any features in their direct dependency on openshift-openapi, only in their dev-dependency. \
The choice of Kubernetes version to support should be left to the final binary crate, so only the binary crate should enable a specific feature. \
If library crates also enable features, it can cause multiple features to be enabled simultaneously, which openshift-openapi does not support.\n\
\n\
If you want to restrict your library crate to support only a single specific version or range of versions of Kubernetes, \
please use the openshift_* version-specific macros to emit different code based on which feature gets enabled in the end.\
");
if let Some(v2) = enabled_versions.next() {
panic!(
"\n\
Both v4_{} and v4_{} features are enabled on the openshift-openapi crate. Only one feature can be enabled at the same time.\n\
\n\
The feature indicates which version of Kubernetes the openshift-openapi crate should support.\n\
\n\
If you have enabled both of these features yourself, please remove one of them. If you are writing a library crate, \
do not enable any features at all. Library crates *must not* enable any features on the openshift-openapi crate.\n\
\n\
If you have not enabled one or both of these features yourself, then one of the library crates in your dependency graph *has*. \
Locate which library crates in your dependency graph depend on openshift-openapi and enable one of its features, and file a bug against them. \
You can search your Cargo.lock for \"openshift-openapi\" to discover these crates.\
",
v1,
v2,
);
}
v1
};
println!(
"cargo:version={}",
0x00_04_00_00_u32 | ((enabled_version as u32) << 8)
);
let mut f = {
let mut out_file: std::path::PathBuf = std::env::var_os("OUT_DIR")
.ok_or_else(|| "OUT_DIR not set")?
.into();
out_file.push("conditional_compilation_macros.rs");
std::io::BufWriter::new(std::fs::File::create(out_file)?)
};
for v in MIN..=MAX {
writeln!(f, "/// This macro evaluates to its contents if the `v4_{}` feature is enabled, otherwise it evaluates to nothing.", v)?;
writeln!(f, "///")?;
writeln!(f, "/// # Examples")?;
writeln!(f, "///")?;
writeln!(f, "/// ```rust")?;
writeln!(f, "/// # #[macro_use] extern crate openshift_openapi;")?;
writeln!(f, "/// openshift_if_4_{}! {{", v)?;
writeln!(f, "/// use openshift_openapi::api::build::v1 as api;")?;
writeln!(f, "/// }}")?;
writeln!(f, "/// ```")?;
if enabled_version == v {
writeln!(
f,
"#[macro_export] macro_rules! openshift_if_4_{} {{ ($($tt:tt)*) => {{ $($tt)* }}; }}",
v
)?;
} else {
writeln!(
f,
"#[macro_export] macro_rules! openshift_if_4_{} {{ ($($tt:tt)*) => {{ }}; }}",
v
)?;
}
writeln!(f)?;
writeln!(f, "/// This macro evaluates to its contents if the `v4_{}` or higher feature is enabled, otherwise it evaluates to nothing.", v)?;
if enabled_version >= v {
writeln!(
f,
"#[macro_export] macro_rules! openshift_if_ge_4_{} {{ ($($tt:tt)*) => {{ $($tt)* }}; }}",
v
)?;
} else {
writeln!(
f,
"#[macro_export] macro_rules! openshift_if_ge_4_{} {{ ($($tt:tt)*) => {{ }}; }}",
v
)?;
}
writeln!(f)?;
writeln!(f, "/// This macro evaluates to its contents if the `v4_{}` or lower feature is enabled, otherwise it evaluates to nothing.", v)?;
if enabled_version <= v {
writeln!(
f,
"#[macro_export] macro_rules! openshift_if_le_4_{} {{ ($($tt:tt)*) => {{ $($tt)* }}; }}",
v
)?;
} else {
writeln!(
f,
"#[macro_export] macro_rules! openshift_if_le_4_{} {{ ($($tt:tt)*) => {{ }}; }}",
v
)?;
}
writeln!(f)?;
}
writeln!(
f,
"/// A macro that emits a `match` expr with the given test expression and arms."
)?;
writeln!(f, "/// The match arms can be annotated with the other conditional compilation macros in this crate so that they're only emitted")?;
writeln!(f, "/// if the predicate is true.")?;
writeln!(f, "#[macro_export] macro_rules! openshift_match {{")?;
writeln!(
f,
" (@inner {{ $test:expr }} {{ $($arms:tt)* }} {{ }}) => {{"
)?;
writeln!(f, " match $test {{ $($arms)* }}")?;
writeln!(f, " }};")?;
for v in MIN..=MAX {
writeln!(f)?;
for (name_suffix, enabled) in &[
("", enabled_version == v),
("_ge", enabled_version >= v),
("_le", enabled_version <= v),
] {
writeln!(f, " (@inner {{ $test:expr }} {{ $($arms:tt)* }} {{ openshift_if{}_1_{}!($($arm:tt)*), $($rest:tt)* }}) => {{", name_suffix, v)?;
if *enabled {
writeln!(f, " openshift_match!(@inner {{ $test }} {{ $($arms)* }} {{ $($arm)*, $($rest)* }})")?;
} else {
writeln!(
f,
" openshift_match!(@inner {{ $test }} {{ $($arms)* }} {{ $($rest)* }})"
)?;
}
writeln!(f, " }};")?;
}
}
writeln!(f)?;
writeln!(f, " (@inner {{ $test:expr }} {{ $($arms:tt)* }} {{ $next_pat:pat $(if $cond:expr)? => $next_expr:expr, $($rest:tt)* }}) => {{")?;
writeln!(f, " openshift_match!(@inner {{ $test }} {{ $($arms)* $next_pat $(if $cond)? => $next_expr, }} {{ $($rest)* }})")?;
writeln!(f, " }};")?;
writeln!(f)?;
writeln!(f, " ($test:expr, {{ $($rest:tt)* }}) => {{")?;
writeln!(
f,
" openshift_match!(@inner {{ $test }} {{ }} {{ $($rest)* }})"
)?;
writeln!(f, " }};")?;
writeln!(f, "}}")?;
Ok(())
}