Skip to content

Conditional compilation based on crate_type #20267

Open
@BenTheElder

Description

@BenTheElder

Ideally we should be able to do something like this:
Cargo.toml:

.....
[lib]
path = "src/lib.rs"
crate-type = ["rlib","dylib"]
.....

src/lib.rs:

....
#[no_mangle]
#[cfg(crate_type="dylib")]
pub unsafe extern "C" fn .....

To allow only exporting the c api in the dylib or perhaps in the dylib and static lib and not in the rust code.
This would be consistent with being able to build multiple library formats but allow the unsafe c methods to not be exported to rust code.

I checked against: http://doc.rust-lang.org/reference.html#conditional-compilation
and did some experimentation myself, with no luck.

If anyone has any better suggestions, I'd love to hear them.

I'm looking to generate very similar rust and c libraries with similar apis where the c library is just a c style wrapper around the rust api, also written in rust. Ideally i'd keep this in a single source and conditionally define the c api's in the dylib for loading from various languages with c ffi's and exclude the c api from the rust lib.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-driverArea: rustc_driver that ties everything together into the `rustc` compilerC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions