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

failed recompaction: Permission denied #2493

Open
Selviniahh opened this issue Sep 5, 2024 · 3 comments
Open

failed recompaction: Permission denied #2493

Selviniahh opened this issue Sep 5, 2024 · 3 comments

Comments

@Selviniahh
Copy link

Selviniahh commented Sep 5, 2024

I only get this error failed recompaction: Permission denied if I want to force running cmake twice. Here's my minimal cmake file and the minimal project itself

untitled.zip

project(untitled)
set(CMAKE_CXX_STANDARD 20)

# Increment counter
file(READ "${CMAKE_SOURCE_DIR}/Counter.txt" COUNTER)
MATH(EXPR COUNTER "${COUNTER}+1")
file(WRITE "${CMAKE_SOURCE_DIR}/Counter.txt" "${COUNTER}")

add_executable(untitled main.cpp)
# Create a custom target to run CMake again
add_custom_command(TARGET untitled POST_BUILD
        COMMAND cmake CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} CMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
        CMAKE_C_COMPILER=${CMAKE_C_COMPILER} CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -G "${CMAKE_GENERATOR}"
        -S ${CMAKE_SOURCE_DIR} -B ${CMAKE_BINARY_DIR})

message(${CMAKE_COMMAND})

ninja's output with -v note that, with MinGW and native ninja on windows, it's same.

FAILED: untitled.exe 
C:\WINDOWS\system32\cmd.exe /C "cd . && C:\msys64\clang64\bin\clang++.exe -g  CMakeFiles/untitled.dir/main.cpp.obj -o untitled.exe -Wl,--out-implib,libuntitled.dll.a -Wl,--major-image-version,0,--minor-image-version,0  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && C:\WINDOWS\system32\cmd.exe /C "cd /D C:\Users\xqwys\CLionProjects\untitled\cmake-build-debug-mingw-clang && C:\Users\xqwys\AppData\Local\Programs\CLion\bin\cmake\win\x64\bin\cmake.exe -G Ninja -S C:/Users/xqwys/CLionProjects/untitled -B C:/Users/xqwys/CLionProjects/untitled/cmake-build-debug-mingw-clang""
C:/Users/xqwys/AppData/Local/Programs/CLion/bin/cmake/win/x64/bin/cmake.exe
-- Configuring done (0.2s)
-- Generating done (0.0s)
CMake Error:
  Running

   'C:/msys64/mingw64/bin/ninja.exe' '-C' 'C:/Users/xqwys/CLionProjects/untitled/cmake-build-debug-mingw-clang' '-t' 'recompact'

  failed with:

   ninja: error: failed recompaction: Permission denied



CMake Error:
  Running

   'C:/msys64/mingw64/bin/ninja.exe' '-C' 'C:/Users/xqwys/CLionProjects/untitled/cmake-build-debug-mingw-clang' '-t' 'restat' 'build.ninja'

  failed with:

   ninja: error: failed recompaction: Permission denied



CMake Generate step failed.  Build files cannot be regenerated correctly.
ninja: build stopped: subcommand failed.```



@Selviniahh
Copy link
Author

Today I just realized every time I get the same error at first try and when I just try to build the intermediates again WITHOUT DOING ANYTHING it just works, yeah that's all It's just works.

I made a video: https://www.youtube.com/watch?v=weA4eTKaESU

@digit-google
Copy link
Contributor

Let me get this straight: your CMakeList.txt generates a Ninja build plan which contains a build rule/action that invokes CMake during the build (i.e. while Ninja is running). This CMake invocation is calling Ninja with -t recompact. The process tree should look like:

cmake --build <build_dir>
   |
   |____ ninja -C <build_dir>
             |
             |__ cmake -B <build_dir>
                    |
                    |___ ninja -C <build_dir> -t recompact

The recompact operation is trying to remove or truncate the build log (to replace it with new content), but this fails because the top-level ninja process still has an opened handle on that file.

On Windows, it is not possible to delete a file if another process still has a handle on it (it is possible on Posix).
This is why your are seeing a permission issue here.

I am curious to know why you would need to run CMake at build time, since Ninja already supports re-running the generator (CMake here) when needed. But what you see is not a Ninja issue, rather that neither Ninja nor CMake are designed to support this use case.

@Selviniahh
Copy link
Author

Selviniahh commented Sep 27, 2024

@digit-google Hi, thanks for reply. I made profile guided optimization with clang with -fprofile-instr-generate option, this will generate profraw files and I have to manually merge and then recompile cmake with using the merged profile file. I wanted to automate this process however I need a way to make CMake somehow act like a file has changed so it can rerun the script without clearing the build system. Since CMake doesn't care changes in binary build folder, I have to manually add a file in source dir or change something in my source.

The solution was just write this

    file(GLOB_RECURSE MYFILE LIST_DIRECTORIES true CONFIGURE_DEPENDS ${CMAKE_BINARY_DIR}/bin/*)

However, there's no mention of this solution anywhere on the internet, nor did AI help. I had to read the documentation. I should open a thread in the CMake forum and mentioned this solution. Let's not close this issue as it could be helpful in the future.

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

No branches or pull requests

2 participants