rust-lang / cmake-rs

Rust build dependency for running cmake
https://docs.rs/cmake
Apache License 2.0
301 stars 121 forks source link

is `cmake` not installed? #118

Open joseph-henry opened 3 years ago

joseph-henry commented 3 years ago

Hello, I've tried to use the cmake crate as a dependency in my crate and while it works locally it fails when I add my crate as a dependency (pulled from crates.io) in a test program:

Updating crates.io index
   Compiling my_lib v0.1.0
error: failed to run custom build command for `my_lib v0.1.0`

Caused by:
  process didn't exit successfully: `./hello_world/target/debug/build/my_lib-8352397569f3c080/build-script-build` (exit code: 101)
  --- stdout
  running: "cmake" "/Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/my_lib-0.1.0/src/native" "-DENABLE_RUST=1" "-DCMAKE_INSTALL_PREFIX=target" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64 -arch x86_64" "-DCMAKE_C_COMPILER=/usr/bin/cc" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64 -arch x86_64" "-DCMAKE_CXX_COMPILER=/usr/bin/c++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64 -arch x86_64" "-DCMAKE_ASM_COMPILER=/usr/bin/cc" "-DCMAKE_BUILD_TYPE=Debug"

  --- stderr
  thread 'main' panicked at '
  failed to execute command: No such file or directory (os error 2)
  is `cmake` not installed?

  build script failed, must exit now', /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.45/src/lib.rs:894:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

With RUST_BACKTRACE=full:

       0:        0x10aa098fe - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h04abbaabf148650a
     1:        0x10aa44f4e - core::fmt::write::h14dac7cadec1cc70
     2:        0x10aa08e1a - std::io::Write::write_fmt::hfaf2e10dfdcc61d8
     3:        0x10aa25339 - std::panicking::default_hook::{{closure}}::h350fee0bf60f2674
     4:        0x10aa24eab - std::panicking::default_hook::h0b4e3bc46e6dcb8d
     5:        0x10aa258ca - std::panicking::rust_panic_with_hook::h8cdc0a575f4a5a7b
     6:        0x10aa0a0c5 - std::panicking::begin_panic_handler::{{closure}}::h7a7b30fd1c313876
     7:        0x10aa09a48 - std::sys_common::backtrace::__rust_end_short_backtrace::h2e099be83c81509d
     8:        0x10aa25443 - _rust_begin_unwind
     9:        0x10aa4e37b - std::panicking::begin_panic_fmt::h170f888f0234d849
    10:        0x10a4ab797 - cmake::fail::hcc02c6703578abbb
                                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.45/src/lib.rs:894:5
    11:        0x10a4ab01e - cmake::run::hbff0b8ae6b19dbe5
                                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.45/src/lib.rs:864:13
    12:        0x10a4a7b12 - cmake::Config::build::h0f5bbc869444f990
                                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.45/src/lib.rs:707:13
    13:        0x10a4a2333 - build_script_build::main::h2b88eca7a261b7c1
                                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/my_lib-0.1.0/build.rs:8:5
    14:        0x10a4a333e - core::ops::function::FnOnce::call_once::h12e0ebb75b1fb6b6
                                 at /private/tmp/rust-20210325-88347-hh2ixd/rustc-1.51.0-src/library/core/src/ops/function.rs:227:5
    15:        0x10a4a3231 - std::sys_common::backtrace::__rust_begin_short_backtrace::h47609506cf3640de
                                 at /private/tmp/rust-20210325-88347-hh2ixd/rustc-1.51.0-src/library/std/src/sys_common/backtrace.rs:125:18
    16:        0x10a4a4b24 - std::rt::lang_start::{{closure}}::h0ceb54217ca0fbdd
                                 at /private/tmp/rust-20210325-88347-hh2ixd/rustc-1.51.0-src/library/std/src/rt.rs:66:18
    17:        0x10aa28a00 - std::rt::lang_start_internal::hf4b96bfc8c02c8b0
    18:        0x10a4a4b01 - std::rt::lang_start::h454b772cdebf3322
                                 at /private/tmp/rust-20210325-88347-hh2ixd/rustc-1.51.0-src/library/std/src/rt.rs:65:5
    19:        0x10a4a27a2 - _main

