Skip to content

Commit

Permalink
Build macOS ARM64 and Windows ARM64 in GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Mar 10, 2024
1 parent d82a04e commit d2ae2a0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
47 changes: 37 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,56 @@ jobs:
strategy:
matrix:
name:
- darwin-x64
- linux-x64
- win32-x64
- darwin
- linux
- win32
include:
- name: darwin-x64
os: macOS-latest
- name: linux-x64
- name: darwin
os: macos-14
arch: arm64
arch2: x64
- name: linux
os: ubuntu-latest
- name: win32-x64
arch: x64
# node-gyp --arch=arm64 still produced x64 binaries.
# Maybe use https://buildjet.com/for-github-actions/docs/about/pricing#arm?
arch2: none
- name: win32
os: windows-latest
arch: x64
arch2: arm64

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn

# Build main arch and run tests.
- run: yarn install --frozen-lockfile
- run: yarn test
- uses: actions/upload-artifact@v4

# Build secondary arch.
- run: npx node-gyp rebuild --arch=${{ matrix.arch2 }}
if: ${{ matrix.arch2 != 'none' }}
- run: node scripts/build-sub-package.js ${{ matrix.arch2 }}
if: ${{ matrix.arch2 != 'none' }}

# Upload artifacts.
- name: Upload node-pty-${{ matrix.name }}-${{ matrix.arch }}
uses: actions/upload-artifact@v4
with:
name: node-pty-${{ matrix.name }}
path: node_modules/@lydell/node-pty-${{ matrix.name }}
name: node-pty-${{ matrix.name }}-${{ matrix.arch }}
path: node_modules/@lydell/node-pty-${{ matrix.name }}-${{ matrix.arch }}
if-no-files-found: error
- name: Upload node-pty-${{ matrix.name }}-${{ matrix.arch2 }}
uses: actions/upload-artifact@v4
with:
name: node-pty-${{ matrix.name }}-${{ matrix.arch2 }}
path: node_modules/@lydell/node-pty-${{ matrix.name }}-${{ matrix.arch2 }}
if-no-files-found: error
if: ${{ matrix.arch2 != 'none' }}

# To download the artifacts for publishing:
# rm -rf artifacts && gh run download RUN_ID --dir artifacts
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"build": "node scripts/build.js && node scripts/build-sub-package.js",
"watch": "tsc -b -w ./src/tsconfig.json",
"lint": "eslint -c .eslintrc.js --ext .ts src/",
"postinstall": "node scripts/post-install.js",
"test": "cross-env NODE_ENV=test mocha -R spec --exit lib/*.test.js",
"posttest": "npm run lint",
"pretest": "npm run build",
Expand Down
21 changes: 13 additions & 8 deletions scripts/build-sub-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const fs = require("fs");
const path = require("path");
const originalPackageJson = require("../package.json");

const PACKAGE_NAME = `@lydell/node-pty-${process.platform}-${process.arch}`;
const {platform} = process;
const [arch = process.arch] = process.argv.slice(2);

const PACKAGE_NAME = `@lydell/node-pty-${platform}-${arch}`;
const REPO_ROOT = path.join(__dirname, "..");
const RELEASE = path.join(REPO_ROOT, "build/Release");
const DIST = path.join(REPO_ROOT, "node_modules", PACKAGE_NAME);
Expand All @@ -13,14 +16,14 @@ fs.mkdirSync(DIST, { recursive: true });
const packageJson = {
"name": PACKAGE_NAME,
"version": originalPackageJson.forkVersion,
"description": `Prebuilt ${process.platform}-${process.arch} binaries for node-pty.`,
"description": `Prebuilt ${platform}-${arch} binaries for node-pty.`,
"repository": {
"type": "git",
"url": "git://github.com/lydell/node-pty.git"
},
"license": originalPackageJson.license,
"os": [ process.platform ],
"cpu": [ process.arch ]
"os": [ platform ],
"cpu": [ arch ]
};

const readme = `
Expand All @@ -35,15 +38,17 @@ fs.writeFileSync(
);

fs.writeFileSync(
path.join(DIST, "README.md"),
readme
path.join(DIST, "README.md"),
readme
);

for (const file of fs.readdirSync(RELEASE)) {
if (file.endsWith(".node") || file.endsWith(".pdb") || file === "spawn-helper") {
fs.copyFileSync(
path.join(RELEASE, file),
path.join(DIST, file)
path.join(RELEASE, file),
path.join(DIST, file)
);
}
}

console.info(PACKAGE_NAME);
Expand Down
32 changes: 27 additions & 5 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,29 @@ const DIST = path.join(REPO_ROOT, "dist");

const variants = [
{
name: "macOS",
platform: "darwin",
arch: "x64",
tested: false,
},
{
name: "macOS ARM",
platform: "darwin",
arch: "arm64",
tested: true,
},
{
name: "Linux x86_64",
platform: "linux",
arch: "x64",
tested: true,
},
{
name: "Windows",
platform: "win32",
arch: "x64",
tested: true,
},
{
platform: "win32",
arch: "arm64",
tested: false,
}
]

Expand Down Expand Up @@ -73,6 +78,23 @@ fs.cpSync(path.join(REPO_ROOT, "typings", "node-pty.d.ts"), path.join(DIST, "nod

fs.writeFileSync(path.join(DIST, "package.json"), JSON.stringify(packageJson, null, 2));

function osName(platform) {
switch (platform) {
case "darwin": return "macOS";
case "linux": return "Linux";
case "win32": return "Windows";
default: return platform;
}
}

function archName(arch) {
switch (arch) {
case "x64": return "x86_64";
case "arm64": return "ARM64";
default: return arch;
}
}

const readme = fs.readFileSync(path.join(REPO_ROOT, "README.md"), "utf8");
const newReadme = `
${readme.trim()}
Expand All @@ -85,6 +107,6 @@ ${readme.trim()}
This package includes prebuilt binaries for the following platforms and architectures:
${variants.map(({ name, platform, arch }) => `- ${name} (${platform}-${arch})`).join("\n")}
${variants.map(({ platform, arch, tested }) => `- ${osName(platform)} ${archName(arch)} (${platform}-${arch})${tested ? "": " (not tested)"}`).join("\n")}
`.trim();
fs.writeFileSync(path.join(DIST, "README.md"), newReadme);

0 comments on commit d2ae2a0

Please sign in to comment.