rust-lang / cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
https://docs.rs/cc
Apache License 2.0
1.82k stars 440 forks source link

Error: `ar` no such file found #1093

Closed wong-justin closed 3 months ago

wong-justin commented 3 months ago

I've been trying to build a vendored C library with cc, but I get this error message:

warning: test-cc@0.1.0: ar: '/home/runner/work/test-cc-glib/test-cc-glib/target/debug/build/test-cc-d41d4b076b508ca8/out/libglib.a': No such file

error: failed to run custom build command for `test-cc v0.1.0 (/home/runner/work/test-cc-glib/test-cc-glib)`

Caused by:
  process didn't exit successfully: `/home/runner/work/test-cc-glib/test-cc-glib/target/debug/build/test-cc-2f258154f2fe7cf4/build-script-build` (exit status: 1)
  --- stdout
  TARGET = Some("x86_64-unknown-linux-gnu")
  HOST = Some("x86_64-unknown-linux-gnu")
  cargo:rerun-if-env-changed=AR_x86_64-unknown-linux-gnu
  AR_x86_64-unknown-linux-gnu = None
  cargo:rerun-if-env-changed=AR_x86_64_unknown_linux_gnu
  AR_x86_64_unknown_linux_gnu = None
  cargo:rerun-if-env-changed=HOST_AR
  HOST_AR = None
  cargo:rerun-if-env-changed=AR
  AR = None
  cargo:rerun-if-env-changed=ARFLAGS_x86_64-unknown-linux-gnu
  ARFLAGS_x86_64-unknown-linux-gnu = None
  cargo:rerun-if-env-changed=ARFLAGS_x86_64_unknown_linux_gnu
  ARFLAGS_x86_64_unknown_linux_gnu = None
  cargo:rerun-if-env-changed=HOST_ARFLAGS
  HOST_ARFLAGS = None
  cargo:rerun-if-env-changed=ARFLAGS
  ARFLAGS = None
  cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
  cargo:warning=ar: '/home/runner/work/test-cc-glib/test-cc-glib/target/debug/build/test-cc-d41d4b076b508ca8/out/libglib.a': No such file

  --- stderr

  error occurred: Command "ar" "s" "/home/runner/work/test-cc-glib/test-cc-glib/target/debug/build/test-cc-d41d4b076b508ca8/out/libglib.a" with args ar did not execute successfully (status code exit status: 1).

I've been following this guide hoping that "just giving a list of .c files to the cc crate" would be sufficient. I expected the C library to finish compiling successfully, but I guess something fails around the archive step. Maybe I'm missing something unrelated to cc?

I reproduced a minimal example of the repo along with a build action.

build.rs:

fn main() {
    let source_c_files = std::fs::read_dir("vendor/glib")
        .unwrap()
        .filter_map(|entry| {
            let path = &entry.expect("couldn't unwrap entry inside vendor/").path();
            let pathstr = path.to_str().expect("couldn't convert vendored .c path to string");
            match path.is_file() 
                & path.ends_with(".c") 
            {
                true => Some(String::from(pathstr)),
                false => None
            }
        });

    // this step currently fails: `ar` cannot find libglib.a
    cc::Build::new()
        .files(source_c_files)
        .compile("glib");
}

Any reasons why the ar command would fail here? Thanks for your help.

NobodyXu commented 3 months ago

It seems like the source_c_files is empty?

From the warning message, it seems the source files are empty

wong-justin commented 3 months ago

🤦‍♂️ yep that's it, thanks.