rust/hg-core/build.rs
author Raphaël Gomès <rgomes@octobus.net>
Thu, 16 Apr 2020 17:31:11 +0200
changeset 44713 97c10b157665
parent 44348 d8d4fa9a7f18
permissions -rw-r--r--
rust: add option of static linking a local Re2 install Previously, only dynamically linking the system-wide install was possible. We force the user to provide one to prevent hard-to-track errors. Differential Revision: https://phab.mercurial-scm.org/D8451
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44348
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// build.rs
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2020 Raphaël Gomès <rgomes@octobus.net>
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
#[cfg(feature = "with-re2")]
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
use cc;
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
44713
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    11
/// Uses either the system Re2 install as a dynamic library or the provided
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    12
/// build as a static library
44348
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
#[cfg(feature = "with-re2")]
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
fn compile_re2() {
44713
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    15
    use cc;
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    16
    use std::path::Path;
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    17
    use std::process::exit;
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    18
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    19
    let msg = r"HG_RE2_PATH must be one of `system|<path to build source clone of Re2>`";
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    20
    let re2 = match std::env::var_os("HG_RE2_PATH") {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    21
        None => {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    22
            eprintln!("{}", msg);
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    23
            exit(1)
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    24
        }
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    25
        Some(v) => {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    26
            if v == "system" {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    27
                None
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    28
            } else {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    29
                Some(v)
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    30
            }
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    31
        }
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    32
    };
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    33
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    34
    let mut options = cc::Build::new();
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    35
    options
44348
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
        .cpp(true)
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
        .flag("-std=c++11")
44713
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    38
        .file("src/re2/rust_re2.cpp");
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    39
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    40
    if let Some(ref source) = re2 {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    41
        options.include(Path::new(source));
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    42
    };
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    43
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    44
    options.compile("librustre.a");
44348
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
44713
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    46
    if let Some(ref source) = &re2 {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    47
        // Link the local source statically
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    48
        println!(
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    49
            "cargo:rustc-link-search=native={}",
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    50
            Path::new(source).join(Path::new("obj")).display()
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    51
        );
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    52
        println!("cargo:rustc-link-lib=static=re2");
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    53
    } else {
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    54
        println!("cargo:rustc-link-lib=re2");
97c10b157665 rust: add option of static linking a local Re2 install
Raphaël Gomès <rgomes@octobus.net>
parents: 44348
diff changeset
    55
    }
44348
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
}
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
fn main() {
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
    #[cfg(feature = "with-re2")]
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
    compile_re2();
d8d4fa9a7f18 rust-re2: add wrapper for calling Re2 from Rust
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
}