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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/*!
Defining and manipulating binaries embedding Python.
*/

use anyhow::Result;
use slog::warn;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tempdir::TempDir;

use super::config::EmbeddedPythonConfig;
use super::distribution::{ExtensionModuleFilter, ParsedPythonDistribution};
use super::embedded_resource::EmbeddedPythonResourcesPrePackaged;
use super::libpython::{derive_importlib, link_libpython, ImportlibData};
use super::pyembed::{derive_python_config, write_data_rs};

/// A self-contained Python executable before it is compiled.
#[derive(Debug)]
pub struct PreBuiltPythonExecutable {
    pub name: String,
    pub distribution: Arc<ParsedPythonDistribution>,
    pub resources: EmbeddedPythonResourcesPrePackaged,
    pub config: EmbeddedPythonConfig,
}

impl PreBuiltPythonExecutable {
    /// Create an instance from a Python distribution, using settings.
    #[allow(clippy::too_many_arguments)]
    pub fn from_python_distribution(
        logger: &slog::Logger,
        distribution: Arc<ParsedPythonDistribution>,
        name: &str,
        config: &EmbeddedPythonConfig,
        extension_module_filter: &ExtensionModuleFilter,
        preferred_extension_module_variants: Option<HashMap<String, String>>,
        include_sources: bool,
        include_resources: bool,
        include_test: bool,
    ) -> Result<Self> {
        let mut resources = EmbeddedPythonResourcesPrePackaged::from_distribution(
            logger,
            distribution.clone(),
            extension_module_filter,
            preferred_extension_module_variants,
            include_sources,
            include_resources,
            include_test,
        )?;

        // Always ensure minimal extension modules are present, otherwise we get
        // missing symbol errors at link time.
        for ext in
            distribution.filter_extension_modules(&logger, &ExtensionModuleFilter::Minimal, None)?
        {
            if !resources.extension_modules.contains_key(&ext.module) {
                resources.add_extension_module(&ext);
            }
        }

        Ok(PreBuiltPythonExecutable {
            name: name.to_string(),
            distribution,
            resources,
            config: config.clone(),
        })
    }

    /// Build a Python library suitable for linking.
    ///
    /// This will take the underlying distribution, resources, and
    /// configuration and produce a new executable binary.
    pub fn build_libpython(
        &self,
        logger: &slog::Logger,
        host: &str,
        target: &str,
        opt_level: &str,
    ) -> Result<PythonLibrary> {
        let resources = self
            .resources
            .package(logger, &self.distribution.python_exe)?;

        let temp_dir = TempDir::new("pyoxidizer-build-exe")?;
        let temp_dir_path = temp_dir.path();

        warn!(
            logger,
            "generating custom link library containing Python..."
        );
        let library_info = link_libpython(
            logger,
            &self.distribution,
            &resources,
            &temp_dir_path,
            host,
            target,
            opt_level,
        )?;

        let mut cargo_metadata: Vec<String> = Vec::new();
        cargo_metadata.extend(library_info.cargo_metadata);

        let libpython_data = std::fs::read(&library_info.libpython_path)?;
        let libpyembeddedconfig_data = std::fs::read(&library_info.libpyembeddedconfig_path)?;

        Ok(PythonLibrary {
            libpython_filename: PathBuf::from(library_info.libpython_path.file_name().unwrap()),
            libpython_data,
            libpyembeddedconfig_filename: PathBuf::from(
                library_info.libpyembeddedconfig_path.file_name().unwrap(),
            ),
            libpyembeddedconfig_data,
            cargo_metadata,
        })
    }

    /// Generate data embedded in binaries representing Python resource data.
    pub fn build_embedded_blobs(&self, logger: &slog::Logger) -> Result<EmbeddedResourcesBlobs> {
        let embedded_resources = self
            .resources
            .package(logger, &self.distribution.python_exe)?;

        let mut module_names = Vec::new();
        let mut modules = Vec::new();
        let mut resources = Vec::new();

        embedded_resources.write_blobs(&mut module_names, &mut modules, &mut resources);

        Ok(EmbeddedResourcesBlobs {
            module_names,
            modules,
            resources,
        })
    }
}

/// A self-contained Python executable after it is built.
pub struct PythonLibrary {
    pub libpython_filename: PathBuf,
    pub libpython_data: Vec<u8>,
    pub libpyembeddedconfig_filename: PathBuf,
    pub libpyembeddedconfig_data: Vec<u8>,
    pub cargo_metadata: Vec<String>,
}

/// Represents serialized data embedded in binaries for loading Python resources.
pub struct EmbeddedResourcesBlobs {
    pub module_names: Vec<u8>,
    pub modules: Vec<u8>,
    pub resources: Vec<u8>,
}

/// Holds filesystem paths to resources required to build a binary embedding Python.
pub struct EmbeddedPythonBinaryPaths {
    pub importlib_bootstrap: PathBuf,
    pub importlib_bootstrap_external: PathBuf,
    pub module_names: PathBuf,
    pub py_modules: PathBuf,
    pub resources: PathBuf,
    pub libpython: PathBuf,
    pub libpyembeddedconfig: PathBuf,
    pub config_rs: PathBuf,
    pub cargo_metadata: PathBuf,
}