My crate's build.rs:

   extern crate bindgen;

use cmake::Config;
use std::env;
use std::path::PathBuf;

fn main() {
    Config::new("src/native").build_target("my_lib-static").define("ENABLE_RUST", "1").out_dir("target").build();

    println!("cargo:rustc-link-search=target/build/lib");
    println!("cargo:rustc-link-lib=static=my_lib");

    // See here for reasoning: https://flames-of-code.netlify.app/blog/rust-and-cmake-cplusplus/

    let target = env::var("TARGET").unwrap();
    if target.contains("apple") {
        println!("cargo:rustc-link-lib=dylib=c++");
    } else if target.contains("linux") {
        println!("cargo:rustc-link-lib=dylib=stdc++");
    } else {
        unimplemented!();
    }

    let bindings = bindgen::Builder::default()
        .header("src/native/include/my_lib.h")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("my_lib.rs"))
        .expect("Couldn't write bindings!");
}

Any advice on how I should proceed?

joseph-henry commented 3 years ago

A clue:

If I replace the dependency on my published crate in the test app from:

[dependencies]
my_lib = "0.1.0"

to

[dependencies]
my_lib = { path = "/my/local/crate/path" }

Everything works, compiling my crate will no longer complain about a missing cmake.

If this didn't work at all it would make more sense, but somehow pulling a crate and building it is different than building the original local copy?

alexcrichton commented 3 years ago

Are you sure cmake is in your PATH when the build script is executed? That's basically what that error message is saying.

NOBLES5E commented 3 years ago

Also encountered this. cmake is in /usr/bin, and the directory is in PATH

NOBLES5E commented 3 years ago

It turns out some files in the cmake argument are missing. The "is cmake not installed?" error message is actually misleading in this case.

fubupc commented 2 years ago

Same problem here. And I found that if comment out Config::out_dir() then everything would be OK.

boydjohnson commented 2 years ago

So I have found that using out_dir works, if the directory passed to out_dir exists, and fails with the "is cmake not installed" error message otherwise.

So for the OP, in the build script change "target", passed to out_dir, to an absolute path, and use std::fs::create_dir_all to make sure the directory exists.


Oh, rereading what https://github.com/rust-lang/cmake-rs/issues/118#issuecomment-865958965 this comment says, I am echoing @NOBLES5E, but providing more information.

boydjohnson commented 2 years ago

Providing more information, and a fix. https://github.com/rust-lang/cmake-rs/blob/00e6b220342a8b0ec4548071928ade38fd5f691b/src/lib.rs#L498 silently ignores an Err, Result if OUT_DIR or parents do not exist.

https://github.com/rust-lang/cmake-rs/blob/00e6b220342a8b0ec4548071928ade38fd5f691b/src/lib.rs#L524 a non-existent directory is passed to Command current_dir, which finally errors in run https://github.com/rust-lang/cmake-rs/blob/00e6b220342a8b0ec4548071928ade38fd5f691b/src/lib.rs#L1019 printing "is cmake not installed".

I think the thing to do is use create_dir_all to make sure the OUT_DIR parents exists. I'll have a PR up shortly.

nazar-pc commented 8 months ago

I just got this error in GitHub Actions on Windows:

