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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::path::PathBuf;
use std::{env, path::Path};

pub fn c_asset_path() -> &'static Path {
    Path::new(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../../../lib/c-api/examples/assets/"
    ))
}

pub fn asset_path() -> &'static Path {
    Path::new(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../../../tests/examples/"
    ))
}

pub fn wasmer_include_path() -> &'static Path {
    Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../../../lib/c-api/"))
}

pub fn wasmer_target_path() -> &'static Path {
    let path = if cfg!(feature = "debug") {
        concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/debug/")
    } else {
        concat!(env!("CARGO_MANIFEST_DIR"), "/../../../target/release/")
    };
    Path::new(path)
}

pub fn wasmer_target_path_2() -> &'static Path {
    let path = if cfg!(feature = "debug") {
        concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../../../target/",
            env!("CARGO_BUILD_TARGET"),
            "/debug/"
        )
    } else {
        concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../../../target/",
            env!("CARGO_BUILD_TARGET"),
            "/release/"
        )
    };
    Path::new(path)
}

/* env var TARGET is set by tests/integration/cli/build.rs on compile-time */

pub const LIBWASMER_FILENAME: &str = {
    if cfg!(windows) {
        "wasmer.lib"
    } else {
        "libwasmer.a"
    }
};

/// Get the path to the `libwasmer.a` static library.
pub fn get_libwasmer_path() -> PathBuf {
    let mut ret = env::var("WASMER_TEST_LIBWASMER_PATH")
        .map(PathBuf::from)
        .unwrap_or_else(|_| wasmer_target_path().join(LIBWASMER_FILENAME));

    if !ret.exists() {
        ret = wasmer_target_path_2().join(LIBWASMER_FILENAME);
    }
    if !ret.exists() {
        panic!("Could not find libwasmer path! {ret:?}");
    }
    ret
}

/// Get the path to the `wasmer` executable to be used in this test.
pub fn get_wasmer_path() -> PathBuf {
    let mut ret = env::var("WASMER_TEST_WASMER_PATH")
        .map(PathBuf::from)
        .unwrap_or_else(|_| wasmer_target_path().join("wasmer"));

    if !ret.exists() {
        ret = wasmer_target_path_2().join("wasmer");
    }
    if !ret.exists() {
        ret = match get_repo_root_path() {
            Some(s) => {
                let release_dir = s.join("target").join("release");

                if cfg!(windows) {
                    release_dir.join("wasmer.exe")
                } else {
                    release_dir.join("wasmer")
                }
            }
            None => {
                panic!("Could not find wasmer executable path! {ret:?}");
            }
        };
    }

    if !ret.exists() {
        ret = match get_repo_root_path() {
            Some(s) => {
                let executable = if cfg!(windows) {
                    "wasmer.exe"
                } else {
                    "wasmer"
                };
                s.join("target")
                    .join(target_lexicon::HOST.to_string())
                    .join("release")
                    .join(executable)
            }
            None => {
                panic!("Could not find wasmer executable path! {ret:?}");
            }
        };
    }

    if !ret.exists() {
        if let Some(root) = get_repo_root_path() {
            use std::process::Stdio;
            let _ = std::process::Command::new("ls")
                .arg(root.join("target"))
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .stdin(Stdio::null())
                .output();
        }
        panic!(
            "cannot find wasmer / wasmer.exe for integration test at '{}'!",
            ret.display()
        );
    }
    ret
}

pub fn get_repo_root_path() -> Option<PathBuf> {
    let mut current_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let mut counter = 0;
    let mut result = None;
    'outer: while counter < 50 {
        counter += 1;
        if current_dir.join("CHANGELOG.md").exists() && current_dir.join("LICENSE").exists() {
            result = Some(current_dir.to_path_buf());
            break 'outer;
        } else {
            current_dir = current_dir.parent()?;
        }
    }
    result
}

pub fn get_wasmer_dir() -> Result<PathBuf, anyhow::Error> {
    if let Ok(s) = std::env::var("WASMER_DIR") {
        Ok(Path::new(&s).to_path_buf())
    } else if let Some(root_dir) = get_repo_root_path().and_then(|root| {
        if root.join("package").exists() {
            Some(root.join("package"))
        } else {
            None
        }
    }) {
        Ok(root_dir)
    } else {
        let home_dir = dirs::home_dir()
            .ok_or(anyhow::anyhow!("no home dir"))?
            .join(".wasmer");
        if home_dir.exists() {
            Ok(home_dir)
        } else {
            Err(anyhow::anyhow!("no .wasmer home dir"))
        }
    }
}