Generate cxx binding in build.rs

This commit is contained in:
topjohnwu 2023-05-19 15:16:54 -07:00
parent 57bd450798
commit 2bcf2e76f1
27 changed files with 169 additions and 49 deletions

View File

@ -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:

2
native/.gitignore vendored
View File

@ -3,3 +3,5 @@ obj
libs
/.externalNativeBuild
/.cxx
*-rs.cpp
*-rs.hpp

View File

@ -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)

74
native/src/Cargo.lock generated
View File

@ -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"

View File

@ -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)

View File

@ -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"

7
native/src/base/build.rs Normal file
View File

@ -0,0 +1,7 @@
use crate::gen::gen_cxx_binding;
mod gen;
fn main() {
gen_cxx_binding("base-rs");
}

38
native/src/base/gen.rs Normal file
View File

@ -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<T> {
fn ok_or_exit(self) -> T;
}
impl<T, E: Display> ResultExt<T> for Result<T, E> {
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();
}
}

View File

@ -5,4 +5,4 @@
#include "../files.hpp"
#include "../misc.hpp"
#include "../logging.hpp"
#include <base-rs.hpp>
#include "../base-rs.hpp"

View File

@ -6,9 +6,6 @@
#include <flags.h>
#include <base.hpp>
// Just need to include it somewhere
#include <base-rs.cpp>
using namespace std;
static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {

View File

@ -3,8 +3,6 @@
#include <cerrno>
#include <cstdarg>
#include <base-rs.hpp>
void LOGD(const char *fmt, ...) __printflike(1, 2);
void LOGI(const char *fmt, ...) __printflike(1, 2);
void LOGW(const char *fmt, ...) __printflike(1, 2);

View File

@ -7,8 +7,6 @@
#include <bitset>
#include <random>
#include <base-rs.hpp>
#define DISALLOW_COPY_AND_MOVE(clazz) \
clazz(const clazz &) = delete; \
clazz(clazz &&) = delete;

View File

@ -5,7 +5,7 @@
#include <poll.h>
#include <fcntl.h>
#include <base-rs.hpp>
#include "base-rs.hpp"
using rust::xpipe2;

View File

@ -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" }

View File

@ -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");
}

View File

@ -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;
}

View File

@ -2,6 +2,8 @@
#include <sys/types.h>
#include "boot-rs.hpp"
#define HEADER_FILE "header"
#define KERNEL_FILE "kernel"
#define RAMDISK_FILE "ramdisk.cpio"

View File

@ -4,8 +4,6 @@
#include "magiskboot.hpp"
#include "compress.hpp"
#include "boot-rs.cpp"
using namespace std;
static void print_formats() {

View File

@ -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" }

8
native/src/core/build.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::gen::gen_cxx_binding;
#[path = "../base/gen.rs"]
mod gen;
fn main() {
gen_cxx_binding("core-rs");
}

View File

@ -10,8 +10,6 @@
#include <db.hpp>
#include <flags.h>
#include <core-rs.cpp>
#include "core.hpp"
using namespace std;

@ -1 +1 @@
Subproject commit 09864b46d76f32a5c6dbb6d15112cca6d1793340
Subproject commit f05bbb7c8fa6639685fb6bb8708dc6ea55d9ddbb

View File

@ -8,7 +8,7 @@
#include <functional>
#include <socket.hpp>
#include <core-rs.hpp>
#include "../core/core-rs.hpp"
#define AID_ROOT 0
#define AID_SHELL 2000

View File

@ -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"

8
native/src/init/build.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::gen::gen_cxx_binding;
#[path = "../base/gen.rs"]
mod gen;
fn main() {
gen_cxx_binding("init-rs");
}

View File

@ -10,8 +10,6 @@
#include "init.hpp"
#include <init-rs.cpp>
using namespace std;
bool unxz(int fd, const uint8_t *buf, size_t size) {

View File

@ -1,5 +1,6 @@
#include <base.hpp>
#include <init-rs.hpp>
#include "init-rs.hpp"
using kv_pairs = std::vector<std::pair<std::string, std::string>>;