Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove libc dependency #39

Open
briansmith opened this issue Oct 22, 2015 · 4 comments
Open

Remove libc dependency #39

briansmith opened this issue Oct 22, 2015 · 4 comments

Comments

@briansmith
Copy link
Owner

Looking at the proposed new libc and considering our current usage, it becomes pretty obvious we don't need to depend on the libc crate and we're better off not doing so. Our needs are so simple we can get away with just defining a couple of types in ring::ffi. If we need a more advanced thing, then we can look at rust-c99.

@briansmith
Copy link
Owner Author

Fixed with 377f611.

@briansmith
Copy link
Owner Author

Look at what we did for wasm32-unknown-unknown:

#[cfg(all(target_arch = "wasm32", target_env = ""))]
mod libc {
    //! The WASM32 ABI is described at
    //! https://github.com/WebAssembly/tool-conventions/blob/master/BasicCABI.md#data-representation

    pub(crate) type c_int = i32;
    pub(crate) type c_uint = u32;

    // "The size_t type is defined as unsigned long."
    // However, we must define it as an alias of `usize` for compatibility with `libc::size_t`.
    pub(crate) type size_t = usize;
}

Basically we can define size_t = usize for all platforms. Then we just need to add code to the c submodule to define c_int and c_uint for all the platforms we support. After that, we can remove the libc dependency except on Linux/Android which need libc::syscall.

@josephlr
Copy link
Contributor

josephlr commented Jul 9, 2019

So one thing that's nice is that in the current libc crate the following is always true.

pub type c_int = i32;
pub type c_uint = u32;
pub type size_t = usize;

This is not something guaranteed by the C spec, but just happens to be true for all platforms rustc can build on a the moment. This means the c module can be simplified to just define these types if we support an arch/os pair.

Note that the definitions of long/ulong actually vary across supported platforms, but that's only needed for getrandom on Linux and getauxval on ARM.

@briansmith
Copy link
Owner Author

We now have:

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
libc = { version = "0.2.148", default-features = false }

This is for CPU feature detection on ARM and AAarch64. Specifically we have:

    use libc::c_ulong;

    // XXX: The `libc` crate doesn't provide `libc::getauxval` consistently
    // across all Android/Linux targets, e.g. musl.
    extern "C" {
        fn getauxval(type_: c_ulong) -> c_ulong;
    }

So we can define ring::c::ulong and extend test_libc_compatible to verify that libc::c_ulong is an alias of this time, and then remove libc as a regular dependency, leaving it as just a dev-dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants