|
| 1 | +# `objc2_block_sys` |
| 2 | + |
| 3 | +[](https://crates.io/crates/objc2_block_sys) |
| 4 | +[](../LICENSE.txt) |
| 5 | +[](https://docs.rs/objc2_block_sys/) |
| 6 | +[](https://github.com/madsmtm/objc2/actions/workflows/apple.yml) |
| 7 | +[](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 |
0 commit comments