-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmain.rs
More file actions
442 lines (369 loc) · 11.6 KB
/
main.rs
File metadata and controls
442 lines (369 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
mod card;
use clap::{Args, CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use eyre::{Context, Result};
mod completions;
use completions::print_completions;
mod develop;
use develop::{devshell, testshell};
mod build;
use build::{run_build, run_build_and_copy};
mod hf;
mod init;
use init::{run_init, InitArgs};
mod list_variants;
use list_variants::list_variants;
mod upload;
use upload::{run_upload, RepoTypeArg, UploadArgs};
mod pyproject;
use pyproject::{clean_pyproject, create_pyproject};
use kernels_data::config::{v3, Build, BuildCompat};
mod nix;
mod skills;
mod util;
use util::{check_or_infer_kernel_dir, parse_and_validate};
mod validate_builds;
use validate_builds::check_builds;
#[derive(Args, Debug)]
struct NixArgs {
/// Maximum number of parallel Nix build jobs.
#[arg(long)]
pub max_jobs: Option<u32>,
/// Number of CPU cores to use for each build job.
#[arg(long)]
pub cores: Option<u32>,
/// Print full build logs on standard error.
#[arg(short = 'L', long)]
pub print_build_logs: bool,
}
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Generate shell completions.
Completions { shell: Shell },
/// Initialize a new kernel project from template.
Init(InitArgs),
/// Build the kernel locally (alias for build-and-copy).
Build {
/// Directory of the kernel project (defaults to current directory).
#[arg(value_name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// Build a specific variant.
#[arg(long)]
variant: Option<String>,
#[command(flatten)]
nix_args: NixArgs,
},
/// Build the kernel and copy artifacts locally.
BuildAndCopy {
/// Directory of the kernel project (defaults to current directory).
#[arg(value_name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
#[command(flatten)]
nix_args: NixArgs,
},
/// Build the kernel and upload to Hugging Face Hub.
BuildAndUpload {
/// Directory of the kernel project (defaults to current directory).
#[arg(value_name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// Build a specific variant.
#[arg(long)]
variant: Option<String>,
#[command(flatten)]
nix_args: NixArgs,
/// Repository ID on the Hugging Face Hub (e.g. `user/my-kernel`).
#[arg(long)]
repo_id: Option<String>,
/// Upload to a specific branch (defaults to `v{version}` from metadata).
#[arg(long)]
branch: Option<String>,
/// Create the repository as private.
#[arg(long)]
private: bool,
/// Repository type on Hugging Face Hub (`kernel` by default, or `model` for legacy repos).
#[arg(long, value_enum, default_value_t = RepoTypeArg::Kernel)]
repo_type: RepoTypeArg,
/// Suppress progress output.
#[arg(long, short)]
quiet: bool,
},
/// Upload kernel build artifacts to the Hugging Face Hub.
Upload(UploadArgs),
/// Validate the build.toml file.
CheckConfig {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
},
/// Validate kernel builds.
CheckBuilds {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
},
/// Generate CMake files for a kernel extension build.
CreatePyproject {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// The directory to write the generated files to
/// (directory of `BUILD_TOML` when absent).
#[arg(name = "TARGET_DIR")]
target_dir: Option<PathBuf>,
/// Force-overwrite existing files.
#[arg(short, long)]
force: bool,
/// This is an optional unique identifier that is suffixed to the
/// kernel name to avoid name collisions. (e.g. Git SHA)
#[arg(long)]
unique_id: Option<String>,
},
/// Spawn a kernel development shell.
Devshell {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// Use a specific variant.
#[arg(long)]
variant: Option<String>,
#[command(flatten)]
nix_args: NixArgs,
},
/// Render the CARD.md template for a kernel.
#[command(hide = true)]
FillCard {
/// Kernel source directory (current directory when not specified).
#[arg(value_name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// File to write the rendered card to (defaults to stdout).
#[arg(value_name = "OUTPUT")]
output: Option<PathBuf>,
},
/// List build variants.
ListVariants {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// Only list variants for the current architecture.
#[arg(long)]
arch: bool,
},
/// Spawn a kernel test shell.
Testshell {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// Use a specific variant.
#[arg(long)]
variant: Option<String>,
#[command(flatten)]
nix_args: NixArgs,
},
/// Update a `build.toml` to the current format.
UpdateBuild {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
},
/// Install skills for AI coding assistants (Claude, Codex, OpenCode).
Skills {
#[command(subcommand)]
command: SkillsCommands,
},
/// Generate Markdown documentation for the CLI.
#[command(hide = true)]
GenerateDocs,
/// Clean generated artifacts.
CleanPyproject {
#[arg(name = "KERNEL_DIR")]
kernel_dir: Option<PathBuf>,
/// The directory to clean from (directory of `BUILD_TOML` when absent).
#[arg(name = "TARGET_DIR")]
target_dir: Option<PathBuf>,
/// Show what would be deleted without actually deleting.
#[arg(short, long)]
dry_run: bool,
/// Force deletion without confirmation.
#[arg(short, long)]
force: bool,
/// This is an optional unique identifier that is suffixed to the
/// kernel name to avoid name collisions. (e.g. Git SHA)
#[arg(long)]
ops_id: Option<String>,
},
}
#[derive(Debug, Subcommand)]
enum SkillsCommands {
/// Install a kernels skill for an AI assistant.
Add {
/// Skill to install.
#[arg(long, value_enum, default_value_t = skills::SkillId::CudaKernels)]
skill: skills::SkillId,
/// Install for Claude.
#[arg(long)]
claude: bool,
/// Install for Codex.
#[arg(long)]
codex: bool,
/// Install for OpenCode.
#[arg(long)]
opencode: bool,
/// Install globally (user-level) instead of in the current project directory.
#[arg(short, long)]
global: bool,
/// Install into a custom destination (path to skills directory).
#[arg(long)]
dest: Option<PathBuf>,
/// Overwrite existing skills in the destination.
#[arg(long)]
force: bool,
},
}
fn main() -> Result<()> {
let args = Cli::parse();
match args.command {
Commands::Completions { shell } => {
print_completions(&mut Cli::command(), shell);
Ok(())
}
Commands::Init(args) => run_init(args),
Commands::Upload(args) => run_upload(args),
Commands::Build {
kernel_dir,
variant,
nix_args,
} => run_build(
kernel_dir,
nix_args.max_jobs,
nix_args.cores,
nix_args.print_build_logs,
variant,
),
Commands::BuildAndCopy {
kernel_dir,
nix_args,
..
} => run_build_and_copy(
kernel_dir,
nix_args.max_jobs,
nix_args.cores,
nix_args.print_build_logs,
),
Commands::BuildAndUpload {
kernel_dir,
variant,
nix_args,
repo_id,
branch,
private,
repo_type,
quiet,
} => {
run_build(
kernel_dir.clone(),
nix_args.max_jobs,
nix_args.cores,
nix_args.print_build_logs,
variant,
)?;
run_upload(UploadArgs {
kernel_dir,
repo_id,
branch,
private,
repo_type,
quiet,
})
}
Commands::CreatePyproject {
kernel_dir,
force,
target_dir,
unique_id,
} => create_pyproject(kernel_dir, target_dir, force, unique_id),
Commands::Devshell {
kernel_dir,
variant,
nix_args,
} => devshell(
kernel_dir,
nix_args.max_jobs,
nix_args.cores,
nix_args.print_build_logs,
variant,
),
Commands::ListVariants { kernel_dir, arch } => list_variants(kernel_dir, arch),
Commands::Testshell {
kernel_dir,
variant,
nix_args,
} => testshell(
kernel_dir,
nix_args.max_jobs,
nix_args.cores,
nix_args.print_build_logs,
variant,
),
Commands::UpdateBuild { kernel_dir } => update_build(kernel_dir),
Commands::CheckConfig { kernel_dir } => {
check_config(kernel_dir)?;
Ok(())
}
Commands::CheckBuilds { kernel_dir } => {
check_builds(kernel_dir)?;
Ok(())
}
Commands::GenerateDocs => {
let markdown = clap_markdown::help_markdown::<Cli>();
print!("{}", markdown);
Ok(())
}
Commands::Skills { command } => match command {
SkillsCommands::Add {
skill,
claude,
codex,
opencode,
global,
dest,
force,
} => skills::add_skill(skill, claude, codex, opencode, global, dest, force),
},
Commands::FillCard { kernel_dir, output } => card::fill_card(kernel_dir, output),
Commands::CleanPyproject {
kernel_dir,
target_dir,
dry_run,
force,
ops_id,
} => clean_pyproject(kernel_dir, target_dir, dry_run, force, ops_id),
}
}
fn check_config(kernel_dir: Option<PathBuf>) -> Result<()> {
let kernel_dir = check_or_infer_kernel_dir(kernel_dir)?;
parse_and_validate(kernel_dir)?;
Ok(())
}
fn update_build(kernel_dir: Option<PathBuf>) -> Result<()> {
let kernel_dir = check_or_infer_kernel_dir(kernel_dir)?;
let build_compat: BuildCompat = parse_and_validate(&kernel_dir)?;
if matches!(build_compat, BuildCompat::V3(_)) {
return Ok(());
}
let build: Build = build_compat
.try_into()
.context("Cannot update build configuration")?;
let v3_build: v3::Build = build.into();
let pretty_toml = toml::to_string_pretty(&v3_build)?;
let build_toml = kernel_dir.join("build.toml");
let mut writer =
BufWriter::new(File::create(&build_toml).wrap_err_with(|| {
format!("Cannot open {} for writing", build_toml.to_string_lossy())
})?);
writer
.write_all(pretty_toml.as_bytes())
.wrap_err_with(|| format!("Cannot write to {}", build_toml.to_string_lossy()))?;
Ok(())
}