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 14, 2024
1 parent e4916a0 commit d0a89c3
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 61 deletions.
77 changes: 58 additions & 19 deletions .github/workflows/linux-daily-check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Linux daily check
name: Linux

on:
schedule:
Expand All @@ -7,34 +7,73 @@ on:

jobs:
update:
name: Linux daily check
runs-on: ubuntu-latest
strategy:
matrix:
arch: [amd64, arm64]
include:
- arch: arm64
is_arm: true
docker_container: null
- arch: amd64
docker_container: ubuntu:devel
container:
image: ${{ matrix.docker_container }}
steps:
- name: Update system
run: sudo apt-get update && sudo apt-get --yes upgrade
- uses: actions/checkout@v4

- 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: Setup ccache
uses: actions/cache@v4
with:
path: .ccache
save-always: true
key: ${{ github.workflow }}-${{ matrix.arch }}

- name: Install building tools
run: sudo apt-get install --yes git python3 python3-pip gcc g++ make ninja-build nodejs sudo
- if: ${{ matrix.is_arm }}
uses: pguyot/arm-runner-action@v2
with:
base_image: https://dietpi.com/downloads/images/testing/DietPi_RPi5-ARMv8-Bookworm.img.xz
import_github_env: yes
image_additional_mb: 10240
commands: |
export CC="gcc"
export CXX="g++"
export ARCH="arm64"
- name: Clone the repo
run: git clone https://github.com/devraymondsh/libnode-distributable
export CCACHE_COMPRESS="true"
export CCACHE_BASEDIR="$GITHUB_WORKSPACE"
export CCACHE_DIR="$CCACHE_BASEDIR/.ccache"
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
mkdir -p "$CCACHE_DIR"
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
sudo apt-get update
sudo apt-get install --yes ca-certificates curl gnupg && curl -sL https://deb.nodesource.com/setup_21.x | sudo bash - && sudo apt-get update
sudo apt-get install --yes git nodejs python3 python3-pip gcc g++ make nasm ccache linux-headers-generic
node index.js
ls node/out/Release
- name: Run the script
- if: ${{ ! matrix.is_arm }}
name: Install building tools
run: |
apt-get install --yes ca-certificates curl gnupg && curl -sL https://deb.nodesource.com/setup_21.x | bash -
apt-get update
apt-get install --yes git nodejs python3 python3-pip gcc g++ make nasm ccache linux-headers-generic
- if: ${{ ! matrix.is_arm }}
name: Run the script
env:
CC: "sccache gcc"
CXX: "sccache g++"
CC: "gcc"
CXX: "g++"
ARCH: ${{ matrix.arch }}
SCCACHE_GHA_ENABLED: "true"
run: cd libnode-distributable && node index.js
run: |
export CCACHE_COMPRESS="true"
export CCACHE_BASEDIR="$GITHUB_WORKSPACE"
export CCACHE_DIR="$CCACHE_BASEDIR/.ccache"
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
mkdir -p "$CCACHE_DIR"
node index.js
- name: Find the shared library
run: find libnode-distributable/node -name libnode.so\* -not -name '*.TOC' -not -name '*.toc' -exec ls {} \;
- if: ${{ ! matrix.is_arm }}
name: Find the shared library
run: ls node/out/Release
57 changes: 57 additions & 0 deletions .github/workflows/m1-daily-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: M1

on:
schedule:
- cron: "0 8 * * *"
workflow_dispatch:

jobs:
update:
strategy:
matrix:
os: [linux]
include:
- os: linux
is_linux: true
runs-on: macos-14
steps:
- uses: actions/checkout@v4

- name: Setup ccache
uses: actions/cache@v4
with:
path: .ccache
save-always: true
key: ${{ github.workflow }}-${{ matrix.os }}

