Skip to content

Commit bf9268d

Browse files
committed
Add README to block sys crate
1 parent 8d5c499 commit bf9268d

File tree

4 files changed

+125
-14
lines changed

4 files changed

+125
-14
lines changed

objc2/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ extern crate std;
7575
#[doc = include_str!("../README.md")]
7676
extern "C" {}
7777

78-
#[cfg(doctest)]
79-
#[doc = include_str!("../../README.md")]
80-
extern "C" {}
81-
8278
pub use objc2_encode::{Encode, EncodeArguments, Encoding, RefEncode};
8379

8480
pub use crate::message::{Message, MessageArguments, MessageError, MessageReceiver};

objc2_block_sys/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repository = "https://github.com/madsmtm/objc2"
1515
documentation = "https://docs.rs/objc2_block_sys/"
1616
license = "MIT"
1717

18-
# readme = "README.md"
18+
readme = "README.md"
1919

2020
# Downstream users can customize the linking!
2121
# See https://doc.rust-lang.org/cargo/reference/build-scripts.html#overriding-build-scripts
@@ -24,14 +24,11 @@ build = "build.rs"
2424

2525
[features]
2626
# Link to Apple's libclosure (exists in libSystem)
27-
# See https://opensource.apple.com/source/libclosure/
28-
# Accessible mirror https://github.com/madsmtm/libclosure
2927
#
3028
# This is the default on Apple platforms
3129
apple = []
3230

3331
# Link to libBlocksRuntime from compiler-rt
34-
# See https://github.com/llvm/llvm-project/tree/release/13.x/compiler-rt/lib/BlocksRuntime
3532
#
3633
# This is the default on non-Apple platforms
3734
compiler-rt = []

