Allow specifying a global var or directory prop for DLL Output Directory that's different from the EXE Output Directory
On windows, libraries may be dynamically loaded. For security purposes, it's a good practice specify full paths to the DLLs when calling LoadLibrary.
Our policy is to install the program's built DLLs into a subdirectory underneath the excecutables. Each executable will load DLLs only from the "Libraries" subdirectory.
Example Layout
For Testing:
After compiling, testing requires that SHARED_LIBRARIES be located in an expected subdirectory. Otherwise, a call to ::LoadLibrary(calculated_fullpath_to_dll)
fails.
- I tried setting the variable CMAKE_RUNTIME_OUTPUT_DIRECTORY but it's used for both EXEs and DLLs. So, then
- I tried setting the variable with this generator_expression, but that's an error because it has to know the target name.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/$<IF:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:libs,>")
- I tried separating the EXE subprojects into different directories from DLL subprojects, but
- CMAKE_RUNTIME_OUTPUT_DIRECTORY is global variable, not a directory property.
- Code for DLLs and their test EXEs are in the same directory
The only solutions I've found so far is to
-
Add a command to each target to copy the DLLs from the OUTPUT_DIRECTORY to the "Libraries" subdirectory. But it makes the cmake code a bit hairy with IF statements; and it does not work very well with third-party sources.
-
Hard-code the OUTPUT_DIRECTORY into each target using a generator statement similar to above. But, it's very brittle to maintain; and it does not work third-party code.
Enhancement Request:
-
It would be great if DLLs and EXEs had different properties for their CMAKE_<RUNTIME>_OUTPUT_DIRECTORY.
-
And also, maybe, even a directory property instead of just a global variable.