Update builder and migrate to ESM #3

Merged
Sirherobrine23 merged 7 commits from ci_zig into main 2024-02-17 16:41:50 +00:00
4 changed files with 72 additions and 229 deletions
Showing only changes of commit 83b05e3be5 - Show all commits

@ -1,100 +0,0 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
linux_test:
runs-on: ubuntu-latest
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout
- uses: actions/setup-node@v4
name: Setup Node.js
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: |
echo "Host arch: $(uname -m)"
export DEBIAN_FRONTEND=noninteractive
sudo apt update
packages=(
"binutils-multiarch"
"build-essential"
);
# if [[ $(uname -m) == "x86_64" ]]; then
# packages+=(
# "libc6-arm64-cross"
# "gcc-*aarch64-linux-gnu"
# "gcc-*aarch64-linux-gnu-base"
# "g++-*aarch64-linux-gnu"
# )
# else
# packages+=(
# "libc6-amd64-cross"
# "g++-*x86-64-linux-gnu"
# "gcc-*x86-64-linux-gnu"
# )
# if [[ $(uname -m) != "aarch64" ]]; then
# packages+=(
# "libc6-arm64-cross"
# "gcc-*aarch64-linux-gnu"
# "gcc-*aarch64-linux-gnu-base"
# "g++-*aarch64-linux-gnu"
# )
# fi
# fi
sudo apt install -y ${packages[@]}
npm install --no-save --ignore-scripts
- name: Build addon
run: npm run dev
- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ runner.os }}_${{ matrix.node_version }}
path: "prebuilds/**"
- name: Test
run: node --require ts-node/register --loader ts-node/esm ./src/index_test.ts
pack_package:
needs: linux_test
runs-on: ubuntu-latest
name: Pack npm package
env:
PACKAGE_VERSION: ${{ github.ref }}
steps:
- uses: actions/checkout@v4
name: Code checkout
- uses: actions/setup-node@v4
name: Setup node.js
with:
node-version: 20.x
registry-url: https://registry.npmjs.org/
- name: Download all artefacts
uses: actions/download-artifact@v3
with:
path: ./prebuilds
- run: npm install --no-save --ignore-scripts
- run: npm pack
- name: Upload npm package
uses: actions/upload-artifact@v3
with:
name: Package_Pack
path: "*.tgz"

@ -13,10 +13,15 @@ jobs:
strategy: strategy:
matrix: matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ] node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
target_os: [ "windows", "linux", "macos" ]
target_arch: [ "x86_64", "aarch64" ]
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
name: Checkout name: Checkout
- uses: goto-bus-stop/setup-zig@v2
name: "Setup zig"
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
name: Setup Node.js name: Setup Node.js
with: with:
@ -26,96 +31,29 @@ jobs:
run: | run: |
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
sudo apt update sudo apt update
sudo apt install -y binutils-multiarch gcc-*aarch64-linux-gnu gcc-*aarch64-linux-gnu-base g++-*aarch64-linux-gnu libc6-arm64-cross sudo apt install -y "binutils-multiarch" "build-essential"
npm install --no-save --ignore-scripts npm install --no-save --ignore-scripts
- name: Build addon - name: Build addon
run: npm run prebuildify -- -v run: npm run dev -- --target_zig=${{ matrix.target_arch }}-${{ matrix.target_os }}
env:
- name: Test CC: zig cc -target ${{ matrix.target_arch }}-${{ matrix.target_os }}
run: sudo -E node --require ts-node/register --loader ts-node/esm ./src/index_test.ts CXX: "zig c++ -target ${{ matrix.target_arch }}-${{ matrix.target_os }}"
AR: "zig ar"
RANLIB: "zig ranlib"
- name: Upload prebuilds interface - name: Upload prebuilds interface
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
retention-days: 7 retention-days: 7
name: prebuilds_${{ runner.os }} name: prebuilds_${{ matrix.target_arch }}-${{ matrix.target_os }}_${{ matrix.node_version }}
path: "prebuilds/**" path: "prebuilds/**"
macos_test:
runs-on: macos-latest
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout
- uses: actions/setup-node@v4
name: Setup Node.js
with:
node-version: ${{ matrix.node_version }}
- name: Setup Go environment
uses: actions/setup-go@v5.0.0
- name: Setup wireguard-go
run: |
cd ..
git clone https://git.zx2c4.com/wireguard-go
cd wireguard-go
echo "WG_INETRFACE=utun15" >> $GITHUB_ENV
go build -v -o "wireguard-go"
sudo ./wireguard-go utun15
- name: Install dependencies
run: npm install --no-save --ignore-scripts
- name: Build addon
run: npm run prebuildify -- -v
- name: Test
run: sudo -E node --require ts-node/register --loader ts-node/esm ./src/index_test.ts
- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ runner.os }}
path: "prebuilds/**"
win_test:
runs-on: windows-latest
strategy:
matrix:
node_version: [ 16.x, 18.x, 19.x, 20.x, 21.x ]
steps:
- uses: actions/checkout@v4
name: Checkout
- uses: actions/setup-node@v4
name: Setup Node.js
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm install --no-save --ignore-scripts
- name: Build addon
run: npm run prebuildify -- -v
- name: Test - name: Test
run: node --require ts-node/register --loader ts-node/esm ./src/index_test.ts run: node --require ts-node/register --loader ts-node/esm ./src/index_test.ts
- name: Upload prebuilds interface
uses: actions/upload-artifact@v3
with:
retention-days: 7
name: prebuilds_${{ runner.os }}
path: "prebuilds/**"
pack_package: pack_package:
needs: [ linux_test, macos_test, win_test ] needs: linux_test
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Pack npm package name: Pack npm package
env: env:

