Skip to content

Commit

Permalink
Generate ctru-sys bindings at build-time
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Jul 28, 2023
1 parent 35d299f commit 5e3aa28
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 19,125 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["ctru-rs", "ctru-sys", "ctru-sys/bindgen-ctru-sys"]
members = ["ctru-rs", "ctru-sys"]
default-members = ["ctru-rs", "ctru-sys"]

[patch.'https://github.com/rust3ds/ctru-rs']
Expand Down
5 changes: 4 additions & 1 deletion ctru-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ctru-sys"
version = "22.2.0+2.2.2-1"
authors = [ "Rust3DS Org", "Ronald Kinard <[email protected]>" ]
authors = ["Rust3DS Org", "Ronald Kinard <[email protected]>"]
license = "Zlib"
links = "ctru"
edition = "2021"
Expand All @@ -10,4 +10,7 @@ edition = "2021"
libc = { version = "0.2.121", default-features = false }

[build-dependencies]
bindgen = { version = "0.65.1", features = ["experimental"] }
cc = "1.0"
doxygen-rs = "0.4.2"
which = "4.4.0"
8 changes: 0 additions & 8 deletions ctru-sys/bindgen-ctru-sys/Cargo.toml

This file was deleted.

63 changes: 0 additions & 63 deletions ctru-sys/bindgen-ctru-sys/src/main.rs

This file was deleted.

38 changes: 0 additions & 38 deletions ctru-sys/bindgen.sh

This file was deleted.

91 changes: 83 additions & 8 deletions ctru-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
use bindgen::callbacks::ParseCallbacks;
use bindgen::{Builder, RustTarget};

use std::env;
use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};

#[derive(Debug)]
struct CustomCallbacks;

impl ParseCallbacks for CustomCallbacks {
fn process_comment(&self, comment: &str) -> Option<String> {
Some(doxygen_rs::transform(comment))
}
}

fn main() {
let dkp_path = env::var("DEVKITPRO").unwrap();
let devkitpro = env::var("DEVKITPRO").unwrap();
let devkitarm = env::var("DEVKITARM").unwrap();
let profile = env::var("PROFILE").unwrap();
let pwd = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

// Link to libctru
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=DEVKITPRO");
println!("cargo:rustc-link-search=native={dkp_path}/libctru/lib");
println!("cargo:rustc-link-search=native={devkitpro}/libctru/lib");
println!(
"cargo:rustc-link-lib=static={}",
match profile.as_str() {
Expand All @@ -19,10 +32,6 @@ fn main() {
}
);

// Link to static inline fns wrapper
println!("cargo:rustc-link-search=native={}", pwd);
println!("cargo:rustc-link-lib=static=extern");

match check_libctru_version() {
Ok((maj, min, patch)) => {
eprintln!("using libctru version {maj}.{min}.{patch}");
Expand All @@ -36,6 +45,72 @@ fn main() {
}
Err(err) => println!("cargo:warning=failed to check libctru version: {err}"),
}

// Build libctru bindings
let include_path = PathBuf::from_iter([devkitpro.as_str(), "libctru", "include"]);
let ctru_header = include_path.join("3ds.h");

let sysroot = Path::new(&devkitarm).join("arm-none-eabi");
let system_include = sysroot.join("include");
let errno_header = system_include.join("errno.h");

let bindings = Builder::default()
.header(ctru_header.to_str().unwrap())
.header(errno_header.to_str().unwrap())
.rust_target(RustTarget::Nightly)
.use_core()
.trust_clang_mangling(false)
.must_use_type("Result")
.layout_tests(false)
.ctypes_prefix("::libc")
.prepend_enum_name(false)
.blocklist_type("u(8|16|32|64)")
.blocklist_type("__builtin_va_list")
.blocklist_type("__va_list")
.opaque_type("MiiData")
.derive_default(true)
.wrap_static_fns(true)
.wrap_static_fns_path(out_dir.join("libctru_statics_wrapper"))
.clang_args([
"--target=arm-none-eabi",
"--sysroot",
sysroot.to_str().unwrap(),
"-isystem",
system_include.to_str().unwrap(),
"-I",
include_path.to_str().unwrap(),
"-mfloat-abi=hard",
"-march=armv6k",
"-mtune=mpcore",
"-mfpu=vfp",
"-DARM11",
"-D__3DS__",
])
.parse_callbacks(Box::new(CustomCallbacks))
.generate()
.expect("unable to generate bindings");

bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings!");

// Compile static inline fns wrapper
let cc = Path::new(devkitarm.as_str()).join("bin/arm-none-eabi-gcc");
let ar = Path::new(devkitarm.as_str()).join("bin/arm-none-eabi-ar");

cc::Build::new()
.compiler(cc)
.archiver(ar)
.include(&include_path)
.file(out_dir.join("libctru_statics_wrapper.c"))
.flag(&format!("-isystem{}", system_include.to_str().unwrap()))
.flag("-march=armv6k")
.flag("-mtune=mpcore")
.flag("-mfloat-abi=hard")
.flag("-mfpu=vfp")
.flag("-mtp=soft")
.flag("-Wno-deprecated-declarations")
.compile("libctru_statics_wrapper");
}

fn parse_version(version: &str) -> Result<(String, String, String), &str> {
Expand Down
Binary file removed ctru-sys/libextern.a
Binary file not shown.
Loading

0 comments on commit 5e3aa28

Please sign in to comment.