Browse Source

Generate ctru-sys bindings at build-time

pull/133/head
Fenrir 1 year ago
parent
commit
073ebd767b
  1. 2
      Cargo.toml
  2. 5
      ctru-sys/Cargo.toml
  3. 8
      ctru-sys/bindgen-ctru-sys/Cargo.toml
  4. 63
      ctru-sys/bindgen-ctru-sys/src/main.rs
  5. 38
      ctru-sys/bindgen.sh
  6. 117
      ctru-sys/build.rs
  7. BIN
      ctru-sys/libextern.a
  8. 19002
      ctru-sys/src/bindings.rs
  9. 6
      ctru-sys/src/lib.rs

2
Cargo.toml

@ -1,5 +1,5 @@ @@ -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']

5
ctru-sys/Cargo.toml

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
[package]
name = "ctru-sys"
version = "22.2.0+2.2.2-1"
authors = [ "Rust3DS Org", "Ronald Kinard <furyhunter600@gmail.com>" ]
authors = ["Rust3DS Org", "Ronald Kinard <furyhunter600@gmail.com>"]
license = "Zlib"
links = "ctru"
edition = "2021"
@ -10,4 +10,7 @@ edition = "2021" @@ -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
ctru-sys/bindgen-ctru-sys/Cargo.toml

@ -1,8 +0,0 @@ @@ -1,8 +0,0 @@
[package]
name = "bindgen-ctru-sys"
version = "0.1.0"
edition = "2021"
[dependencies]
bindgen = { version = "0.65.1", features = ["experimental"] }
doxygen-rs = "0.4.2"

63
ctru-sys/bindgen-ctru-sys/src/main.rs

@ -1,63 +0,0 @@ @@ -1,63 +0,0 @@
use bindgen::callbacks::ParseCallbacks;
use bindgen::{Builder, RustTarget};
use std::path::PathBuf;
#[derive(Debug)]
struct CustomCallbacks;
impl ParseCallbacks for CustomCallbacks {
fn process_comment(&self, comment: &str) -> Option<String> {
Some(doxygen_rs::transform(comment))
}
}
fn main() {
let devkitpro = std::env::var("DEVKITPRO").expect("DEVKITPRO not set in environment");
let devkitarm = std::env::var("DEVKITARM").expect("DEVKITARM not set in environment");
let include_path = PathBuf::from_iter([devkitpro.as_str(), "libctru", "include"]);
let ctru_header = include_path.join("3ds.h");
let sysroot = PathBuf::from(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)
.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(Box::new(std::io::stdout()))
.expect("failed to write bindings");
}

38
ctru-sys/bindgen.sh

@ -1,38 +0,0 @@ @@ -1,38 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
echo "Determining libctru version..."
pacman=dkp-pacman
if ! command -v $pacman &>/dev/null; then
pacman=pacman
if ! command -v $pacman &>/dev/null; then
echo >&2 "ERROR: Unable to automatically determine libctru version!"
exit 1
fi
fi
LIBCTRU_VERSION="$($pacman -Qi libctru | grep Version | cut -d: -f 2 | tr -d ' ')"
CTRU_SYS_VERSION="$(
printf '%s' "$LIBCTRU_VERSION" |
cut -d- -f1 |
sed -E 's/^([0-9]+)\.([0-9.]+)$/\1\2/'
)"
echo "Generating bindings.rs..."
cargo run --package bindgen-ctru-sys > src/bindings.rs
echo "Formatting generated files..."
cargo fmt --all
echo "Compiling static inline wrappers..."
arm-none-eabi-gcc -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft \
-I${DEVKITPRO}/libctru/include \
-I${DEVKITPRO}/libctru/include/3ds \
-O -c -o extern.o /tmp/bindgen/extern.c
arm-none-eabi-ar -rcs libextern.a extern.o
rm extern.o
echo "Generated bindings for ctru-sys version \"${CTRU_SYS_VERSION}.x+${LIBCTRU_VERSION}\""

117
ctru-sys/build.rs

@ -1,16 +1,29 @@ @@ -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() {
@ -19,10 +32,6 @@ fn main() { @@ -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}");
@ -36,9 +45,97 @@ fn main() { @@ -36,9 +45,97 @@ fn main() {
}
Err(err) => println!("cargo:warning=failed to check libctru version: {err}"),
}
let gcc_version = get_gcc_version(PathBuf::from(&devkitarm).join("bin/arm-none-eabi-gcc"));
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 gcc_include = PathBuf::from(format!(
"{devkitarm}/lib/gcc/arm-none-eabi/{gcc_version}/include"
));
let errno_header = system_include.join("errno.h");
// Build libctru bindings
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(),
"-isystem",
gcc_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("-march=armv6k")
.flag("-mtune=mpcore")
.flag("-mfloat-abi=hard")
.flag("-mfpu=vfp")
.flag("-mtp=soft")
.flag("-Wno-deprecated-declarations")
.compile("ctru_statics_wrapper");
}
fn get_gcc_version(path_to_gcc: PathBuf) -> String {
let Output { stdout, .. } = Command::new(path_to_gcc)
.arg("--version")
.stderr(Stdio::inherit())
.output()
.unwrap();
let stdout_str = String::from_utf8_lossy(&stdout);
stdout_str
.split(|c: char| c.is_whitespace())
.nth(4)
.unwrap()
.to_string()
}
fn parse_version(version: &str) -> Result<(String, String, String), &str> {
fn parse_libctru_version(version: &str) -> Result<(String, String, String), &str> {
let versions: Vec<_> = version
.split(|c| c == '.' || c == '-')
.map(String::from)
@ -90,6 +187,6 @@ fn check_libctru_version() -> Result<(String, String, String), Box<dyn Error>> { @@ -90,6 +187,6 @@ fn check_libctru_version() -> Result<(String, String, String), Box<dyn Error>> {
println!("cargo:rerun-if-changed={file}");
}
let (lib_major, lib_minor, lib_patch) = parse_version(lib_version)?;
let (lib_major, lib_minor, lib_patch) = parse_libctru_version(lib_version)?;
Ok((lib_major, lib_minor, lib_patch))
}

BIN
ctru-sys/libextern.a

Binary file not shown.

19002
ctru-sys/src/bindings.rs generated

File diff suppressed because it is too large Load Diff

6
ctru-sys/src/lib.rs

@ -5,12 +5,10 @@ @@ -5,12 +5,10 @@
#![allow(clippy::all)]
pub mod result;
mod bindings;
pub use bindings::*;
pub use result::*;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
/// In lieu of a proper errno function exposed by libc
/// (<https://github.com/rust-lang/libc/issues/1995>).
pub unsafe fn errno() -> s32 {

Loading…
Cancel
Save