@@ -57,6 +57,8 @@ fn main() -> std::io::Result<()> {
57
57
58
58
#[ cfg( any( feature = "interface" , feature = "operations" ) ) ]
59
59
mod common {
60
+ pub const CONFIG_FILE : & str = "custom_config.h" ;
61
+
60
62
#[ cfg( feature = "prefix" ) ]
61
63
use bindgen:: callbacks:: { ItemInfo , ParseCallbacks } ;
62
64
@@ -84,7 +86,7 @@ mod common {
84
86
// Configure the MbedTLS build for making Mbed Crypto
85
87
if !:: std:: process:: Command :: new ( mbedtls_config)
86
88
. arg ( "--write" )
87
- . arg ( & ( out_dir + "/config.h" ) )
89
+ . arg ( & ( out_dir + "/" + CONFIG_FILE ) )
88
90
. arg ( "crypto" )
89
91
. status ( )
90
92
. map_err ( |_| Error :: new ( ErrorKind :: Other , "configuring mbedtls failed" ) ) ?
@@ -120,70 +122,75 @@ mod common {
120
122
}
121
123
}
122
124
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 < ( ) > {
124
129
let header = mbed_include_dir. clone ( ) + "/psa/crypto.h" ;
125
130
131
+ println ! ( "using mbedtls include directory of: {}" , mbed_include_dir) ;
126
132
println ! ( "cargo:rerun-if-changed={}" , header) ;
127
133
128
134
let out_dir = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
129
135
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 ( )
132
138
. clang_arg ( format ! ( "-I{}" , out_dir) )
133
- . clang_arg ( "-DMBEDTLS_CONFIG_FILE=<config.h>" )
134
139
. clang_arg ( format ! ( "-I{}" , mbed_include_dir) )
135
140
. header ( "src/c/shim.h" )
136
141
. blocklist_type ( "max_align_t" )
137
142
. 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 ) ;
146
144
147
145
#[ 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
+ } ) ?;
164
162
165
163
let out_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
166
164
shim_bindings. write_to_file ( out_path. join ( "shim_bindings.rs" ) ) ?;
167
165
168
166
Ok ( ( ) )
169
167
}
170
168
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 > {
172
174
let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
173
175
174
176
let shimlib_name = "libmbedcryptoshim.a" ;
175
177
176
178
// Compile and package the shim library
177
- cc:: Build :: new ( )
179
+ let mut cfg = cc:: Build :: new ( ) ;
180
+ _ = cfg
178
181
. include ( & out_dir)
179
- . define ( "MBEDTLS_CONFIG_FILE" , "<config.h>" )
180
182
. include ( include_dir)
181
183
. file ( "./src/c/shim.c" )
182
184
. warnings ( true )
183
185
. flag ( "-Werror" )
184
186
. 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)
187
194
. map_err ( |_| Error :: new ( ErrorKind :: Other , "compiling shim.c failed" ) ) ?;
188
195
189
196
// Also link shim library
@@ -209,8 +216,8 @@ mod interface {
209
216
pub fn script_interface ( ) -> Result < ( ) > {
210
217
if let Ok ( include_dir) = env:: var ( "MBEDTLS_INCLUDE_DIR" ) {
211
218
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 ) ?;
214
221
Ok ( ( ) )
215
222
} else {
216
223
Err ( Error :: new (
@@ -253,7 +260,10 @@ mod operations {
253
260
// Build the MbedTLS libraries
254
261
let mbed_build_path = Config :: new ( & mbedtls_dir)
255
262
. cflag ( format ! ( "-I{}" , out_dir) )
256
- . cflag ( "-DMBEDTLS_CONFIG_FILE='<config.h>'" )
263
+ . cflag ( format ! (
264
+ "-DMBEDTLS_CONFIG_FILE='\" {}\" '" ,
265
+ common:: CONFIG_FILE
266
+ ) )
257
267
. define ( "ENABLE_PROGRAMS" , "OFF" )
258
268
. define ( "ENABLE_TESTING" , "OFF" )
259
269
. build ( ) ;
@@ -276,22 +286,25 @@ mod operations {
276
286
let lib;
277
287
let statically;
278
288
let include;
289
+ let external_mbedtls;
279
290
280
291
if env:: var ( "MBEDTLS_LIB_DIR" ) . is_err ( ) ^ env:: var ( "MBEDTLS_INCLUDE_DIR" ) . is_err ( ) {
281
292
return Err ( Error :: new (
282
293
ErrorKind :: Other ,
283
294
"both environment variables MBEDTLS_LIB_DIR and MBEDTLS_INCLUDE_DIR need to be set for operations feature" ,
284
295
) ) ;
285
296
}
286
- common:: configure_mbed_crypto ( ) ?;
287
297
if let ( Ok ( lib_dir) , Ok ( include_dir) ) =
288
298
( env:: var ( "MBEDTLS_LIB_DIR" ) , env:: var ( "MBEDTLS_INCLUDE_DIR" ) )
289
299
{
300
+ println ! ( "Found environment varibales, using external MbedTLS" ) ;
290
301
lib = lib_dir;
291
302
include = include_dir;
292
303
statically = cfg ! ( feature = "static" ) || env:: var ( "MBEDCRYPTO_STATIC" ) . is_ok ( ) ;
304
+ external_mbedtls = true ;
293
305
} else {
294
306
println ! ( "Did not find environment variables, building MbedTLS!" ) ;
307
+ common:: configure_mbed_crypto ( ) ?;
295
308
let mut mbed_lib_dir = compile_mbed_crypto ( ) ?;
296
309
let mut mbed_include_dir = mbed_lib_dir. clone ( ) ;
297
310
mbed_lib_dir. push ( "lib" ) ;
@@ -300,12 +313,13 @@ mod operations {
300
313
lib = mbed_lib_dir. to_str ( ) . unwrap ( ) . to_owned ( ) ;
301
314
include = mbed_include_dir. to_str ( ) . unwrap ( ) . to_owned ( ) ;
302
315
statically = true ;
316
+ external_mbedtls = false ;
303
317
}
304
318
305
319
// Linking to PSA Crypto library is only needed for the operations.
306
320
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 ) {
309
323
Ok ( _) => Ok ( ( ) ) ,
310
324
Err ( e) => Err ( e) ,
311
325
}
@@ -320,10 +334,12 @@ mod operations {
320
334
"both environment variables MBEDTLS_LIB_DIR and MBEDTLS_INCLUDE_DIR need to be set for operations feature" ,
321
335
) ) ;
322
336
}
323
- common :: configure_mbed_crypto ( ) ? ;
337
+
324
338
if let ( Ok ( lib_dir) , Ok ( include_dir) ) =
325
339
( env:: var ( "MBEDTLS_LIB_DIR" ) , env:: var ( "MBEDTLS_INCLUDE_DIR" ) )
326
340
{
341
+ println ! ( "Building with external MBEDTLS" ) ;
342
+
327
343
// Request rustc to link the Mbed Crypto library
328
344
let link_type = if cfg ! ( feature = "static" ) || env:: var ( "MBEDCRYPTO_STATIC" ) . is_ok ( ) {
329
345
"static"
@@ -333,19 +349,20 @@ mod operations {
333
349
println ! ( "cargo:rustc-link-search=native={}" , lib_dir) ;
334
350
println ! ( "cargo:rustc-link-lib={}=mbedcrypto" , link_type) ;
335
351
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 ) ?;
338
354
} else {
339
355
println ! ( "Did not find environment variables, building MbedTLS!" ) ;
356
+ common:: configure_mbed_crypto ( ) ?;
340
357
let mut mbed_lib_dir = compile_mbed_crypto ( ) ?;
341
358
let mut mbed_include_dir = mbed_lib_dir. clone ( ) ;
342
359
mbed_lib_dir. push ( "lib" ) ;
343
360
mbed_include_dir. push ( "include" ) ;
344
361
let main_lib = mbed_lib_dir. join ( "libmbedcrypto.a" ) ;
345
362
346
363
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 ) ?;
349
366
350
367
// Modify and copy the libraries into a new directory.
351
368
let llib_path = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) . join ( "llib" ) ;
0 commit comments