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

Expose control of import prefix to Cargo builds #347

Merged
merged 5 commits into from
Oct 9, 2020
Merged

Expose control of import prefix to Cargo builds #347

merged 5 commits into from
Oct 9, 2020

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Oct 9, 2020

This PR adds a global configuration object called cxx_build::CFG.


  • CFG.include_prefix

Presently the only exposed configuration is the include_prefix, the prefix at which C++ code from your crate as well as directly dependent crates can access the code generated during this build.

By default, the include_prefix is equal to the name of the current crate. That means if our crate is called demo and has Rust source files in a src/ directory and maybe some handwritten C++ header files in an include/ directory, then the current crate as well as downstream crates might include them as follows:

  // include one of the handwritten headers:
#include "demo/include/wow.h"

  // include a header generated from Rust cxx::bridge:
#include "demo/src/lib.rs.h"

By modifying CFG.include_prefix we can substitute a prefix that is different from the crate name if desired. Here we'll change it to "path/to" which will make import paths take the form "path/to/include/wow.h" and "path/to/src/lib.rs.h".

// build.rs

use cxx_bridge::CFG;

fn main() {
    CFG.include_prefix = "path/to";

    cxx_build::bridge("src/lib.rs")
        .file("src/demo.cc") // probably contains `#include "path/to/src/lib.rs.h"`
        /* ... */
        .compile("demo");
}

Note that cross-crate imports are only made available between direct dependencies. Another crate must directly depend on your crate in order to #include its headers; a transitive dependency is not sufficient. Additionally, headers from a direct dependency are only importable if the dependency's Cargo.toml manifest contains a links key. If not, its headers will not be importable from outside of the same crate.

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

Successfully merging this pull request may close these issues.

None yet

1 participant