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
214
215
216
217
218
219
220
221
222
#[cfg(feature = "auditwheel")]
use auditwheel_rs;
use build_rust;
use cargo_metadata;
use cargo_toml::CargoTomlMetadata;
use cargo_toml::CargoTomlMetadataPyo3Pack;
use failure::{Error, ResultExt};
use metadata::WheelMetadata;
use std::collections::HashMap;
use std::fs::create_dir_all;
use std::fs::read_to_string;
use std::path::PathBuf;
use toml;
use wheel::build_wheel;
use CargoToml;
use Metadata21;
use PythonInterpreter;

/// Since there is no known way to list the installed python versions platform independent (or just
/// generally to list all binaries in $PATH, which could then be filtered down),
/// this is a workaround (which works until python 4 is released, which won't be too soon)
const PYTHON_INTERPRETER: &[&str] = &[
    "python2.7",
    "python3.5",
    "python3.6",
    "python3.7",
    "python3.8",
    "python3.9",
];

/// High level API for building wheels from a crate which can be also used for the CLI
#[derive(Debug, Serialize, Deserialize, StructOpt, Clone, Eq, PartialEq)]
#[serde(default)]
pub struct BuildContext {
    #[structopt(short = "i", long = "interpreter")]
    /// The python versions to build wheels for, given as the names of the interpreters.
    /// Uses a built-in list if not explicitly set.
    pub interpreter: Vec<String>,
    /// The crate providing the python bindings
    #[structopt(short = "b", long = "bindings-crate", default_value = "pyo3")]
    pub binding_crate: String,
    #[structopt(
        short = "m",
        long = "manifest-path",
        parse(from_os_str),
        default_value = "."
    )]
    /// The path to the Cargo.toml or the directory containing it
    pub manifest_path: PathBuf,
    /// The directory to store the built wheels in. Defaults to a new "wheels" directory in the
    /// project's target directory
    #[structopt(short = "w", long = "wheel-dir", parse(from_os_str))]
    pub wheel_dir: Option<PathBuf>,
    /// Don't rebuild if a wheel with the same name is already present
    #[structopt(long = "use-cached")]
    pub use_cached: bool,
    /// Do a debug build (don't pass --release to cargo)
    #[structopt(short = "d", long = "debug")]
    pub debug: bool,
    /// Don't check for manylinux compliance
    #[structopt(long = "skip-auditwheel")]
    pub skip_auditwheel: bool,
}

impl Default for BuildContext {
    fn default() -> Self {
        BuildContext {
            interpreter: PYTHON_INTERPRETER.iter().map(ToString::to_string).collect(),
            binding_crate: "pyo3".to_string(),
            manifest_path: PathBuf::from("."),
            wheel_dir: None,
            use_cached: false,
            debug: false,
            skip_auditwheel: false,
        }
    }
}

impl BuildContext {
    /// Builds wheels for a Cargo project for all given python versions. Returns the paths where
    /// the wheels are saved and the Python metadata describing the cargo project
    ///
    /// Defaults to 2.7 and 3.{5, 6, 7, 8, 9} if not python versions are given and silently
    /// ignores all non-existent python versions. Runs [auditwheel_rs()].if the auditwheel feature
    /// isn't deactivated
    pub fn build_wheels(
        self,
    ) -> Result<(Vec<(PathBuf, Option<PythonInterpreter>)>, WheelMetadata), Error> {
        let manifest_dir;
        let manifest_file;

        // Manifest paths come in two flavors: Pointing to the file or pointing to the directory
        if self.manifest_path.is_dir() {
            manifest_dir = self.manifest_path.canonicalize().unwrap();
            manifest_file = self
                .manifest_path
                .join("Cargo.toml")
                .canonicalize()
                .unwrap();
        } else if self.manifest_path.is_file() {
            manifest_dir = self
                .manifest_path
                .parent()
                .unwrap()
                .canonicalize()
                .unwrap()
                .to_path_buf();
            manifest_file = self.manifest_path.clone().canonicalize().unwrap();
        } else {
            bail!(
                "The manifest path {} is neither is directory nor a file",
                self.manifest_path.display()
            );
        };

        let contents = read_to_string(&manifest_file).context(format!(
            "Can't read Cargo.toml at {}",
            manifest_file.display(),
        ))?;

        let cargo_toml: CargoToml =
            toml::from_str(&contents).context("Failed to parse Cargo.toml")?;
        let metadata21 = Metadata21::from_cargo_toml(&cargo_toml, &manifest_dir)
            .context("Failed to transform Cargo metadata into python metadata")?;

        let scripts = match cargo_toml.package.metadata {
            Some(CargoTomlMetadata {
                pyo3_pack:
                    Some(CargoTomlMetadataPyo3Pack {
                        scripts: Some(ref scripts),
                    }),
            }) => scripts.clone(),
            _ => HashMap::new(),
        };

        let metadata = WheelMetadata {
            metadata21,
            scripts,
        };

        let available_version = if !self.interpreter.is_empty() {
            PythonInterpreter::find_all(&self.interpreter)?
        } else {
            let default_vec = PYTHON_INTERPRETER
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>();
            PythonInterpreter::find_all(&default_vec)?
        };

        println!(
            "Found {}",
            available_version
                .iter()
                .map(|v| v.to_string())
                .collect::<Vec<String>>()
                .join(", ")
        );

        let wheel_dir;
        match self.wheel_dir.clone() {
            Some(dir) => wheel_dir = dir,
            None => {
                // Failure fails here since cargo_toml does some weird stuff on their side
                let cargo_toml = cargo_metadata::metadata(Some(&manifest_file))
                    .map_err(|e| format_err!("Cargo metadata failed: {}", e))?;
                wheel_dir = PathBuf::from(cargo_toml.target_directory).join("wheels");
            }
        }

        create_dir_all(&wheel_dir).context("Failed to create the target directory for the wheels")?;

        let mut wheels = Vec::new();
        for python_version in available_version {
            let wheel_path = wheel_dir.join(format!(
                "{}-{}-{}.whl",
                &metadata.metadata21.name,
                &metadata.metadata21.version,
                python_version.get_tag()
            ));

            if self.use_cached && wheel_path.exists() {
                println!("Using cached wheel for {}", &python_version);
                wheels.push((wheel_path, Some(python_version)));
                continue;
            }

            let artifact = build_rust(
                &metadata.metadata21.name,
                &manifest_file,
                &self,
                &python_version,
            ).context("Failed to build a native library through cargo")?;
            if !self.skip_auditwheel {
                #[cfg(feature = "auditwheel")]
                auditwheel_rs(&artifact).context("Failed to ensure manylinux compliance")?;
            }
            build_wheel(&metadata, &python_version, &artifact, &wheel_path)?;

            wheels.push((wheel_path, Some(python_version)));
        }

        #[cfg(feature = "sdist")]
        {
            let sdist_path = wheel_dir.join(format!(
                "{}-{}.tar.gz",
                &metadata.metadata21.name, &metadata.metadata21.version
            ));

            println!(
                "Building the source distribution to {}",
                sdist_path.display()
            );
            ::build_source_distribution(&self, &metadata, &sdist_path)
                .context("Failed to build the source distribution")?;

            wheels.push((sdist_path, None));
        }

        Ok((wheels, metadata))
    }
}