79785554

Date: 2025-10-08 14:19:31
Score: 1
Natty:
Report link

ASan DLL copy build event

Rather than figuring out absolute paths of the specific MSVC version installed, property macros can be used for this. This can be used in a build event to copy the necessary DLLs required for the address sanitizer automatically. I've come up with the following command for this:

echo "Copying ASan DLLs"
xcopy /Y /D "$(ExecutablePath.Split(';')[0])\clang_rt.asan_*.dll" "$(OutDir)"
xcopy /Y /D "$(ExecutablePath.Split(';')[0])\clang_rt.asan_*.pdb" "$(OutDir)"

How this works

As pointed out by @Brandlingo in a comment, the macro $(VCToolsInstallDir) expands to "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\<version>". I've further found the $(ExecutablePath) macro that expands to a list of executable directories where the first directory is "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\<version>\bin\Host<arch>\<arch>". Exactly where the correct ASan DLLs for the specific build configuration are. (Saving the hassle to add the target architecture to the path manually)

Because the $(ExecutablePath) macro contains multiple executable directories, the bin\Host<arch>\<arch> one has to be extracted from that. The list is semicolon-separated and luckily basic .NET operations are supported on these macros, so a .Split(';')[0] gets just that first directory. (For me this is always the "...MSVC\<version>\bin\Host<arch>\<arch>" one)

Downside

If the order of executable paths in $(ExecutablePath) ever change, this bricks. If anyone can find a macro that directly expands to "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\<version>\bin\Host<arch>\<arch>" containing only this path, please let us know. I've only found targeted paths to specific build configurations in $(VC_ExecutablePath_x86), $(VC_ExecutablePath_x64), $(VC_ExecutablePath_x86_ARM), ... no general one for it.

Reasons:
  • RegEx Blacklisted phrase (2.5): please let us know
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Brandlingo
  • High reputation (-1):
Posted by: Vinz