objc2_block_sys/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# `objc2_block_sys`
2+
3+
[![Latest version](https://badgen.net/crates/v/objc2_block_sys)](https://crates.io/crates/objc2_block_sys)
4+
[![License](https://badgen.net/badge/license/MIT/blue)](../LICENSE.txt)
5+
[![Documentation](https://docs.rs/objc2_block_sys/badge.svg)](https://docs.rs/objc2_block_sys/)
6+
[![Apple CI](https://github.com/madsmtm/objc2/actions/workflows/apple.yml/badge.svg)](https://github.com/madsmtm/objc2/actions/workflows/apple.yml)
7+
[![GNUStep CI](https://github.com/madsmtm/objc2/actions/workflows/gnustep.yml/badge.svg)](https://github.com/madsmtm/objc2/actions/workflows/gnustep.yml)
8+
9+
Raw Rust bindings to Apple's C language extension of blocks
10+
11+
## Runtime Support
12+
13+
This library is basically just a raw interface to the aptly specified [Blocks
14+
ABI](https://clang.llvm.org/docs/Block-ABI-Apple.html). However, different
15+
runtime implementations exist and act in slightly different ways (and have
16+
several different helper functions), the most important aspect being that the
17+
libraries are named differently, so the linking must take that into account.
18+
19+
The user can choose the desired runtime by using the relevant cargo feature
20+
flags, see the following sections:
21+
22+
23+
### Apple's [`libclosure`](https://opensource.apple.com/source/libclosure/)
24+
25+
- Feature flag: `apple`.
26+
27+
This is naturally the most sophisticated runtime, and it has quite a lot more
28+
features than the specification mandates. This is used by default on Apple
29+
platforms when no feature flags are specified.
30+
31+
The minimum required operating system versions are as follows:
32+
- macOS: `10.6`
33+
- iOS: `3.2`
34+
- tvOS: Unknown
35+
- watchOS: Unknown
36+
37+
Though in practice Rust itself requires higher versions than this.
38+
39+
A git mirror of the sources is available [here](https://github.com/madsmtm/libclosure).
40+
41+
42+
### LLVM `compiler-rt`'s [`libBlocksRuntime`](https://github.com/llvm/llvm-project/tree/release/13.x/compiler-rt/lib/BlocksRuntime)
43+
44+
- Feature flag: `compiler-rt`.
45+
46+
This is the default runtime on all non-Apple platforms when no feature flags
47+
are specified.
48+
49+
This is effectively just a copy of Apple's older (around macOS 10.6) runtime,
50+
and is now used in [Swift's `libdispatch`] and [Swift's Foundation] as well.
51+
52+
This can be easily used on many Linux systems with the `libblocksruntime-dev`
53+
package.
54+
55+
[Swift's `libdispatch`]: https://github.com/apple/swift-corelibs-libdispatch/tree/swift-5.5.1-RELEASE/src/BlocksRuntime
56+
[Swift's Foundation]: https://github.com/apple/swift-corelibs-foundation/tree/swift-5.5.1-RELEASE/Sources/BlocksRuntime
57+
58+
59+
### GNUStep's [`libobjc2`](https://github.com/gnustep/libobjc2)
60+
61+
- Feature flag: `gnustep-1-7`, `gnustep-1-8`, `gnustep-1-9`, `gnustep-2-0` and
62+
`gnustep-2-1` depending on the version you're using.
63+
64+
GNUStep is a bit odd, because it bundles blocks support into its Objective-C
65+
runtime. This means we have to link to `libobjc`, and this is done by
66+
depending on the `objc2_sys` crate. A bit unorthodox, yes, but it works.
67+
68+
Sources:
69+
- [`Block.h`](https://github.com/gnustep/libobjc2/blob/v2.1/objc/blocks_runtime.h)
70+
- [`Block_private.h`](https://github.com/gnustep/libobjc2/blob/v2.1/objc/blocks_private.h)
71+
72+
73+
### Microsoft's [`WinObjC`](https://github.com/microsoft/WinObjC)
74+
75+
- Feature flag: `winobjc`.
76+
77+
Essentially just [a fork](https://github.com/microsoft/libobjc2) based on
78+
GNUStep's `libobjc2` version 1.8.
79+
80+
81+
### [`ObjFW`](https://github.com/ObjFW/ObjFW) (WIP)
82+
83+
- Feature flag: `objfw`.
84+
85+
TODO.
86+
87+
88+
## C Compiler configuration
89+
90+
To our knowledge, currently only `clang` supports the [Language Specification
91+
for Blocks][block-lang]. To assist in compiling C (or Objective-C) sources
92+
using these features, this crate's build script expose the `DEP_BLOCK_CC_ARGS`
93+
environment variable to downstream build scripts.
94+
95+
Example usage in your `build.rs` (using the `cc` crate) would be as follows:
96+
97+
```rust , ignore
98+
fn main() {
99+
let mut builder = cc::Build::new();
100+
builder.compiler("clang");
101+
builder.file("my_script_using_blocks.c");
102+
103+
for flag in std::env::var("DEP_BLOCK_CC_ARGS").unwrap().split(' ') {
104+
builder.flag(flag);
105+
}
106+
107+
builder.compile("libmy_script_using_blocks.a");
108+
}
109+
```
110+
111+
[block-lang]: https://clang.llvm.org/docs/BlockLanguageSpec.html

objc2_block_sys/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
//! # Raw bindings to Apple's C language extension of blocks
12
//!
2-
//! See:
3-
//! - https://github.com/apple/swift-corelibs-libdispatch/tree/main/src/BlocksRuntime
4-
//! - https://github.com/apple/swift-corelibs-foundation/tree/main/Sources/BlocksRuntime
5-
//! - https://clang.llvm.org/docs/BlockLanguageSpec.html
6-
//! - https://clang.llvm.org/docs/Block-ABI-Apple.html
3+
//! The documentation for these bindings is a mix from GNUStep's and Apple's
4+
//! sources, but the [ABI specification][ABI] is really the place you should
5+
//! be looking!
6+
//!
7+
//! See also the `README.md` for more info.
8+
//!
9+
//! [ABI]: https://clang.llvm.org/docs/Block-ABI-Apple.html
710
811
// Update in Cargo.toml as well.
912
#![doc(html_root_url = "https://docs.rs/objc2_block_sys/0.0.0")]
@@ -12,6 +15,10 @@
1215
#[cfg(feature = "gnustep-1-7")]
1316
extern crate objc2_sys;
1417

18+
#[cfg(doctest)]
19+
#[doc = include_str!("../README.md")]
20+
extern "C" {}
21+
1522
use core::cell::UnsafeCell;
1623
use core::ffi::c_void;
1724
use core::marker::{PhantomData, PhantomPinned};
@@ -90,7 +97,7 @@ pub const BLOCK_IS_GLOBAL: block_flags = 1 << 28;
9097
/// (true, true) => ABI.2010.3.16, stret calling convention, presence of signature field,
9198
/// }
9299
///
93-
/// See https://clang.llvm.org/docs/Block-ABI-Apple.html#high-level
100+
/// See <https://clang.llvm.org/docs/Block-ABI-Apple.html#high-level>
94101
#[doc(alias = "BLOCK_USE_SRET")]
95102
#[doc(alias = "BLOCK_HAS_DESCRIPTOR")] // compiler-rt || macOS 10.6
96103
pub const BLOCK_USE_STRET: block_flags = 1 << 29;

0 commit comments

Comments
 (0)