@@ -37,13 +37,105 @@ impl ToString for DtlsBackend {
37
37
}
38
38
}
39
39
40
+ fn get_builder_espidf ( ) -> bindgen:: Builder {
41
+ embuild:: espidf:: sysenv:: output ( ) ;
42
+ let esp_idf_path = embuild:: espidf:: sysenv:: idf_path ( ) . ok_or ( "missing IDF path" ) . unwrap ( ) ;
43
+ let esp_idf_buildroot = env:: var ( "DEP_ESP_IDF_ROOT" ) . unwrap ( ) ;
44
+ let esp_include_path = embuild:: espidf:: sysenv:: cincl_args ( ) . ok_or ( "missing IDF cincl args" ) . unwrap ( ) ;
45
+ let embuild_env = embuild:: espidf:: sysenv:: env_path ( ) . ok_or ( "missing IDF env path" ) . unwrap ( ) ;
46
+ let esp_arch = env:: var ( "CARGO_CFG_TARGET_ARCH" ) . unwrap ( ) ;
47
+ let cfg_flags = embuild:: espidf:: sysenv:: cfg_args ( ) . ok_or ( "missing cfg flags from IDF" ) . unwrap ( ) ;
48
+
49
+ // Determine compiler path
50
+ env:: set_var ( "PATH" , & embuild_env) ;
51
+ let cmake_info = embuild:: cmake:: Query :: new (
52
+ & Path :: new ( & esp_idf_buildroot) . join ( "build" ) ,
53
+ "cargo" ,
54
+ & [ embuild:: cmake:: file_api:: ObjKind :: Codemodel , embuild:: cmake:: file_api:: ObjKind :: Toolchains , embuild:: cmake:: file_api:: ObjKind :: Cache ] ,
55
+ ) . unwrap ( ) . get_replies ( ) . unwrap ( ) ;
56
+ let compiler = cmake_info
57
+ . get_toolchains ( ) . map_err ( |_e| "Can't get toolchains" )
58
+ . and_then ( |mut t| {
59
+ t. take ( embuild:: cmake:: file_api:: codemodel:: Language :: C )
60
+ . ok_or ( "No C toolchain" )
61
+ } )
62
+ . and_then ( |t| {
63
+ t. compiler
64
+ . path
65
+ . ok_or ( "No compiler path set" )
66
+ } ) . unwrap ( ) ;
67
+
68
+ // Parse include arguments
69
+ let arg_splitter = regex:: Regex :: new ( r##"(?:[^\\]"[^"]*[^\\]")?(\s)"## ) . unwrap ( ) ;
70
+ let apostrophe_remover = regex:: Regex :: new ( r##"^"(?<content>.*)"$"## ) . unwrap ( ) ;
71
+ let esp_clang_args = arg_splitter. split (
72
+ esp_include_path. args . as_str ( )
73
+ ) . map ( |x| apostrophe_remover. replace ( x. trim ( ) , "$content" ) . to_string ( ) ) . collect :: < Vec < String > > ( ) ;
74
+ let bindgen_builder = embuild:: bindgen:: Factory {
75
+ clang_args : esp_clang_args. clone ( ) ,
76
+ linker : Some ( compiler) ,
77
+ mcu : None ,
78
+ force_cpp : false ,
79
+ sysroot : None
80
+ } . builder ( ) . unwrap ( ) ;
81
+
82
+ let clang_target = if esp_arch. starts_with ( "riscv32" ) { "riscv32" } else { esp_arch. as_str ( ) } ;
83
+ let short_target = if esp_arch. starts_with ( "riscv32" ) { "riscv" } else { esp_arch. as_str ( ) } ;
84
+ let target_mcu = if cfg_flags. get ( "esp32" ) . is_some ( ) { "esp32" }
85
+ else if cfg_flags. get ( "esp32s2" ) . is_some ( ) { "esp32s2" }
86
+ else if cfg_flags. get ( "esp32s3" ) . is_some ( ) { "esp32s3" }
87
+ else if cfg_flags. get ( "esp32c3" ) . is_some ( ) { "esp32c3" }
88
+ else if cfg_flags. get ( "esp32c2" ) . is_some ( ) { "esp32c2" }
89
+ else if cfg_flags. get ( "esp32h2" ) . is_some ( ) { "esp32h2" }
90
+ else if cfg_flags. get ( "esp32c5" ) . is_some ( ) { "esp32c5" }
91
+ else if cfg_flags. get ( "esp32c6" ) . is_some ( ) { "esp32c6" }
92
+ else if cfg_flags. get ( "esp32p4" ) . is_some ( ) { "esp32p4" }
93
+ else { panic ! ( "unknown ESP target MCU, please add target to libcoap-sys build.rs file!" ) } ;
94
+
95
+ return bindgen_builder
96
+ . clang_args ( & esp_clang_args)
97
+ . clang_arg ( "-target" )
98
+ . clang_arg ( clang_target)
99
+ . clang_arg ( "-DESP_PLATFORM" )
100
+ . clang_arg ( "-DLWIP_IPV4=1" )
101
+ . clang_arg ( "-DLWIP_IPV6=1" )
102
+ . clang_arg ( format ! ( "-I{}/components/lwip/lwip/src/include" , esp_idf_path) )
103
+ . clang_arg ( format ! ( "-I{}/components/lwip/port/freertos/include" , esp_idf_path) )
104
+ . clang_arg ( format ! ( "-I{}/components/esp_system/include" , esp_idf_path) )
105
+ . clang_arg ( format ! ( "-I{}/components/freertos/esp_additions/include" , esp_idf_path) )
106
+ . clang_arg ( format ! ( "-I{}/components/freertos/esp_additions/include/freertos" , esp_idf_path) )
107
+ . clang_arg ( format ! ( "-I{}/components/freertos/esp_additions/arch/{}/include" , esp_idf_path, short_target) ) // for older espidf
108
+ . clang_arg ( format ! ( "-I{}/components/freertos/config/{}/include" , esp_idf_path, short_target) ) // for newer espidf
109
+ . clang_arg ( format ! ( "-I{}/components/{}/include" , esp_idf_path, short_target) )
110
+ . clang_arg ( format ! ( "-I{}/components/freertos/FreeRTOS-Kernel-SMP/include" , esp_idf_path) )
111
+ . clang_arg ( format ! ( "-I{}/components/freertos/FreeRTOS-Kernel-SMP/portable/{}/include/freertos" , esp_idf_path, short_target) )
112
+ . clang_arg ( format ! ( "-I{}/components/soc/{}/include" , esp_idf_path, target_mcu) )
113
+ . clang_arg ( format ! ( "-I{}/components/heap/include" , esp_idf_path) )
114
+ . clang_arg ( format ! ( "-I{}/components/esp_rom/include" , esp_idf_path) )
115
+ . clang_arg ( format ! ( "-I{}/managed_components/espressif__coap/libcoap/include" , esp_idf_buildroot) )
116
+ . clang_arg ( format ! ( "-I{}/build/config/" , esp_idf_buildroot) )
117
+ . allowlist_type ( "epoll_event" ) ;
118
+ }
119
+
120
+ fn get_builder ( ) -> bindgen:: Builder {
121
+ bindgen:: Builder :: default ( )
122
+ . blocklist_type ( "epoll_event" )
123
+ }
124
+
40
125
fn main ( ) {
41
126
println ! ( "cargo:rerun-if-changed=src/libcoap/" ) ;
42
127
println ! ( "cargo:rerun-if-changed=src/wrapper.h" ) ;
43
- let mut bindgen_builder = bindgen:: Builder :: default ( ) ;
44
128
// Read required environment variables.
45
129
let orig_pkg_config = std:: env:: var_os ( "PKG_CONFIG_PATH" ) . map ( |v| String :: from ( v. to_str ( ) . unwrap ( ) ) ) ;
46
130
let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
131
+ let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) ;
132
+
133
+ let mut bindgen_builder = match target_os. as_str ( ) {
134
+ "espidf" => get_builder_espidf ( ) ,
135
+ _ => get_builder ( )
136
+ } ;
137
+
138
+
47
139
48
140
let mut dtls_backend = Option :: None ;
49
141
if cfg ! ( feature = "dtls" ) {
@@ -86,7 +178,7 @@ fn main() {
86
178
}
87
179
88
180
// Build vendored library if feature was set.
89
- if cfg ! ( feature = "vendored" ) {
181
+ if cfg ! ( feature = "vendored" ) && target_os . as_str ( ) != "espidf" {
90
182
let libcoap_src_dir = Path :: new ( & out_dir) . join ( "libcoap" ) ;
91
183
// Read Makeflags into vector of strings
92
184
//let make_flags: Vec<String> = std::env::var_os("CARGO_MAKEFLAGS")
@@ -275,72 +367,74 @@ fn main() {
275
367
env:: set_current_dir ( current_dir_backup) . expect ( "unable to switch back to source dir" ) ;
276
368
}
277
369
278
- // Tell cargo to link libcoap.
279
- println ! (
280
- "cargo:rustc-link-lib={}{}" ,
281
- cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" ) ,
282
- format!(
283
- "coap-3-{}" ,
284
- & dtls_backend
285
- . as_ref( )
286
- . map( |v| v. to_string( ) )
287
- . unwrap_or_else( || "notls" . to_string( ) )
288
- )
289
- . as_str( )
290
- ) ;
370
+ if target_os. as_str ( ) != "espidf" {
371
+ // Tell cargo to link libcoap.
372
+ println ! (
373
+ "cargo:rustc-link-lib={}{}" ,
374
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" ) ,
375
+ format!(
376
+ "coap-3-{}" ,
377
+ & dtls_backend
378
+ . as_ref( )
379
+ . map( |v| v. to_string( ) )
380
+ . unwrap_or_else( || "notls" . to_string( ) )
381
+ )
382
+ . as_str( )
383
+ ) ;
291
384
292
- // For the DTLS libraries, we need to tell cargo which external libraries to link.
293
- // Note that these linker instructions have to be added *after* the linker instruction
294
- // for libcoap itself, as some linkers require dependencies to be in reverse order.
295
- if let Some ( dtls_backend) = dtls_backend {
296
- match dtls_backend {
297
- DtlsBackend :: TinyDtls => {
298
- // Handled by tinydtls-sys
299
- } ,
300
- DtlsBackend :: OpenSsl => {
301
- // Handled by openssl-sys
302
- } ,
303
- DtlsBackend :: MbedTls => {
304
- // If mbedtls is vendored, mbedtls-sys-auto already takes care of linking.
305
- if env:: var_os ( "DEP_MBEDTLS_INCLUDE" ) . is_none ( ) {
306
- // We aren't using mbedtls-sys-auto if we aren't vendoring (as it doesn't support
307
- // mbedtls >= 3.0.0), so we need to tell cargo to link to mbedtls ourselves.
385
+ // For the DTLS libraries, we need to tell cargo which external libraries to link.
386
+ // Note that these linker instructions have to be added *after* the linker instruction
387
+ // for libcoap itself, as some linkers require dependencies to be in reverse order.
388
+ if let Some ( dtls_backend) = dtls_backend {
389
+ match dtls_backend {
390
+ DtlsBackend :: TinyDtls => {
391
+ // Handled by tinydtls-sys
392
+ } ,
393
+ DtlsBackend :: OpenSsl => {
394
+ // Handled by openssl-sys
395
+ } ,
396
+ DtlsBackend :: MbedTls => {
397
+ // If mbedtls is vendored, mbedtls-sys-auto already takes care of linking.
398
+ if env:: var_os ( "DEP_MBEDTLS_INCLUDE" ) . is_none ( ) {
399
+ // We aren't using mbedtls-sys-auto if we aren't vendoring (as it doesn't support
400
+ // mbedtls >= 3.0.0), so we need to tell cargo to link to mbedtls ourselves.
308
401
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
- ) ;
402
+ if let Some ( mbedtls_lib_path) = env:: var_os ( "MBEDTLS_LIBRARY_PATH" ) {
403
+ println ! ( "cargo:rustc-link-search=native={}" , mbedtls_lib_path. to_str( ) . unwrap( ) )
404
+ }
405
+ // Try to find mbedtls using pkg-config, will emit cargo link statements if successful
406
+ if env:: var_os ( "MBEDTLS_LIBRARY_PATH" ) . is_some ( ) || pkg_config:: Config :: new ( ) . statik ( cfg ! ( feature = "static" ) ) . probe ( "mbedtls" ) . is_err ( ) {
407
+ // couldn't find using pkg-config or MBEDTLS_LIBRARY_PATH was set, just try
408
+ // linking with given library search path
409
+ println ! ( "cargo:rustc-link-lib={}mbedtls" ,
410
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" )
411
+ ) ;
412
+ println ! ( "cargo:rustc-link-lib={}mbedx509" ,
413
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" )
414
+ ) ;
415
+ println ! ( "cargo:rustc-link-lib={}mbedcrypto" ,
416
+ cfg!( feature = "static" ) . then( || "static=" ) . unwrap_or( "dylib=" )
417
+ ) ;
418
+ }
325
419
}
326
- }
327
- } ,
328
- DtlsBackend :: GnuTls => {
329
- // gnutls-sys is unmaintained, so we need to link to gnutls ourselves.
420
+ } ,
421
+ DtlsBackend :: GnuTls => {
422
+ // gnutls-sys is unmaintained, so we need to link to gnutls ourselves.
330
423
331
- // try pkg-config
332
- if probe_library ( "gnutls" ) . is_err ( ) {
333
- // if that doesn't work, try using the standard library search path.
334
- println ! ( "cargo:rustc-link-lib=gnutls" )
335
- }
336
- } ,
424
+ // try pkg-config
425
+ if probe_library ( "gnutls" ) . is_err ( ) {
426
+ // if that doesn't work, try using the standard library search path.
427
+ println ! ( "cargo:rustc-link-lib=gnutls" )
428
+ }
429
+ } ,
430
+ }
337
431
}
338
432
}
339
433
340
434
bindgen_builder = bindgen_builder
341
435
. header ( "src/wrapper.h" )
342
436
. default_enum_style ( EnumVariation :: Rust { non_exhaustive : true } )
343
- . formatter ( bindgen :: Formatter :: None )
437
+ . rustfmt_bindings ( false )
344
438
// Causes invalid syntax for some reason, so we have to disable it.
345
439
. generate_comments ( false )
346
440
. dynamic_link_require_all ( true )
@@ -353,7 +447,6 @@ fn main() {
353
447
// We use the definitions made by the libc crate instead
354
448
. blocklist_type ( "sockaddr(_in|_in6)?" )
355
449
. blocklist_type ( "in6?_(addr|port)(_t)?" )
356
- . blocklist_type ( "epoll_event" )
357
450
. blocklist_type ( "in6_addr__bindgen_ty_1" )
358
451
. blocklist_type ( "(__)?socklen_t" )
359
452
. blocklist_type ( "fd_set" )
0 commit comments