My project has a custom target that prepares some files that are later used in a test. The command of the custom target uses a tool that requires some shared libraries to be on the path. I don’t want to force my users to add it to the system path so I looked for a way to add it just for the custom target. My system is Visual Studio 2019 on Windows.
According to the first answer in this SO post the variable CMAKE_MSVCIDE_RUN_PATH is the tool for this. It should add a line set PATH=<path/to/add>;%PATH% to the custom build step in VS. However it doesn’t seem to work for me–the line is not present. I could add the line manually but since there are many such custom targets, this would be a hassle and would clutter up the code because I’d need to add if (MSVC) clauses.
I prepared a minimal example. It’d be easier to add it as an attachment but since I’m new user that’s not possible so I paste the code below.


project(msvcide_run_path_example LANGUAGES NONE)
cmake_minimum_required(VERSION 3.20)
cmake_path(CONVERT "${CMAKE_SOURCE_DIR}/test_prep" TO_NATIVE_PATH_LIST test_prep_path)
set(CMAKE_MSVCIDE_RUN_PATH ${test_prep_path}) # this doesn't work
add_custom_target (test_prep 
	#COMMAND set "PATH=${test_prep_path};%PATH%" # uncomment this to make it work
	COMMAND "test_prep.bat")
  NAME "my_test"
  COMMAND "FC.exe" "test_file1.txt" "test_file2.txt"  

The tool to prepare the files (test_prep.bat) is located in subdirectory test_prep which needs to be added to the path. The contents of the batch file is as follows:

echo test > test_file1.txt
echo test > test_file2.txt

Why not pass the full path to test_prep.bat to COMMAND? Wouldn’t that let the . entry in %PATH% do what you want?

The example was just to demonstrate that the CMAKE_MSVCIDE_RUN_PATH variable doesn’t work here. In my real project adding the dir to the path is necessary because of a shared library. In other words test_prep.bat is in reality a binary that loads a shared library.

Hmm. I don’t seem to see a test for this variable. @brad.king?

The implementation is here. It adds set PATH=... to the generated custom command script inside the .vxcproj file. It has nothing to do with add_test or ctest.

Are you saying that the fact that CMAKE_MSVCIDE_RUN_PATH doesn’t work in my example is expected/correct behavior? That’s not what I would expect based on the docs.

The docs you linked say "when executing add_custom_command() or add_custom_target() ". That has nothing to do with add_test().

But my code does use add_custom_target() and that’s where I want to add the path. The custom target prepares files later used in a test but that’s irrelevant I would say.

Sorry, I misread the example.

The logic I linked is conditioned on MSVC_IDE being set, but that is only set here when a language is enabled. Adding set(MSVC_IDE 1) can work around the problem.

I’ve opened CMake Issue 22343 for this.

OK thanks, also for the workaround!