Skip to content

Having problems expanding macros with build.rs #758

@iquerejeta

Description

@iquerejeta

I encountered a problem with the expansion of macros using build.rs. Here is a minimal example to point out the problems I'm having. I have the following lib.rs.

// lib.rs file

macro_rules! number {
    ($name:ident, $number:expr) => {
        #[no_mangle]
        pub extern "C" fn $name () -> u64 {
            $number
        }
    }
}

number!(two, 2);

with the following cbindgen.toml

language = "C"

[parse.expand]
crates = ["testing"]

If I run rustup run nightly cbindgen ./ --config cbindgen.toml --crate testing --output bindings.h, the macros are expanded, and I get the expected file bindings.h.

However, if I try to expand macros and build the binding using a build.rs file, the compilation takes a lot and returns an error at the end of it. The build files I've tried are the following:

// With the builder struct

extern crate cbindgen;
use std::env;

fn main() {
    cbindgen::Builder::new()
        .with_crate(env::var("CARGO_MANIFEST_DIR").unwrap())
        .with_parse_expand(&["testing"])
        .generate()
        .expect("Unable to generate bindings")
        .write_to_file("bindings.h");
}

and

// With the generate function using the same cbindgen.toml as above
extern crate cbindgen;

fn main() {
    cbindgen::generate(std::env::var("CARGO_MANIFEST_DIR").unwrap())
        .expect("Unable to generate bindings")
        .write_to_file("bindings.h");
}

When running RUST_BACKTRACE=1 cargo build I get a ton of lines like this (not copying all because would be huge):

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n               at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/ops/function.rs:227:5\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

and the final lines of the error are

  stack backtrace:
     0: rust_begin_unwind
               at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/panicking.rs:498:5
     1: core::panicking::panic_fmt
               at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:116:14
     2: core::result::unwrap_failed
               at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/result.rs:1690:5
     3: core::result::Result<T,E>::expect
               at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/result.rs:975:23
     4: build_script_build::main
               at ./build.rs:4:5
     5: core::ops::function::FnOnce::call_once
               at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/ops/function.rs:227:5
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Is this a known issue? Or am I maybe doing something wrong?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions