Skip to content

Commit

Permalink
feat(napi/parser): add more linux-musl targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Feb 5, 2024
1 parent 6fe9300 commit 839e7c5
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 35 deletions.
48 changes: 41 additions & 7 deletions .github/workflows/release_napi_parser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ jobs:
target: aarch64-unknown-linux-gnu
code-target: linux-arm64-gnu

- os: macos-14
- os: ubuntu-latest
target: x86_64-unknown-linux-musl
code-target: linux-x64-musl

- os: ubuntu-latest
target: aarch64-unknown-linux-musl
code-target: linux-arm64-musl

- os: macos-latest
target: x86_64-apple-darwin
code-target: darwin-x64

- os: macos-14
- os: macos-latest
target: aarch64-apple-darwin
code-target: darwin-arm64

Expand All @@ -75,14 +83,41 @@ jobs:
steps:
- uses: actions/checkout@v4

### install musl dependencies ###
#
- uses: goto-bus-stop/setup-zig@v2
if: ${{ contains(matrix.target, 'musl') }}
with:
version: 0.11.0

- name: Install cargo-zigbuild
if: ${{ contains(matrix.target, 'musl') }}
uses: taiki-e/install-action@v2
with:
tool: cargo-zigbuild

### install non-musl dependencies ###

- name: Install cross
if: ${{ !contains(matrix.target, 'musl') }}
uses: taiki-e/install-action@cross

### Build

- name: Add Rust Target
run: rustup target add ${{ matrix.target }}

- name: Build with cross
run: cross build -p oxc_napi_parser --release --target=${{ matrix.target }}
if: ${{ !contains(matrix.target, 'musl') }}
run: cross build --release -p oxc_parser_napi --target=${{ matrix.target }}

- name: Build with zig
if: ${{ contains(matrix.target, 'musl') }}
env:
RUSTFLAGS: "-C target-feature=-crt-static"
run: cargo zigbuild --release -p oxc_parser_napi --target=${{ matrix.target }}

### Build Done

- name: Move file on ${{ matrix.os }}
shell: bash
Expand All @@ -94,7 +129,7 @@ jobs:
- name: Test
working-directory: napi/parser
if: ${{ contains(matrix.target, 'x86') }} # Need docker for aarch64
if: ${{ contains(matrix.target, 'x86') && !contains(matrix.target, 'musl') }} # Need docker for aarch64
run: |
ls
node test.mjs
Expand All @@ -115,7 +150,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: binaries
name: binaries-${{ matrix.code-target }}
path: |
*.zip
*.tar.gz
Expand All @@ -124,7 +159,6 @@ jobs:
name: Publish NAPI
runs-on: ubuntu-latest
permissions:
contents: write # for softprops/action-gh-release@v1
id-token: write # for `npm publish --provenance`
needs:
- build
Expand All @@ -140,7 +174,7 @@ jobs:
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
name: binaries
merge-multiple: true

