| 1 |
use std::{cell::RefCell, env, path::PathBuf, process, str::FromStr}; |
| 2 |
|
| 3 |
use getopts::Options; |
| 4 |
use hprof::Profiler; |
| 5 |
use libgir::{self as gir, Config, Library, WorkMode}; |
| 6 |
|
| 7 |
fn print_usage(program: &str, opts: Options) { |
| 8 |
let brief = format!( |
| 9 |
"Usage: {program} [options] [<library> <version>] |
| 10 |
{program} (-h | --help)" |
| 11 |
); |
| 12 |
print!("{}", opts.usage(&brief)); |
| 13 |
} |
| 14 |
|
| 15 |
trait OptionStr { |
| 16 |
fn as_str_ref(&self) -> Option<&str>; |
| 17 |
} |
| 18 |
|
| 19 |
impl<S: AsRef<str>> OptionStr for Option<S> { |
| 20 |
fn as_str_ref(&self) -> Option<&str> { |
| 21 |
self.as_ref().map(|string| string.as_ref()) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[allow(clippy::large_enum_variant)] |
| 26 |
enum RunKind { |
| 27 |
Config(Config), |
| 28 |
CheckGirFile(String), |
| 29 |
} |
| 30 |
|
| 31 |
fn build_config() -> Result<RunKind, String> { |
| 32 |
let args: Vec<_> = env::args().collect(); |
| 33 |
let program = args[0].clone(); |
| 34 |
|
| 35 |
let mut options = Options::new(); |
| 36 |
options.optopt( |
| 37 |
"c", |
| 38 |
"config", |
| 39 |
"Config file path (default: Gir.toml)", |
| 40 |
"CONFIG", |
| 41 |
); |
| 42 |
options.optflag("h", "help", "Show this message"); |
| 43 |
options.optmulti( |
| 44 |
"d", |
| 45 |
"girs-directories", |
| 46 |
"Directories for GIR files", |
| 47 |
"GIRSPATH", |
| 48 |
); |
| 49 |
options.optopt( |
| 50 |
"m", |
| 51 |
"mode", |
| 52 |
"Work mode: doc, normal, sys or not_bound", |
| 53 |
"MODE", |
| 54 |
); |
| 55 |
options.optopt("o", "target", "Target path", "PATH"); |
| 56 |
options.optopt("p", "doc-target-path", "Doc target path", "PATH"); |
| 57 |
options.optflag("b", "make-backup", "Make backup before generating"); |
| 58 |
options.optflag("s", "stats", "Show statistics"); |
| 59 |
options.optflag("", "disable-format", "Disable formatting generated code"); |
| 60 |
options.optopt( |
| 61 |
"", |
| 62 |
"check-gir-file", |
| 63 |
"Check if the given `.gir` file is valid", |
| 64 |
"PATH", |
| 65 |
); |
| 66 |
|
| 67 |
let matches = options.parse(&args[1..]).map_err(|e| e.to_string())?; |
| 68 |
|
| 69 |
if let Some(check_gir_file) = matches.opt_str("check-gir-file") { |
| 70 |
return Ok(RunKind::CheckGirFile(check_gir_file)); |
| 71 |
} |
| 72 |
|
| 73 |
if matches.opt_present("h") { |
| 74 |
print_usage(&program, options); |
| 75 |
process::exit(0); |
| 76 |
} |
| 77 |
|
| 78 |
let work_mode = match matches.opt_str("m") { |
| 79 |
None => None, |
| 80 |
Some(s) => match WorkMode::from_str(&s) { |
| 81 |
Ok(w) => Some(w), |
| 82 |
Err(e) => { |
| 83 |
eprintln!("Error (switching to default work mode): {e}"); |
| 84 |
None |
| 85 |
} |
| 86 |
}, |
| 87 |
}; |
| 88 |
|
| 89 |
Config::new( |
| 90 |
matches.opt_str("c").as_str_ref(), |
| 91 |
work_mode, |
| 92 |
&matches.opt_strs("d"), |
| 93 |
matches.free.first().as_str_ref(), |
| 94 |
matches.free.get(1).as_str_ref(), |
| 95 |
matches.opt_str("o").as_str_ref(), |
| 96 |
matches.opt_str("doc-target-path").as_str_ref(), |
| 97 |
matches.opt_present("b"), |
| 98 |
matches.opt_present("s"), |
| 99 |
matches.opt_present("disable-format"), |
| 100 |
) |
| 101 |
.map(RunKind::Config) |
| 102 |
} |
| 103 |
|
| 104 |
fn run_check(check_gir_file: &str) -> Result<(), String> { |
| 105 |
let path = PathBuf::from(check_gir_file); |
| 106 |
if !path.is_file() { |
| 107 |
return Err(format!("`{check_gir_file}`: file not found",)); |
| 108 |
} |
| 109 |
let lib_name = path |
| 110 |
.file_stem() |
| 111 |
.ok_or(format!("Failed to get file stem from `{check_gir_file}`",))?; |
| 112 |
let lib_name = lib_name |
| 113 |
.to_str() |
| 114 |
.ok_or_else(|| "failed to convert OsStr to str".to_owned())?; |
| 115 |
let mut library = Library::new(lib_name); |
| 116 |
let parent = path.parent().ok_or(format!( |
| 117 |
"Failed to get parent directory from `{check_gir_file}`", |
| 118 |
))?; |
| 119 |
|
| 120 |
library.read_file(&[parent], &mut vec![lib_name.to_owned()]) |
| 121 |
} |
| 122 |
|
| 123 |
fn init_logger() { |
| 124 |
let env = env_logger::Env::default().filter_or("GIR_LOG", "gir=warn,libgir=warn"); |
| 125 |
|
| 126 |
env_logger::init_from_env(env); |
| 127 |
} |
| 128 |
|
| 129 |
fn main() -> Result<(), String> { |
| 130 |
init_logger(); |
| 131 |
|
| 132 |
let mut cfg = match build_config() { |
| 133 |
Ok(RunKind::CheckGirFile(check_gir_file)) => return run_check(&check_gir_file), |
| 134 |
Ok(RunKind::Config(cfg)) => cfg, |
| 135 |
Err(err) => return Err(err), |
| 136 |
}; |
| 137 |
cfg.check_disable_format(); |
| 138 |
|
| 139 |
let statistics = Profiler::new("Gir"); |
| 140 |
statistics.start_frame(); |
| 141 |
|
| 142 |
let watcher_total = statistics.enter("Total"); |
| 143 |
|
| 144 |
let mut library = { |
| 145 |
let _watcher = statistics.enter("Loading"); |
| 146 |
|
| 147 |
let mut library = Library::new(&cfg.library_name); |
| 148 |
library.read_file(&cfg.girs_dirs, &mut vec![cfg.library_full_name()])?; |
| 149 |
library |
| 150 |
}; |
| 151 |
|
| 152 |
{ |
| 153 |
let _watcher = statistics.enter("Preprocessing"); |
| 154 |
library.preprocessing(cfg.work_mode); |
| 155 |
} |
| 156 |
|
| 157 |
{ |
| 158 |
let _watcher = statistics.enter("Update library by config"); |
| 159 |
gir::update_version::apply_config(&mut library, &cfg); |
| 160 |
} |
| 161 |
|
| 162 |
{ |
| 163 |
let _watcher = statistics.enter("Postprocessing"); |
| 164 |
library.postprocessing(&cfg); |
| 165 |
} |
| 166 |
|
| 167 |
{ |
| 168 |
let _watcher = statistics.enter("Resolving type ids"); |
| 169 |
cfg.resolve_type_ids(&library); |
| 170 |
} |
| 171 |
|
| 172 |
{ |
| 173 |
let _watcher = statistics.enter("Checking versions"); |
| 174 |
gir::update_version::check_function_real_version(&mut library); |
| 175 |
} |
| 176 |
|
| 177 |
let mut env = { |
| 178 |
let _watcher = statistics.enter("Namespace/symbol/class analysis"); |
| 179 |
|
| 180 |
let namespaces = gir::namespaces_run(&library); |
| 181 |
let symbols = gir::symbols_run(&library, &namespaces); |
| 182 |
let class_hierarchy = gir::class_hierarchy_run(&library); |
| 183 |
|
| 184 |
gir::Env { |
| 185 |
library, |
| 186 |
config: cfg, |
| 187 |
namespaces, |
| 188 |
symbols: RefCell::new(symbols), |
| 189 |
class_hierarchy, |
| 190 |
analysis: Default::default(), |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
if env.config.work_mode != WorkMode::Sys { |
| 195 |
let _watcher = statistics.enter("Analyzing"); |
| 196 |
gir::analysis_run(&mut env); |
| 197 |
} |
| 198 |
|
| 199 |
if env.config.work_mode != WorkMode::DisplayNotBound { |
| 200 |
let _watcher = statistics.enter("Generating"); |
| 201 |
gir::codegen_generate(&env); |
| 202 |
} |
| 203 |
|
| 204 |
if !env.config.disable_format && env.config.work_mode.is_generate_rust_files() { |
| 205 |
let _watcher = statistics.enter("Formatting"); |
| 206 |
gir::fmt::format(&env.config.target_path); |
| 207 |
} |
| 208 |
|
| 209 |
drop(watcher_total); |
| 210 |
statistics.end_frame(); |
| 211 |
|
| 212 |
if env.config.show_statistics { |
| 213 |
statistics.print_timing(); |
| 214 |
} |
| 215 |
if env.config.work_mode == WorkMode::DisplayNotBound { |
| 216 |
env.library.show_non_bound_types(&env); |
| 217 |
} |
| 218 |
|
| 219 |
Ok(()) |
| 220 |
} |