Failed recompaction error in add_custom_command

Hi,

I’m using following code to automatically create a dependency graph during build

    add_custom_command(
        OUTPUT ${CMAKE_BINARY_DIR}/_graphviz
        COMMAND ${CMAKE_COMMAND} "--graphviz=graphviz/deps.dot" .
        COMMAND dot -Tsvg graphviz/deps.dot -o deps.svg
        COMMAND dot -Tpng graphviz/deps.dot -o deps.png
        COMMENT "Plotting dependencies graph to deps.svg"
        DEPENDS firmware
        WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
    add_custom_target(graphviz ALL
        DEPENDS ${CMAKE_BINARY_DIR}/_graphviz)

It cause the following error:

[build] FAILED: _graphviz C:/git/x/build/debug/_graphviz 
[build] C:\WINDOWS\system32\cmd.exe /C "cd /D C:\git\x\build\debug && C:\msys64\mingw64\bin\cmake.exe --graphviz=graphviz/deps.dot . && dot -Tsvg graphviz/deps.dot -o deps.svg && dot -Tpng graphviz/deps.dot -o deps.png"
[build] -- Configuring done (1.3s)
[build] -- Generating done (4.8s)
[build] CMake Error:
[build]   Running
[build] 
[build]    'C:/msys64/mingw64/bin/ninja.exe' '-C' 'C:/git/x/build/debug' '-t' 'recompact'
[build] 
[build]   failed with:
[build] 
[build]    ninja: error: failed recompaction: Permission denied
[build] 
[build] 
[build] 
[build] CMake Error:
[build]   Running
[build] 
[build]    'C:/msys64/mingw64/bin/ninja.exe' '-C' 'C:/git/x/build/debug' '-t' 'restat' 'build.ninja'
[build] 
[build]   failed with:
[build] 
[build]    ninja: error: failed recompaction: Permission denied
[build] 
[build] 
[build] 
[build] Generate graphviz: C:/git/x/build/debug/graphviz/deps.dot
[build] CMake Generate step failed.  Build files cannot be regenerated correctly.
[build] ninja: build stopped: subcommand failed.

The error is caused by the call of ${CMAKE_COMMAND}.

How to fix it?
Is there a better way to generate the dependency graph during configuration. e.g. implicit --graphviz option?

By searching on Google, I saw some issues with the same error.
Maybe you should give your environment (espacially your CMake release).

Yes, I have also seen forum posts regarding recompaction errors, but mainly some old related to logging, which should be fixed.

I’m developing on Windows 11, with MSYS/MINGW64 environment and I’m using VSCode. CMake version is 3.29.2

Besides this problem with the recompaction error, actually my goal is to create dependency graph during configuration. I have not found a possibility to enable graphviz when using profiles. So this is a workaround to call cmake recursively just for the purpose of the graph.

It seems like there’s something that is preventing ninja from performing recompaction. Has the build been interacted with as a different user at any point? Could something else be poking the tree somehow?

I’m facing the same issue with

cmake --version
cmake version 3.30.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

and

ninja --version
1.12.1

My error is also occurring by doing the following,

add_custom_command(
    OUTPUT ${GRAPHVIZ_OUTPUT_FILE}
    COMMAND ${CMAKE_COMMAND} --graphviz=${GRAPHVIZ_OUTPUT_FILE} ${CMAKE_BINARY_DIR}
    COMMENT "Generating Graphviz .dot file"
)

I’ve tried cleaning my build directory, re-buildng from scratching, installing and uninstalling ninja and cmake but nothing seems to work.

Other important things:

  • running windows 11 inside parallels
  • installed latest graphviz via Download_windows zip:
def install_graphviz_windows():
    graphviz_url = "https://graphviz.gitlab.io/_pages/Download/Download_windows.html"
    subprocess.run(["winget", "install", "-e", "--id", "Graphviz.Graphviz"], check=True)

You’re trying to re-run CMake during the build in a recursive way. I would recommend you don’t do that. In some build systems, that fails (I think Xcode rejects it now, it didn’t in the past).

In the next CMake feature release (3.31), CMake presets will gain support for a graphviz field in the configure presets. That’s a better way to incorporate running graphviz with your configuration step by default.

1 Like

In the next CMake feature release (3.31), CMake presets will gain support for a graphviz field in the configure presets. That’s a better way to incorporate running graphviz with your configuration step by default.

Gotcha; that sounds much needed!

You’re trying to re-run CMake during the build in a recursive way. I would recommend you don’t do that. In some build systems, that fails (I think Xcode rejects it now, it didn’t in the past).

Isn’t the PID different? Why would that cause a recursive-like execution?

In the Xcode case, I think it detects a recursive call (yes, including separate processes) and it fails with some kind of locking-related failure. I don’t recall the details, sorry.

Eagerly awaiting 3.31 then!

Update: 3.31 is in Release Candidate state.