-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.rs
More file actions
173 lines (142 loc) · 5.02 KB
/
Copy pathcommands.rs
File metadata and controls
173 lines (142 loc) · 5.02 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
use crate::config::{ConfigGenerator, ConfigReader};
use crate::error::Result;
use crate::kconfig::{Parser, SymbolTable};
use clap::{Parser as ClapParser, Subcommand};
use std::path::PathBuf;
#[derive(ClapParser, Debug)]
#[command(name = "xconf")]
#[command(about = "Rust Kconfig tool - Kbuild configuration system", long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Parse a Kconfig file and display the AST
Parse {
/// Path to Kconfig file
#[arg(short, long, default_value = "Kconfig")]
kconfig: PathBuf,
/// Source tree path
#[arg(short, long, default_value = ".")]
srctree: PathBuf,
},
/// Apply a defconfig
Defconfig {
/// Path to defconfig file
defconfig: PathBuf,
/// Path to Kconfig file
#[arg(short, long, default_value = "Kconfig")]
kconfig: PathBuf,
/// Source tree path
#[arg(short, long, default_value = ".")]
srctree: PathBuf,
},
/// Interactive menu configuration (TUI)
Menuconfig {
/// Path to Kconfig file
#[arg(short, long, default_value = "Kconfig")]
kconfig: PathBuf,
/// Source tree path
#[arg(short, long, default_value = ".")]
srctree: PathBuf,
},
/// Generate configuration files
Generate {
/// Path to .config file
#[arg(short, long, default_value = ".config")]
config: PathBuf,
/// Path to Kconfig file
#[arg(short, long, default_value = "Kconfig")]
kconfig: PathBuf,
/// Source tree path
#[arg(short, long, default_value = ".")]
srctree: PathBuf,
},
/// Load an existing .config and detect changes (oldconfig)
Oldconfig {
/// Path to existing .config file
#[arg(short, long, default_value = ".config")]
config: PathBuf,
/// Path to Kconfig file
#[arg(short, long, default_value = "Kconfig")]
kconfig: PathBuf,
/// Source tree path
#[arg(short, long, default_value = ".")]
srctree: PathBuf,
/// Automatically apply defaults to new symbols
#[arg(long)]
auto_defaults: bool,
},
/// Save current configuration
Saveconfig {
/// Output path for .config
#[arg(short, long, default_value = ".config")]
output: PathBuf,
/// Path to Kconfig file (to get current symbols)
#[arg(short, long, default_value = "Kconfig")]
kconfig: PathBuf,
/// Source tree path
#[arg(short, long, default_value = ".")]
srctree: PathBuf,
},
}
pub fn parse_command(kconfig: PathBuf, srctree: PathBuf) -> Result<()> {
println!("Parsing Kconfig file: {}", kconfig.display());
println!("Source tree: {}", srctree.display());
let mut parser = Parser::new(&kconfig, &srctree)?;
let ast = parser.parse()?;
println!("\nParsed {} entries", ast.entries.len());
println!("\nAST:");
for (i, entry) in ast.entries.iter().enumerate() {
println!("{}: {:?}", i, entry);
}
Ok(())
}
pub fn generate_command(config: PathBuf, kconfig: PathBuf, srctree: PathBuf) -> Result<()> {
println!("Generating configuration files...");
println!("Config: {}", config.display());
println!("Kconfig: {}", kconfig.display());
// Parse Kconfig
let mut parser = Parser::new(&kconfig, &srctree)?;
let _ast = parser.parse()?;
// Read .config
let config_values = ConfigReader::read(&config)?;
// Build symbol table
let mut symbols = SymbolTable::new();
for (name, value) in config_values {
// Extract symbol type from name (simplified)
symbols.add_symbol(name.clone(), crate::kconfig::SymbolType::Bool);
symbols.set_value(&name, value);
}
// Generate auto.conf
ConfigGenerator::generate_auto_conf("auto.conf", &symbols)?;
println!("Generated auto.conf");
// Generate autoconf.h
ConfigGenerator::generate_autoconf_h("autoconf.h", &symbols)?;
println!("Generated autoconf.h");
Ok(())
}
pub fn run_cli() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Parse { kconfig, srctree } => {
parse_command(kconfig, srctree)
}
Commands::Defconfig { defconfig, kconfig, srctree } => {
crate::cli::defconfig::defconfig_command(defconfig, kconfig, srctree)
}
Commands::Menuconfig { kconfig, srctree } => {
crate::cli::menuconfig::menuconfig_command(kconfig, srctree)
}
Commands::Generate { config, kconfig, srctree } => {
generate_command(config, kconfig, srctree)
}
Commands::Oldconfig { config, kconfig, srctree, auto_defaults } => {
crate::cli::oldconfig::oldconfig_command(config, kconfig, srctree, auto_defaults)
}
Commands::Saveconfig { output, kconfig, srctree } => {
crate::cli::saveconfig::saveconfig_command(output, kconfig, srctree)
}
}
}