Skip to content

Commit ea0499c

Browse files
billatarmtgonzalezorlandoarm
authored andcommitted
psa-crypto-sys: dont configure mbedtls on external builds
When setting env vars to use a external mbedTLS library, for example provided by a system distro, like so: MBEDTLS_INCLUDE_DIR="/usr/include/" MBEDTLS_LIB_DIR="/usr/lib64" The build would configure the vendored mbedtls. Fix this by only configuring the vendored mbedtls when being used. This would allow clones without the submodule using a system supplied mbedtls to succeed. Signed-off-by: Bill Roberts <bill.roberts@arm.com>
1 parent f2d2351 commit ea0499c

File tree

1 file changed

+62
-45
lines changed

1 file changed

+62
-45
lines changed

psa-crypto-sys/build.rs

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ fn main() -> std::io::Result<()> {
5757

5858
#[cfg(any(feature = "interface", feature = "operations"))]
5959
mod common {
60+
pub const CONFIG_FILE: &str = "custom_config.h";
61+
6062
#[cfg(feature = "prefix")]
6163
use bindgen::callbacks::{ItemInfo, ParseCallbacks};
6264

@@ -84,7 +86,7 @@ mod common {
8486
// Configure the MbedTLS build for making Mbed Crypto
8587
if !::std::process::Command::new(mbedtls_config)
8688
.arg("--write")
87-
.arg(&(out_dir + "/config.h"))
89+
.arg(&(out_dir + "/" + CONFIG_FILE))
8890
.arg("crypto")
8991
.status()
9092
.map_err(|_| Error::new(ErrorKind::Other, "configuring mbedtls failed"))?
@@ -120,70 +122,75 @@ mod common {
120122
}
121123
}
122124

123-
pub fn generate_mbed_crypto_bindings(mbed_include_dir: String) -> Result<()> {
125+
pub fn generate_mbed_crypto_bindings(
126+
mbed_include_dir: String,
127+
external_mbedtls: bool,
128+
) -> Result<()> {
124129
let header = mbed_include_dir.clone() + "/psa/crypto.h";
125130

131+
println!("using mbedtls include directory of: {}", mbed_include_dir);
126132
println!("cargo:rerun-if-changed={}", header);
127133

128134
let out_dir = env::var("OUT_DIR").unwrap();
129135

130-
#[cfg(not(feature = "prefix"))]
131-
let shim_bindings = bindgen::Builder::default()
136+
// Common shim builder settings
137+
let mut shim_builder = bindgen::Builder::default()
132138
.clang_arg(format!("-I{}", out_dir))
133-
.clang_arg("-DMBEDTLS_CONFIG_FILE=<config.h>")
134139
.clang_arg(format!("-I{}", mbed_include_dir))
135140
.header("src/c/shim.h")
136141
.blocklist_type("max_align_t")
137142
.generate_comments(false)
138-
.size_t_is_usize(true)
139-
.generate()
140-
.map_err(|_| {
141-
Error::new(
142-
ErrorKind::Other,
143-
"Unable to generate bindings to mbed crypto",
144-
)
145-
})?;
143+
.size_t_is_usize(true);
146144

147145
#[cfg(feature = "prefix")]
148-
let shim_bindings = bindgen::Builder::default()
149-
.clang_arg(format!("-I{}", out_dir))
150-
.clang_arg("-DMBEDTLS_CONFIG_FILE=<config.h>")
151-
.clang_arg(format!("-I{}", mbed_include_dir))
152-
.header("src/c/shim.h")
153-
.blocklist_type("max_align_t")
154-
.generate_comments(false)
155-
.size_t_is_usize(true)
156-
.parse_callbacks(Box::new(RenameCallbacks {}))
157-
.generate()
158-
.map_err(|_| {
159-
Error::new(
160-
ErrorKind::Other,
161-
"Unable to generate bindings to mbed crypto",
162-
)
163-
})?;
146+
{
147+
shim_builder = shim_builder.parse_callbacks(Box::new(RenameCallbacks {}));
148+
}
149+
150+
if !external_mbedtls {
151+
shim_builder =
152+
shim_builder.clang_arg(format!("-DMBEDTLS_CONFIG_FILE=\"{}\"", CONFIG_FILE));
153+
}
154+
155+
// Build the bindings
156+
let shim_bindings = shim_builder.generate().map_err(|_| {
157+
Error::new(
158+
ErrorKind::Other,
159+
"Unable to generate bindings to mbed crypto",
160+
)
161+
})?;
164162

165163
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
166164
shim_bindings.write_to_file(out_path.join("shim_bindings.rs"))?;
167165

168166
Ok(())
169167
}
170168

171-
pub fn compile_shim_library(include_dir: String, metadata: bool) -> Result<PathBuf> {
169+
pub fn compile_shim_library(
170+
include_dir: String,
171+
metadata: bool,
172+
external_mbedtls: bool,
173+
) -> Result<PathBuf> {
172174
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
173175

174176
let shimlib_name = "libmbedcryptoshim.a";
175177

176178
// Compile and package the shim library
177-
cc::Build::new()
179+
let mut cfg = cc::Build::new();
180+
_ = cfg
178181
.include(&out_dir)
179-
.define("MBEDTLS_CONFIG_FILE", "<config.h>")
180182
.include(include_dir)
181183
.file("./src/c/shim.c")
182184
.warnings(true)
183185
.flag("-Werror")
184186
.opt_level(2)
185-
.cargo_metadata(metadata)
186-
.try_compile(shimlib_name)
187+
.cargo_metadata(metadata);
188+
189+
if !external_mbedtls {
190+
_ = cfg.flag(&format!("-DMBEDTLS_CONFIG_FILE=\"{}\"", CONFIG_FILE));
191+
}
192+
193+
cfg.try_compile(shimlib_name)
187194
.map_err(|_| Error::new(ErrorKind::Other, "compiling shim.c failed"))?;
188195

189196
// Also link shim library
@@ -209,8 +216,8 @@ mod interface {
209216
pub fn script_interface() -> Result<()> {
210217
if let Ok(include_dir) = env::var("MBEDTLS_INCLUDE_DIR") {
211218
common::configure_mbed_crypto()?;
212-
common::generate_mbed_crypto_bindings(include_dir.clone())?;
213-
let _ = common::compile_shim_library(include_dir, true)?;
219+
common::generate_mbed_crypto_bindings(include_dir.clone(), false)?;
220+
let _ = common::compile_shim_library(include_dir, true, false)?;
214221
Ok(())
215222
} else {
216223
Err(Error::new(
@@ -253,7 +260,10 @@ mod operations {
253260
// Build the MbedTLS libraries
254261
let mbed_build_path = Config::new(&mbedtls_dir)
255262
.cflag(format!("-I{}", out_dir))
256-
.cflag("-DMBEDTLS_CONFIG_FILE='<config.h>'")
263+
.cflag(format!(
264+
"-DMBEDTLS_CONFIG_FILE='\"{}\"'",
265+
common::CONFIG_FILE
266+
))
257267
.define("ENABLE_PROGRAMS", "OFF")
258268
.define("ENABLE_TESTING", "OFF")
259269
.build();
@@ -276,22 +286,25 @@ mod operations {
276286
let lib;
277287
let statically;
278288
let include;
289+
let external_mbedtls;
279290

280291
if env::var("MBEDTLS_LIB_DIR").is_err() ^ env::var("MBEDTLS_INCLUDE_DIR").is_err() {
281292
return Err(Error::new(
282293
ErrorKind::Other,
283294
"both environment variables MBEDTLS_LIB_DIR and MBEDTLS_INCLUDE_DIR need to be set for operations feature",
284295
));
285296
}
286-
common::configure_mbed_crypto()?;
287297
if let (Ok(lib_dir), Ok(include_dir)) =
288298
(env::var("MBEDTLS_LIB_DIR"), env::var("MBEDTLS_INCLUDE_DIR"))
289299
{
300+
println!("Found environment varibales, using external MbedTLS");
290301
lib = lib_dir;
291302
include = include_dir;
292303
statically = cfg!(feature = "static") || env::var("MBEDCRYPTO_STATIC").is_ok();
304+
external_mbedtls = true;
293305
} else {
294306
println!("Did not find environment variables, building MbedTLS!");
307+
common::configure_mbed_crypto()?;
295308
let mut mbed_lib_dir = compile_mbed_crypto()?;
296309
let mut mbed_include_dir = mbed_lib_dir.clone();
297310
mbed_lib_dir.push("lib");
@@ -300,12 +313,13 @@ mod operations {
300313
lib = mbed_lib_dir.to_str().unwrap().to_owned();
301314
include = mbed_include_dir.to_str().unwrap().to_owned();
302315
statically = true;
316+
external_mbedtls = false;
303317
}
304318

305319
// Linking to PSA Crypto library is only needed for the operations.
306320
link_to_lib(lib, statically);
307-
common::generate_mbed_crypto_bindings(include.clone())?;
308-
match common::compile_shim_library(include, false) {
321+
common::generate_mbed_crypto_bindings(include.clone(), external_mbedtls)?;
322+
match common::compile_shim_library(include, false, external_mbedtls) {
309323
Ok(_) => Ok(()),
310324
Err(e) => Err(e),
311325
}
@@ -320,10 +334,12 @@ mod operations {
320334
"both environment variables MBEDTLS_LIB_DIR and MBEDTLS_INCLUDE_DIR need to be set for operations feature",
321335
));
322336
}
323-
common::configure_mbed_crypto()?;
337+
324338
if let (Ok(lib_dir), Ok(include_dir)) =
325339
(env::var("MBEDTLS_LIB_DIR"), env::var("MBEDTLS_INCLUDE_DIR"))
326340
{
341+
println!("Building with external MBEDTLS");
342+
327343
// Request rustc to link the Mbed Crypto library
328344
let link_type = if cfg!(feature = "static") || env::var("MBEDCRYPTO_STATIC").is_ok() {
329345
"static"
@@ -333,19 +349,20 @@ mod operations {
333349
println!("cargo:rustc-link-search=native={}", lib_dir);
334350
println!("cargo:rustc-link-lib={}=mbedcrypto", link_type);
335351

336-
common::generate_mbed_crypto_bindings(include_dir.clone())?;
337-
let _ = common::compile_shim_library(include_dir, true)?;
352+
common::generate_mbed_crypto_bindings(include_dir.clone(), true)?;
353+
let _ = common::compile_shim_library(include_dir, true, true)?;
338354
} else {
339355
println!("Did not find environment variables, building MbedTLS!");
356+
common::configure_mbed_crypto()?;
340357
let mut mbed_lib_dir = compile_mbed_crypto()?;
341358
let mut mbed_include_dir = mbed_lib_dir.clone();
342359
mbed_lib_dir.push("lib");
343360
mbed_include_dir.push("include");
344361
let main_lib = mbed_lib_dir.join("libmbedcrypto.a");
345362

346363
let include = mbed_include_dir.to_str().unwrap().to_owned();
347-
common::generate_mbed_crypto_bindings(include.clone())?;
348-
let shim_lib = common::compile_shim_library(include, false)?;
364+
common::generate_mbed_crypto_bindings(include.clone(), false)?;
365+
let shim_lib = common::compile_shim_library(include, false, false)?;
349366

350367
// Modify and copy the libraries into a new directory.
351368
let llib_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("llib");

0 commit comments

Comments
 (0)