Skip to content

Commit dd3c422

Browse files
authored
Merge pull request #148 from madsmtm/faster-build
Remove `objc2` build script
2 parents b6b9f9c + 4b70749 commit dd3c422

File tree

10 files changed

+102
-102
lines changed

10 files changed

+102
-102
lines changed

objc-sys/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[package]
22
name = "objc-sys"
3-
version = "0.2.0-alpha.1" # Remember to update html_root_url in lib.rs
3+
# Remember to update `html_root_url` in lib.rs, the `links` key, and the
4+
# exception function name.
5+
version = "0.2.0-alpha.1"
46
authors = ["Mads Marquart <[email protected]>"]
57
edition = "2021"
68

@@ -43,6 +45,12 @@ winobjc = ["gnustep-1-8"]
4345
# TODO
4446
objfw = []
4547

48+
# Private/unstable features - must not be relied on!
49+
unstable-exception = ["cc"]
50+
51+
[build-dependencies]
52+
cc = { version = "1", optional = true }
53+
4654
[package.metadata.docs.rs]
4755
default-target = "x86_64-apple-darwin"
4856
no-default-features = true

objc-sys/build.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn main() {
6767
// println!("cargo:rustc-cfg=libobjc2_strict_apple_compat");
6868

6969
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
70+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
7071

7172
let mut apple = env::var_os("CARGO_FEATURE_APPLE").is_some();
7273
let mut gnustep = env::var_os("CARGO_FEATURE_GNUSTEP_1_7").is_some();
@@ -138,12 +139,21 @@ fn main() {
138139
};
139140
println!("cargo:rustc-cfg={}", runtime_cfg);
140141

142+
if let Apple(runtime) = &runtime {
143+
// A few things are defined differently depending on the __OBJC2__
144+
// variable, which is set for all platforms except 32-bit macOS.
145+
if let (MacOS(_), "x86") = (runtime, &*target_arch) {
146+
println!("cargo:rustc-cfg=apple_old");
147+
} else {
148+
println!("cargo:rustc-cfg=apple_new");
149+
}
150+
}
151+
141152
let clang_runtime = match &runtime {
142153
Apple(runtime) => {
143154
// The fragile runtime is expected on i686-apple-darwin, see:
144155
// https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/Driver/ToolChains/Darwin.h#L228-L231
145156
// https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/Driver/ToolChains/Clang.cpp#L3639-L3640
146-
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
147157
let clang_runtime_str = match (runtime, &*target_arch) {
148158
(MacOS(_), "x86") => "macosx-fragile",
149159
(MacOS(_), _) => "macosx",
@@ -211,4 +221,25 @@ fn main() {
211221
// Link to libobjc
212222
println!("cargo:rustc-link-lib=dylib=objc");
213223
}
224+
225+
// We do this compilation step here instead of in `objc2` to cut down on
226+
// the total number of build scripts required.
227+
#[cfg(feature = "unstable-exception")]
228+
{
229+
if std::env::var("DOCS_RS").is_ok() {
230+
// docs.rs doesn't have clang, so skip building this. The
231+
// documentation will still work since it doesn't need to link.
232+
return;
233+
}
234+
println!("cargo:rerun-if-changed=extern/exception.m");
235+
236+
let mut builder = cc::Build::new();
237+
builder.file("extern/exception.m");
238+
239+
for flag in cc_args.split(' ') {
240+
builder.flag(flag);
241+
}
242+
243+
builder.compile("librust_objc_sys_0_2_try_catch_exception.a");
244+
}
214245
}

objc2/extern/exception.m renamed to objc-sys/extern/exception.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
/// See <https://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-runtime-objc-retain>.
77
id objc_retain(id value);
88

9-
// We return `unsigned char`, since it is guaranteed to be an `u8` on all platforms
10-
unsigned char rust_objc_try_catch_exception(void (*f)(void *), void *context, id *error) {
9+
/// Unsure how C name resolution works, so we make sure to version this symbol.
10+
///
11+
/// Return `unsigned char` since it is guaranteed to be `u8` on all platforms.
12+
unsigned char rust_objc_sys_0_2_try_catch_exception(void (*f)(void *), void *context, id *error) {
1113
@try {
1214
f(context);
1315
if (error) {

objc-sys/src/exception-macos-x86.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

objc-sys/src/exception.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22
//! Apple: `objc-exception.h`
33
//! GNUStep: `eh_personality.c`, which is a bit brittle to rely on, but I
44
//! think it's fine...
5-
#[cfg(not(objfw))]
5+
#[cfg(any(not(objfw), feature = "unstable-exception"))]
66
use core::ffi::c_void;
7-
#[cfg(apple)]
7+
#[cfg(apple_new)]
88
use std::os::raw::c_int;
9+
#[cfg(feature = "unstable-exception")]
10+
use std::os::raw::c_uchar;
911

10-
#[cfg(apple)]
12+
#[cfg(apple_new)]
1113
use crate::objc_class;
1214
use crate::objc_object;
1315

1416
/// Remember that this is non-null!
15-
#[cfg(apple)]
17+
#[cfg(apple_new)]
1618
pub type objc_exception_matcher =
1719
unsafe extern "C" fn(catch_type: *mut objc_class, exception: *mut objc_object) -> c_int;
1820

1921
/// Remember that this is non-null!
20-
#[cfg(apple)]
22+
#[cfg(apple_new)]
2123
pub type objc_exception_preprocessor =
2224
unsafe extern "C" fn(exception: *mut objc_object) -> *mut objc_object;
2325

2426
/// Remember that this is non-null!
25-
#[cfg(apple)]
27+
#[cfg(apple_new)]
2628
pub type objc_uncaught_exception_handler = unsafe extern "C" fn(exception: *mut objc_object);
2729

2830
#[cfg(objfw)]
@@ -32,45 +34,79 @@ pub type objc_uncaught_exception_handler =
3234
/// Only available on macOS.
3335
///
3436
/// Remember that this is non-null!
35-
#[cfg(all(apple, target_os = "macos"))]
37+
#[cfg(all(apple_new, target_os = "macos"))]
3638
pub type objc_exception_handler =
3739
unsafe extern "C" fn(unused: *mut objc_object, context: *mut c_void);
3840

3941
extern_c! {
40-
#[cfg(not(objfw))]
42+
#[cfg(any(gnustep, apple_new))]
4143
pub fn objc_begin_catch(exc_buf: *mut c_void) -> *mut objc_object;
42-
#[cfg(not(objfw))]
44+
#[cfg(any(gnustep, apple_new))]
4345
pub fn objc_end_catch();
4446
/// See [`objc-exception.h`].
4547
///
4648
/// [`objc-exception.h`]: https://github.com/apple-oss-distributions/objc4/blob/objc4-818.2/runtime/objc-exception.h
4749
pub fn objc_exception_throw(exception: *mut objc_object) -> !;
48-
#[cfg(apple)]
50+
#[cfg(apple_new)]
4951
pub fn objc_exception_rethrow() -> !;
52+
53+
#[cfg(apple_old)]
54+
pub fn objc_exception_try_enter(exception_data: *const c_void);
55+
56+
#[cfg(apple_old)]
57+
pub fn objc_exception_try_exit(exception_data: *const c_void);
58+
59+
// objc_exception_extract
60+
// objc_exception_match
61+
// objc_exception_get_functions
62+
// objc_exception_set_functions
63+
5064
#[cfg(any(gnustep, winobjc))]
5165
pub fn objc_exception_rethrow(exc_buf: *mut c_void) -> !;
5266

53-
#[cfg(apple)]
67+
#[cfg(apple_new)]
5468
pub fn objc_setExceptionMatcher(f: objc_exception_matcher) -> objc_exception_matcher;
55-
#[cfg(apple)]
69+
#[cfg(apple_new)]
5670
pub fn objc_setExceptionPreprocessor(
5771
f: objc_exception_preprocessor,
5872
) -> objc_exception_preprocessor;
59-
#[cfg(any(apple, objfw))]
73+
#[cfg(any(apple_new, objfw))]
6074
pub fn objc_setUncaughtExceptionHandler(
6175
f: objc_uncaught_exception_handler,
6276
) -> objc_uncaught_exception_handler;
6377

6478
/// Only available on macOS.
65-
#[cfg(all(apple, target_os = "macos"))]
79+
#[cfg(all(apple_new, target_os = "macos"))]
6680
pub fn objc_addExceptionHandler(f: objc_exception_handler, context: *mut c_void) -> usize;
6781
/// Only available on macOS.
68-
#[cfg(all(apple, target_os = "macos"))]
82+
#[cfg(all(apple_new, target_os = "macos"))]
6983
pub fn objc_removeExceptionHandler(token: usize);
7084

7185
// Only available when ENABLE_OBJCXX is set, and a useable C++ runtime is
7286
// present when building libobjc2.
7387
//
7488
// #[cfg(gnustep)]
7589
// pub fn objc_set_apple_compatible_objcxx_exceptions(newValue: c_int) -> c_int;
90+
91+
/// Call the given function inside an Objective-C `@try/@catch` block.
92+
///
93+
/// Defined in `extern/exception.m` and compiled in `build.rs`.
94+
///
95+
/// Alternatively, we could manually write assembly for this function like
96+
/// [`objrs` does][manual-asm] does, that would cut down on a build stage
97+
/// (and would probably give us a bit better performance), but it gets
98+
/// unwieldy _very_ quickly, so I chose the much more stable option.
99+
///
100+
/// Another thing to remember: While Rust's and Objective-C's unwinding
101+
/// mechanisms are similar now, Rust's is explicitly unspecified, and they
102+
/// may diverge significantly in the future; so handling this in pure Rust
103+
/// (using mechanisms like core::intrinsics::r#try) is not an option!
104+
///
105+
/// [manual-asm]: https://gitlab.com/objrs/objrs/-/blob/b4f6598696b3fa622e6fddce7aff281770b0a8c2/src/exception.rs
106+
#[cfg(feature = "unstable-exception")]
107+
pub fn rust_objc_sys_0_2_try_catch_exception(
108+
f: extern "C" fn(*mut c_void),
109+
context: *mut c_void,
110+
error: *mut *mut objc_object,
111+
) -> c_uchar;
76112
}

objc-sys/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ macro_rules! extern_c {
9393
mod class;
9494
mod constants;
9595

96-
#[cfg(not(all(apple, target_os = "macos", target_arch = "x86")))]
9796
mod exception;
98-
#[cfg(all(apple, target_os = "macos", target_arch = "x86"))]
99-
#[path = "exception-macos-x86.rs"]
100-
mod exception;
101-
10297
mod message;
10398
mod method;
10499
mod object;

objc-sys/src/various.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ pub type IMP = Option<unsafe extern "C" fn()>;
2424
// /// Not available on macOS x86.
2525
// ///
2626
// /// Remember that this is non-null!
27-
// #[cfg(all(apple, not(all(target_os = "macos", target_arch = "x86"))))]
27+
// #[cfg(apple_new)]
2828
// pub type objc_hook_getClass =
2929
// unsafe extern "C" fn(name: *const c_char, out_cls: *mut *const crate::objc_class) -> BOOL;
3030
//
3131
// /// Not available on macOS x86.
3232
// ///
3333
// /// Remember that this is non-null!
34-
// #[cfg(all(apple, not(all(target_os = "macos", target_arch = "x86"))))]
34+
// #[cfg(apple_new)]
3535
// pub type objc_hook_lazyClassNamer =
3636
// unsafe extern "C" fn(cls: *const crate::objc_class) -> *const c_char;
3737

@@ -94,7 +94,7 @@ extern_c! {
9494
// /// Not available on macOS x86.
9595
// ///
9696
// /// Remember that this is non-null!
97-
// #[cfg(all(apple, not(all(target_os = "macos", target_arch = "x86"))))]
97+
// #[cfg(apple_new)]
9898
// pub fn objc_setHook_getClass(
9999
// new_value: objc_hook_getClass,
100100
// out_old_value: *mut objc_hook_getClass,
@@ -103,7 +103,7 @@ extern_c! {
103103
// /// Not available on macOS x86.
104104
// ///
105105
// /// Remember that this is non-null!
106-
// #[cfg(all(apple, not(all(target_os = "macos", target_arch = "x86"))))]
106+
// #[cfg(apple_new)]
107107
// pub fn objc_setHook_lazyClassNamer(
108108
// new_value: objc_hook_lazyClassNamer,
109109
// out_old_value: *mut objc_hook_lazyClassNamer,

objc2/Cargo.toml

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

19-
build = "build.rs"
20-
2119
# NOTE: 'unstable' features are _not_ considered part of the SemVer contract,
2220
# and may be removed in a minor release.
2321
[features]
2422
default = ["apple"]
2523

2624
# Enables `objc2::exception::throw` and `objc2::exception::catch`
27-
exception = ["cc"]
25+
exception = ["objc-sys/unstable-exception"]
2826

2927
# Wrap every `objc2::msg_send` call in a `@try/@catch` block
3028
catch_all = ["exception"]
@@ -56,9 +54,6 @@ malloc_buf = { version = "1.0", optional = true }
5654
objc-sys = { path = "../objc-sys", version = "=0.2.0-alpha.1", default-features = false }
5755
objc2-encode = { path = "../objc2-encode", version = "=2.0.0-beta.2", default-features = false }
5856

59-
[build-dependencies]
60-
cc = { version = "1", optional = true }
61-
6257
[dev-dependencies]
6358
iai = { version = "0.1", git = "https://github.com/madsmtm/iai", branch = "callgrind" }
6459

objc2/build.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

objc2/src/exception.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,11 @@
2020
use core::ffi::c_void;
2121
use core::mem;
2222
use core::ptr;
23-
use std::os::raw::c_uchar;
2423

2524
use crate::ffi;
2625
use crate::rc::{Id, Shared};
2726
use crate::runtime::Object;
2827

29-
extern "C" {
30-
/// Call the given function inside an Objective-C `@try/@catch` block.
31-
///
32-
/// Defined in `extern/exception.m` and compiled in `build.rs`.
33-
///
34-
/// Alternatively, we could manually write assembly for this function like
35-
/// [`objrs` does][manual-asm] does, that would cut down on a build stage
36-
/// (and would probably give us a bit better performance), but it gets
37-
/// unwieldy _very_ quickly, so I chose the much more stable option.
38-
///
39-
/// Another thing to remember: While Rust's and Objective-C's unwinding
40-
/// mechanisms are similar now, Rust's is explicitly unspecified, and they
41-
/// may diverge significantly in the future; so handling this in pure Rust
42-
/// (using mechanisms like core::intrinsics::r#try) is not an option!
43-
///
44-
/// [manual-asm]: https://gitlab.com/objrs/objrs/-/blob/b4f6598696b3fa622e6fddce7aff281770b0a8c2/src/exception.rs
45-
fn rust_objc_try_catch_exception(
46-
f: extern "C" fn(*mut c_void),
47-
context: *mut c_void,
48-
error: *mut *mut ffi::objc_object,
49-
) -> c_uchar;
50-
}
51-
5228
/// Throws an Objective-C exception.
5329
///
5430
/// The argument must be a pointer to an Objective-C object.
@@ -87,7 +63,7 @@ unsafe fn try_no_ret<F: FnOnce()>(closure: F) -> Result<(), Option<Id<Object, Sh
8763
let context = &mut closure as *mut _ as *mut c_void;
8864

8965
let mut exception = ptr::null_mut();
90-
let success = unsafe { rust_objc_try_catch_exception(f, context, &mut exception) };
66+
let success = unsafe { ffi::rust_objc_sys_0_2_try_catch_exception(f, context, &mut exception) };
9167

9268
if success == 0 {
9369
Ok(())

0 commit comments

Comments
 (0)