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.76k stars 425 forks source link

Compiler family detection failed - cl.exe "cannot open source file" #1082

Closed hanna-kruppe closed 4 weeks ago

hanna-kruppe commented 1 month ago

With cc version 1.0.98, on a Windows machine with Visual Studio 2022, I get this warning whenever compiler family detection runs (due to my use of flag_if_supported, it seems):

warning: Compiler family detection failed due to error: ToolExecError: Command "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe" "-E" "C:\Users\Hanna\develop\testcc\target\debug\build\testcc-12ad717fb85fc69b\out\2340633798679377677detect_compiler_family.c" with args C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe did not execute successfully (status code exit code: 2).

The full build script output with CC_ENABLE_DEBUG_OUTPUT=1 is:

``` [testcc 0.1.0] TARGET = Some("x86_64-pc-windows-msvc") [testcc 0.1.0] OPT_LEVEL = Some("0") [testcc 0.1.0] HOST = Some("x86_64-pc-windows-msvc") [testcc 0.1.0] cargo:rerun-if-env-changed=CC_x86_64-pc-windows-msvc [testcc 0.1.0] CC_x86_64-pc-windows-msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=CC_x86_64_pc_windows_msvc [testcc 0.1.0] CC_x86_64_pc_windows_msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=HOST_CC [testcc 0.1.0] HOST_CC = None [testcc 0.1.0] cargo:rerun-if-env-changed=CC [testcc 0.1.0] CC = None [testcc 0.1.0] cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS [testcc 0.1.0] CRATE_CC_NO_DEFAULTS = None [testcc 0.1.0] CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2") [testcc 0.1.0] DEBUG = Some("true") [testcc 0.1.0] cargo:rerun-if-env-changed=CFLAGS_x86_64-pc-windows-msvc [testcc 0.1.0] CFLAGS_x86_64-pc-windows-msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=CFLAGS_x86_64_pc_windows_msvc [testcc 0.1.0] CFLAGS_x86_64_pc_windows_msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=HOST_CFLAGS [testcc 0.1.0] HOST_CFLAGS = None [testcc 0.1.0] cargo:rerun-if-env-changed=CFLAGS [testcc 0.1.0] CFLAGS = None [testcc 0.1.0] cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT [testcc 0.1.0] running: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.40.33807\\bin\\HostX64\\x64\\cl.exe" "-E" "C:\\Users\\Hanna\\develop\\testcc\\target\\debug\\build\\testcc-12ad717fb85fc69b\\out\\2340633798679377677detect_compiler_family.c" [testcc 0.1.0] cargo:warning=Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33811 for x64 [testcc 0.1.0] cargo:warning=Copyright (C) Microsoft Corporation. All rights reserved. [testcc 0.1.0] cargo:warning= [testcc 0.1.0] cargo:warning=2340633798679377677detect_compiler_family.c [testcc 0.1.0] cargo:warning=c1: fatal error C1083: Cannot open source file: 'C:\Users\Hanna\develop\testcc\target\debug\build\testcc-12ad717fb85fc69b\out\2340633798679377677detect_compiler_family.c': Permission denied [testcc 0.1.0] exit code: 2 [testcc 0.1.0] cargo:warning=Compiler family detection failed due to error: ToolExecError: Command "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.40.33807\\bin\\HostX64\\x64\\cl.exe" "-E" "C:\\Users\\Hanna\\develop\\testcc\\target\\debug\\build\\testcc-12ad717fb85fc69b\\out\\2340633798679377677detect_compiler_family.c" with args C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe did not execute successfully (status code exit code: 2). [testcc 0.1.0] cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS [testcc 0.1.0] CRATE_CC_NO_DEFAULTS = None [testcc 0.1.0] CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2") [testcc 0.1.0] cargo:rerun-if-env-changed=CFLAGS_x86_64-pc-windows-msvc [testcc 0.1.0] CFLAGS_x86_64-pc-windows-msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=CFLAGS_x86_64_pc_windows_msvc [testcc 0.1.0] CFLAGS_x86_64_pc_windows_msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=HOST_CFLAGS [testcc 0.1.0] HOST_CFLAGS = None [testcc 0.1.0] cargo:rerun-if-env-changed=CFLAGS [testcc 0.1.0] CFLAGS = None [testcc 0.1.0] cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT [testcc 0.1.0] running: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.40.33807\\bin\\HostX64\\x64\\cl.exe" "-nologo" "-MD" "-Z7" "-Brepro" "-W4" "-FoC:\\Users\\Hanna\\develop\\testcc\\target\\debug\\build\\testcc-12ad717fb85fc69b\\out\\d1fba762150c532c-blah.o" "-c" "blah.c" [testcc 0.1.0] blah.c [testcc 0.1.0] exit code: 0 [testcc 0.1.0] cargo:rerun-if-env-changed=AR_x86_64-pc-windows-msvc [testcc 0.1.0] AR_x86_64-pc-windows-msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=AR_x86_64_pc_windows_msvc [testcc 0.1.0] AR_x86_64_pc_windows_msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=HOST_AR [testcc 0.1.0] HOST_AR = None [testcc 0.1.0] cargo:rerun-if-env-changed=AR [testcc 0.1.0] AR = None [testcc 0.1.0] cargo:rerun-if-env-changed=ARFLAGS_x86_64-pc-windows-msvc [testcc 0.1.0] ARFLAGS_x86_64-pc-windows-msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=ARFLAGS_x86_64_pc_windows_msvc [testcc 0.1.0] ARFLAGS_x86_64_pc_windows_msvc = None [testcc 0.1.0] cargo:rerun-if-env-changed=HOST_ARFLAGS [testcc 0.1.0] HOST_ARFLAGS = None [testcc 0.1.0] cargo:rerun-if-env-changed=ARFLAGS [testcc 0.1.0] ARFLAGS = None [testcc 0.1.0] running: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.40.33807\\bin\\HostX64\\x64\\lib.exe" "-out:C:\\Users\\Hanna\\develop\\testcc\\target\\debug\\build\\testcc-12ad717fb85fc69b\\out\\libblah.a" "-nologo" "C:\\Users\\Hanna\\develop\\testcc\\target\\debug\\build\\testcc-12ad717fb85fc69b\\out\\d1fba762150c532c-blah.o" [testcc 0.1.0] exit code: 0 [testcc 0.1.0] cargo:rustc-link-search=native=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\atlmfc\lib\x64 [testcc 0.1.0] cargo:rustc-link-lib=static=blah [testcc 0.1.0] cargo:rustc-link-search=native=C:\Users\Hanna\develop\testcc\target\debug\build\testcc-12ad717fb85fc69b\out warning: Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33811 for x64 warning: Copyright (C) Microsoft Corporation. All rights reserved. warning: warning: 2340633798679377677detect_compiler_family.c warning: c1: fatal error C1083: Cannot open source file: 'C:\Users\Hanna\develop\testcc\target\debug\build\testcc-12ad717fb85fc69b\out\2340633798679377677detect_compiler_family.c': Permission denied warning: Compiler family detection failed due to error: ToolExecError: Command "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.40.33807\\bin\\HostX64\\x64\\cl.exe" "-E" "C:\\Users\\Hanna\\develop\\testcc\\target\\debug\\build\\testcc-12ad717fb85fc69b\\out\\2340633798679377677detect_compiler_family.c" with args C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe did not execute successfully (status code exit code: 2). ```

Note that (re-)creating detect_compiler_family.c manually at that exact path and running the exact cl.exe command on it works just fine. So there must be something wrong with how the tempfile is created during build script execution.

NobodyXu commented 1 month ago

Tempfile is created using https://github.com/rust-lang/cc-rs/blob/main/src/tempfile.rs

We already make sure the directories are created before creating the file:

https://github.com/rust-lang/cc-rs/blob/3ba23569a623074748a3030f382afd22483555df/src/tool.rs#L119

I noticed that

hanna-kruppe commented 4 weeks ago

Closing the file seems to fix it, good call. Flushing with File::sync_all doesn't seem to make a difference. I created a patch at #1087.