forked from AMDESE/linux-svsm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
253 lines (217 loc) · 8.37 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
extern crate bindgen;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const LIBCRT_SRC: &str = "external/libcrt";
const LIBM_SRC: &str = "external/libm";
const WOLFSSL_SRC: &str = "external/wolfssl";
const MSTPM_SRC: &str = "external/ms-tpm-20-ref";
const BUILD_DIR: &str = "external/build";
struct CommandWithArgs<'a>(String, Vec<&'a str>);
fn build_library(src_dir: &str, commands: &Vec<CommandWithArgs>) {
let pwd = env::current_dir().unwrap();
env::set_current_dir(src_dir).expect("failed to cd to build directory");
for cmd in commands {
Command::new(&cmd.0)
.args(&cmd.1)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
.unwrap();
}
env::set_current_dir(pwd).expect("failed to cd to build directory");
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=libtpm.h");
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let libcrt_src = format!("{}/{}", manifest_dir, LIBCRT_SRC);
let libm_src = format!("{}/{}", manifest_dir, LIBM_SRC);
let wolfssl_src = format!("{}/{}", manifest_dir, WOLFSSL_SRC);
let mstpm_src = Path::new(&format!("{}/{}", manifest_dir, MSTPM_SRC)).join("TPMCmd");
let build_dir = format!("{}/{}", manifest_dir, BUILD_DIR);
// Create $(pwd)/external/build/
fs::create_dir_all(&build_dir).expect("Failed to create build directory");
// Create $(pwd)/external/build/lib
let build_lib = Path::new(&build_dir).join("lib");
fs::create_dir_all(&build_lib).expect("Failed to create build directory");
/*let build = |src_dir: &str, build_dir: &str| {
let pwd = env::current_dir().unwrap();
env::set_current_dir(src_dir).expect("failed to change to build directory");
Command::new("make")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()
.unwrap();
let input_dir = Path::new(src_dir);
let output_fname = input_dir.file_name().unwrap().to_str().unwrap().to_owned() + ".a";
let input_path = input_dir.join(&output_fname);
let output_path = Path::new(build_dir).join("lib").join(&output_fname);
fs::copy(input_path, output_path).unwrap();
env::set_current_dir(pwd).expect("failed to cd to root directory");
};*/
let mut build_cmds_libcrt: Vec<CommandWithArgs> = Vec::new();
let install_prefix = format!("PREFIX={}", build_lib.to_str().unwrap());
build_cmds_libcrt.push(CommandWithArgs(
"make".to_string(),
["install", &install_prefix].to_vec(),
));
build_library(&libcrt_src, &build_cmds_libcrt);
let mut build_cmds_libm: Vec<CommandWithArgs> = Vec::new();
build_cmds_libm.push(CommandWithArgs(
"make".to_string(),
["install", &install_prefix].to_vec(),
));
build_library(&libm_src, &build_cmds_libm);
//build(&libcrt_src, &build_dir);
//build(&libm_src, &build_dir);
let common_config_args = [
"--enable-static",
"--disable-shared",
&format!("--prefix={}", build_dir),
];
let wolfssl_cflags = format!(
"-fno-stack-protector -fPIE -nostdinc -Wno-error=float-equal -I{} -I{} -I{}",
Path::new(&wolfssl_src).join("amd-svsm").to_str().unwrap(),
Path::new(&libcrt_src).join("include").to_str().unwrap(),
Path::new(&libm_src).join("include").to_str().unwrap()
);
let wolfssl_ldflags = format!(
"-L{} -lcrt -lm",
Path::new(&build_dir).join("lib").to_str().unwrap()
);
let cflags = format!("CFLAGS={}", wolfssl_cflags);
let ldflags = format!("LDFLAGS={}", wolfssl_ldflags);
let mut wolfssl_config_args = common_config_args.to_vec();
wolfssl_config_args.push("--enable-usersettings");
wolfssl_config_args.push("--host=x86_64");
wolfssl_config_args.push(&cflags);
wolfssl_config_args.push(&ldflags);
wolfssl_config_args.push("--disable-crypttests");
wolfssl_config_args.push("--disable-examples");
let mut build_cmds_wolfssl: Vec<CommandWithArgs> = Vec::new();
build_cmds_wolfssl.push(CommandWithArgs("./autogen.sh".to_string(), Vec::new()));
build_cmds_wolfssl.push(CommandWithArgs(
"./configure".to_string(),
wolfssl_config_args,
));
build_cmds_wolfssl.push(CommandWithArgs("make".to_string(), Vec::new()));
build_cmds_wolfssl.push(CommandWithArgs("make".to_string(), ["install"].to_vec()));
build_library(&wolfssl_src, &build_cmds_wolfssl);
let mstpm_cflags_inner = format!(
"-fno-stack-protector -DSIMULATION=NO -DEPHEMERAL_NV -DVTPM -DWOLF_USER_SETTINGS -I{} -I{} -I{} -I{}",
Path::new(&wolfssl_src).join("amd-svsm").to_str().unwrap(),
Path::new(&libcrt_src).join("include").to_str().unwrap(),
Path::new(&libm_src).join("include").to_str().unwrap(),
Path::new(&build_dir).join("include").to_str().unwrap(),
);
let lcrypto_cflags_inner = format!(
"-I{}",
Path::new(&build_dir)
.join("include")
.join("wolfssl")
.to_str()
.unwrap()
);
let libs_inner = format!(
"{} {}",
Path::new(&build_dir)
.join("lib")
.join("libcrt.a")
.to_str()
.unwrap(),
Path::new(&build_dir)
.join("lib")
.join("libm.a")
.to_str()
.unwrap()
);
let mstpm_cflags = format!("CFLAGS={}", mstpm_cflags_inner);
let lcrypto_cflags = format!("LIBCRYPTO_CFLAGS={}", lcrypto_cflags_inner);
let lcrypto_libs = format!(
"LIBCRYPTO_LIBS={}",
Path::new(&build_dir)
.join("lib")
.join("libwolfssl.a")
.to_str()
.unwrap()
);
let mstpm_libs = format!("LIBS={}", libs_inner);
let mut mstpm_config_args = common_config_args.to_vec();
mstpm_config_args.push("--with-crypto-engine=Wolf");
mstpm_config_args.push(&mstpm_cflags);
mstpm_config_args.push(&lcrypto_cflags);
mstpm_config_args.push(&lcrypto_libs);
mstpm_config_args.push(&mstpm_libs);
let mut build_cmds_mstpm: Vec<CommandWithArgs> = Vec::new();
build_cmds_mstpm.push(CommandWithArgs("./bootstrap".to_string(), Vec::new()));
build_cmds_mstpm.push(CommandWithArgs(
"./configure".to_string(),
mstpm_config_args,
));
build_cmds_mstpm.push(CommandWithArgs(
"make".to_string(),
["Platform/src/libplatform.a"].to_vec(),
));
build_cmds_mstpm.push(CommandWithArgs(
"make".to_string(),
["tpm/src/libtpm.a"].to_vec(),
));
build_cmds_mstpm.push(CommandWithArgs(
"cp".to_string(),
["Platform/src/libplatform.a", build_lib.to_str().unwrap()].to_vec(),
));
build_cmds_mstpm.push(CommandWithArgs(
"cp".to_string(),
["tpm/src/libtpm.a", build_lib.to_str().unwrap()].to_vec(),
));
build_library(&mstpm_src.to_str().unwrap(), &build_cmds_mstpm);
let bindings = bindgen::Builder::default()
.use_core()
.ctypes_prefix("cty")
.clang_arg("-DHASH_LIB=Wolf")
.clang_arg("-DSYM_LIB=Wolf")
.clang_arg("-DMATH_LIB=Wolf")
.clang_arg(format!(
"-I{}",
Path::new(&wolfssl_src).join("amd-svsm").to_str().unwrap()
))
.clang_arg(format!(
"-I{}",
Path::new(&build_dir).join("include").to_str().unwrap()
))
.clang_arg(format!(
"-I{}",
mstpm_src.join("tpm").join("include").to_str().unwrap()
))
.clang_arg(format!(
"-I{}",
mstpm_src
.join("tpm")
.join("include")
.join("prototypes")
.to_str()
.unwrap()
))
.clang_arg(format!(
"-I{}",
mstpm_src.join("Platform").join("include").to_str().unwrap()
))
.clang_arg(format!(
"-I{}",
mstpm_src
.join("Platform")
.join("include")
.join("prototypes")
.to_str()
.unwrap()
))
.header("libtpm.h")
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}