Skip to content

Commit

Permalink
fix(cli): zig cross armv7 (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 9, 2022
1 parent 00b09bc commit 2abc946
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/linux-aarch64.yaml
Expand Up @@ -2,7 +2,6 @@ name: Linux-aarch64

env:
DEBUG: 'napi:*'
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: 'aarch64-linux-gnu-gcc-9'

on:
push:
Expand Down Expand Up @@ -41,7 +40,7 @@ jobs:
path: |
~/.cargo/registry
~/.cargo/git
key: stable-linux-aarch64-gnu-node@16-cargo-cache
key: stable-linux-aarch64-gnu-node@18-cargo-cache

- name: Install ziglang
uses: goto-bus-stop/setup-zig@v2
Expand Down
42 changes: 22 additions & 20 deletions cli/src/build.ts
Expand Up @@ -58,12 +58,14 @@ function processZigLinkerArgs(platform: string, args: string[]) {
return newArgs
}
if (platform.includes('linux')) {
return args.map((arg) => {
if (arg === '-lgcc_s') {
return '-lunwind'
}
return arg
})
return args
.map((arg) => {
if (arg === '-lgcc_s') {
return '-lunwind'
}
return arg
})
.filter((arg) => arg !== '-march=armv7-a')
}
return args
}
Expand Down Expand Up @@ -328,10 +330,11 @@ export class BuildCommand extends Command {
this.zigABIVersion ?? (isCrossForLinux && triple.abi === 'gnu')
? DEFAULT_GLIBC_TARGET
: null
const zigTarget = `${ZIG_PLATFORM_TARGET_MAP[triple.raw]}${
const mappedZigTarget = ZIG_PLATFORM_TARGET_MAP[triple.raw]
const zigTarget = `${mappedZigTarget}${
zigABIVersion ? `.${zigABIVersion}` : ''
}`
if (!zigTarget) {
if (!mappedZigTarget) {
throw new Error(`${triple.raw} can not be cross compiled by zig`)
}
const paths = envPaths('napi-rs')
Expand Down Expand Up @@ -371,24 +374,22 @@ export class BuildCommand extends Command {
)
await writeFileAsync(
CCWrapperShell,
`${SHEBANG_SH}zig cc -target ${zigTarget} ${forwardArgs}`,
`${SHEBANG_SH}node ${linkerWrapper} cc ${forwardArgs}`,
{
mode: '777',
},
)
await writeFileAsync(
CXXWrapperShell,
`${SHEBANG_SH}zig c++ -target ${zigTarget} ${forwardArgs}`,
`${SHEBANG_SH}node ${linkerWrapper} c++ ${forwardArgs}`,
{
mode: '777',
},
)

await writeFileAsync(
linkerWrapper,
`${SHEBANG_NODE}const{writeFileSync} = require('fs')\n${processZigLinkerArgs.toString()}\nconst {status} = require('child_process').spawnSync('zig', ['${
triple.platform === 'win32' ? 'c++' : 'cc'
}', ...processZigLinkerArgs('${
`${SHEBANG_NODE}const{writeFileSync} = require('fs')\n${processZigLinkerArgs.toString()}\nconst {status} = require('child_process').spawnSync('zig', [process.argv[2] === "c++" || process.argv[2] === "cc" ? "" : "cc", ...processZigLinkerArgs('${
triple.raw
}', process.argv.slice(2)), '-target', '${zigTarget}'], { stdio: 'inherit', shell: true })\nwriteFileSync('${linkerWrapper.replaceAll(
'\\',
Expand Down Expand Up @@ -822,22 +823,23 @@ async function writeJsBinding(

async function patchArmFeaturesHForArmTargets() {
let zigExePath: string
let zigLibDir: string | undefined
try {
const zigEnv = JSON.parse(execSync(`zig env`, { encoding: 'utf8' }).trim())
zigExePath = zigEnv['zig_exe']
zigLibDir = zigEnv['lib_dir']
} catch (e) {
throw new Error(
'Cannot get zig env correctly, please ensure the zig is installed correctly on your system',
)
}
try {
await writeFileAsync(
join(zigExePath, '../lib/libc/glibc/sysdeps/arm/arm-features.h'),
ARM_FEATURES_H,
{
mode: 0o644,
},
)
const p = zigLibDir
? join(zigLibDir, 'libc/glibc/sysdeps/arm/arm-features.h')
: join(zigExePath, '../lib/libc/glibc/sysdeps/arm/arm-features.h')
await writeFileAsync(p, ARM_FEATURES_H, {
mode: 0o644,
})
} catch (e) {
throw new Error(
`Cannot patch arm-features.h, error: ${
Expand Down
8 changes: 8 additions & 0 deletions examples/napi/Cargo.toml
Expand Up @@ -8,6 +8,9 @@ version = "0.1.0"
[lib]
crate-type = ["cdylib"]

[features]
snmalloc = ["snmalloc-rs"]

[dependencies]
chrono = "0.4"
futures = "0.3"
Expand All @@ -27,6 +30,11 @@ serde_derive = "1"
serde_json = "1"
tokio = { version = "1.20.0", features = ["full"] }

[dependencies.snmalloc-rs]
version = "0.3"
features = ["build_cc", "local_dynamic_tls"]
optional = true

[build-dependencies]
napi-build = { path = "../../crates/build" }

Expand Down
4 changes: 4 additions & 0 deletions examples/napi/src/lib.rs
Expand Up @@ -7,6 +7,10 @@ extern crate napi_derive;
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "snmalloc")]
#[global_allocator]
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

#[napi]
/// This is a const
pub const DEFAULT_COST: u32 = 12;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -27,5 +27,5 @@
"lib": ["dom", "DOM.Iterable", "ES2019", "ES2020", "esnext"]
},
"include": ["."],
"exclude": ["node_modules", "bench", "cli/scripts", "scripts"]
"exclude": ["node_modules", "bench", "cli/scripts", "scripts", "target"]
}

0 comments on commit 2abc946

Please sign in to comment.