sn99
sn99

Reputation: 941

Unable to make llvm bindings using bindgen

I am trying to complete the Kaleidoscope tutorial in Rust.

But I seem to be struck on codegen.

My build.rs :

extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main(){
    println!("cargo:rustc-link-lib=llvm");
    println!("cargo:rerun-if-changed=wrapper.h");

    let bindings = bindgen::Builder::default()
        .header("wrapper.h")
        .clang_arg("llvm-config --cxxflags --ldflags --system-libs --libs core")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("unable to generate bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("couldn't write bindings!");
}

My wrapper.h :

#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"

I want to be able to pass the following flags clang++ `llvm-config --cxxflags --ldflags --system-libs --libs core` from the tutorial.

The current error I am getting :

build-script-build` (exit code: 101)
--- stdout
cargo:rustc-link-lib=llvm
cargo:rerun-if-changed=wrapper.h

--- stderr
warning: llvm-config --cxxflags --ldflags --system-libs --libs core: 'linker' input unused [-Wunused-command-line-argument]
/usr/include/llvm/Support/Compiler.h:19:10: fatal error: 'new' file not found
warning: llvm-config --cxxflags --ldflags --system-libs --libs core: 'linker' input unused [-Wunused-command-line-argument], err: false
/usr/include/llvm/Support/Compiler.h:19:10: fatal error: 'new' file not found, err: true
thread 'main' panicked at 'unable to generate bindings: ()', src/libcore/result.rs:1188:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Upvotes: 2

Views: 2096

Answers (2)

sn99
sn99

Reputation: 941

Thanks to jplatte for pointing out to execute llvm-config --cxxflags --ldflags --system-libs --libs core and feeding it to clang_args.

The error remained because of trying to bind c rather than c++, final build.rsafter renaming wrapper.h to wrapper.hpp and passing -x -- c++ :

extern crate bindgen;

use std::env;
use std::path::PathBuf;
use std::{process::Command, str};

fn main(){
    println!("cargo:rustc-link-lib=llvm");
    println!("cargo:rerun-if-changed=wrapper.hpp");

    let llvm_config_out = Command::new("llvm-config")
        .args(&["--cxxflags", "--ldflags", "--system-libs", "--libs", "core"])
        .output()
        .expect("failed to execute llvm-config");

    let llvm_clang_args = llvm_config_out
        .stdout
        .split(|byte| byte.is_ascii_whitespace())
        .map(|arg| str::from_utf8(arg).unwrap());

    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .clang_arg("-x").clang_arg("c++") // c++ flag
        .clang_args(llvm_clang_args)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("unable to generate bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("couldn't write bindings!");
}

Upvotes: 2

jplatte
jplatte

Reputation: 1141

You're passing literally llvm-config --cxxflags --ldflags --system-libs --libs core to clang, but what you need to do is execute llvm-config --cxxflags --ldflags --system-libs --libs core and pass the resulting arguments to clang (this is what the backtick notation does in a shell).

So before the bindgen invocation, do something like this (not tested):

use std::{process::Command, str::from_utf8};

let llvm_config_out = Command::new("llvm-config")
    .args(&["--cxxflags", "--ldflags", "--system-libs", "--libs", "core"])
    .output()
    .expect("failed to execute llvm-config");

let llvm_clang_args = llvm_config_out
    .stdout
    .split(|byte| byte.is_ascii_whitespace())
    .map(|arg| str::from_utf8(arg).unwrap());

Then you can pass llvm_clang_args to the bindgen builder as .clang_args(llvm_clang_args) (not .clang_arg, notice the extra s).

Upvotes: 2

Related Questions