From 519386124c7e687e4872c6944fe32703ab21917e Mon Sep 17 00:00:00 2001 From: "sijie.sun" Date: Fri, 10 Jan 2025 23:02:35 +0800 Subject: [PATCH] initial version --- .github/workflows/rust.yml | 32 ++++++++++++++++++++++++++++++++ .gitignore | 10 ++++++++++ .gitmodules | 3 +++ Cargo.toml | 16 ++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 3 +++ build.rs | 33 +++++++++++++++++++++++++++++++++ kcp | 1 + src/lib.rs | 5 +++++ wrapper.h | 1 + 10 files changed, 125 insertions(+) create mode 100644 .github/workflows/rust.yml create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.rs create mode 160000 kcp create mode 100644 src/lib.rs create mode 100644 wrapper.h diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..c0063e9 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,32 @@ +name: Rust + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@master + - name: Checkout submodules + uses: srt32/git-actions@v0.0.3 + with: + args: git submodule update --init --recursive + - name: Set up Clang + uses: egor-tensin/setup-clang@v1 + with: + version: latest + platform: x64 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..088ba6b --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8969e86 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "kcp"] + path = kcp + url = https://github.com/skywind3000/kcp diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..abb5e83 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "kcp-sys" +version = "0.1.0" +edition = "2021" +authors = ["sunsijie@buaa.edu.cn"] +description = "Safe bindings for KCP transport protocol" +repository = "https://github.com/EasyTier/kcp-sys" +readme = "README.md" +license = "MIT" +keywords = ["kcp", "bindings"] + +[dependencies] + +[build-dependencies] +bindgen = "0.71.1" +cc = "1.2.7" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..23aaf62 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 b23r0 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8289177 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# kcp-sys + +Safe bindings to the [kcp](https://github.com/skywind3000/kcp) transport protocol library. diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..adf26a8 --- /dev/null +++ b/build.rs @@ -0,0 +1,33 @@ +use std::env; +use std::path::{Path, PathBuf}; + +fn main() { + println!("cargo:rustc-link-lib=kcp"); + let dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let fulldir = Path::new(&dir).join("kcp"); + + let mut config = cc::Build::new(); + config.include(fulldir.clone()); + config.file(fulldir.join("ikcp.c")); + config.opt_level(3); + config.warnings(false); + config.compile("libkcp.a"); + println!("cargo:rustc-link-search=native={}", fulldir.display()); + + println!("cargo:rerun-if-changed=kcp/ikcp.h"); + println!("cargo:rerun-if-changed=kcp/ikcp.c"); + println!("cargo:rerun-if-changed=wrapper.h"); + + let bindings = bindgen::Builder::default() + .header("wrapper.h") + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + .allowlist_function("ikcp_.*") + .opaque_type("ikcpcb") + .generate() + .expect("Unable to generate bindings"); + + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/kcp b/kcp new file mode 160000 index 0000000..7f98058 --- /dev/null +++ b/kcp @@ -0,0 +1 @@ +Subproject commit 7f9805887b0909c52c825925f123e7a84da37167 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..a38a13a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/wrapper.h b/wrapper.h new file mode 100644 index 0000000..7adbb0f --- /dev/null +++ b/wrapper.h @@ -0,0 +1 @@ +#include "kcp/ikcp.h" \ No newline at end of file