Skip to content

Commit fcbcb0a

Browse files
committed
Overwrite gtests property specific output dirs
1 parent d4d2442 commit fcbcb0a

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

cmake/googletest.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ if(NOT googletest_POPULATED)
2525
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
2626
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
2727
CMAKE_SET_TARGET_FOLDER("gtest" "Tests/Dependencies")
28-
# Suppress warnigns from this target.
28+
# Suppress warnigns and overwrite output directories
29+
include(${CMAKE_CURRENT_LIST_DIR}/set_output_directories.cmake)
2930
include(${CMAKE_CURRENT_LIST_DIR}/warnings.cmake)
3031
if(TARGET gtest)
3132
DisableCompilerWarnings(TARGET gtest)
33+
OverwriteOutputDirectoryProperties(TARGET gtest)
3234
endif()
3335
endif()
3436

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)