diff --git a/Cargo.lock b/Cargo.lock index 23242bd..826bef2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,16 @@ version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" +[[package]] +name = "cc" +version = "1.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6354c81bbfd62d9cfa9cb3c773c2b7b2a3a482d569de977fd0e961f6e7c00583" +dependencies = [ + "find-msvc-tools", + "shlex", +] + [[package]] name = "cexpr" version = "0.6.0" @@ -69,6 +79,12 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "find-msvc-tools" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db" + [[package]] name = "glob" version = "0.3.3" @@ -190,6 +206,7 @@ name = "rust-multi-os-template" version = "0.1.0" dependencies = [ "bindgen", + "cc", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6ffb6c3..1d3265a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2024" [dependencies] [build-dependencies] -bindgen = "0.72.1" \ No newline at end of file +bindgen = "0.72.1" +cc = "1.2.54" \ No newline at end of file diff --git a/build.rs b/build.rs index 3affed4..21e0206 100644 --- a/build.rs +++ b/build.rs @@ -1,79 +1,16 @@ -use std::env; -use std::path::PathBuf; +use std::{env, path::PathBuf}; fn main() { - // This is the directory where the `c` library is located. - let libdir_path = PathBuf::from("hello") - // Canonicalize the path as `rustc-link-search` requires an absolute - // path. - .canonicalize() - .expect("cannot canonicalize path"); + let target = env::var("TARGET").unwrap(); - // This is the path to the `c` headers file. - let headers_path = libdir_path.join("hello.h"); - let headers_path_str = headers_path.to_str().expect("Path is not a valid string"); + cc::Build::new().file("hello/hello.c").compile("hello"); - // This is the path to the intermediate object file for our library. - let obj_path = libdir_path.join("hello.o"); - // This is the path to the static library file. - let lib_path = libdir_path.join("libhello.a"); + let bindings = bindgen::builder() + .header("hello/hello.h") + .use_core() + .clang_arg(format!("--target={target}")) + .generate().unwrap(); - // Tell cargo to look for shared libraries in the specified directory - println!("cargo:rustc-link-search={}", libdir_path.to_str().unwrap()); - - // Tell cargo to tell rustc to link our `hello` library. Cargo will - // automatically know it must look for a `libhello.a` file. - println!("cargo:rustc-link-lib=hello"); - - // Run `clang` to compile the `hello.c` file into a `hello.o` object file. - // Unwrap if it is not possible to spawn the process. - if !std::process::Command::new("clang") - .arg("-c") - .arg("-o") - .arg(&obj_path) - .arg(libdir_path.join("hello.c")) - .output() - .expect("could not spawn `clang`") - .status - .success() - { - // Panic if the command was not successful. - panic!("could not compile object file"); - } - - // Run `ar` to generate the `libhello.a` file from the `hello.o` file. - // Unwrap if it is not possible to spawn the process. - if !std::process::Command::new("ar") - .arg("rcs") - .arg(lib_path) - .arg(obj_path) - .output() - .expect("could not spawn `ar`") - .status - .success() - { - // Panic if the command was not successful. - panic!("could not emit library file"); - } - - // 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(headers_path_str) - // Tell cargo to invalidate the built crate whenever any of the - // included header files changed. - .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) - // 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()).join("bindings.rs"); - bindings - .write_to_file(out_path) - .expect("Couldn't write bindings!"); -} \ No newline at end of file + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings.write_to_file(out_path.join("hello.rs")).unwrap(); +} diff --git a/src/lib.rs b/src/lib.rs index 428a22c..a8a5486 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,4 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] -include!(concat!(env!("OUT_DIR"), "/bindings.rs")); \ No newline at end of file +include!(concat!(env!("OUT_DIR"), "/hello.rs")); \ No newline at end of file