Skip to content

Commit 5678012

Browse files
committed
a working rust compiler
1 parent 5ec0687 commit 5678012

66 files changed

Lines changed: 4292 additions & 3151 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
name: Build and Release Binaries
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
settings:
15+
- host: macos-latest
16+
target: x86_64-apple-darwin
17+
build: yarn build:rs --target x86_64-apple-darwin
18+
- host: macos-latest
19+
target: aarch64-apple-darwin
20+
build: yarn build:rs --target aarch64-apple-darwin
21+
- host: windows-latest
22+
target: x86_64-pc-windows-msvc
23+
build: yarn build:rs --target x86_64-pc-windows-msvc
24+
- host: windows-latest
25+
target: i686-pc-windows-msvc
26+
build: yarn build:rs --target i686-pc-windows-msvc
27+
- host: ubuntu-latest
28+
target: x86_64-unknown-linux-gnu
29+
build: yarn build:rs --target x86_64-unknown-linux-gnu
30+
- host: ubuntu-latest
31+
target: x86_64-unknown-linux-musl
32+
build: |
33+
sudo apt-get update
34+
sudo apt-get install musl-tools
35+
yarn build:rs --target x86_64-unknown-linux-musl
36+
- host: ubuntu-latest
37+
target: aarch64-unknown-linux-gnu
38+
build: |
39+
sudo apt-get update
40+
sudo apt-get install gcc-aarch64-linux-gnu
41+
yarn build:rs --target aarch64-unknown-linux-gnu
42+
- host: ubuntu-latest
43+
target: i686-unknown-linux-gnu
44+
build: |
45+
sudo apt-get update
46+
sudo apt-get install gcc-multilib
47+
yarn build:rs --target i686-unknown-linux-gnu
48+
- host: ubuntu-latest
49+
target: armv7-unknown-linux-gnueabihf
50+
build: |
51+
sudo apt-get update
52+
sudo apt-get install gcc-arm-linux-gnueabihf
53+
yarn build:rs --target armv7-unknown-linux-gnueabihf
54+
- host: ubuntu-latest
55+
target: x86_64-unknown-freebsd
56+
build: yarn build:rs --target x86_64-unknown-freebsd
57+
58+
name: Build ${{ matrix.settings.target }}
59+
runs-on: ${{ matrix.settings.host }}
60+
61+
steps:
62+
- uses: actions/checkout@v4
63+
64+
- name: Setup Node.js
65+
uses: actions/setup-node@v4
66+
with:
67+
node-version: '18'
68+
cache: 'yarn'
69+
70+
- name: Setup Rust
71+
uses: dtolnay/rust-toolchain@stable
72+
with:
73+
targets: ${{ matrix.settings.target }}
74+
75+
- name: Cache cargo
76+
uses: actions/cache@v4
77+
with:
78+
path: |
79+
~/.cargo/registry/index/
80+
~/.cargo/registry/cache/
81+
~/.cargo/git/db/
82+
.cargo-cache
83+
target/
84+
key: ${{ matrix.settings.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
85+
86+
- name: Install dependencies
87+
run: yarn install --frozen-lockfile
88+
89+
- name: Build binary
90+
run: ${{ matrix.settings.build }}
91+
92+
- name: Upload binary
93+
uses: actions/upload-artifact@v4
94+
with:
95+
name: binaries-${{ matrix.settings.target }}
96+
path: |
97+
*.node
98+
cjs/*.node
99+
esm/*.node
100+
101+
release:
102+
name: Create Release
103+
runs-on: ubuntu-latest
104+
needs: build
105+
if: startsWith(github.ref, 'refs/tags/')
106+
107+
steps:
108+
- uses: actions/checkout@v4
109+
110+
- name: Download all artifacts
111+
uses: actions/download-artifact@v4
112+
with:
113+
path: artifacts
114+
115+
- name: Create Release
116+
id: create_release
117+
uses: actions/create-release@v1
118+
env:
119+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
120+
with:
121+
tag_name: ${{ github.ref }}
122+
release_name: Release ${{ github.ref }}
123+
draft: false
124+
prerelease: false
125+
126+
- name: Upload Release Assets
127+
run: |
128+
for dir in artifacts/binaries-*; do
129+
target=$(basename "$dir" | sed 's/binaries-//')
130+
for file in "$dir"/*.node; do
131+
if [ -f "$file" ]; then
132+
asset_name="idea-parser-${target}.node"
133+
echo "Uploading $file as $asset_name"
134+
curl \
135+
-X POST \
136+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
137+
-H "Content-Type: application/octet-stream" \
138+
--data-binary @"$file" \
139+
"https://uploads.github.com/repos/${{ github.repository }}/releases/${{ steps.create_release.outputs.id }}/assets?name=$asset_name"
140+
fi
141+
done
142+
done

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,9 @@ packages/idea-language/server/package-lock.json
149149
packages/idea-transformer/tests/out
150150
.DS_Store
151151

152-
.clinerules
152+
.clinerules
153+
old/
154+
155+
#rust
156+
target/
157+
**/*.rs.bk

docs/parser/Binaries.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# Binary Distribution Strategy
2+
3+
This document explains how the Rust binaries are built and distributed for the `@stackpress/idea-parser` npm package.
4+
5+
## Overview
6+
7+
The package uses a hybrid approach for binary distribution:
8+
9+
1. **GitHub Actions** builds pre-compiled binaries for multiple platforms
10+
2. **Postinstall script** downloads the appropriate binary for the user's platform
11+
3. **Fallback to source compilation** if no pre-built binary is available
12+
13+
## Supported Platforms
14+
15+
The following platforms are supported with pre-built binaries:
16+
17+
- **macOS**:
18+
- x86_64 (Intel)
19+
- aarch64 (Apple Silicon)
20+
- **Windows**:
21+
- x86_64 (64-bit)
22+
- i686 (32-bit)
23+
- **Linux**:
24+
- x86_64 (GNU)
25+
- x86_64 (musl)
26+
- aarch64 (ARM64)
27+
- i686 (32-bit)
28+
- armv7 (ARM)
29+
- **FreeBSD**:
30+
- x86_64
31+
32+
## How It Works
33+
34+
### 1. GitHub Actions Workflow
35+
36+
The `.github/workflows/build-binaries.yml` workflow:
37+
38+
- Triggers on version tags (`v*`) or manual dispatch
39+
- Builds binaries for all supported platforms using a matrix strategy
40+
- Uploads binaries as artifacts
41+
- Creates a GitHub Release with all binaries attached
42+
43+
### 2. Postinstall Script
44+
45+
The `scripts/install.cjs` script:
46+
47+
- Detects the user's platform and architecture
48+
- Maps it to the corresponding Rust target triple
49+
- Attempts to download the pre-built binary from GitHub Releases
50+
- Falls back to building from source if download fails
51+
- Places the binary in the correct locations for both CJS and ESM builds
52+
53+
### 3. Platform Detection
54+
55+
The script maps Node.js platform/architecture to Rust target triples:
56+
57+
```javascript
58+
const platformMap = {
59+
'darwin': {
60+
'x64': 'x86_64-apple-darwin',
61+
'arm64': 'aarch64-apple-darwin'
62+
},
63+
'win32': {
64+
'x64': 'x86_64-pc-windows-msvc',
65+
'ia32': 'i686-pc-windows-msvc'
66+
},
67+
'linux': {
68+
'x64': 'x86_64-unknown-linux-gnu',
69+
'arm64': 'aarch64-unknown-linux-gnu',
70+
'ia32': 'i686-unknown-linux-gnu',
71+
'arm': 'armv7-unknown-linux-gnueabihf'
72+
},
73+
'freebsd': {
74+
'x64': 'x86_64-unknown-freebsd'
75+
}
76+
};
77+
```
78+
79+
## Release Process
80+
81+
To create a new release with binaries:
82+
83+
1. Update the version in `package.json`
84+
2. Create and push a git tag:
85+
```bash
86+
git tag v0.6.3
87+
git push origin v0.6.3
88+
```
89+
3. The GitHub Actions workflow will automatically:
90+
- Build binaries for all platforms
91+
- Create a GitHub Release
92+
- Upload all binaries to the release
93+
94+
## Binary Naming Convention
95+
96+
Binaries are named using the pattern:
97+
```
98+
idea-parser-{target-triple}.node
99+
```
100+
101+
Examples:
102+
- `idea-parser-aarch64-apple-darwin.node`
103+
- `idea-parser-x86_64-pc-windows-msvc.node`
104+
- `idea-parser-x86_64-unknown-linux-gnu.node`
105+
106+
## Fallback Behavior
107+
108+
If a pre-built binary is not available for the user's platform:
109+
110+
1. The script will attempt to build from source using `yarn build:rs`
111+
2. This requires the user to have:
112+
- Rust toolchain installed
113+
- Appropriate build tools for their platform
114+
3. The build process uses the existing `napi` configuration
115+
116+
## Testing
117+
118+
To test the installation process:
119+
120+
```bash
121+
# Test the postinstall script directly
122+
node scripts/install.cjs
123+
124+
# Test with npm install (in a clean environment)
125+
npm install
126+
```
127+
128+
## Troubleshooting
129+
130+
### Binary Download Fails
131+
132+
If the binary download fails, check:
133+
1. Internet connectivity
134+
2. GitHub Releases page for the version
135+
3. Binary exists for your platform
136+
137+
### Build from Source Fails
138+
139+
If building from source fails, ensure:
140+
1. Rust is installed (`rustup` recommended)
141+
2. Build tools are available:
142+
- **Windows**: Visual Studio Build Tools
143+
- **macOS**: Xcode Command Line Tools
144+
- **Linux**: `build-essential` package
145+
146+
### Platform Not Supported
147+
148+
If your platform is not supported:
149+
1. Check if it's in the platform map in `scripts/install.cjs`
150+
2. Add support by updating the GitHub Actions workflow
151+
3. Update the platform map in the install script
152+
153+
## Benefits
154+
155+
This approach provides:
156+
157+
- **Fast installation** for supported platforms (no compilation needed)
158+
- **Broad compatibility** with fallback to source compilation
159+
- **Automatic updates** when new versions are released
160+
- **Reduced package size** (binaries hosted separately)
161+
- **CI/CD integration** for consistent builds across platforms

package.json

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
"example"
66
],
77
"scripts": {
8-
"build": "yarn build:parser && yarn build:transformer && yarn build:idea && yarn build:example",
9-
"build:parser": "yarn --cwd packages/idea-parser build",
10-
"build:language": "yarn --cwd packages/idea-language build",
11-
"build:transformer": "yarn --cwd packages/idea-transformer build",
12-
"build:idea": "yarn --cwd packages/idea build",
13-
"build:example": "yarn --cwd example build",
14-
"report": "yarn report:env nyc yarn test && nyc report -r lcov",
15-
"report:env": "NODE_OPTIONS=\"--disable-warning=ExperimentalWarning --experimental-loader @istanbuljs/esm-loader-hook\"",
8+
"idea": "yarn --cwd packages/idea",
9+
"parser": "yarn --cwd packages/idea-parser",
10+
"transformer": "yarn --cwd packages/idea-transformer",
11+
"example": "yarn --cwd example",
12+
"build": "yarn parser build && yarn transformer build && yarn idea build && yarn example build",
1613
"transform": "yarn --cwd example transform",
17-
"test": "yarn test:parser && yarn test:transformer",
18-
"test:parser": "yarn --cwd packages/idea-parser test",
19-
"test:transformer": "yarn --cwd packages/idea-transformer test"
14+
"test": "yarn parser test && yarn transformer test",
15+
"report": "yarn report:env nyc yarn test && nyc report -r lcov",
16+
"report:env": "NODE_OPTIONS=\"--disable-warning=ExperimentalWarning --experimental-loader @istanbuljs/esm-loader-hook\""
2017
},
2118
"devDependencies": {
2219
"@istanbuljs/esm-loader-hook": "0.3.0",

0 commit comments

Comments
 (0)