- name: Install building tools
run: |
sudo chown -R $(whoami) $(brew --prefix)/*
# homebrew fails to update python 3.9.1 to 3.9.1.1 due to unlinking failure
sudo rm -f /usr/local/bin/2to3 || true
# homebrew fails to update python from 3.9 to 3.10 due to another unlinking failure
sudo rm -f /usr/local/bin/idle3 || true
sudo rm -f /usr/local/bin/pydoc3 || true
sudo rm -f /usr/local/bin/python3 || true
sudo rm -f /usr/local/bin/python3-config || true
brew install git node ninja nasm llvm ccache aarch64-elf-binutils aarch64-elf-gcc
- name: Run the script
env:
OS: linux
ARCH: arm64
CC: "ccache aarch64-elf-gcc"
CXX: "ccache aarch64-elf-gcc"
run: |
export CCACHE_COMPRESS="true"
export CCACHE_BASEDIR="$GITHUB_WORKSPACE"
export CCACHE_DIR="$CCACHE_BASEDIR/.ccache"
# export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
mkdir -p "$CCACHE_DIR"
node index.js
- name: Find the shared library
run: ls node/out/Release
51 changes: 38 additions & 13 deletions .github/workflows/macos-daily-check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Macos daily check
name: Macos

on:
schedule:
Expand All @@ -7,28 +7,53 @@ 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:
- uses: actions/checkout@v4

- name: Setup ccache
uses: actions/cache@v4
with:
path: .ccache
save-always: true
key: ${{ github.workflow }}-${{ matrix.arch }}

- name: Install building tools
run: brew install node python ninja nasm git
run: |
sudo chown -R $(whoami) $(brew --prefix)/*
- name: Clone the repo
run: git clone https://github.com/devraymondsh/libnode-distributable
# homebrew fails to update python 3.9.1 to 3.9.1.1 due to unlinking failure
sudo rm -f /usr/local/bin/2to3 || true
# homebrew fails to update python from 3.9 to 3.10 due to another unlinking failure
sudo rm -f /usr/local/bin/idle3 || true
sudo rm -f /usr/local/bin/pydoc3 || true
sudo rm -f /usr/local/bin/python3 || true
sudo rm -f /usr/local/bin/python3-config || true
- name: Run sccache-cache
uses: mozilla-actions/[email protected]
brew install git node ninja nasm ccache
- name: Run the script
env:
CC: "sccache cc"
CXX: "sccache c++"
CC: "gcc"
CXX: "g++"
ARCH: ${{ matrix.arch }}
SCCACHE_GHA_ENABLED: "true"
run: cd libnode-distributable && node index.js
run: |
export CCACHE_COMPRESS="true"
export CCACHE_BASEDIR="$GITHUB_WORKSPACE"
export CCACHE_DIR="$CCACHE_BASEDIR/.ccache"
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
mkdir -p "$CCACHE_DIR"
node index.js
- name: Find the shared library
run: find libnode-distributable/node -name libnode.so\* -not -name '*.TOC' -not -name '*.toc' -exec ls {} \;
run: ls node/out/Release
26 changes: 13 additions & 13 deletions .github/workflows/windows-daily-check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Windows daily check
name: Windows

on:
schedule:
Expand All @@ -7,37 +7,37 @@ on:

jobs:
update:
name: Windows daily check
name: Windows
runs-on: windows-latest
strategy:
matrix:
arch: [amd64, amd64_arm64]
steps:
- uses: actions/checkout@v4

# - name: Setup ccache
# uses: actions/cache@v4
# with:
# key: ${{ github.workflow }}-${{ matrix.arch }}
# path: C:\.ccache

- uses: ilammy/msvc-dev-cmd@v1
with:
arch: ${{ matrix.arch }}

- uses: MinoruSekine/setup-scoop@v3
with:
buckets: extras
update_path: true
run_as_admin: true
scoop_update: true

- name: Install building tools
run: scoop install nodejs python ninja nasm git

- name: Clone the repo
run: git clone https://github.com/devraymondsh/libnode-distributable

- name: Run sccache-cache
uses: mozilla-actions/[email protected]
run: scoop install git nodejs python ninja nasm

- name: Run the script
env:
ARCH: ${{ matrix.arch }}
SCCACHE_GHA_ENABLED: "true"
run: cd libnode-distributable ; node index.js
run: node index.js

- name: Find the shared library
run: dir libnode-distributable\node\out\Release\libnode.dll
run: dir out\Release\libnode.dll
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node
node
.ccache
20 changes: 20 additions & 0 deletions dockerfiles/linux-arm64/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM ubuntu:devel

WORKDIR /app

ENV CC="gcc"
ENV CXX="g++"
ENV ARCH="arm64"
ENV CCACHE_BASEDIR="/app"
ENV CCACHE_COMPRESS="true"
ENV CCACHE_DIR="$CCACHE_BASEDIR/.ccache"
ENV PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"

RUN mkdir -p "$CCACHE_DIR"

RUN sudo apt-get update
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
RUN sudo apt-get install --yes git nodejs python3 python3-pip gcc g++ make nasm ccache linux-headers-generic

ENTRYPOINT [ "node", "/app/index.js", "&&", "ls", "/app/node/out/Release" ]

44 changes: 29 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ import { cpus } from "node:os";
import fs from "node:fs/promises";
import { spawn } from "node:child_process";

let CC = process.env.CC;
let CXX = process.env.CXX;
let ARCH = process.env.ARCH;
if (!CC) CC = "gcc";
if (!CXX) CXX = "g++";
if (!ARCH) ARCH = "amd64";
let OS = process.env.OS;
const ARCH = process.env.ARCH == "amd64" ? "x64" : "arm64";

const coreCount = cpus().length;
const threadCount = coreCount * 2;
const arch = ARCH == "amd64" ? "x64" : "arm64";
let os;
let current_os;
switch (process.platform) {
case "darwin":
os = "mac";
current_os = "mac";
break;
case "win32":
os = "win";
current_os = "win";
break;
default:
os = "linux";
current_os = "linux";
break;
}

if (!OS) OS = current_os;

const nodejsGithubRepo = "https://github.com/nodejs/node";
const removeTheVCharacter = (str) => str.replace("v", "");

Expand Down Expand Up @@ -57,7 +54,7 @@ const isANewerVersion = (oldVer, newVer) => {

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

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

Expand Down Expand Up @@ -85,16 +82,33 @@ 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",
// "/p:CLToolExe=cl.exe /p:CLToolPath=C:\\Users\\runneradmin\\.cargo\\bin\\cl.exe",
],
"node"
);
} else {
if (ARCH === "arm64") {
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],
["--shared", "--dest-cpu", ARCH, "--dest-os", OS, ...extraArgs],
"node"
);
await spawnAsync("make", [`-j${threadCount}`], "node");
Expand Down

0 comments on commit d0a89c3

Please sign in to comment.