wasmer_compiler_cli/commands/
config.rs1use crate::VERSION;
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::env;
5use std::path::PathBuf;
6
7#[derive(Debug, Parser)]
8pub struct Config {
10 #[clap(long, conflicts_with = "pkg_config")]
12 prefix: bool,
13
14 #[clap(long, conflicts_with = "pkg_config")]
16 bindir: bool,
17
18 #[clap(long, conflicts_with = "pkg_config")]
20 includedir: bool,
21
22 #[clap(long, conflicts_with = "pkg_config")]
24 libdir: bool,
25
26 #[clap(long, conflicts_with = "pkg_config")]
28 libs: bool,
29
30 #[clap(long, conflicts_with = "pkg_config")]
32 cflags: bool,
33
34 #[clap(long)]
37 pkg_config: bool,
38}
39
40impl Config {
41 pub fn execute(&self) -> Result<()> {
43 self.inner_execute()
44 .context("failed to retrieve the wasmer config".to_string())
45 }
46 fn inner_execute(&self) -> Result<()> {
47 let key = "WASMER_DIR";
48 let wasmer_dir = env::var(key)
49 .ok()
50 .or_else(|| option_env!("WASMER_INSTALL_PREFIX").map(str::to_string))
51 .or_else(|| {
52 #[allow(deprecated)]
55 let dir = std::env::home_dir()?.join(".wasmer").to_str()?.to_string();
56
57 Some(dir)
58 })
59 .context(format!(
60 "failed to retrieve the {key} environment variables",
61 ))?;
62
63 let prefix = PathBuf::from(wasmer_dir);
64
65 let prefixdir = prefix.display().to_string();
66 let bindir = prefix.join("bin").display().to_string();
67 let includedir = prefix.join("include").display().to_string();
68 let libdir = prefix.join("lib").display().to_string();
69 let cflags = format!("-I{includedir}");
70 let libs = format!("-L{libdir} -lwasmer");
71
72 if self.pkg_config {
73 println!("prefix={prefixdir}");
74 println!("exec_prefix={bindir}");
75 println!("includedir={includedir}");
76 println!("libdir={libdir}");
77 println!();
78 println!("Name: wasmer");
79 println!("Description: The Wasmer library for running WebAssembly");
80 println!("Version: {VERSION}");
81 println!("Cflags: {cflags}");
82 println!("Libs: {libs}");
83 return Ok(());
84 }
85
86 if self.prefix {
87 println!("{prefixdir}");
88 }
89 if self.bindir {
90 println!("{bindir}");
91 }
92 if self.includedir {
93 println!("{includedir}");
94 }
95 if self.libdir {
96 println!("{libdir}");
97 }
98 if self.libs {
99 println!("{libs}");
100 }
101 if self.cflags {
102 println!("{cflags}");
103 }
104 Ok(())
105 }
106}