1

I'm trying to add a custom target to my cmake setup. It's needed as I want to execute particular tool with a set of arguments. The executable itself is prebuild and located in the repo which I want to build (so there is explicit .exe in repo, it's not builded).

I used following cmake command:

add_custom_target(some_target ALL DEPENDS ${INPUT_FILES} COMMAND <path_to_repository>/<path_within_repository>/my_tool.exe "arguments")

This is crux of a problem:

<path_to_repository>/<path_within_repository>/my_tool.exe

The path to the executable is build as a absolute path which uses some dynamic and static parts (<path_to_repository> is obviously dynamic and deducted before the "add_custom_target". <path_within_repository> is static one, hardcoded within cmake.)

So for example we have:

C:/user/repos/source/MY_REPO/PROJECT/Sources/SpecialTool/bin/my_tool.exe
<path_to_repo ------------->/<path_within_repository ------->

Problem is when I start to execute the cmake and build. From the logs I can see that because of some reason the part of <path_to_repository> ALWAYS gets replaced by "../../" (although I use message() to print the full <path_to_repository>/<path_within_repository> and it is correct). So when it comes to executing the command I get a following in make logs:

../../Sources/SpecialTool/bin/my_tool.exe

which fails with a rather simple error message of

'..' is not recognized as an internal or external command,

operable program or batch file.

as it tries to build it on Windows, so obviously the relative path with "/" instead of "\" doesn't work (Source/SpecialTool/bin/my_tool.exe would work, but apparently you cannot use slashes and relative path on Windows).

So there are 2 problems:

  1. Why the part of absolute path gets replaced with a relative one (so instead of <path_to_repo> make uses "../../" )?
  2. How to enforce the use of "\" instead of "/" (so that the relative ..\..\Sources\...\my_tool.exe path works) ?

I've already tried putting backslash in the path ( so C:\\user\\repos\\source\\MY_REPO\\PROJECT\\Sources\\SpecialTool\\bin\\my_tool.exe), use quotes etc. and it seems nothing have had worked. I've also tried to use add_custom_command and then use it in add_custom_target, but that gave me exactly the same behavior as a direct call to command in add_custom_target.

The only instance when this problem does not occur is when I manually put the "my_tool.exe" somewhere above the repository itself in folder hierarchy (so C:/user/repos/source/my_tool.exe or C:/my_tool.exe, naturally the path then doesn't contain full path to builded repository itself). It is also not possible for me to actually use this when writing the cmake for this project.

eciu
  • 163
  • 2
  • 2
  • 6
  • Perhaps, I missed this in your question, but what generator are you using? – Kevin Aug 10 '20 at 21:38
  • Try `file(TO_NATIVE_PATH` and convert the path from cmake-ish to windows-ish. – KamilCuk Aug 10 '20 at 22:22
  • I use "Unix Makefiles" generator. – eciu Aug 11 '20 at 06:09
  • Also `file(TO_NATIVE_PATH` doesn't seem to work. I give as an argument both / and \ paths and there is no change in the output of that (`file`) command as a result the error in add_custom_target is still there). – eciu Aug 11 '20 at 07:09
  • I've changed the generator from "Unix Makefiles" to "Ninja". It has helped, as I can see now the path to executable is not altered in any way and thus the command can be called correctly. While this is a neat workaround, I would still love to understand why does the "Unix Makefiles" generator does this silent replacement, and how to make it work on a Windows based system. So using "Ninja" generator is a neat workaround, but not exactly a full solution to a problem. – eciu Aug 11 '20 at 09:50
  • Why are you using "Unix Makefiles" on a Windows machine, as there is no native Unix support... what is your compiler? On Windows, one typically uses "MinGW Makefiles", "NMake Makefiles", or "Ninja" instead, and uses the MinGW compiler, or a provided Visual Studio compiler. – Kevin Aug 11 '20 at 11:10
  • I had a set of different compilers to be used, and (possible wrongly) understood I should be using "Unix Makefiles" to setup a proper cross-compilation (with a usage of toolchains). Funny enough up to this point everything seemed to be working like a charm. – eciu Aug 11 '20 at 11:31

0 Answers0