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
use crate::VERSION;
use anyhow::{Context, Result};
use clap::Parser;
use std::env;
use std::path::PathBuf;

#[derive(Debug, Parser)]
/// The options for the `wasmer config` subcommand
pub struct Config {
    /// Print the installation prefix.
    #[clap(long, conflicts_with = "pkg_config")]
    prefix: bool,

    /// Directory containing Wasmer executables.
    #[clap(long, conflicts_with = "pkg_config")]
    bindir: bool,

    /// Directory containing Wasmer headers.
    #[clap(long, conflicts_with = "pkg_config")]
    includedir: bool,

    /// Directory containing Wasmer libraries.
    #[clap(long, conflicts_with = "pkg_config")]
    libdir: bool,

    /// Libraries needed to link against Wasmer components.
    #[clap(long, conflicts_with = "pkg_config")]
    libs: bool,

    /// C compiler flags for files that include Wasmer headers.
    #[clap(long, conflicts_with = "pkg_config")]
    cflags: bool,

    /// It outputs the necessary details for compiling
    /// and linking a program to Wasmer, using the `pkg-config` format.
    #[clap(long)]
    pkg_config: bool,
}

impl Config {
    /// Runs logic for the `config` subcommand
    pub fn execute(&self) -> Result<()> {
        self.inner_execute()
            .context("failed to retrieve the wasmer config".to_string())
    }
    fn inner_execute(&self) -> Result<()> {
        let key = "WASMER_DIR";
        let wasmer_dir = env::var(key)
            .ok()
            .or_else(|| option_env!("WASMER_INSTALL_PREFIX").map(str::to_string))
            .or_else(|| {
                // Allowing deprecated function home_dir since it works fine,
                // and will never be removed from std.
                #[allow(deprecated)]
                let dir = std::env::home_dir()?.join(".wasmer").to_str()?.to_string();

                Some(dir)
            })
            .context(format!(
                "failed to retrieve the {key} environment variables",
            ))?;

        let prefix = PathBuf::from(wasmer_dir);

        let prefixdir = prefix.display().to_string();
        let bindir = prefix.join("bin").display().to_string();
        let includedir = prefix.join("include").display().to_string();
        let libdir = prefix.join("lib").display().to_string();
        let cflags = format!("-I{includedir}");
        let libs = format!("-L{libdir} -lwasmer");

        if self.pkg_config {
            println!("prefix={prefixdir}");
            println!("exec_prefix={bindir}");
            println!("includedir={includedir}");
            println!("libdir={libdir}");
            println!();
            println!("Name: wasmer");
            println!("Description: The Wasmer library for running WebAssembly");
            println!("Version: {VERSION}");
            println!("Cflags: {cflags}");
            println!("Libs: {libs}");
            return Ok(());
        }

        if self.prefix {
            println!("{prefixdir}");
        }
        if self.bindir {
            println!("{bindir}");
        }
        if self.includedir {
            println!("{includedir}");
        }
        if self.libdir {
            println!("{libdir}");
        }
        if self.libs {
            println!("{libs}");
        }
        if self.cflags {
            println!("{cflags}");
        }
        Ok(())
    }
}