diff --git a/build.py b/build.py index 3c8872c13..ca9b6191a 100755 --- a/build.py +++ b/build.py @@ -238,6 +238,8 @@ def run_ndk_build(flags): def run_cargo_build(args): os.chdir(op.join("native", "src")) + native_out = op.join("..", "out") + targets = set(args.target) & set(rust_targets) if "resetprop" in args.target: targets.add("magisk") @@ -245,33 +247,6 @@ def run_cargo_build(args): env = os.environ.copy() env["CARGO_BUILD_RUSTC"] = op.join(rust_bin, "rustc" + EXE_EXT) - # Install cxxbridge and generate C++ bindings - native_out = op.join("..", "out") - local_cargo_root = op.join(native_out, ".cargo") - cfg = op.join(".cargo", "config.toml") - cfg_bak = op.join(".cargo", "config.toml.bak") - try: - # Hide the config file for cargo install - mv(cfg, cfg_bak) - cxx_src = op.join("external", "cxx-rs", "gen", "cmd") - mkdir_p(local_cargo_root) - cmds = [cargo, "install", "--root", local_cargo_root, "--path", cxx_src] - if not args.verbose: - cmds.append("-q") - proc = execv(cmds, env) - if proc.returncode != 0: - error("cxxbridge-cmd installation failed!") - finally: - # Make sure the config file rename is always reverted - mv(cfg_bak, cfg) - cxxbridge = op.join(local_cargo_root, "bin", "cxxbridge" + EXE_EXT) - mkdir(native_gen_path) - for p in ["base", "boot", "core", "init", "sepolicy"]: - text = cmd_out([cxxbridge, op.join(p, "lib.rs")]) - write_if_diff(op.join(native_gen_path, f"{p}-rs.cpp"), text) - text = cmd_out([cxxbridge, "--header", op.join(p, "lib.rs")]) - write_if_diff(op.join(native_gen_path, f"{p}-rs.hpp"), text) - # Start building the actual build commands cmds = [cargo, "build"] for target in targets: diff --git a/native/.gitignore b/native/.gitignore index 861b7b95e..4a4f41a4a 100644 --- a/native/.gitignore +++ b/native/.gitignore @@ -3,3 +3,5 @@ obj libs /.externalNativeBuild /.cxx +*-rs.cpp +*-rs.hpp diff --git a/native/src/Android.mk b/native/src/Android.mk index a954a5ce3..5fcf00a33 100644 --- a/native/src/Android.mk +++ b/native/src/Android.mk @@ -32,6 +32,7 @@ LOCAL_SRC_FILES := \ core/thread.cpp \ core/resetprop/persist.cpp \ core/resetprop/resetprop.cpp \ + core/core-rs.cpp \ su/su.cpp \ su/connect.cpp \ su/pts.cpp \ @@ -85,7 +86,8 @@ LOCAL_SRC_FILES := \ init/rootdir.cpp \ init/getinfo.cpp \ init/twostage.cpp \ - init/selinux.cpp + init/selinux.cpp \ + init/init-rs.cpp include $(BUILD_EXECUTABLE) @@ -116,7 +118,8 @@ LOCAL_SRC_FILES := \ boot/dtb.cpp \ boot/ramdisk.cpp \ boot/pattern.cpp \ - boot/cpio.cpp + boot/cpio.cpp \ + boot/boot-rs.cpp include $(BUILD_EXECUTABLE) diff --git a/native/src/Cargo.lock b/native/src/Cargo.lock index 7c5d290a6..bbb045280 100644 --- a/native/src/Cargo.lock +++ b/native/src/Cargo.lock @@ -29,6 +29,7 @@ version = "0.0.0" dependencies = [ "cfg-if", "cxx", + "cxx-gen", "libc", ] @@ -56,6 +57,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "cxx" version = "1.0.94" @@ -65,6 +76,16 @@ dependencies = [ "cxxbridge-macro", ] +[[package]] +name = "cxx-gen" +version = "0.7.94" +dependencies = [ + "codespan-reporting", + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "cxxbridge-flags" version = "1.0.94" @@ -183,6 +204,7 @@ version = "0.0.0" dependencies = [ "base", "cxx", + "cxx-gen", "num-derive", "num-traits", ] @@ -194,6 +216,7 @@ dependencies = [ "base", "byteorder", "cxx", + "cxx-gen", "protobuf", "protobuf-codegen", ] @@ -204,6 +227,7 @@ version = "0.0.0" dependencies = [ "base", "cxx", + "cxx-gen", ] [[package]] @@ -247,9 +271,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" dependencies = [ "unicode-ident", ] @@ -389,6 +413,15 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.40" @@ -415,6 +448,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "which" version = "4.4.0" @@ -426,6 +465,37 @@ dependencies = [ "once_cell", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/native/src/base/Android.mk b/native/src/base/Android.mk index c8ebb8113..ae3f9561b 100644 --- a/native/src/base/Android.mk +++ b/native/src/base/Android.mk @@ -15,6 +15,7 @@ LOCAL_SRC_FILES := \ selinux.cpp \ logging.cpp \ stream.cpp \ + base-rs.cpp \ ../external/cxx-rs/src/cxx.cc include $(BUILD_STATIC_LIBRARY) diff --git a/native/src/base/Cargo.toml b/native/src/base/Cargo.toml index a487fb03d..a966c0ae7 100644 --- a/native/src/base/Cargo.toml +++ b/native/src/base/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" [lib] path = "lib.rs" +[build-dependencies] +cxx-gen = { path = "../external/cxx-rs/gen/lib" } + [dependencies] cxx = { path = "../external/cxx-rs" } libc = "0.2" diff --git a/native/src/base/build.rs b/native/src/base/build.rs new file mode 100644 index 000000000..f9862b8bc --- /dev/null +++ b/native/src/base/build.rs @@ -0,0 +1,7 @@ +use crate::gen::gen_cxx_binding; + +mod gen; + +fn main() { + gen_cxx_binding("base-rs"); +} diff --git a/native/src/base/gen.rs b/native/src/base/gen.rs new file mode 100644 index 000000000..5456c46e5 --- /dev/null +++ b/native/src/base/gen.rs @@ -0,0 +1,38 @@ +// This file hosts shared build script logic + +use std::fmt::Display; +use std::fs::File; +use std::io::Write; +use std::process; + +use cxx_gen::Opt; + +trait ResultExt { + fn ok_or_exit(self) -> T; +} + +impl ResultExt for Result { + fn ok_or_exit(self) -> T { + match self { + Ok(r) => r, + Err(e) => { + eprintln!("error occurred: {}", e); + process::exit(1); + } + } + } +} + +pub fn gen_cxx_binding(name: &str) { + println!("cargo:rerun-if-changed=lib.rs"); + let opt = Opt::default(); + let gen = cxx_gen::generate_header_and_cc_with_path("lib.rs", &opt); + { + let mut cpp = File::create(format!("{}.cpp", name)).unwrap(); + cpp.write_all(gen.implementation.as_slice()).ok_or_exit(); + } + { + let mut hpp = File::create(format!("{}.hpp", name)).unwrap(); + hpp.write_all(gen.header.as_slice()).ok_or_exit(); + } +} diff --git a/native/src/base/include/base.hpp b/native/src/base/include/base.hpp index a8481f353..1518ed48c 100644 --- a/native/src/base/include/base.hpp +++ b/native/src/base/include/base.hpp @@ -5,4 +5,4 @@ #include "../files.hpp" #include "../misc.hpp" #include "../logging.hpp" -#include +#include "../base-rs.hpp" diff --git a/native/src/base/logging.cpp b/native/src/base/logging.cpp index 11fb93cd9..aab721a5f 100644 --- a/native/src/base/logging.cpp +++ b/native/src/base/logging.cpp @@ -6,9 +6,6 @@ #include #include -// Just need to include it somewhere -#include - using namespace std; static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) { diff --git a/native/src/base/logging.hpp b/native/src/base/logging.hpp index 2e706186f..405864ce3 100644 --- a/native/src/base/logging.hpp +++ b/native/src/base/logging.hpp @@ -3,8 +3,6 @@ #include #include -#include - void LOGD(const char *fmt, ...) __printflike(1, 2); void LOGI(const char *fmt, ...) __printflike(1, 2); void LOGW(const char *fmt, ...) __printflike(1, 2); diff --git a/native/src/base/misc.hpp b/native/src/base/misc.hpp index 7fc5ad8cb..b7c241a0f 100644 --- a/native/src/base/misc.hpp +++ b/native/src/base/misc.hpp @@ -7,8 +7,6 @@ #include #include -#include - #define DISALLOW_COPY_AND_MOVE(clazz) \ clazz(const clazz &) = delete; \ clazz(clazz &&) = delete; diff --git a/native/src/base/xwrap.hpp b/native/src/base/xwrap.hpp index 8c4c1d1cd..490ce517d 100644 --- a/native/src/base/xwrap.hpp +++ b/native/src/base/xwrap.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include "base-rs.hpp" using rust::xpipe2; diff --git a/native/src/boot/Cargo.toml b/native/src/boot/Cargo.toml index e6c393db6..bb1b270f7 100644 --- a/native/src/boot/Cargo.toml +++ b/native/src/boot/Cargo.toml @@ -9,6 +9,7 @@ path = "lib.rs" [build-dependencies] protobuf-codegen = "3.2.0" +cxx-gen = { path = "../external/cxx-rs/gen/lib" } [dependencies] base = { path = "../base" } diff --git a/native/src/boot/build.rs b/native/src/boot/build.rs index 8f07fe9ad..f30aefc15 100644 --- a/native/src/boot/build.rs +++ b/native/src/boot/build.rs @@ -1,5 +1,10 @@ use protobuf_codegen::Customize; +use crate::gen::gen_cxx_binding; + +#[path = "../base/gen.rs"] +mod gen; + fn main() { println!("cargo:rerun-if-changed=update_metadata.proto"); protobuf_codegen::Codegen::new() @@ -9,4 +14,6 @@ fn main() { .customize(Customize::default().gen_mod_rs(false)) .out_dir(".") .run_from_script(); + + gen_cxx_binding("boot-rs"); } diff --git a/native/src/boot/lib.rs b/native/src/boot/lib.rs index d1ad803f1..25ed2d22d 100644 --- a/native/src/boot/lib.rs +++ b/native/src/boot/lib.rs @@ -9,6 +9,7 @@ mod update_metadata; #[cxx::bridge(namespace = "rust")] pub mod ffi { extern "C++" { + include!("compress.hpp"); pub unsafe fn decompress(in_: *const u8, in_size: u64, fd: i32) -> bool; } diff --git a/native/src/boot/magiskboot.hpp b/native/src/boot/magiskboot.hpp index eebc11df1..e401654eb 100644 --- a/native/src/boot/magiskboot.hpp +++ b/native/src/boot/magiskboot.hpp @@ -2,6 +2,8 @@ #include +#include "boot-rs.hpp" + #define HEADER_FILE "header" #define KERNEL_FILE "kernel" #define RAMDISK_FILE "ramdisk.cpio" diff --git a/native/src/boot/main.cpp b/native/src/boot/main.cpp index dd1728483..487e66300 100644 --- a/native/src/boot/main.cpp +++ b/native/src/boot/main.cpp @@ -4,8 +4,6 @@ #include "magiskboot.hpp" #include "compress.hpp" -#include "boot-rs.cpp" - using namespace std; static void print_formats() { diff --git a/native/src/core/Cargo.toml b/native/src/core/Cargo.toml index 5d7ed4208..a59f55eb8 100644 --- a/native/src/core/Cargo.toml +++ b/native/src/core/Cargo.toml @@ -7,6 +7,9 @@ edition = "2021" crate-type = ["staticlib"] path = "lib.rs" +[build-dependencies] +cxx-gen = { path = "../external/cxx-rs/gen/lib" } + [dependencies] base = { path = "../base" } cxx = { path = "../external/cxx-rs" } diff --git a/native/src/core/build.rs b/native/src/core/build.rs new file mode 100644 index 000000000..e9f2d1cf6 --- /dev/null +++ b/native/src/core/build.rs @@ -0,0 +1,8 @@ +use crate::gen::gen_cxx_binding; + +#[path = "../base/gen.rs"] +mod gen; + +fn main() { + gen_cxx_binding("core-rs"); +} diff --git a/native/src/core/daemon.cpp b/native/src/core/daemon.cpp index 6dc38f4ff..33594245e 100644 --- a/native/src/core/daemon.cpp +++ b/native/src/core/daemon.cpp @@ -10,8 +10,6 @@ #include #include -#include - #include "core.hpp" using namespace std; diff --git a/native/src/external/cxx-rs b/native/src/external/cxx-rs index 09864b46d..f05bbb7c8 160000 --- a/native/src/external/cxx-rs +++ b/native/src/external/cxx-rs @@ -1 +1 @@ -Subproject commit 09864b46d76f32a5c6dbb6d15112cca6d1793340 +Subproject commit f05bbb7c8fa6639685fb6bb8708dc6ea55d9ddbb diff --git a/native/src/include/daemon.hpp b/native/src/include/daemon.hpp index f58a23be5..b68f1c2d0 100644 --- a/native/src/include/daemon.hpp +++ b/native/src/include/daemon.hpp @@ -8,7 +8,7 @@ #include #include -#include +#include "../core/core-rs.hpp" #define AID_ROOT 0 #define AID_SHELL 2000 diff --git a/native/src/init/Cargo.toml b/native/src/init/Cargo.toml index 19cebe69b..b687b5ebd 100644 --- a/native/src/init/Cargo.toml +++ b/native/src/init/Cargo.toml @@ -7,6 +7,9 @@ edition = "2021" crate-type = ["staticlib"] path = "lib.rs" +[build-dependencies] +cxx-gen = { path = "../external/cxx-rs/gen/lib" } + [dependencies] base = { path = "../base" } cxx = "1.0.69" diff --git a/native/src/init/build.rs b/native/src/init/build.rs new file mode 100644 index 000000000..21b31a551 --- /dev/null +++ b/native/src/init/build.rs @@ -0,0 +1,8 @@ +use crate::gen::gen_cxx_binding; + +#[path = "../base/gen.rs"] +mod gen; + +fn main() { + gen_cxx_binding("init-rs"); +} diff --git a/native/src/init/init.cpp b/native/src/init/init.cpp index fc9652061..9b6ad4721 100644 --- a/native/src/init/init.cpp +++ b/native/src/init/init.cpp @@ -10,8 +10,6 @@ #include "init.hpp" -#include - using namespace std; bool unxz(int fd, const uint8_t *buf, size_t size) { diff --git a/native/src/init/init.hpp b/native/src/init/init.hpp index 6a9aaf422..96592421e 100644 --- a/native/src/init/init.hpp +++ b/native/src/init/init.hpp @@ -1,5 +1,6 @@ #include -#include + +#include "init-rs.hpp" using kv_pairs = std::vector>;