Skip to content

适配ohos#61

Open
imndx wants to merge 1 commit into
nashaofu:masterfrom
imndx:master
Open

适配ohos#61
imndx wants to merge 1 commit into
nashaofu:masterfrom
imndx:master

Conversation

@imndx

@imndx imndx commented May 20, 2026

Copy link
Copy Markdown

你好
这些代码是在 AI 协助下实现的,测试可以工作

另外:

  1. 对 xcap 的依赖,现在写了个相对路径../xcap
  2. ohos上,截图之前,需要先申请ohos.permission.CUSTOM_SCREEN_CAPTURE权限

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds OpenHarmony (OHOS) support for the native node-screenshots binding, primarily by adjusting how N-API symbols are resolved under OHOS’s linker namespace isolation and by adding OHOS cross-compilation configuration.

Changes:

  • Adds an OHOS-specific .init_array initializer plus a local dlopen override to ensure Electron’s libelectron.so (V8 N-API) is used instead of the system ArkTS N-API.
  • Adds OHOS cross-compile linker wrappers and Cargo target configuration for *-unknown-linux-ohos.
  • Updates build/link settings and target lists to include OHOS, and switches xcap to a local path dependency.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/ohos_init.rs Adds OHOS runtime initialization to preload libelectron.so and intercept dlopen("libelectron.so").
src/lib.rs Conditionally includes the OHOS init module for target_env = "ohos".
build.rs Adds OHOS-only linker args (whole-archive shim, version script, symbolic binding behavior).
scripts/ohos_napi.map Version script to localize napi_*/uv_*/node_* and keep only napi_register_module_v1 global.
scripts/ohos-clang-aarch64.sh Cargo linker wrapper for OHOS aarch64.
scripts/ohos-clang-arm.sh Cargo linker wrapper for OHOS armv7.
scripts/ohos-clang-x86_64.sh Cargo linker wrapper for OHOS x86_64.
.cargo/config.toml Adds OHOS target linker configuration pointing to wrapper scripts.
Cargo.toml Disables default napi features, enables napi4, and changes xcap to a path dependency.
package.json Adds OHOS triples to napi.targets.
lib.rs Adds a new top-level lib.rs file (appears unrelated to this crate’s actual source layout).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Cargo.toml
napi = { version = "3.0.0", default-features = false, features = ["napi4"] }
napi-derive = "3.0.0"
xcap = { version = "0.4.1", features = ["image"] }
xcap = { path = "../xcap", features = ["image"] }
Comment thread build.rs
Comment on lines +3 to +16
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let manifest_path = std::path::Path::new(&manifest_dir);

let shim_a = manifest_path
.parent()
.expect("workspace parent dir")
.join("electron-napi-library/lib/libshim.a");

println!("cargo:warning=libshim.a path: {}", shim_a.display());
assert!(
shim_a.exists(),
"libshim.a not found at {}",
shim_a.display()
);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这儿编译时,依赖了 electron-napi-library,是从ohos_electron 的适配示例里面拿到的。
可能和electron版本有关系,目前 ohos electron主要适配了v34
electron-v34 electron-napi-library.zip

Comment thread build.rs
Comment on lines +46 to +52
let ver_script = manifest_path.join("scripts/ohos_napi.map");
println!("cargo:warning=version script: {}", ver_script.display());
println!(
"cargo:rustc-link-arg=-Wl,--version-script={}",
ver_script.display()
);
println!("cargo:rustc-link-arg=-Wl,-Bsymbolic-functions");
Comment thread src/ohos_init.rs
Comment on lines +41 to +46
// Write diagnostics to a file — eprintln! does not reach hilog on OHOS.
let mut log = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open("/data/storage/el1/base/nscr.log")
.ok();
Comment thread lib.rs
Comment on lines +1 to +6
use std::env;

mod android;
mod wasi;
mod windows;

Comment thread package.json
Comment on lines +36 to +39
"aarch64-pc-windows-msvc",
"aarch64-unknown-linux-ohos",
"armv7-unknown-linux-ohos",
"x86_64-unknown-linux-ohos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants