feat(cross-compile): add cross-compilation support with --target flag, friendly target aliases, and automatic rustup target installation

This commit is contained in:
2026-02-09 18:29:48 +00:00
parent 7a80cc8a7a
commit 4a391d9ddc
5 changed files with 186 additions and 29 deletions

View File

@@ -27,16 +27,21 @@ export class CargoRunner {
return result.stdout.trim();
}
public async build(options: { debug?: boolean; clean?: boolean } = {}): Promise<ICargoRunResult> {
public async build(options: { debug?: boolean; clean?: boolean; target?: string } = {}): Promise<ICargoRunResult> {
if (options.clean) {
console.log('Cleaning previous build...');
await this.clean();
}
const profile = options.debug ? '' : ' --release';
const command = `cd ${this.rustDir} && cargo build${profile}`;
if (options.target) {
await this.ensureTarget(options.target);
}
console.log(`Running: cargo build${profile}`);
const profile = options.debug ? '' : ' --release';
const targetFlag = options.target ? ` --target ${options.target}` : '';
const command = `cd ${this.rustDir} && cargo build${profile}${targetFlag}`;
console.log(`Running: cargo build${profile}${targetFlag}`);
const result = await this.shell.exec(command);
return {
@@ -46,6 +51,22 @@ export class CargoRunner {
};
}
/**
* Ensures a rustup target is installed. If not present, installs it via `rustup target add`.
*/
public async ensureTarget(triple: string): Promise<void> {
const listResult = await this.shell.execSilent('rustup target list --installed');
const installedTargets = listResult.stdout.split('\n').map((l) => l.trim());
if (installedTargets.includes(triple)) {
return;
}
console.log(`Installing rustup target: ${triple}`);
const addResult = await this.shell.exec(`rustup target add ${triple}`);
if (addResult.exitCode !== 0) {
throw new Error(`Failed to install rustup target ${triple}`);
}
}
public async clean(): Promise<ICargoRunResult> {
const command = `cd ${this.rustDir} && cargo clean`;
const result = await this.shell.exec(command);