Skip to content

Commit

Permalink
Add cross compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Apr 11, 2024
1 parent e4916a0 commit 673d481
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/linux-daily-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@ jobs:
strategy:
matrix:
arch: [amd64, arm64]
include:
- arch: arm64
is_arm: true
steps:
# - if: ${{ matrix.is_arm }}
# uses: pguyot/arm-runner-action@v2
# with:
# base_image: raspios_lite_arm64:latest

- name: Update system
run: sudo apt-get update && sudo apt-get --yes upgrade

- name: Install Nodejs 21 repo
run: sudo apt-get install --yes ca-certificates curl gnupg && curl -sL https://deb.nodesource.com/setup_21.x | sudo bash - && sudo apt-get update

- name: Install building tools
run: sudo apt-get install --yes git python3 python3-pip gcc g++ make ninja-build nodejs sudo
run: sudo apt-get install --yes git python3 python3-pip gcc g++ make build-essential binutils libz-dev linux-libc-dev nodejs sudo

- if: ${{ matrix.is_arm }}
name: Install cross platform building tools
run: sudo apt-get install --yes crossbuild-essential-arm64 linux-libc-dev-arm64-cross binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

- name: Clone the repo
run: git clone https://github.com/devraymondsh/libnode-distributable
Expand All @@ -30,8 +42,6 @@ jobs:

- name: Run the script
env:
CC: "sccache gcc"
CXX: "sccache g++"
ARCH: ${{ matrix.arch }}
SCCACHE_GHA_ENABLED: "true"
run: cd libnode-distributable && node index.js
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/macos-daily-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ on:
jobs:
update:
name: Macos daily check
runs-on: macos-latest
strategy:
matrix:
arch: [amd64, arm64]
include:
- arch: arm64
os: macos-14
is_arm: true
- arch: amd64
os: macos-13
runs-on: ${{ matrix.os }}
steps:
- run: sysctl -a | grep machdep.cpu

- name: Install building tools
run: brew install node python ninja nasm git

Expand All @@ -24,8 +32,6 @@ jobs:

- name: Run the script
env:
CC: "sccache cc"
CXX: "sccache c++"
ARCH: ${{ matrix.arch }}
SCCACHE_GHA_ENABLED: "true"
run: cd libnode-distributable && node index.js
Expand Down
68 changes: 60 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { spawn } from "node:child_process";
let CC = process.env.CC;
let CXX = process.env.CXX;
let ARCH = process.env.ARCH;
let CROSS_COMPILE = process.env.CROSS_COMPILE;
if (!CC) CC = "gcc";
if (!CXX) CXX = "g++";
if (!ARCH) ARCH = "amd64";
if (!CROSS_COMPILE) CROSS_COMPILE = false;

const coreCount = cpus().length;
const threadCount = coreCount * 2;
Expand Down Expand Up @@ -55,11 +57,16 @@ const isANewerVersion = (oldVer, newVer) => {
return false;
};

const spawnAsync = (program, args, cwd) =>
const spawnAsync = (program, args, cwd, env) =>
new Promise((resolve, reject) => {
console.log([program, ...args].join(" "));
console.log("ENV: ", process.env);
console.log("Running:", [program, ...args].join(" "));

const child = spawn(program, args, cwd ? { cwd } : {});
const child = spawn(
program,
args,
cwd ? { cwd, env: { ...process.env, ...env } } : {}
);

child.stdout.on("data", (chunk) => console.log(chunk.toString()));
child.stderr.on("data", (chunk) => console.error(chunk.toString()));
Expand All @@ -85,17 +92,62 @@ if (!syncFs.existsSync("node")) {
`v${latestNodeVersion}`,
"--depth=1",
],
undefined
undefined,
{}
);
}

let extraArgs = [];
if (process.platform == "win32") {
await spawnAsync("vcbuild.bat", [arch, "dll"], "node");
await spawnAsync("vcbuild.bat", [arch, "dll"], "node", {});
} else {
let env;
if (process.platform === "linux") {
if (arch === "arm64") {
process.env.CC_host = "sccache gcc";
process.env.CXX_host = "sccache g++";
process.env.CC = "sccache aarch64-linux-gnu-gcc";
process.env.CXX = "sccache aarch64-linux-gnu-g++";

env = {
CC_host: "sccache gcc",
CXX_host: "sccache g++",
CC: "sccache aarch64-linux-gnu-gcc",
CXX: "sccache aarch64-linux-gnu-g++",
};
} else {
process.env.CC = "sccache gcc";
process.env.CXX = "sccache g++";

env = {
CC: "sccache gcc",
CXX: "sccache g++",
};
}
} else {
process.env.CC = "sccache cc";
process.env.CXX = "sccache c++";

env = {
CC: "sccache cc",
CXX: "sccache c++",
};
}

if (arch === "arm64") {
if (os === "linux") extraArgs.push("--cross-compiling");

extraArgs.push("--with-arm-float-abi");
extraArgs.push("hard");
extraArgs.push("--with-arm-fpu");
extraArgs.push("neon");
}

await spawnAsync(
"./configure",
["--ninja", "--shared", "--dest-cpu", arch, "--dest-os", os],
"node"
["--shared", "--dest-cpu", arch, "--dest-os", os, ...extraArgs],
"node",
env
);
await spawnAsync("make", [`-j${threadCount}`], "node");
await spawnAsync("make", [`-j${threadCount}`], "node", env);
}

0 comments on commit 673d481

Please sign in to comment.