Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hanna-kruppe opened this issue Jun 5, 2024 · 2 comments · Fixed by #1087 or #1102
Closed

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

hanna-kruppe opened this issue Jun 5, 2024 · 2 comments · Fixed by #1087 or #1102

Comments

@hanna-kruppe
Copy link
Contributor

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
Copy link
Collaborator

NobodyXu commented Jun 6, 2024

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:

std::fs::create_dir_all(&out_dir).map_err(|err| Error {

I noticed that

  • it doesn't flush the data
  • it doesn't close the file. IIRC windows sometimes require the file to be closed before it can be used?

@hanna-kruppe
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants