# add\_custom\_command adds nonsense "driveletter:" to step

**URL:** <https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [April 17, 2020, 6:05pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049 "2020-04-17T18:05:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xaymar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xaymar/32/477_2.png) [@Xaymar](https://discourse.cmake.org/u/Xaymar)\
**Post date:** [April 17, 2020, 6:05pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/1 "2020-04-17T18:05:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [April 17, 2020, 9:01pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/2 "2020-04-17T21:01:30Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![Xaymar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xaymar/32/477_2.png) [@Xaymar](https://discourse.cmake.org/u/Xaymar)\
**Post date:** [April 17, 2020, 9:49pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/3 "2020-04-17T21:49:36Z")

</div>

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. 😕

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [April 17, 2020, 10:41pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/4 "2020-04-17T22:41:44Z")

</div>

`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?

---

<div class="post-metadata">

**Author:** ![Xaymar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xaymar/32/477_2.png) [@Xaymar](https://discourse.cmake.org/u/Xaymar)\
**Post date:** [April 17, 2020, 11:09pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/5 "2020-04-17T23:09:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [April 18, 2020, 8:11am UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/6 "2020-04-18T08:11:08Z")

</div>

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:`

![image](https://discourse.cmake.org/uploads/default/original/1X/bfeffea8cd767c4987411c306230e7d177b71683.png)

I hope this helps!

---

<div class="post-metadata">

**Author:** ![Xaymar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xaymar/32/477_2.png) [@Xaymar](https://discourse.cmake.org/u/Xaymar)\
**Post date:** [April 18, 2020, 11:21am UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/7 "2020-04-18T11:21:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Xaymar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xaymar/32/477_2.png) [@Xaymar](https://discourse.cmake.org/u/Xaymar)\
**Post date:** [April 18, 2020, 11:54am UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/8 "2020-04-18T11:54:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [April 19, 2020, 7:41pm UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/9 "2020-04-19T19:41:46Z")

</div>

> [@Xaymar](#):
>
> you can achieve the same with “cd /D”

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

![image](https://discourse.cmake.org/uploads/default/original/1X/970a88ab2b61145be1629ad03b8eabf7adaf2abc.png)

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [April 20, 2020, 7:12am UTC](https://discourse.cmake.org/t/add-custom-command-adds-nonsense-driveletter-to-step/1049/10 "2020-04-20T07:12:43Z")

</div>

`/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>
```
