CMake Version: 3.17.1

Using the WORKING_DIRECTORY option in add_custom_command completely breaks the generated build step. For an unknown reason, CMake generates an extra line which is usually just “driveletter:” (C:, D:, E:, …).

This is the command that causes that:

``````add_custom_command(
TARGET \${_target}_CLANG-TIDY
POST_BUILD
COMMAND \${CLANG_TIDY_BIN}
ARGS \${_el}
WORKING_DIRECTORY \${target_binary_dir}
)
``````

Which generates:

``````setlocal
cd C:\Tools
if %errorlevel% neq 0 goto :cmEnd
C: // <-- This is the problem, where does it even come from?
if %errorlevel% neq 0 goto :cmEnd
"C:\Program Files\LLVM\bin\clang-tidy.exe" D:\Projects\Xaymar\obs-studio\build\64\UI\frontend-plugins\streamfx\source\module.cpp
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
``````
• target_binary_dir only contains “C:\Tools”, no other entries.
• CLANG_TIDY_BIN is the path to clang-tidy.exe
• _target is the current target name.
• _el is the target source file

Anyone have any idea why this happens, and how to actually solve it?

I guess `add_custom_command` expects so called “cmake-style paths”. You can use `file(TO_CMAKE_PATH)` to convert a native Windows path into a cmake-style path (see https://cmake.org/cmake/help/latest/command/file.html#path-conversion for more details).

It is also advised to always double-quote paths, since some function might interpret a single path containing spaces as several space-separated arguments.

I hope this helps!

Neither cmake-style paths or native paths work unfortunately, it was one of the first things I tried. Even manually giving it “…/Tools” (autoformatting turns it into …, but its two .) still added a nonsense C: to it.

`cd C:\Tools` changes the working directory of the `C:` drive to `C:\Tools`. But if the current drive is `X:` then an addition `C:` has to be done to switch the drive.

Is it really adding `C: //`? Because that would be a problem.
Otherwise why would having `C:` be a problem at all?

1 Like

See actual generated commands in the OP. It adds a 2nd command with just C: and nothing else.

As @fdk17 mentioned, having `C:` in a batch script is totally valid and necessary in case the current working directory is on another drive.

I made a similar example, though manually in `cmd.exe`, and from `C:` to `D:`

I hope this helps!

1 Like

Hi McMartin, you can achieve the same with “cd /D”. The second D: there actually returns a failure, so the script aborts at that moment. Which is my current problem.

1 Like

Did some additional testing now. The commands start working when the drive is not a network mounted drive, or an NTFS mounted directory. I suppose that is an acceptable limitation, though ‘cd /D’ doesn’t seem have this limitation. Will just move the project and necessary files to local directories instead.

1 Like

`cd /D` doesn’t work for me…

`/D` is an option that must be passed to the directory-changing `cd` command, and means “also change the drive if applicable.” So the correct syntax would be

``````C:\Users\Alain>cd /D D:\dev

D:\dev>``````
2 Likes