Skip to content

Omit contents of #includes in final generated rust file #1200

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

Closed
michaeleiselsc opened this issue Dec 29, 2017 · 6 comments
Closed

Omit contents of #includes in final generated rust file #1200

michaeleiselsc opened this issue Dec 29, 2017 · 6 comments

Comments

@michaeleiselsc
Copy link

Input C/C++ Header

#include <Foundation/Foundation.h>

typedef NS_OPTIONS(NSUInteger, LogFlag) {
    /**
     *  0...00001 LogFlagError
     */
    LogFlagError = (1 << 0),
    
    /**
     *  0...00010 LogFlagWarning
     */
    LogFlagWarning = (1 << 1),
    
    /**
     *  0...00100 LogFlagInfo
     */
    LogFlagInfo = (1 << 2),
    
    /**
     *  0...01000 LogFlagDebug
     */
    SCLogFlagDebug = (1 << 3),
    
    /**
     *  0...10000 LogFlagVerbose
     */
    LogFlagVerbose = (1 << 4)
};

Bindgen Invocation

fn main() {
    // Tell cargo to tell rustc to link the system bzip2
    // shared library.
    println!("cargo:rustc-link-lib=bz2");

    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let sys_root = "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.0.sdk";
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header("../SCRustLogger.h")
        .clang_arg("-x")
        .clang_arg("objective-c")
        .clang_arg("-arch")
        .clang_arg("arm64")
        .clang_arg(format!("-isysroot{}", sys_root))
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

Actual Results

It creates a bindgen file for everything within the #included header, when in fact I just want that header for its preprocessor defines.

Expected Results

I'd like some way to have it just emit the contents of the file, and thus only use the #includes to set up the preprocessor to parse the contents of the file.

@emilio
Copy link
Contributor

emilio commented Jan 2, 2018

You can use the whitelisting arguments to whitelist just what you need, like whitelist_type("LogFlag"). Is that not enough?

@michaeleiselsc
Copy link
Author

yes, that will work, thanks!

@emilio
Copy link
Contributor

emilio commented Jan 3, 2018

Ok, great, thanks again for filing! :)

@emilio emilio closed this as completed Jan 3, 2018
@tjkirch
Copy link

tjkirch commented Apr 3, 2018

You can whitelist specifics from the headers, but the larger the library, the more cruft you need in your bindgen configuration. You also have to keep it up to date as the library changes, which seems to go against the goal of bindgen taking away this work.

Has anyone else had trouble with this, or come up with a way to exclude system includes? Funny enough, I see that the bindgen CI checks that there are no system includes... https://github.com/rust-lang-nursery/rust-bindgen/blob/master/ci/no-includes.sh

@emilio
Copy link
Contributor

emilio commented Apr 3, 2018

Has anyone else had trouble with this, or come up with a way to exclude system includes? Funny enough, I see that the bindgen CI checks that there are no system includes... https://github.com/rust-lang-nursery/rust-bindgen/blob/master/ci/no-includes.sh

Yeah, that's intentional to avoid non-deterministic bindings.

You can whitelist specifics from the headers, but the larger the library, the more cruft you need in your bindgen configuration. You also have to keep it up to date as the library changes, which seems to go against the goal of bindgen taking away this work.

The idea is to use regexes to catch items using the naming convention of the library. Then after that all gets whitelisted recursively. See https://github.com/servo/mozangle/blob/3da78f0a27399a4673520d26ad5e7b9be559d07e/build.rs#L21 for example.

If the library you're binding against doesn't have any sort of type / function naming convention or what not, that can indeed be a problem. If you point me to an specific example we can try to figure out what's the best solution for that. Mind opening a new issue for it though?

@MSxDOS
Copy link

MSxDOS commented Jun 20, 2018

If you point me to an specific example we can try to figure out what's the best solution for that.

Try Windows SDK. 100KB header in -> 4MB .rs out with loads of useless junk inside mixed with what was in the actual header.

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

No branches or pull requests

4 participants