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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// This file is part of Gear.

// Copyright (C) 2022-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::cargo_toolchain::Toolchain;
use anyhow::{ensure, Context, Result};
use std::{env, path::PathBuf, process::Command};

use crate::builder_error::BuilderError;

/// Helper to deal with the `cargo` command.
pub struct CargoCommand {
    path: String,
    manifest_path: PathBuf,
    profile: String,
    rustc_flags: Vec<&'static str>,
    target_dir: PathBuf,
    features: Vec<String>,
    toolchain: Toolchain,
    check_recommended_toolchain: bool,
    force_recommended_toolchain: bool,
    paths_to_remap: Vec<(PathBuf, &'static str)>,
}

impl CargoCommand {
    /// Initialize new cargo command.
    pub fn new() -> CargoCommand {
        CargoCommand {
            path: "rustup".to_string(),
            manifest_path: "Cargo.toml".into(),
            profile: "dev".to_string(),
            rustc_flags: vec!["-C", "link-arg=--import-memory", "-C", "linker-plugin-lto"],
            target_dir: "target".into(),
            features: vec![],
            toolchain: Toolchain::try_from_rustup().expect("Failed to get toolchain from rustup"),
            check_recommended_toolchain: false,
            force_recommended_toolchain: false,
            paths_to_remap: vec![],
        }
    }
}

impl Default for CargoCommand {
    fn default() -> Self {
        Self::new()
    }
}

impl CargoCommand {
    /// Set path to the `Cargo.toml` file.
    pub fn set_manifest_path(&mut self, path: PathBuf) {
        self.manifest_path = path;
    }

    /// Set path to the `target` directory.
    pub fn set_target_dir(&mut self, path: PathBuf) {
        self.target_dir = path;
    }

    /// Set profile.
    ///
    /// Possible values: `dev`, `release`.
    pub fn set_profile(&mut self, profile: String) {
        self.profile = profile;
    }

    /// Set features.
    pub fn set_features(&mut self, features: &[String]) {
        self.features = features.into();
    }

    /// Sets whether to check the version of the recommended toolchain.
    pub(crate) fn set_check_recommended_toolchain(&mut self, check_recommended_toolchain: bool) {
        self.check_recommended_toolchain = check_recommended_toolchain;
    }

    /// Sets whether to force the version of the recommended toolchain.
    pub(crate) fn set_force_recommended_toolchain(&mut self, force_recommended_toolchain: bool) {
        self.force_recommended_toolchain = force_recommended_toolchain;
    }

    /// Set paths to remap.
    ///
    /// Used to hide the username from the panic message.
    pub fn set_paths_to_remap(&mut self, paths_to_remap: &[(PathBuf, &'static str)]) {
        self.paths_to_remap = paths_to_remap.into();
    }

    /// Execute the `cargo` command with invoking supplied arguments.
    pub fn run(&self) -> Result<()> {
        if self.check_recommended_toolchain {
            self.toolchain.check_recommended_toolchain()?;
        }

        let toolchain = if self.force_recommended_toolchain {
            Toolchain::recommended_nightly()
        } else {
            self.toolchain.clone()
        };

        let mut cargo = Command::new(&self.path);
        if self.force_recommended_toolchain {
            self.clean_up_environment(&mut cargo);
        }
        cargo
            .arg("run")
            .arg(toolchain.raw_toolchain_str().as_ref())
            .arg("cargo")
            .arg("rustc")
            .arg("--target=wasm32-unknown-unknown")
            .arg("--color=always")
            .arg(format!("--manifest-path={}", self.manifest_path.display()))
            .arg("--profile")
            .arg(&self.profile);

        if !self.features.is_empty() {
            cargo.arg("--features");
            cargo.arg(self.features.join(","));
        }

        cargo
            .arg("--")
            .args(&self.rustc_flags)
            .env("CARGO_TARGET_DIR", &self.target_dir)
            .env("__GEAR_WASM_BUILDER_NO_BUILD", "1"); // Don't build the original crate recursively

        self.remove_cargo_encoded_rustflags(&mut cargo);

        if !self.paths_to_remap.is_empty() {
            // `--remap-path-prefix` is used to remove username from panic messages
            // https://doc.rust-lang.org/rustc/command-line-arguments.html#--remap-path-prefix-remap-source-names-in-output
            let global_encoded_rustflags = self
                .paths_to_remap
                .iter()
                .map(|(from, to)| format!("--remap-path-prefix={from}={to}", from = from.display()))
                .collect::<Vec<_>>()
                .join("\x1f");

            // The environment variable `CARGO_ENCODED_RUSTFLAGS` is used to globally remap path prefix.
            // It is also separated by `\x1f` to support folders with spaces or any unusual characters.
            // Unlike `cargo rust`, this is useful for passing flags to all dependencies (i.e. globally).
            cargo.env("CARGO_ENCODED_RUSTFLAGS", global_encoded_rustflags);
        }

        let status = cargo.status().context("unable to execute cargo command")?;
        ensure!(
            status.success(),
            BuilderError::CargoRunFailed(status.to_string())
        );

        Ok(())
    }

    fn clean_up_environment(&self, command: &mut Command) {
        // Inherited build script environment variables must be removed
        // so that they cannot change the behavior of the cargo package manager.

        // https://doc.rust-lang.org/cargo/reference/environment-variables.html
        // `RUSTC_WRAPPER` and `RUSTC_WORKSPACE_WRAPPER` are not removed due to tools like sccache.
        const INHERITED_ENV_VARS: &[&str] = &[
            "CARGO",
            "CARGO_MANIFEST_DIR",
            "CARGO_MANIFEST_LINKS",
            "CARGO_MAKEFLAGS",
            "OUT_DIR",
            "TARGET",
            "HOST",
            "NUM_JOBS",
            "OPT_LEVEL",
            "PROFILE",
            "RUSTC",
            "RUSTDOC",
            "RUSTC_LINKER",
            "CARGO_ENCODED_RUSTFLAGS",
        ];

        for env_var in INHERITED_ENV_VARS {
            command.env_remove(env_var);
        }

        const INHERITED_ENV_VARS_WITH_PREFIX: &[&str] =
            &["CARGO_FEATURE_", "CARGO_CFG_", "DEP_", "CARGO_PKG_"];

        for (env_var, _) in env::vars() {
            for prefix in INHERITED_ENV_VARS_WITH_PREFIX {
                if env_var.starts_with(prefix) {
                    command.env_remove(&env_var);
                }
            }
        }
    }

    fn remove_cargo_encoded_rustflags(&self, command: &mut Command) {
        // substrate's wasm-builder removes these vars so do we
        // check its source for details
        command.env_remove("CARGO_ENCODED_RUSTFLAGS");
        command.env_remove("RUSTFLAGS");
    }
}