Skip to content

Commit e8a1c09

Browse files
committed
Puts blocks behind a switch.
Since rust-lang#1378 broke a bunch of OSX builds. Most people don't care about them and they're in some OSX system headers which means that this could break normal C and C++ stuff. This introduces --generate-block / generate_block to generate these signatures, and adds tests so that this is getting tested.
1 parent 820435c commit e8a1c09

File tree

7 files changed

+68
-6
lines changed

7 files changed

+68
-6
lines changed

src/codegen/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,10 @@ impl CodeGenerator for Type {
621621
inst.codegen(ctx, result, item)
622622
}
623623
TypeKind::BlockPointer(inner) => {
624+
if !ctx.options().generate_block {
625+
return;
626+
}
627+
624628
let inner_item = inner.into_resolver()
625629
.through_type_refs()
626630
.resolve(ctx);
@@ -3135,6 +3139,10 @@ impl TryToRustTy for Type {
31353139
TypeKind::TemplateAlias(..) |
31363140
TypeKind::Alias(..) |
31373141
TypeKind::BlockPointer(..) => {
3142+
if self.is_block_pointer() && !ctx.options().generate_block {
3143+
let void = raw_type(ctx, "c_void");
3144+
return Ok(void.to_ptr(/* is_const = */ false));
3145+
}
31383146
let template_params = item.used_template_params(ctx)
31393147
.into_iter()
31403148
.filter(|param| param.is_template_param(ctx, &()))

src/ir/ty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ impl Type {
9090
self.name.as_ref().map(|name| &**name)
9191
}
9292

93+
/// Whether this is a block pointer type.
94+
pub fn is_block_pointer(&self) -> bool {
95+
match self.kind {
96+
TypeKind::BlockPointer(..) => true,
97+
_ => false,
98+
}
99+
}
100+
93101
/// Is this a compound type?
94102
pub fn is_comp(&self) -> bool {
95103
match self.kind {

src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ impl Builder {
373373
output_vector.push("--objc-extern-crate".into());
374374
}
375375

376+
if self.options.generate_block {
377+
output_vector.push("--generate-block".into());
378+
}
379+
376380
if self.options.block_extern_crate {
377381
output_vector.push("--block-extern-crate".into());
378382
}
@@ -704,6 +708,12 @@ impl Builder {
704708
self
705709
}
706710

711+
/// Generate proper block signatures instead of void pointers.
712+
pub fn generate_block(mut self, doit: bool) -> Self {
713+
self.options.generate_block = doit;
714+
self
715+
}
716+
707717
/// Generate `#[macro_use] extern crate block;` instead of `use block;`
708718
/// in the prologue of the files generated from apple block files
709719
pub fn block_extern_crate(mut self, doit: bool) -> Self {
@@ -1465,6 +1475,10 @@ struct BindgenOptions {
14651475
/// generate '#[macro_use] extern crate objc;'
14661476
objc_extern_crate: bool,
14671477

1478+
/// Instead of emitting 'use block;' to files generated from objective c files,
1479+
/// generate '#[macro_use] extern crate block;'
1480+
generate_block: bool,
1481+
14681482
/// Instead of emitting 'use block;' to files generated from objective c files,
14691483
/// generate '#[macro_use] extern crate block;'
14701484
block_extern_crate: bool,
@@ -1593,6 +1607,7 @@ impl Default for BindgenOptions {
15931607
generate_comments: true,
15941608
generate_inline_functions: false,
15951609
whitelist_recursively: true,
1610+
generate_block: false,
15961611
objc_extern_crate: false,
15971612
block_extern_crate: false,
15981613
enable_mangling: true,

src/options.rs

+7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ where
132132
Arg::with_name("objc-extern-crate")
133133
.long("objc-extern-crate")
134134
.help("Use extern crate instead of use for objc."),
135+
Arg::with_name("generate-block")
136+
.long("generate-block")
137+
.help("Generate block signatures instead of void pointers."),
135138
Arg::with_name("block-extern-crate")
136139
.long("block-extern-crate")
137140
.help("Use extern crate instead of use for block."),
@@ -496,6 +499,10 @@ where
496499
builder = builder.objc_extern_crate(true);
497500
}
498501

502+
if matches.is_present("generate-block") {
503+
builder = builder.generate_block(true);
504+
}
505+
499506
if matches.is_present("block-extern-crate") {
500507
builder = builder.block_extern_crate(true);
501508
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
#![cfg(target_os = "macos")]
10+
11+
extern crate block;
12+
extern "C" {
13+
#[link_name = "\u{1}_Z8atexit_bU13block_pointerFvvE"]
14+
pub fn atexit_b(arg1: _bindgen_ty_id_20);
15+
}
16+
pub type dispatch_data_t = *mut ::std::os::raw::c_void;
17+
pub type dispatch_data_applier_t = _bindgen_ty_id_27;
18+
extern "C" {
19+
#[link_name = "\u{1}_Z19dispatch_data_applyPvU13block_pointerFbS_yPKvyE"]
20+
pub fn dispatch_data_apply(data: dispatch_data_t, applier: dispatch_data_applier_t) -> bool;
21+
}
22+
pub type _bindgen_ty_id_20 = *const ::block::Block<(), ()>;
23+
pub type _bindgen_ty_id_27 =
24+
*const ::block::Block<(dispatch_data_t, usize, *const ::std::os::raw::c_void, usize), bool>;

tests/expectations/tests/blocks.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
)]
99
#![cfg(target_os = "macos")]
1010

11-
extern crate block;
1211
extern "C" {
1312
#[link_name = "\u{1}_Z8atexit_bU13block_pointerFvvE"]
14-
pub fn atexit_b(arg1: _bindgen_ty_id_19);
13+
pub fn atexit_b(arg1: *mut ::std::os::raw::c_void);
1514
}
1615
pub type dispatch_data_t = *mut ::std::os::raw::c_void;
17-
pub type dispatch_data_applier_t = _bindgen_ty_id_26;
16+
pub type dispatch_data_applier_t = *mut ::std::os::raw::c_void;
1817
extern "C" {
1918
#[link_name = "\u{1}_Z19dispatch_data_applyPvU13block_pointerFbS_yPKvyE"]
2019
pub fn dispatch_data_apply(data: dispatch_data_t, applier: dispatch_data_applier_t) -> bool;
2120
}
22-
pub type _bindgen_ty_id_19 = *const ::block::Block<(), ()>;
23-
pub type _bindgen_ty_id_26 =
24-
*const ::block::Block<(dispatch_data_t, usize, *const ::std::os::raw::c_void, usize), bool>;

tests/headers/blocks-signature.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// bindgen-flags: --generate-block --block-extern-crate -- -fblocks
2+
// bindgen-osx-only
3+
4+
#include "blocks.hpp"

0 commit comments

Comments
 (0)