use std::ffi::OsString;
#[cfg(not(any(windows, target_family = "wasm")))]
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
#[cfg(windows)]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[cfg(not(any(windows, target_family = "wasm")))]
#[repr(transparent)]
pub struct MallocConf(*const u8);
#[cfg(not(any(windows, target_family = "wasm")))]
unsafe impl Sync for MallocConf {}
#[cfg(not(any(windows, target_family = "wasm")))]
#[unsafe(export_name = "_rjem_malloc_conf")]
pub static MALLOC_CONF: MallocConf =
MallocConf(c"narenas:4,dirty_decay_ms:0,muzzy_decay_ms:0,retain:true".as_ptr() as *const u8);
const TOOLS: [(&str, &str, fn()); 4] = [
("decode", "tippecanoe-decode", tippecanoe::decode::main),
(
"enumerate",
"tippecanoe-enumerate",
tippecanoe::enumerate::main,
),
("overzoom", "tippecanoe-overzoom", overzoom),
(
"json-tool",
"tippecanoe-json-tool",
tippecanoe::jsontool::main,
),
];
fn main() {
let argv: Vec<String> = std::env::args_os()
.map(|a| a.to_string_lossy().into_owned())
.collect();
#[cfg(windows)]
let run_as_name = std::env::var(RUN_AS).ok();
#[cfg(unix)]
let run_as_name: Option<String> = None;
let name = match &run_as_name {
Some(program) => program.as_str(),
None => argv
.first()
.map(|a| tippecanoe::platform::program_name(a))
.unwrap_or("manby"),
};
if let Some(&(_, _, tool)) = TOOLS.iter().find(|t| t.1 == name) {
return tool();
}
let subcommand = argv.get(1).map(String::as_str).unwrap_or("");
if let Some(&(_, program, _)) = TOOLS
.iter()
.find(|t| t.0 == subcommand || t.1 == subcommand)
{
run_as(program, &argv[0]);
}
let status = manby::command::run(&argv);
if status != 0 {
std::process::exit(status);
}
}
fn overzoom() {
let argv: Vec<String> = std::env::args_os()
.map(|a| a.to_string_lossy().into_owned())
.collect();
if let Err(e) = manby::overzoom::parse_args(&argv).and_then(|o| manby::overzoom::run(&o)) {
if !e.message.is_empty() {
eprintln!("{}", e.message);
}
std::process::exit(e.exit_code);
}
}
#[cfg(windows)]
const RUN_AS: &str = "MANBY_RUN_AS";
fn run_as(program: &str, argv0: &str) -> ! {
let args: Vec<OsString> = std::env::args_os().skip(2).collect();
let exe = std::env::current_exe().unwrap_or_else(|e| {
eprintln!(
"{}: can't find this program to run {}: {}",
argv0, program, e
);
std::process::exit(tippecanoe::errors::EXIT_OPEN);
});
let mut command = std::process::Command::new(exe);
command.args(args);
#[cfg(unix)]
let err = {
use std::os::unix::process::CommandExt;
command.arg0(program).exec()
};
#[cfg(windows)]
let err = match command.env(RUN_AS, program).status() {
Ok(status) => std::process::exit(status.code().unwrap_or(1)),
Err(e) => e,
};
eprintln!("{}: can't run {}: {}", argv0, program, err);
std::process::exit(tippecanoe::errors::EXIT_OPEN);
}