- name: Unzip
uses: montudor/action-zip@v1
Expand Down
4 changes: 4 additions & 0 deletions napi/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export function parseSync(sourceText: string, options?: ParserOptions | undefine
/**
* Returns a binary AST in flexbuffers format.
* This is a POC API. Error handling is not done yet.
* # Panics
*
* * File extension is invalid
* * FlexbufferSerializer serialization error
*/
export function parseSyncBuffer(sourceText: string, options?: ParserOptions | undefined | null): Buffer
/**
Expand Down
45 changes: 44 additions & 1 deletion napi/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim();
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
Expand Down Expand Up @@ -237,6 +237,49 @@ switch (platform) {
loadError = e
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'parser.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./parser.linux-riscv64-musl.node')
} else {
nativeBinding = require('@oxc-parser/binding-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'parser.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./parser.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@oxc-parser/binding-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'parser.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./parser.linux-s390x-gnu.node')
} else {
nativeBinding = require('@oxc-parser/binding-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
Expand Down
4 changes: 3 additions & 1 deletion napi/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "node test.mjs"
},
"devDependencies": {
"@napi-rs/cli": "^2.15.2",
"@napi-rs/cli": "^2.18.0",
"flatbuffers": "^23.5.26"
},
"engines": {
Expand All @@ -22,6 +22,8 @@
"aarch64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
"aarch64-unknown-linux-gnu",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl",
"x86_64-apple-darwin",
"aarch64-apple-darwin"
]
Expand Down
12 changes: 8 additions & 4 deletions napi/parser/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions npm/oxc-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "oxc-parser",
"version": "0.2.0",
"version": "0.3.0-alpha.0",
"description": "Oxc Parser Node API",
"main": "index.js",
"files": [
"index.d.ts",
"index.js"
],
"keywords": ["Parser"],
"author": "Boshen and oxc contributors",
"license": "MIT",
"homepage": "https://oxc-project.github.io",
"bugs": "https://github.com/oxc-project/oxc/issues",
"repository": {
"type": "git",
"url": "https://github.com/oxc-project/oxc.git",
"directory": "npm/oxc-parser"
},
"funding": {
"url": "https://github.com/sponsors/Boshen"
}
},
"main": "index.js",
"files": [
"index.d.ts",
"index.js"
]
}
32 changes: 20 additions & 12 deletions npm/oxc-parser/scripts/generate-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ console.log('PACKAGES_ROOT', PACKAGES_ROOT);
console.log('BINARY_ROOT', BINARY_ROOT);
console.log('MANIFEST_PATH', MANIFEST_PATH);

const LIBC_MAPPING = {
"gnu": "glibc",
"musl": "musl",
}

const rootManifest = JSON.parse(
fs.readFileSync(MANIFEST_PATH).toString("utf-8")
);

function package_name(target) {
return `@oxc-parser/binding-${target}`
}

function generateNativePackage(target) {
const binaryName = `parser.${target}.node`;

Expand All @@ -38,24 +42,25 @@ function generateNativePackage(target) {
fs.mkdirSync(packageRoot);

// Generate the package.json manifest
const { version, license, repository } = rootManifest;
const { version, author, license, homepage, bugs, repository } = rootManifest;

const [os, cpu, third] = target.split("-");
const triple = target.split("-");
const platform = triple[0];
const arch = triple[1];
const libc = triple[2] && { libc: [LIBC_MAPPING[triple[2]]] }
const manifest = {
name: package_name(target),
version,
main: binaryName,
files: [binaryName],
os: [os],
cpu: [cpu],
author,
license,
repository
homepage,
bugs,
repository,
os: [platform],
cpu: [arch],
...libc
};

if (cpu == "linux" && third == "gnu") {
manifest.libc = ["glibc"];
}

const manifestPath = resolve(packageRoot, "package.json");
console.log(`Create manifest ${manifestPath}`);
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
Expand Down Expand Up @@ -94,11 +99,14 @@ function writeManifest() {
}
}

// NOTE: Must update npm/oxc-parser/package.json
const TARGETS = [
"win32-x64-msvc",
"win32-arm64-msvc",
"linux-x64-gnu",
"linux-arm64-gnu",
"linux-x64-musl",
"linux-arm64-musl",
"darwin-x64",
"darwin-arm64",
];
Expand Down
6 changes: 3 additions & 3 deletions npm/oxlint/scripts/generate-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function generateNativePackage(target) {
const platform = triple[0];
const arch = triple[1];
const libc = triple[2] && { libc: [LIBC_MAPPING[triple[2]]] }
const manifest = JSON.stringify({
const manifest = {
name: packageName,
version,
author,
Expand All @@ -49,11 +49,11 @@ function generateNativePackage(target) {
os: [platform],
cpu: [arch],
...libc
});
};

const manifestPath = resolve(packageRoot, "package.json");
console.log(`Create manifest ${manifestPath}`);
fs.writeFileSync(manifestPath, manifest);
fs.writeFileSync(manifestPath, JSON.stringify(manifest));

// Copy the binary
const ext = platform === "win32" ? ".exe" : "";
Expand Down

0 comments on commit 839e7c5

Please sign in to comment.