Skip to content

Commit 8b2d5d2

Browse files
committed
do a few cbindgen things
1 parent 28661e5 commit 8b2d5d2

File tree

15 files changed

+220
-59
lines changed

15 files changed

+220
-59
lines changed

Cargo.lock

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ You'll probably need to copy/look-at some code from the extensions in *this* rep
6363

6464

6565
### Why doesn't this use C++ helper crates?
66-
I just didn't... I could say it's simpler or easier to keep the C++ separate, which might be true. There's obvious value in using bindgen/cbindgen so you don't mess up FFI function declarations. The inline C++ with the `cpp` crate looks very useful. The original srcwrtimer extensions mainly keep the SourceMod interfacing on the C++ side which calls to Rust functions to natives, so there's not a lot of fancy `Rust <-> C++` FFI going on.
66+
It's a bit simpler to just make headers with extern functions in both Rust & C++, but things will be investigated more eventually. Using cbindgen and bindgen is planned too.
6767

68-
Helper crates (which aren't used):
69-
- [cpp](https://crates.io/crates/cpp)
68+
Helper crates:
69+
- [cpp](https://crates.io/crates/cpp) (Unused)
7070
- Allows you to embed C++ code in a Rust file.
71-
- [cxx](https://crates.io/crates/cxx)
71+
- [cxx](https://crates.io/crates/cxx) (Unused)
7272
- A `Rust <-> C++` binding/bridging crate.
73-
- [autocxx](https://crates.io/crates/autocxx)
73+
- [autocxx](https://crates.io/crates/autocxx) (Unused)
7474
- A more magical cxx.
75-
- [bindgen](https://crates.io/crates/bindgen)
75+
- [bindgen](https://crates.io/crates/bindgen) (TODO)
7676
- Generate Rust bindings to C/C++ libraries...
77-
- [cbindgen](https://crates.io/crates/cbindgen)
78-
- Generate C/C++ bindings to Rust libraries... TODO
77+
- [cbindgen](https://crates.io/crates/cbindgen) (WIP usage)
78+
- Generate C/C++ bindings to Rust libraries...
7979

8080
### Why does this use C++ at all?
8181
Interfacing with SourceMod through Rust can be tedious. You *can* do it in pure Rust thanks to the [sm-ext-rs](https://github.com/srcwr/sm-ext-rs) (srcwr fork) crate that [asherkin](https://github.com/asherkin) made, but lacking hl2sdk support makes it impractical for many things.

extshared/src/IFileObject.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use std::io::Seek;
77
use std::io::SeekFrom;
88
use std::io::Write;
99

10+
/// cbindgen:no-export
1011
#[repr(transparent)]
1112
pub struct IFileObject {
12-
pub vtable: usize,
13+
pub vtable: usize
1314
}
1415

1516
unsafe extern "C" {

extshared_build_helper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ homepage.workspace = true
99
repository.workspace = true
1010

1111
[dependencies]
12-
#cbindgen = { version = "0.28", default-features = false } # we don't want a dep on clap so 'default-features = false'
12+
cbindgen = { version = "0.28", default-features = false } # we don't want a dep on clap so 'default-features = false'
1313
cc = { version = "1.0", features = ["parallel"] }
1414
regex = "1"
1515
vergen-gitcl = { version = "1.0", features = ["build"] }

extshared_build_helper/cbindgen.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ parse_deps = true
1919
# Otherwise the serde_json::Value & extshared::{IFileObject, ICellArray} can't be reached...
2020
# Probably slows down bindgen...
2121
include = ["extshared", "serde_json"]
22+
23+
[export]
24+
# Some things that are done from extshared/src/rust_exports.h but want to be pulled in anyway...
25+
exclude = ["rust_sdk_on_unload", "rust_sdk_on_all_loaded", "rust_on_core_map_start", "rust_on_core_map_end"]

extshared_build_helper/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ pub fn smext_build() -> cc::Build {
382382
// slurps the extension's src dir (not the src dir build_template.rs is in)
383383
slurp_folder(&mut build, "src");
384384

385-
/*
385+
build
386+
}
387+
388+
pub fn do_cbindgen() {
386389
println!("cargo:rerun-if-changed=../extshared_build_helper/cbindgen.toml");
387390
cbindgen::generate_with_config(
388391
".",
@@ -394,7 +397,4 @@ pub fn smext_build() -> cc::Build {
394397
"src/rust_exports_{}.h",
395398
std::env::var("CARGO_PKG_NAME").unwrap()
396399
));
397-
*/
398-
399-
build
400400
}

smbz2/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ fn main() {
88
let outdir = std::env::var("OUT_DIR").unwrap();
99
generate_inc_defines_and_enums(&outdir, "../srcwrtimer/addons/sourcemod/scripting/include/bzip2.inc", "BZIP2");
1010

11+
do_cbindgen();
12+
1113
let build = smext_build();
1214
compile_lib(build, "smext");
1315
}

smbz2/src/natives_bzip2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "../../extshared/src/extension.h"
77
#include "../../extshared/src/coreident.hpp"
8-
#include "rust_exports_bzip2.h"
8+
#include "rust_exports_smbz2.h"
99

1010

1111
extern const sp_nativeinfo_t BZ2Natives[];

smbz2/src/rust_exports_bzip2.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

smbz2/src/rust_exports_smbz2.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
4+
5+
#include <stdint.h>
6+
#include <stddef.h>
7+
8+
extern "C" {
9+
10+
uint32_t rust_BZ2_XompressFile(const char *infile, const char *outfile, const char *infilefull, const char *outfilefull, int32_t compressionLevel, void *forward, int32_t data);
11+
12+
} // extern "C"

0 commit comments

Comments
 (0)