rustrust-compiler-plugin

How do I properly register a Rust compiler plugin?


I've been pulling my hair out over trying to get a Rust compiler plugin to register properly.

I have a separate crate called rust_to_glsl that is in a subdirectory of my main code.

Inside the rust_to_glsl/src/lib.rs file I have the following

#![feature(plugin_registrar)]
#![feature(rustc_private)]
#![crate_type="dylib"]

extern crate rustc;
extern crate syntax;

#[doc(hidden)]
#[plugin_registrar]
pub fn registrar(registry: &mut rustc::plugin::Registry) {
    println!("Test");
    registry.register_macro("to_glsl", expand);
}

Plus I have defined the actual expand function.

In src/main.rs I'm trying to pull in that plugin/crate.

#![feature(plugin)]
#![plugin(rust_to_glsl)]

And then I'm trying to use that macro here:

fn main() {
    let glsl = to_glsl!(
        const x: uint = 5;

        static texture: &Texture2d = 1;

        fn hello() {
            min(5, 3 * 1 + 5)
        }
    );

    println!("{}", glsl);
}

If I compile that, I get this error:

Compiling opal v0.1.0 (file:///Users/chris/Code/Rust/gl)
src/main.rs:2:11: 2:23 error: can't find crate for `rust_to_glsl`
src/main.rs:2 #![plugin(rust_to_glsl)]

During the build process I notice that rust_to_glsl isn't being compiled (with cargo build).

If I add in extern crate rust_to_glsl, then it compiles rust_to_glsl. But then squarks by saying

49:23 error: macro undefined: 'to_glsl!'
src/main.rs:49     let glsl = to_glsl!(

I'm not sure if I need to use the macro or if all macros are global.


Solution

  • Turns out i need to add this to rust_to_glsl/Cargo.toml

    [lib]
    name = "rust_to_glsl"
    crate-type = ["dylib"]
    

    And then include it not as a build-dependency but normal dependency