Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow additional arguments to be passed to cargo build #633

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ function logIf(multiple: boolean, action: string, cwd: string, module: string) {
}
}

function parseArgv(argv: string[]): { cli: string[], extra: string[] } {
let splitAt = argv.indexOf('--');

// No additional arguments provided
if (splitAt < 0) {
return {
cli: argv,
extra: [],
};
}

return {
cli: argv.slice(0, splitAt),
extra: argv.slice(splitAt + 1)
};
}

function parseModules(cwd: string, names: string[], paths: boolean) {
let modules = names.length
? names.map(m => paths ? path.resolve(cwd, m)
Expand Down Expand Up @@ -61,7 +78,7 @@ const spec: Spec = {
content: "Neon is a tool for building native Node.js modules with Rust."
}, {
header: "Synopsis",
content: "$ neon [options] <command>"
content: "$ neon [options] <command> -- [cargo options]"
}, {
header: "Command List",
content: [{ name: "new", summary: "Create a new Neon project." },
Expand Down Expand Up @@ -180,14 +197,15 @@ const spec: Spec = {
return;
}

let extra = options.extra as string[];
let { modules, multiple } = parseModules(this.cwd,
(options.modules || []) as string[],
!!options.path);

for (let module of modules) {
logIf(multiple, "building", this.cwd, module);

await neon_build(module, this.toolchain, !!options.release);
await neon_build(module, this.toolchain, !!options.release, extra);
}
}
},
Expand Down Expand Up @@ -293,8 +311,10 @@ export default class CLI {

try {
let { command, argv } = parsed;
let { cli, extra } = parseArgv(argv);
let options = { extra, ...cliArgs(spec[command].args, { argv: cli }) };
await spec[command].action.call(this,
cliArgs(spec[command].args, { argv }),
options,
cliUsage(spec[command].usage));
} catch (e) {
console.error(style.error(e.message));
Expand Down
5 changes: 3 additions & 2 deletions cli/src/ops/neon_build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as rust from '../rust';

export default async function neon_build(root: string,
toolchain: rust.Toolchain = 'default',
release: boolean) {
release: boolean,
args: string[]) {
let project = await Project.create(root);
await project.build(toolchain, release);
await project.build(toolchain, release, args);
}
4 changes: 2 additions & 2 deletions cli/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class Project {
});
}

async build(toolchain: rust.Toolchain, release: boolean) {
async build(toolchain: rust.Toolchain, release: boolean, args: string[]) {
let target = new Target(this.crate, { release: release });
let settings = BuildSettings.current(toolchain);

Expand All @@ -50,7 +50,7 @@ export default class Project {

// 2. Build the dylib.
log("running cargo");
await target.build(toolchain, settings);
await target.build(toolchain, settings, args);

// 3. Copy the dylib as the main addon file.
log("generating " + path.join(this.crate.subdirectory, this.crate.nodefile));
Expand Down
5 changes: 3 additions & 2 deletions cli/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ export default class Target {
}

async build(toolchain: rust.Toolchain,
settings: BuildSettings)
settings: BuildSettings,
additionalArgs: string[])
{
let releaseFlags = this.release ? ["--release"] : [];
let targetFlags = this.triple ? ["--target=" + this.triple] : [];

let args = ['build'].concat(releaseFlags, targetFlags);
let args = ['build'].concat(releaseFlags, targetFlags, additionalArgs);

try {
let result = await rust.spawn("cargo", args, toolchain, {
Expand Down