1- # Set the locations of ARCHIVE (.lib/.a), LIBRARY (MODULE .dll/.so, SHARED.so) and BINARY (.dll,.exe/binary)
1+ # Set CMake variables which influence the location of otuput artifacts such as executables, .so, .dll, .lib files.
2+ # Do this by controlling the default variables, and also providing a function to reset per-target properties to the current global variables.
23
34# Only set these if they have not already been set.
45# Set them to be relative to the Project Source directory, i.e. the location of the first call to CMake
@@ -12,3 +13,33 @@ endif()
1213if (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY )
1314 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin/$<CONFIG >)
1415endif ()
16+ if (NOT CMAKE_PDB_OUTPUT_DIRECTORY )
17+ set (CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin/$<CONFIG >)
18+ endif ()
19+
20+ # Provide a target specific method to overwrite the values set on a target to the current values of the global CMAKE variables.
21+ # This is to overwrite per target setting such as in gtest when using add_subdirectory.
22+ function (OverwriteOutputDirectoryProperties )
23+ # Parse the expected arguments, prefixing variables.
24+ cmake_parse_arguments (
25+ OODP
26+ ""
27+ "TARGET"
28+ ""
29+ ${ARGN}
30+ )
31+ # Ensure that a target has been passed, and that it is a valid target.
32+ if (NOT OODP_TARGET)
33+ message ( FATAL_ERROR "OverwriteOutputDirectoryProperties: 'TARGET' argument required." )
34+ elseif (NOT TARGET ${OODP_TARGET} )
35+ message ( FATAL_ERROR "OverwriteOutputDirectoryProperties: TARGET '${OVERWRITE_OUTPUT_DIRECTORY_PROPERTIES_TARGET} ' is not a valid target" )
36+ endif ()
37+ # Set the various target properties to current cmake global variable
38+ set_target_properties (${OODP_TARGET}
39+ PROPERTIES
40+ ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} "
41+ LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY} "
42+ RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY} "
43+ PDB_OUTPUT_DIRECTORY "${CMAKE_PDB_OUTPUT_DIRECTORY} "
44+ )
45+ endfunction ()
0 commit comments