@@ -61,6 +61,7 @@ fn main() {
61
61
if dtls_backend. is_some ( ) {
62
62
multiple_backends = true ;
63
63
}
64
+ println ! ( "cargo:rerun-if-env-changed=MBEDTLS_LIBRARY_PATH" ) ;
64
65
dtls_backend = Some ( DtlsBackend :: MbedTls ) ;
65
66
}
66
67
if cfg ! ( feature = "dtls_backend_openssl" ) {
@@ -187,8 +188,28 @@ fn main() {
187
188
. unwrap ( )
188
189
. join ( "build" )
189
190
. join ( "library" ) ;
190
- build_config. env ( "CFLAGS" , format ! ( "-I{}" , mbedtls_include. to_str( ) . unwrap( ) ) ) ;
191
- build_config. env ( "LDFLAGS" , format ! ( "-L{}" , mbedtls_library_path. to_str( ) . unwrap( ) ) ) ;
191
+ build_config. env ( "MbedTLS_CFLAGS" , format ! ( "-I{}" , mbedtls_include. to_str( ) . unwrap( ) ) ) ;
192
+ build_config. env ( "MbedTLS_LIBS" , format ! ( "-lmbedtls -lmbedcrypto -lmbedx509 -L{}" , mbedtls_library_path. to_str( ) . unwrap( ) ) ) ;
193
+ } else {
194
+ // If we're not vendoring mbedtls, allow manually setting the mbedtls
195
+ // library path (required if linking statically, which is always the case
196
+ // when vendoring libcoap).
197
+ if let Some ( mbedtls_lib_path) = env:: var_os ( "MBEDTLS_LIBRARY_PATH" ) {
198
+ build_config. env ( "MbedTLS_LIBS" , format ! ( "-lmbedtls -lmbedcrypto -lmbedx509 -L{}" , mbedtls_lib_path. to_str( ) . unwrap( ) ) ) ;
199
+ if let Some ( mbedtls_include_path) = env:: var_os ( "MBEDTLS_INCLUDE_PATH" ) {
200
+ build_config. env ( "MbedTLS_CFLAGS" , format ! ( "-I{}" , mbedtls_include_path. to_str( ) . unwrap( ) ) ) ;
201
+ }
202
+ } else {
203
+ // mbedtls will get pkg-config support in the near future, prepare for that
204
+ if let Ok ( lib) = & pkg_config:: Config :: new ( ) . cargo_metadata ( false ) . probe ( "mbedtls" ) {
205
+ let mut lib_flags = "-lmbedtls -lmbedcrypto -lmbedx509" . to_string ( ) ;
206
+ lib_flags. push_str ( lib. link_paths . iter ( ) . map ( |x| format ! ( "-L{} " , x. display( ) ) ) . collect :: < String > ( ) . as_str ( ) ) ;
207
+ build_config. env ( "MbedTLS_LIBS" , lib_flags) ;
208
+ build_config. env ( "MbedTLS_CFLAGS" , lib. link_paths . iter ( ) . map ( |x| format ! ( "-I{}" , x. display( ) ) ) . collect :: < String > ( ) ) ;
209
+ } else {
210
+ println ! ( "cargo:warning=You have enabled libcoap vendoring with mbedtls, but haven't provided a static library path for mbedtls (MBEDTLS_LIBRARY_PATH environment variable is unset). Building might fail because of that." ) ;
211
+ }
212
+ }
192
213
}
193
214
} ,
194
215
DtlsBackend :: GnuTls => {
@@ -214,7 +235,9 @@ fn main() {
214
235
// Disable tests and examples as well as test coverage
215
236
. disable ( "tests" , None )
216
237
. disable ( "examples" , None )
217
- . disable ( "gcov" , None ) ;
238
+ . disable ( "gcov" , None )
239
+ // TODO allow multithreaded access
240
+ . disable ( "thread-safe" , None ) ;
218
241
219
242
// Enable debug symbols if enabled in Rust
220
243
match std:: env:: var_os ( "DEBUG" ) . unwrap ( ) . to_str ( ) . unwrap ( ) {
@@ -255,7 +278,7 @@ fn main() {
255
278
// Tell cargo to link libcoap.
256
279
println ! (
257
280
"cargo:rustc-link-lib={}{}" ,
258
- cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "" ) ,
281
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib= " ) ,
259
282
format!(
260
283
"coap-3-{}" ,
261
284
& dtls_backend
@@ -265,10 +288,10 @@ fn main() {
265
288
)
266
289
. as_str( )
267
290
) ;
268
-
291
+
269
292
// For the DTLS libraries, we need to tell cargo which external libraries to link.
270
293
// Note that these linker instructions have to be added *after* the linker instruction
271
- // for libcoap itself, as some linkers require dependencies to be in reverse order.
294
+ // for libcoap itself, as some linkers require dependencies to be in reverse order.
272
295
if let Some ( dtls_backend) = dtls_backend {
273
296
match dtls_backend {
274
297
DtlsBackend :: TinyDtls => {
@@ -280,15 +303,25 @@ fn main() {
280
303
DtlsBackend :: MbedTls => {
281
304
// If mbedtls is vendored, mbedtls-sys-auto already takes care of linking.
282
305
if env:: var_os ( "DEP_MBEDTLS_INCLUDE" ) . is_none ( ) {
283
- // We aren't using mbedtls-sys-auto if we aren't vendoring (as it doesn't support
306
+ // We aren't using mbedtls-sys-auto if we aren't vendoring (as it doesn't support
284
307
// mbedtls >= 3.0.0), so we need to tell cargo to link to mbedtls ourselves.
285
-
286
- // Try to find mbedtls using pkg-config
287
- if probe_library ( "mbedtls" ) . is_err ( ) {
288
- // couldn't find using pkg-config, just try linking with default library search path
289
- println ! ( "cargo:rustc-link-lib=mbedtls" ) ;
290
- println ! ( "cargo:rustc-link-lib=mbedx509" ) ;
291
- println ! ( "cargo:rustc-link-lib=mbedcrypto" ) ;
308
+
309
+ if let Some ( mbedtls_lib_path) = env:: var_os ( "MBEDTLS_LIBRARY_PATH" ) {
310
+ println ! ( "cargo:rustc-link-search=native={}" , mbedtls_lib_path. to_str( ) . unwrap( ) )
311
+ }
312
+ // Try to find mbedtls using pkg-config, will emit cargo link statements if successful
313
+ if env:: var_os ( "MBEDTLS_LIBRARY_PATH" ) . is_some ( ) || pkg_config:: Config :: new ( ) . statik ( cfg ! ( feature = "static" ) ) . probe ( "mbedtls" ) . is_err ( ) {
314
+ // couldn't find using pkg-config or MBEDTLS_LIBRARY_PATH was set, just try
315
+ // linking with given library search path
316
+ println ! ( "cargo:rustc-link-lib={}mbedtls" ,
317
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" )
318
+ ) ;
319
+ println ! ( "cargo:rustc-link-lib={}mbedx509" ,
320
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" )
321
+ ) ;
322
+ println ! ( "cargo:rustc-link-lib={}mbedcrypto" ,
323
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" )
324
+ ) ;
292
325
}
293
326
}
294
327
} ,
0 commit comments