Files
firefox/toolkit/library/rust/shared/lib.rs
T
Nathan Froyd 49a4f52929 Bug 1373878 - part 3 - add stylo_tests as a RustTest; r=rillian
The easy part of this patch is the addition of the RustTest itself.

The more difficult to understand part of the patch is the changes to all
of our Rust build configuration.  We do this due to a bug in cargo:

https://github.com/rust-lang/cargo/issues/3923

where features on dependent crates are not correctly taken into account
when determining whether cached artifacts on disk are valid and whether
they should be evicted from the disk cache.  The practical upshot of
this behavior is that, say, running gtests during normal development
when files in libxul are modified will:

* rebuild some Rust dependencies for libxul;
* link libxul;
* rebuild those same Rust dependencies *again* for libxul-gtest, since
  we have different features active and therefore the old artifacts look
  to be out of date;
* link libxul-gtest.

Needless to say, this is highly annoying and counterproductive behavior.

The "fix" is to ensure that the gkrust-shared crate explicitly depends
on crates and assigns features to them such that the feature sets do not
change between normal builds and testing builds.  This is admittedly
fragile, but it is not the first time this has come up, and is probably
not the last.
2017-10-02 09:21:22 -04:00

94 lines
3.1 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#[cfg(feature="servo")]
extern crate geckoservo;
extern crate mp4parse_capi;
extern crate nsstring;
extern crate nserror;
extern crate rust_url_capi;
#[cfg(feature = "quantum_render")]
extern crate webrender_bindings;
#[cfg(feature = "cubeb_pulse_rust")]
extern crate cubeb_pulse;
extern crate encoding_c;
extern crate encoding_glue;
#[cfg(feature = "cubeb-remoting")]
extern crate audioipc_client;
#[cfg(feature = "cubeb-remoting")]
extern crate audioipc_server;
extern crate u2fhid;
extern crate log;
extern crate syn;
use std::boxed::Box;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::panic;
// This workaround is fixed in Rust 1.19. For details, see bug 1358151.
thread_local!(static UNUSED_THREAD_LOCAL: () = ());
#[no_mangle]
pub extern "C" fn rust_init_please_remove_this_after_updating_rust_1_19() {
UNUSED_THREAD_LOCAL.with(|_| ());
}
/// Used to implement `nsIDebug2::RustPanic` for testing purposes.
#[no_mangle]
pub extern "C" fn intentional_panic(message: *const c_char) {
panic!("{}", unsafe { CStr::from_ptr(message) }.to_string_lossy());
}
/// Contains the panic message, if set.
static mut PANIC_REASON: Option<*const str> = None;
/// Configure a panic hook to capture panic messages for crash reports.
///
/// We don't store this in `gMozCrashReason` because:
/// a) Rust strings aren't null-terminated, so we'd have to allocate
/// memory to get a null-terminated string
/// b) The panic=abort handler is going to call `abort()` on non-Windows,
/// which is `mozalloc_abort` for us, which will use `MOZ_CRASH` and
/// overwrite `gMozCrashReason` with an unhelpful string.
#[no_mangle]
pub extern "C" fn install_rust_panic_hook() {
let default_hook = panic::take_hook();
panic::set_hook(Box::new(move |info| {
// Try to handle &str/String payloads, which should handle 99% of cases.
let payload = info.payload();
// We'll hold a raw *const str here, but it will be OK because
// Rust is going to abort the process before the payload could be
// deallocated.
if let Some(s) = payload.downcast_ref::<&str>() {
unsafe { PANIC_REASON = Some(*s as *const str); }
} else if let Some(s) = payload.downcast_ref::<String>() {
unsafe { PANIC_REASON = Some(s.as_str() as *const str); }
} else {
// Not the most helpful thing, but seems unlikely to happen
// in practice.
println!("Unhandled panic payload!");
}
// Fall through to the default hook so we still print the reason and
// backtrace to the console.
default_hook(info);
}));
}
#[no_mangle]
pub extern "C" fn get_rust_panic_reason(reason: *mut *const c_char, length: *mut usize) -> bool {
unsafe {
if let Some(s) = PANIC_REASON {
*reason = s as *const c_char;
*length = (*s).len();
true
} else {
false
}
}
}