Skip to content

Commit 6989add

Browse files
committed
Overwrite gtests property specific output dirs
1 parent 1173aee commit 6989add

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

cmake/dependencies/googletest.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ if(NOT googletest_POPULATED)
2727
CMAKE_SET_TARGET_FOLDER("gtest" "Tests/Dependencies")
2828
# Suppress warnigns from this target.
2929
include(${CMAKE_CURRENT_LIST_DIR}/../warnings.cmake)
30+
# Set output directories
31+
include(${CMAKE_CURRENT_LIST_DIR}/../set_output_directories.cmake)
3032
if(TARGET gtest)
3133
DisableCompilerWarnings(TARGET gtest)
34+
OverwriteOutputDirectoryProperties(TARGET gtest)
3235
endif()
3336
endif()
3437

cmake/set_output_directories.cmake

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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()
1213
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
1314
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
1415
endif()
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

Comments
 (0)