/// Represents resources to embed Python in a binary.
pub struct EmbeddedPythonBinaryData {
    pub config: EmbeddedPythonConfig,
    pub library: PythonLibrary,
    pub importlib: ImportlibData,
    pub resources: EmbeddedResourcesBlobs,
    pub host: String,
    pub target: String,
}

impl EmbeddedPythonBinaryData {
    pub fn from_pre_built_python_executable(
        exe: &PreBuiltPythonExecutable,
        logger: &slog::Logger,
        host: &str,
        target: &str,
        opt_level: &str,
    ) -> Result<EmbeddedPythonBinaryData> {
        let library = exe.build_libpython(logger, host, target, opt_level)?;
        let resources = exe.build_embedded_blobs(logger)?;
        warn!(
            logger,
            "deriving custom importlib modules to support in-memory importing"
        );
        let importlib = derive_importlib(&exe.distribution)?;

        Ok(EmbeddedPythonBinaryData {
            config: exe.config.clone(),
            library,
            importlib,
            resources,
            host: host.to_string(),
            target: target.to_string(),
        })
    }

    /// Write out files needed to link a binary.
    pub fn write_files(&self, dest_dir: &Path) -> Result<EmbeddedPythonBinaryPaths> {
        let importlib_bootstrap = dest_dir.join("importlib_bootstrap");
        let mut fh = File::create(&importlib_bootstrap)?;
        fh.write_all(&self.importlib.bootstrap_bytecode)?;

        let importlib_bootstrap_external = dest_dir.join("importlib_bootstrap_external");
        let mut fh = File::create(&importlib_bootstrap_external)?;
        fh.write_all(&self.importlib.bootstrap_external_bytecode)?;

        let module_names = dest_dir.join("py-module-names");
        let mut fh = File::create(&module_names)?;
        fh.write_all(&self.resources.module_names)?;

        let py_modules = dest_dir.join("py-modules");
        let mut fh = File::create(&py_modules)?;
        fh.write_all(&self.resources.modules)?;

        let resources = dest_dir.join("python-resources");
        let mut fh = File::create(&resources)?;
        fh.write_all(&self.resources.resources)?;

        let libpython = dest_dir.join(&self.library.libpython_filename);
        let mut fh = File::create(&libpython)?;
        fh.write_all(&self.library.libpython_data)?;

        let libpyembeddedconfig = dest_dir.join(&self.library.libpyembeddedconfig_filename);
        let mut fh = File::create(&libpyembeddedconfig)?;
        fh.write_all(&self.library.libpyembeddedconfig_data)?;

        let config_rs_data = derive_python_config(
            &self.config,
            &importlib_bootstrap,
            &importlib_bootstrap_external,
            &py_modules,
            &resources,
        );
        let config_rs = dest_dir.join("data.rs");
        write_data_rs(&config_rs, &config_rs_data)?;

        let mut cargo_metadata_lines = Vec::new();
        cargo_metadata_lines.extend(self.library.cargo_metadata.clone());

        // Tell Cargo where libpythonXY is located.
        cargo_metadata_lines.push(format!(
            "cargo:rustc-link-search=native={}",
            dest_dir.display()
        ));

        // Give pyembed the path to the config file.
        cargo_metadata_lines.push(format!(
            "cargo:rustc-env=PYEMBED_DATA_RS_PATH={}",
            config_rs.display()
        ));

        let cargo_metadata = dest_dir.join("cargo_metadata.txt");
        let mut fh = File::create(&cargo_metadata)?;
        fh.write_all(cargo_metadata_lines.join("\n").as_bytes())?;

        Ok(EmbeddedPythonBinaryPaths {
            importlib_bootstrap,
            importlib_bootstrap_external,
            module_names,
            py_modules,
            resources,
            libpython,
            libpyembeddedconfig,
            config_rs,
            cargo_metadata,
        })
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::py_packaging::distribution::ExtensionModuleFilter;
    use crate::testutil::*;

    pub fn get_prebuilt(logger: &slog::Logger) -> Result<PreBuiltPythonExecutable> {
        let distribution = get_default_distribution()?;
        let mut resources = EmbeddedPythonResourcesPrePackaged::default();

        // We need to add minimal extension modules so builds actually work. If they are missing,
        // we'll get missing symbol errors during linking.
        for ext in
            distribution.filter_extension_modules(logger, &ExtensionModuleFilter::Minimal, None)?
        {
            resources.add_extension_module(&ext);
        }

        let config = EmbeddedPythonConfig::default();

        Ok(PreBuiltPythonExecutable {
            name: "testapp".to_string(),
            distribution,
            resources,
            config,
        })
    }

    pub fn get_embedded(logger: &slog::Logger) -> Result<EmbeddedPythonBinaryData> {
        EmbeddedPythonBinaryData::from_pre_built_python_executable(
            &get_prebuilt(logger)?,
            &get_logger()?,
            env!("HOST"),
            env!("HOST"),
            "0",
        )
    }

    #[test]
    fn test_write_embedded_files() -> Result<()> {
        let logger = get_logger()?;
        let embedded = get_embedded(&logger)?;
        let temp_dir = tempdir::TempDir::new("pyoxidizer-test")?;

        embedded.write_files(temp_dir.path())?;

        Ok(())
    }
}