According to the Rust bindgen tutorial at https://rust-lang.github.io/rust-bindgen/tutorial-3.html, I am trying to generate the FFI for existing C header files. However, I am having trouble linking the header files.
The repository at https://github.com/studersi/rust-test-apache holds all the files necessary to reproduce the issue including a README file containing the necessary commands. The most relevant files are listed at the end of the question.
I am using a Docker container to build the project (see Dockerfile below). The C header files are downloaded from the Internet and placed in the subdirectory downloads/
.
The includes for the linker (see build.rs below) are based on the includes the apxs
tool uses to build a similar C example according to the Apache httpd module development guide on which my project is based.
$ apxs -a -c mod_example.c
/usr/share/apr-1.0/build/libtool --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -prefer-pic -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/apache2 -I/usr/include/apr-1.0 -I/usr/include/apr-1.0 -I/usr/include -c -o mod_example.lo mod_example.c && touch mod_example.slo
libtool: compile: x86_64-linux-gnu-gcc -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/apache2 -I/usr/include/apr-1.0 -I/usr/include/apr-1.0 -I/usr/include -c mod_example.c -fPIC -DPIC -o .libs/mod_example.o
/usr/share/apr-1.0/build/libtool --mode=link --tag=disable-static x86_64-linux-gnu-gcc -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -o mod_example.la -rpath /usr/lib/apache2/modules -module -avoid-version mod_example.lo
libtool: link: x86_64-linux-gnu-gcc -shared -fPIC -DPIC .libs/mod_example.o -Wl,--as-needed -Wl,-z -Wl,relro -Wl,-z -Wl,now -Wl,-soname -Wl,mod_example.so -o .libs/mod_example.so
libtool: link: ( cd ".libs" && rm -f "mod_example.la" && ln -s "../mod_example.la" "mod_example.la" )
After starting the container and building the Cargo project, I get errors that the header files to be converted cannot be found.
$ cargo build
Compiling mod_rs v0.1.0 (/home/rustacian/mod_rs)
error: failed to run custom build command for `mod_rs v0.1.0 (/home/rustacian/mod_rs)`
Caused by:
process didn't exit successfully: `/home/rustacian/mod_rs/target/debug/build/mod_rs-a68981041b3e3908/build-script-build` (exit code: 101)
--- stdout
cargo:include=/home/rustacian/downloads/httpd-2.4.46/include
cargo:include=/home/rustacian/downloads/apr-1.7.0/include
cargo:include=/usr/include/apache2
cargo:include=/usr/include/apr-1.0
cargo:rerun-if-changed=wrapper.h
--- stderr
wrapper.h:2:10: fatal error: 'apr_hash.h' file not found
wrapper.h:2:10: fatal error: 'apr_hash.h' file not found, err: true
thread 'main' panicked at 'Unable to generate bindings: ()', build.rs:34:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Note that I already tried including the header files using different commands (cargo:include
and .clang_arg
) and from different locations (downloaded manually and installed using system packages) but nothing has worked so far.
[...]
println!("cargo:include=/home/rustacian/downloads/httpd-2.4.46/include");
println!("cargo:include=/home/rustacian/downloads/apr-1.7.0/include");
println!("cargo:include=/usr/include/apache2");
println!("cargo:include=/usr/include/apr-1.0");
[...]
.clang_arg("-I/usr/include/apache2 -I/usr/include/apr-1.0 -I/home/rustacian/downloads/httpd-2.4.46/include -I/home/rustacian/downloads/apr-1.7.0/include")
[...]
Why is bindgen unable to find the header files? Any help would be appreciated.
Dockerfile
FROM rust
ARG USERNAME="rustacian"
RUN apt-get update
RUN apt-get -y install --no-install-recommends apache2-dev llvm librust-bindgen-dev
RUN groupadd -r ${USERNAME} && useradd -s /bin/bash -g ${USERNAME} ${USERNAME}
USER ${USERNAME}
WORKDIR /home/${USERNAME}
build.rs
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
// Tell cargo to tell rustc to link the
// shared library.
println!("cargo:include=/home/rustacian/downloads/httpd-2.4.46/include");
println!("cargo:include=/home/rustacian/downloads/apr-1.7.0/include");
println!("cargo:include=/usr/include/apache2");
println!("cargo:include=/usr/include/apr-1.0");
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h")
// Add the includes for C header files.
// Derived from https://httpd.apache.org/docs/2.4/developer/modguide.html
// $ apxs -a -c mod_example.c
.clang_arg("-I/usr/include/apache2 -I/usr/include/apr-1.0 -I/home/rustacian/downloads/httpd-2.4.46/include -I/home/rustacian/downloads/apr-1.7.0/include")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// 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!");
}
wrapper.h
#include "stdio.h"
#include "apr_hash.h"
#include "ap_config.h"
#include "ap_provider.h"
#include "httpd.h"
#include "http_core.h"
#include "http_config.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
Cargo.toml
[package]
name = "mod_rs"
version = "0.1.0"
authors = ["studersi <*****@******.***>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "*"
[build-dependencies]
bindgen = "*"