wasmer_compiler_cli/commands/
config.rs

1use crate::VERSION;
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::env;
5use std::path::PathBuf;
6
7#[derive(Debug, Parser)]
8/// The options for the `wasmer config` subcommand
9pub struct Config {
10    /// Print the installation prefix.
11    #[clap(long, conflicts_with = "pkg_config")]
12    prefix: bool,
13
14    /// Directory containing Wasmer executables.
15    #[clap(long, conflicts_with = "pkg_config")]
16    bindir: bool,
17
18    /// Directory containing Wasmer headers.
19    #[clap(long, conflicts_with = "pkg_config")]
20    includedir: bool,
21
22    /// Directory containing Wasmer libraries.
23    #[clap(long, conflicts_with = "pkg_config")]
24    libdir: bool,
25
26    /// Libraries needed to link against Wasmer components.
27    #[clap(long, conflicts_with = "pkg_config")]
28    libs: bool,
29
30    /// C compiler flags for files that include Wasmer headers.
31    #[clap(long, conflicts_with = "pkg_config")]
32    cflags: bool,
33
34    /// It outputs the necessary details for compiling
35    /// and linking a program to Wasmer, using the `pkg-config` format.
36    #[clap(long)]
37    pkg_config: bool,
38}
39
40impl Config {
41    /// Runs logic for the `config` subcommand
42    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                // Allowing deprecated function home_dir since it works fine,
53                // and will never be removed from std.
54                #[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}