如何在 Rust 货物构建脚本上调用批处理构建器脚本?

How to call a batch builder script on a Rust cargo build script?

如何在 Rust 货物构建脚本上调用批处理构建器脚本?

项目 rust-onig requires to compile the oniguruma C project. This is originated from the question 试图在 windows 上构建 oniguruma。后来在 Visual Studio 2015 年 windows 上注意到,我们需要调用 make_win32.bat 而不是 cmake 来构建 oniguruma C 项目。

但是我不知道如何让 cargo 构建器调用 make_win32.bat 构建脚本。基于 build-script, I edited the default builder build.rs 试图使用 cmake 而不是 make_win32.bat:

extern crate pkg_config;
extern crate cmake;

// use std::env;

// fn compile_with_cmake() {
//     use cmake::Config;

//     let static_link = env::var("CARGO_FEATURE_STATIC_ONIG").is_ok();

//     // Builds the project in the directory located in `oniguruma`, installing it
//     // into $OUT_DIR
//     let mut c = Config::new("oniguruma");

//     let dst = if static_link {
//                   c.define("BUILD_SHARED_LIBS", "OFF")
//               } else {
//                   c.define("CMAKE_MACOSX_RPATH", "NO")
//               }
//               .build();

//     let link_type = if static_link {
//         "static"
//     } else {
//         "dylib"
//     };

//     println!("cargo:rustc-link-search=native={}",
//              dst.join("build").display());
//     println!("cargo:rustc-link-lib={}=onig", link_type);
// }

pub fn main() {

    println!("Opening MAIN!!!!!!!!\n\n\n\n");
    use std::process::Command;

    let status = Command::new("D:\User\Downloads\rust-onig\onig_sys\oniguruma\make_win32.bat").status().unwrap_or_else(|e|
    {
        panic!("failed to execute process: {}", e)
    });

    println!("process exited with: {}", status);
    println!("process exited with: {}", status);
    println!("process exited with: {}", status);
    println!("process exited with: {}", status);
    println!("process exited with: {}", status);

    // if let Ok(_) = pkg_config::find_library("oniguruma") {
    //     return;
    // }

    // compile_with_cmake();
}

但是我的构建器脚本没有被调用,因为消息 process exited with 从未显示过:

D:\Downloads\rust-onig>cargo build
   Compiling onig_sys v61.1.0 (file:///D:/User/Downloads/rust-onig/onig_sys)
   Compiling onig v1.2.0 (file:///D:/User/Downloads/rust-onig)
warning: unused import: `c_ulong`, #[warn(unused_imports)] on by default
 --> src\names.rs:6:27
  |
6 | use libc::{c_int, c_uint, c_ulong, c_void, c_uchar};
  |                           ^^^^^^^

    Finished dev [unoptimized + debuginfo] target(s) in 11.31 secs

D:\Downloads\rust-onig>

显示什么,我们什么时候尝试使用cmake构建器可以在问题上查看这是源于问题

存在的问题cmake生成了几个Visual Studio项目,完全不知道如何构建oniguruma。能够有效构建它的唯一解决方案是 make_win32.bat,它已经包含在 oniguruma 中。此外,我们需要使用 make_win32.bat 而不是让 cmake 生成 Visual Studio.

我在 -vv 中调用了它,正如 user1034749 评论的那样,它输出正确并且 运行 make_win32.bat 文件。

我只需要在 make_win32.bat 中放入命令 cd D:\rust-onig\src 因为批处理文件 运行 在其他地方的某个文件夹中。

D:\rust-onig>cargo build -vv
       Fresh libc v0.2.21
       Fresh pkg-config v0.3.9
       Fresh lazy_static v0.2.4
       Fresh bitflags v0.7.0
   Compiling onig_sys v61.3.0 (file:///D:/User/Downloads/rust-onig/onig_sys)
     Running `D:\rust-onig\target\debug\build\onig_sys-a911e1c40e7ed561\build-script-build`
Opening MAIN!!!!!!!!





D:\rust-onig\onig_sys>cd /d D:\rust-onig\onig_sys\oniguruma\src

D:\rust-onig\onig_sys\oniguruma\src>copy config.h.win32 config.h
        1 file(s) copied.

D:\rust-onig\onig_sys\oniguruma\src>nmake -f Makefile.windows

Microsoft (R) Program Maintenance Utility Version 14.00.23506.0
Copyright (C) Microsoft Corporation.  All rights reserved.

process exited with: exit code: 0
process exited with: exit code: 0
process exited with: exit code: 0
process exited with: exit code: 0
process exited with: exit code: 0
     Running `rustc --crate-name onig_sys onig_sys\src\lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 --cfg "feature=\"static_onig\"" -C metadata=071c5dad786f940b -C extra-filename=-071c5dad786f940b --out-dir D:\rust-onig\target\debug\deps -L dependency=D:\rust-onig\target\debug\deps --extern libc=D:\rust-onig\target\debug\deps\liblibc-5dc7b85e748840b4.rlib`
   Compiling onig v1.2.2 (file:///D:/User/Downloads/rust-onig)
     Running `rustc --crate-name onig src\lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=9cad49cfdc2b5108 -C extra-filename=-9cad49cfdc2b5108 --out-dir D:\rust-onig\target\debug\deps -L dependency=D:\rust-onig\target\debug\deps --extern libc=D:\rust-onig\target\debug\deps\liblibc-5dc7b85e748840b4.rlib --extern lazy_static=D:\rust-onig\target\debug\deps\liblazy_static-82c2efae9a7bf732.rlib --extern onig_sys=D:\rust-onig\target\debug\deps\libonig_sys-071c5dad786f940b.rlib --extern bitflags=D:\rust-onig\target\debug\deps\libbitflags-65ddff5d2b91509e.rlib`
    Finished dev [unoptimized + debuginfo] target(s) in 8.92 secs

更新

rust-onig developer had added built-in supported to call nmake directly instead of using a batch file: