view rust/hgcli/build.rs @ 43271:99394e6c5d12

rust-dirstate-status: add first Rust implementation of `dirstate.status` Note: This patch also added the rayon crate as a Cargo dependency. It will help us immensely in making Rust code parallel and easy to maintain. It is a stable, well-known, and supported crate maintained by people on the Rust team. The current `dirstate.status` method has grown over the years through bug reports and new features to the point where it got too big and too complex. This series does not yet improve the logic, but adds a Rust fast-path to speed up certain cases. Tested on mozilla-try-2019-02-18 with zstd compression: - `hg diff` on an empty working copy: - c: 1.64(+-)0.04s - rust+c before this change: 2.84(+-)0.1s - rust+c: 849(+-)40ms - `hg commit` when creating a file: - c: 5.960s - rust+c before this change: 5.828s - rust+c: 4.668s - `hg commit` when updating a file: - c: 4.866s - rust+c before this change: 4.371s - rust+c: 3.855s - `hg status -mard` - c: 1.82(+-)0.04s - rust+c before this change: 2.64(+-)0.1s - rust+c: 896(+-)30ms The numbers are clear: the current Rust `dirstatemap` implementation is super slow, its performance needs to be addressed. This will be done in a future series, immediately after this one, with the goal of getting Rust to be at least to the speed of the Python + C implementation in all cases before the 5.2 freeze. At worse, we gate dirstatemap to only be used in those cases. Cases where the fast-path is not executed: - for commands that need ignore support (`status`, for example) - if subrepos are found (should not be hard to add, but winter is coming) - any other matcher than an `alwaysmatcher`, like patterns, etc. - with extensions like `sparse` and `fsmonitor` The next step after this is to rethink the logic to be closer to Jane Street's Valentin Gatien-Baron's Rust fast-path which does a lot less work when possible. Differential Revision: https://phab.mercurial-scm.org/D7058
author Raphaël Gomès <rgomes@octobus.net>
date Fri, 11 Oct 2019 13:39:57 +0200
parents 24b5106e3e1e
children ce088b38f92b
line wrap: on
line source

// build.rs -- Configure build environment for `hgcli` Rust package.
//
// Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.

use std::collections::HashMap;
use std::env;
use std::path::Path;
use std::process::Command;

struct PythonConfig {
    python: String,
    config: HashMap<String, String>,
}

fn get_python_config() -> PythonConfig {
    // The python27-sys crate exports a Cargo variable defining the full
    // path to the interpreter being used.
    let python = env::var("DEP_PYTHON27_PYTHON_INTERPRETER").expect(
        "Missing DEP_PYTHON27_PYTHON_INTERPRETER; bad python27-sys crate?",
    );

    if !Path::new(&python).exists() {
        panic!(
            "Python interpreter {} does not exist; this should never happen",
            python
        );
    }

    // This is a bit hacky but it gets the job done.
    let separator = "SEPARATOR STRING";

    let script = "import sysconfig; \
c = sysconfig.get_config_vars(); \
print('SEPARATOR STRING'.join('%s=%s' % i for i in c.items()))";

    let mut command = Command::new(&python);
    command.arg("-c").arg(script);

    let out = command.output().unwrap();

    if !out.status.success() {
        panic!(
            "python script failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    let stdout = String::from_utf8_lossy(&out.stdout);
    let mut m = HashMap::new();

    for entry in stdout.split(separator) {
        let mut parts = entry.splitn(2, "=");
        let key = parts.next().unwrap();
        let value = parts.next().unwrap();
        m.insert(String::from(key), String::from(value));
    }

    PythonConfig {
        python: python,
        config: m,
    }
}

#[cfg(not(target_os = "windows"))]
fn have_shared(config: &PythonConfig) -> bool {
    match config.config.get("Py_ENABLE_SHARED") {
        Some(value) => value == "1",
        None => false,
    }
}

#[cfg(target_os = "windows")]
fn have_shared(config: &PythonConfig) -> bool {
    use std::path::PathBuf;

    // python27.dll should exist next to python2.7.exe.
    let mut dll = PathBuf::from(&config.python);
    dll.pop();
    dll.push("python27.dll");

    return dll.exists();
}

const REQUIRED_CONFIG_FLAGS: [&str; 2] = ["Py_USING_UNICODE", "WITH_THREAD"];

fn main() {
    let config = get_python_config();

    println!("Using Python: {}", config.python);
    println!("cargo:rustc-env=PYTHON_INTERPRETER={}", config.python);

    let prefix = config.config.get("prefix").unwrap();

    println!("Prefix: {}", prefix);

    // TODO Windows builds don't expose these config flags. Figure out another
    // way.
    #[cfg(not(target_os = "windows"))]
    for key in REQUIRED_CONFIG_FLAGS.iter() {
        let result = match config.config.get(*key) {
            Some(value) => value == "1",
            None => false,
        };

        if !result {
            panic!("Detected Python requires feature {}", key);
        }
    }

    // We need a Python shared library.
    if !have_shared(&config) {
        panic!("Detected Python lacks a shared library, which is required");
    }

    let ucs4 = match config.config.get("Py_UNICODE_SIZE") {
        Some(value) => value == "4",
        None => false,
    };

    if !ucs4 {
        #[cfg(not(target_os = "windows"))]
        panic!("Detected Python doesn't support UCS-4 code points");
    }
}