From 1a6e51579b3a6fbb0ed9c437f4d84c56ecc1a40a Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Mon, 24 Feb 2025 15:58:46 -0600 Subject: [PATCH] Compiling and linking with sword --- Cargo.lock | 156 ++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 6 +- build.rs | 15 +++++ flake.lock | 44 ++++---------- flake.nix | 25 +++++++- src/cxx/mgr.cc | 8 +++ src/cxx/mgr.h | 14 +++++ src/lib.rs | 15 +---- src/mgr.rs | 21 +++++++ 9 files changed, 251 insertions(+), 53 deletions(-) create mode 100644 build.rs create mode 100644 src/cxx/mgr.cc create mode 100644 src/cxx/mgr.h create mode 100644 src/mgr.rs diff --git a/Cargo.lock b/Cargo.lock index 6561fc6..44989a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,16 @@ dependencies = [ "shlex", ] +[[package]] +name = "cfg-expr" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d4ba6e40bd1184518716a6e1a781bf9160e286d219ccdb8ab2612e74cfe4789" +dependencies = [ + "smallvec", + "target-lexicon", +] + [[package]] name = "clap" version = "4.5.31" @@ -112,12 +122,40 @@ dependencies = [ "syn", ] +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + [[package]] name = "foldhash" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indexmap" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "link-cplusplus" version = "1.0.9" @@ -127,6 +165,18 @@ dependencies = [ "cc", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + [[package]] name = "proc-macro2" version = "1.0.93" @@ -157,12 +207,47 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3cf7c11c38cb994f3d40e8a8cde3bbd1f72a435e4c49e85d6553d8312306152" +[[package]] +name = "serde" +version = "1.0.218" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.218" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "smallvec" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" + [[package]] name = "strsim" version = "0.11.1" @@ -170,11 +255,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] -name = "sword-rs" +name = "sword_rs" version = "0.1.0" dependencies = [ "cxx", "cxx-build", + "system-deps", ] [[package]] @@ -188,6 +274,25 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "system-deps" +version = "7.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d23aaf9f331227789a99e8de4c91bf46703add012bdfd45fdecdfb2975a005" +dependencies = [ + "cfg-expr", + "heck", + "pkg-config", + "toml", + "version-compare", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + [[package]] name = "termcolor" version = "1.4.1" @@ -197,6 +302,40 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "toml" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "unicode-ident" version = "1.0.17" @@ -209,6 +348,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "version-compare" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" + [[package]] name = "winapi-util" version = "0.1.9" @@ -290,3 +435,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7f4ea97f6f78012141bcdb6a216b2609f0979ada50b20ca5b52dde2eac2bb1" +dependencies = [ + "memchr", +] diff --git a/Cargo.toml b/Cargo.toml index c6caf70..5a2a9a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sword-rs" +name = "sword_rs" version = "0.1.0" edition = "2021" @@ -8,3 +8,7 @@ cxx = "1.0.141" [build-dependencies] cxx-build = "1.0.141" +system-deps = "7.0.3" + +[package.metadata.system-deps] +sword = ">= 1.8.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..c623576 --- /dev/null +++ b/build.rs @@ -0,0 +1,15 @@ +use system_deps; + +fn main() { + let deps = system_deps::Config::new().probe().unwrap(); + + cxx_build::bridge("src/mgr.rs") + .file("src/cxx/mgr.cc") + .includes(deps.all_include_paths()) + .compile("sword_rs"); + + println!("cargo:rerun-if-changed=src/mgr.rs"); + println!("cargo:rerun-if-changed=src/cxx/mgr.cc"); + println!("cargo:rerun-if-changed=src/cxx/mgr.h"); + println!("cargo:rustc-link-lib=sword"); +} diff --git a/flake.lock b/flake.lock index 3723c82..d62bc10 100644 --- a/flake.lock +++ b/flake.lock @@ -74,7 +74,9 @@ "inputs": { "flake-compat": "flake-compat", "gitignore": "gitignore", - "nixpkgs": "nixpkgs" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { "lastModified": 1737465171, @@ -92,7 +94,9 @@ }, "naersk": { "inputs": { - "nixpkgs": "nixpkgs_2" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { "lastModified": 1739824009, @@ -110,11 +114,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1730768919, - "narHash": "sha256-8AKquNnnSaJRXZxc5YmF/WfmxiHX6MMZZasRP6RRQkE=", + "lastModified": 1740303746, + "narHash": "sha256-XcdiWLEhjJkMxDLKQJ0CCivmYYCvA5MDxu9pMybM5kM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "a04d33c0c3f1a59a2c1cb0c6e34cd24500e5a1dc", + "rev": "2d068ae5c6516b2d04562de50a58c682540de9bf", "type": "github" }, "original": { @@ -136,41 +140,13 @@ "url": "https://github.com/NixOS/nixpkgs/archive/072a6db25e947df2f31aab9eccd0ab75d5b2da11.tar.gz" } }, - "nixpkgs_2": { - "locked": { - "lastModified": 0, - "narHash": "sha256-EO1ygNKZlsAC9avfcwHkKGMsmipUk1Uc0TbrEZpkn64=", - "path": "/nix/store/kdynjy1mbgkdg4p196v9gx6ljpf7q4nk-source", - "type": "path" - }, - "original": { - "id": "nixpkgs", - "type": "indirect" - } - }, - "nixpkgs_3": { - "locked": { - "lastModified": 1740303746, - "narHash": "sha256-XcdiWLEhjJkMxDLKQJ0CCivmYYCvA5MDxu9pMybM5kM=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "2d068ae5c6516b2d04562de50a58c682540de9bf", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, "root": { "inputs": { "flake-parts": "flake-parts", "flake-root": "flake-root", "hooks": "hooks", "naersk": "naersk", - "nixpkgs": "nixpkgs_3" + "nixpkgs": "nixpkgs" } } }, diff --git a/flake.nix b/flake.nix index 77348b2..60cb815 100644 --- a/flake.nix +++ b/flake.nix @@ -5,8 +5,14 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; flake-parts.url = "github:hercules-ci/flake-parts"; flake-root.url = "github:srid/flake-root"; - hooks.url = "github:cachix/git-hooks.nix"; - naersk.url = "github:nix-community/naersk"; + hooks = { + url = "github:cachix/git-hooks.nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + naersk = { + url = "github:nix-community/naersk"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = @@ -49,6 +55,10 @@ buildInputs = self'.checks.pre-commit.enabledPackages; + nativeBuildInputs = with pkgs; [ + pkg-config + ]; + RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc; inherit (self'.checks.pre-commit) shellHook; @@ -87,7 +97,16 @@ # This is not special, this is just flakes packages = rec { default = rustpkg; - rustpkg = naersk-lib.buildPackage ./.; + rustpkg = naersk-lib.buildPackage { + src = ./.; + buildInputs = with pkgs; [ + sword + ]; + nativeBuildInputs = with pkgs; [ + pkg-config + ]; + doCheck = true; + }; }; }; }; diff --git a/src/cxx/mgr.cc b/src/cxx/mgr.cc new file mode 100644 index 0000000..947fb41 --- /dev/null +++ b/src/cxx/mgr.cc @@ -0,0 +1,8 @@ +#include "mgr.h" +#include + +Mgr::Mgr() : mgr(new sword::SWMgr()) { } + +std::unique_ptr new_mgr() { + return std::unique_ptr(new Mgr()); +} diff --git a/src/cxx/mgr.h b/src/cxx/mgr.h new file mode 100644 index 0000000..4153bf6 --- /dev/null +++ b/src/cxx/mgr.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +class Mgr { + public: + Mgr(); + + private: + sword::SWMgr* mgr; +}; + +std::unique_ptr new_mgr(); diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..7983903 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod mgr; diff --git a/src/mgr.rs b/src/mgr.rs new file mode 100644 index 0000000..15be09b --- /dev/null +++ b/src/mgr.rs @@ -0,0 +1,21 @@ +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("sword_rs/src/cxx/mgr.h"); + + type Mgr; + + fn new_mgr() -> UniquePtr; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_create_mgr() { + let mgr = ffi::new_mgr(); + assert!(!mgr.is_null()); + } +}