Skip to content

Commit 5cb29db

Browse files
committed
add cross compilation
1 parent bee3fbc commit 5cb29db

File tree

5 files changed

+65
-33
lines changed

5 files changed

+65
-33
lines changed

.github/workflows/release.yaml

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,30 @@ jobs:
2424
- name: Install pnpm
2525
uses: pnpm/action-setup@v4
2626

27-
# linux
28-
- name: Install Rust
29-
if: ${{ matrix.os == 'ubuntu-latest' }}
30-
uses: dtolnay/rust-toolchain@master
31-
with:
32-
toolchain: stable
33-
targets: ${{ matrix.arch == 'arm64' && 'aarch64-unknown-linux-gnu' || 'x86_64-unknown-linux-gnu' }}
27+
- name: Install Linux Dependencies
28+
if: runner.os == 'Linux'
29+
run: |
30+
sudo apt-get update
31+
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
3432
35-
# macos
36-
- name: Install Rust
37-
if: ${{ matrix.os == 'macos-latest' }}
38-
uses: dtolnay/rust-toolchain@master
39-
with:
40-
toolchain: stable
41-
targets: ${{ matrix.arch == 'arm64' && 'aarch64-apple-darwin' || 'x86_64-apple-darwin' }}
33+
- name: Get Target Name
34+
id: get_target
35+
env:
36+
npm_config_arch: ${{ matrix.arch }}
37+
shell: bash
38+
run: |
39+
TARGET_NAME=$(node ./utils/getTarget.js)
40+
echo "target_name=$TARGET_NAME" >> $GITHUB_OUTPUT
4241
43-
# windows
4442
- name: Install Rust
45-
if: ${{ matrix.os == 'windows-latest' }}
4643
uses: dtolnay/rust-toolchain@master
4744
with:
4845
toolchain: stable
49-
targets: ${{ matrix.arch == 'arm64' && 'aarch64-pc-windows-msvc' || 'x86_64-pc-windows-msvc' }}
46+
targets: ${{ steps.get_target.outputs.target_name }}
5047

5148
- name: Building
49+
env:
50+
npm_config_arch: ${{ matrix.arch }}
5251
run: pnpm install
5352

5453
- name: Uploading

build.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ const { promises: fs, constants } = require('fs');
44
const { promisify } = require('util');
55
const execFile = promisify(require('child_process').execFile);
66

7-
const { spawn, paths, exists } = require('./shared');
7+
const { getRustTarget } = require('./utils/getTarget');
8+
const { spawn, buildPaths, exists } = require('./shared');
89

910
(async () => {
10-
if (await exists(paths.exeFinal)) {
11+
if (await exists(buildPaths.exeFinal)) {
1112
return;
1213
}
1314

14-
if (!(await exists(paths.bin))) {
15-
await fs.mkdir(paths.bin, { recursive: true });
15+
if (!(await exists(buildPaths.bin))) {
16+
await fs.mkdir(buildPaths.bin, { recursive: true });
1617
}
1718

1819
await spawn('cargo', [
1920
'build',
20-
`--manifest-path=${path.join(paths.submodule, 'Cargo.toml')}`,
21-
`--target-dir=${paths.build}`,
21+
`--target=${getRustTarget()}`,
22+
`--manifest-path=${path.join(buildPaths.submodule, 'Cargo.toml')}`,
23+
`--target-dir=${buildPaths.build}`,
2224
'--release'
2325
]);
2426
await fs.copyFile(
25-
paths.exeOut,
26-
paths.exeFinal
27+
buildPaths.exeOut,
28+
buildPaths.exeFinal
2729
);
2830
})();

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const { exists, paths, spawn } = require('./shared')
1+
const { exists, runtimePaths, spawn } = require('./shared')
22
const { promisify } = require('util');
33

44
const dumpSymbolSync = function (binaryPath, callback) {
5-
exists(paths.exeFinal)
5+
exists(runtimePaths.exeFinal)
66
.then(binExists => {
77
if (!binExists) {
88
throw new Error('Unable to find "dump_syms"');
99
}
1010

11-
return spawn(paths.exeFinal, [binaryPath]);
11+
return spawn(runtimePaths.exeFinal, [binaryPath]);
1212
})
1313
.then(b => callback(null, b))
1414
.catch(e => callback(e, null))

shared.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const path = require('path');
22
const { promises: fs } = require('fs');
33
const { spawn: spawnSync } = require('child_process');
44

5+
const { getTargetArch } = require('./utils/getTarget');
6+
57
const spawn = (...args) => new Promise((resolve, reject) => {
68
let stdout = Buffer.alloc(0);
79
let stderr = Buffer.alloc(0);
@@ -27,12 +29,13 @@ const exists = async (filePath) => fs.access(filePath).then(() => true).catch(e
2729
module.exports.exists = exists;
2830

2931
const exe = process.platform === 'win32' ? '.exe' : '';
30-
const paths = {
32+
const getPaths = (arch) => ({
3133
submodule: path.join(__dirname, 'deps', 'dump_syms'),
32-
bin: path.join(__dirname, 'bin', `${process.platform}-${process.arch}`),
34+
bin: path.join(__dirname, 'bin', `${process.platform}-${arch}`),
3335
build: path.join(__dirname, 'build'),
3436
exeOut: path.join(__dirname, 'build', 'release', `dump_syms${exe}`),
35-
exeFinal: path.join(__dirname, 'bin', `${process.platform}-${process.arch}`, `dump_syms${exe}`),
36-
};
37+
exeFinal: path.join(__dirname, 'bin', `${process.platform}-${arch}`, `dump_syms${exe}`),
38+
});
3739

38-
module.exports.paths = paths;
40+
module.exports.runtimePaths = getPaths(process.arch);
41+
module.exports.buildPaths = getPaths(getTargetArch());

utils/getTarget.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const getTargetArch = () => {
2+
return process.env.npm_config_arch || process.arch;
3+
}
4+
5+
module.exports.getTargetArch = getTargetArch;
6+
7+
const getRustTarget = (platform = process.platform, arch = getTargetArch()) => {
8+
let target = '';
9+
10+
if (platform === 'linux') {
11+
target += arch === 'arm64' ? 'aarch64-unknown-linux-gnu' : 'x86_64-unknown-linux-gnu';
12+
} else if (platform === 'darwin') {
13+
target += arch === 'arm64' ? 'aarch64-apple-darwin' : 'x86_64-apple-darwin';
14+
} else if (platform === 'win32') {
15+
target += arch === 'arm64' ? 'aarch64-pc-windows-msvc' : 'x86_64-pc-windows-msvc';
16+
} else {
17+
throw new Error(`Unsupported platform: ${platform}`);
18+
}
19+
20+
return target;
21+
};
22+
23+
module.exports.getRustTarget = getRustTarget;
24+
25+
if (require.main === module) {
26+
const target = getRustTarget();
27+
console.log(target);
28+
}

0 commit comments

Comments
 (0)