``` error: failed to run custom build command for `hwlocality-sys v0.2.0 (https://github.com/HadrienG2/hwlocality?rev=3141847b0a463f38adcf623a2d720931757a38ae#3141847b)` Caused by: process didn't exit successfully: `C:\actions-runner\_work\subspace\subspace\target\debug\build\hwlocality-sys-a6c49baecb681a6b\build-script-build` (exit code: 101) --- stdout CMAKE_TOOLCHAIN_FILE_x86_64-pc-windows-msvc = None CMAKE_TOOLCHAIN_FILE_x86_64_pc_windows_msvc = None HOST_CMAKE_TOOLCHAIN_FILE = None CMAKE_TOOLCHAIN_FILE = None CMAKE_GENERATOR_x86_64-pc-windows-msvc = None CMAKE_GENERATOR_x86_64_pc_windows_msvc = None HOST_CMAKE_GENERATOR = None CMAKE_GENERATOR = None CMAKE_PREFIX_PATH_x86_64-pc-windows-msvc = None CMAKE_PREFIX_PATH_x86_64_pc_windows_msvc = None HOST_CMAKE_PREFIX_PATH = None CMAKE_PREFIX_PATH = None CMAKE_x86_64-pc-windows-msvc = None CMAKE_x86_64_pc_windows_msvc = None HOST_CMAKE = None CMAKE = None running: "cmake" "C:\\actions-runner\\_work\\subspace\\subspace\\target\\debug\\build\\hwlocality-sys-8b8a577ac3eb1c10\\out\\hwloc\\contrib\\windows-cmake" "-G" "Visual Studio 17 2022" "-Thost=x64" "-Ax64" "-DCMAKE_INSTALL_PREFIX=C:\\actions-runner\\_work\\subspace\\subspace\\target\\debug\\build\\hwlocality-sys-8b8a577ac3eb1c10\\out" "-DCMAKE_C_FLAGS= -nologo -MD -Brepro" "-DCMAKE_C_FLAGS_DEBUG= -nologo -MD -Brepro" "-DCMAKE_CXX_FLAGS= -nologo -MD -Brepro" "-DCMAKE_CXX_FLAGS_DEBUG= -nologo -MD -Brepro" "-DCMAKE_ASM_FLAGS= -nologo -MD -Brepro" "-DCMAKE_ASM_FLAGS_DEBUG= -nologo -MD -Brepro" "-DCMAKE_BUILD_TYPE=Debug" --- stderr thread 'main' panicked at C:\Users\Administrator\.cargo\registry\src\index.crates.io-6f17d22bba15001f\cmake-0.1.50\src\lib.rs:1098:5: failed to execute command: program not found is `cmake` not installed? ```

The strange thing is that according to https://github.com/actions/runner-images/blob/main/images/windows/Windows2022-Readme.md official Windows server 2022 runners do have CMake installed on them.

UPD: My apologies, it was a custom GitHub Actions runner.

boydjohnson commented 8 months ago

UPD: My apologies, it was a custom GitHub Actions runner. @nazar-pc did you have success with the windows-2022 runner?

nazar-pc commented 8 months ago

@nazar-pc did you have success with the windows-2022 runner?

Yes, with both official GitHub runner and self-hosted

XieJiSS commented 4 months ago

Encountered the same issue. My workaround is:

// build.rs
use std::env;

// irrelevant - for better error message
extern crate miette;

fn main() -> miette::Result<()> {
  env::set_var("CMAKE_aarch64_apple_darwin", "/opt/homebrew/bin/cmake");  // <-- add this line
  // ...
}

Basically, this workaround allows cmake::Config::cmake_executable to use a user-defined executable as cmake, and by specifying the full path to cmake, I managed to get rid of the is cmake not installed? error. The source code tells us to use a format of CMAKE_{target.replace('-', "_")}, so one can run this shell command to get a target triplet for cmake:

# see-also: https://wiki.archlinux.org/title/Rust_package_guidelines
echo CMAKE_$(rustc -vV | sed -n 's/host: //p' | sed -e 's/-/_/g')

Side note:

thewh1teagle commented 1 month ago

Are you sure cmake is in your PATH when the build script is executed? That's basically what that error message is saying.

I still encounter this issue on macOS 14.5 aarch64 with rustc 1.79.0 (129f3b996 2024-06-10) The only cmake in my system is the dmg installed one from https://cmake.org/download/

cmake was indeed in the PATH. but it didn't found it. The fix I needed to do:

sudo ln -s /Applications/CMake.app/Contents/bin/cmake /usr/local/bin/cmake