@ -1,9 +1,8 @@
#include <napi.h> #include <napi.h>
#include <wginterface.hh> #include <wginterface.hh>
#include <net/if.h>
unsigned long maxName() { unsigned long maxName() {
return IFNAMSIZ; return 16;
} }
std::string versionDrive() { std::string versionDrive() {

@ -8,22 +8,26 @@ import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Fix ESM __dirname const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Fix ESM __dirname
const nodeGyp = path.resolve(createRequire(import.meta.url).resolve("node-gyp"), "../../bin/node-gyp.js"); // Node gyp script const nodeGyp = path.resolve(createRequire(import.meta.url).resolve("node-gyp"), "../../bin/node-gyp.js"); // Node gyp script
const env = Object.assign({}, process.env); const env = Object.assign({}, process.env);
const args = process.argv.slice(2);
const prebuilds = path.resolve(__dirname, "../prebuilds"); const prebuilds = path.resolve(__dirname, "../prebuilds");
const buildDir = path.resolve(__dirname, "../build") /* Build Directory */, buildRelease = path.join(buildDir, "Release"), buildDebug = path.join(buildDir, "Debug"); const buildDir = path.resolve(__dirname, "../build") /* Build Directory */, buildRelease = path.join(buildDir, "Release"), buildDebug = path.join(buildDir, "Debug");
/**
* @param {string} path
* @returns
*/
async function exist(path) { async function exist(path) {
return fs.open(path).then(e => e.close().then(() => true, () => true), () => false); return fs.open(path).then(e => e.close().then(() => true, () => true), () => false);
} }
/** /**
*
* @param {string} command * @param {string} command
* @param {string[]} args * @param {string[]} args
* @param {Omit<import("child_process").ForkOptions, "stdio">} options * @param {Omit<import("child_process").ForkOptions, "stdio">} options
*/ */
async function fork(command, args, options) { async function fork(command, args, options) {
if (options) options["stdio"] = undefined; if (options) options["stdio"] = undefined;
console.log("%s", command, ...args);
return new Promise((done, reject) => { return new Promise((done, reject) => {
const child = child_process.fork(command, args, options); const child = child_process.fork(command, args, options);
child.on("error", reject); child.on("error", reject);
@ -76,60 +80,62 @@ async function migrateBuildAddon(platform, arch) {
await fs.rm(buildDir, { recursive: true, force: true }); await fs.rm(buildDir, { recursive: true, force: true });
} }
if (process.argv.slice(2).at(0) === "build") { if (args.at(0) === "build") {
let archs = []; const config = {
if (process.argv.includes("--clean")) { clean: args.includes("--clean"),
zigUse: false,
/** @type {{ os: NodeJS.Platform, arch: NodeJS.Architecture, extra_env?: Record<string, string> }[]} */
targets: [ { os: process.platform, arch: process.arch } ],
};
if (args.some(s => s.startsWith("--zig_target=")||s.startsWith("--target="))) {
config.zigUse = true;
config.targets = [];
const check = /^([a-zA-Z0-9_\-]+)-([a-zA-Z0-9_]+)$/;
const ts = args.filter(s => s.startsWith("--zig_target=")||s.startsWith("--target=")).map(s => (s[3] === "z" ? s.slice(13) : s.slice(9)).trim());
for (const t of ts) {
if (!(check.test(t))) {
console.error("Target (%s) is invalid!", t);
process.exit(-1);
}
let [, arch_t, os_t ] = (check.exec(t)||[]);
if (os_t === "windows") os_t = "win32";
else if (os_t === "macos") os_t = "darwin";
if (arch_t === "aarch64") arch_t = "arm64";
else if (arch_t === "x86_64") arch_t = "x64";
config.targets.push({
os: os_t,
arch: arch_t,
extra_env: {
AR: "zig ar",
RANLIB: "zig ranlib",
CC: ("zig cc -target ").concat(t),
CXX: ("zig c++ -target ").concat(t),
}
});
}
} else if (args.some(s => s.startsWith("--arch=") && s.slice(7).trim().length > 0)) {
config.targets = args.filter(s => s.startsWith("--arch=") && s.slice(7).trim().length > 0).map(s => s.slice(7)).map(arch => ({ os: process.platform, arch }));
}
if (config.clean) {
if (await exist(buildDir)) await fs.rm(buildDir, { recursive: true, force: true }); if (await exist(buildDir)) await fs.rm(buildDir, { recursive: true, force: true });
if (await exist(prebuilds)) await fs.rm(prebuilds, { recursive: true, force: true }); if (await exist(prebuilds)) await fs.rm(prebuilds, { recursive: true, force: true });
} }
if (process.argv.includes("--auto")) {
if (process.platform === "linux") archs.push("x64", "arm64");
else if (process.platform === "win32") archs.push("x64", "arm64");
else if (process.platform === "darwin") archs.push("x64", "arm64");
else archs.push(process.arch);
} else {
process.argv.slice(2).filter(f => f.startsWith("--arch=")).map(arch => arch.slice(7));
if (archs.length <= 0) archs.push(process.arch);
}
for (const arch of Array.from(new Set(archs))) {
if (process.platform === "linux" && arch !== process.arch) {
if (arch === "x64") {
// x86_64-linux-gnu-gcc
env.CC = "x86_64-linux-gnu-gcc";
env.CXX = "x86_64-linux-gnu-g++";
} else if (arch === "arm64") {
// aarch64-linux-gnu-gcc
env.CC = "aarch64-linux-gnu-gcc";
env.CXX = "aarch64-linux-gnu-g++";
}
} else if (process.platform === "win32" && arch !== process.arch) {
let skip = true;
for (const vsPath of [ "C:\\Program Files (x86)\\Microsoft Visual Studio", "C:\\Program Files\\Microsoft Visual Studio" ]) {
if (!(await exist(vsPath))) continue;
const year = ((await fs.readdir(vsPath)).filter(s => !(isNaN(Number(s)))).sort((a, b) => (Number(a) < Number(b)) ? -1 : 0).at(-1));
if (!year) continue;
for (const vsEdition of await fs.readdir(path.join(vsPath, year))) {
if (await exist(path.join(vsPath, year, vsEdition, "MSBuild\\Current\\Bin", arch))) {
if (skip) skip = false;
break;
}
}
if (!skip) break;
}
if (skip) {
console.info("Arch not avaible to copiler!");
continue;
}
}
for (const { os, arch, extra_env } of config.targets) {
console.log("\nBuild to %O/%O, envs: %O", os, arch, extra_env);
const args = [ "rebuild" ];
if (os !== "android") args.push("-j", "max");
args.push(("--arch=").concat(arch));
try { try {
console.log("Bulding to %O\n", arch); await fork(nodeGyp, args, { env: Object.assign({}, env, extra_env) });
await fork(nodeGyp, ["rebuild", ...(process.platform !== "android"?["-j", "max"]:[]), ("--arch=").concat(arch)], {env}); await migrateBuildAddon(os, arch);
console.log("Migrating addons!");
await migrateBuildAddon(process.platform, arch);
} catch (err) { } catch (err) {
if (process.platform === "win32" && arch !== process.arch) continue; console.error(err);
throw err; process.exit(-1);
} }
} }
} else if (!(await exist(path.join(prebuilds, `${process.platform}_${process.arch}`)) || await exist(buildRelease))) { } else if (!(await exist(path.join(prebuilds, `${process.platform}_${process.arch}`)) || await exist(buildRelease))) {