From 6989adde497ca8d2ac1316796beb85d9da0d8997 Mon Sep 17 00:00:00 2001 From: Peter Heywood Date: Thu, 19 Aug 2021 13:48:32 +0100 Subject: [PATCH] Overwrite gtests property specific output dirs --- cmake/dependencies/googletest.cmake | 3 +++ cmake/set_output_directories.cmake | 33 ++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cmake/dependencies/googletest.cmake b/cmake/dependencies/googletest.cmake index 89e454fe8..1eaebd70b 100644 --- a/cmake/dependencies/googletest.cmake +++ b/cmake/dependencies/googletest.cmake @@ -27,8 +27,11 @@ if(NOT googletest_POPULATED) CMAKE_SET_TARGET_FOLDER("gtest" "Tests/Dependencies") # Suppress warnigns from this target. include(${CMAKE_CURRENT_LIST_DIR}/../warnings.cmake) + # Set output directories + include(${CMAKE_CURRENT_LIST_DIR}/../set_output_directories.cmake) if(TARGET gtest) DisableCompilerWarnings(TARGET gtest) + OverwriteOutputDirectoryProperties(TARGET gtest) endif() endif() diff --git a/cmake/set_output_directories.cmake b/cmake/set_output_directories.cmake index 1c61d2e85..3fc51f565 100644 --- a/cmake/set_output_directories.cmake +++ b/cmake/set_output_directories.cmake @@ -1,4 +1,5 @@ -# Set the locations of ARCHIVE (.lib/.a), LIBRARY (MODULE .dll/.so, SHARED.so) and BINARY (.dll,.exe/binary) +# Set CMake variables which influence the location of otuput artifacts such as executables, .so, .dll, .lib files. +# Do this by controlling the default variables, and also providing a function to reset per-target properties to the current global variables. # Only set these if they have not already been set. # 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() if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) endif() +if(NOT CMAKE_PDB_OUTPUT_DIRECTORY) + set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) +endif() + +# Provide a target specific method to overwrite the values set on a target to the current values of the global CMAKE variables. +# This is to overwrite per target setting such as in gtest when using add_subdirectory. +function(OverwriteOutputDirectoryProperties) + # Parse the expected arguments, prefixing variables. + cmake_parse_arguments( + OODP + "" + "TARGET" + "" + ${ARGN} + ) + # Ensure that a target has been passed, and that it is a valid target. + if(NOT OODP_TARGET) + message( FATAL_ERROR "OverwriteOutputDirectoryProperties: 'TARGET' argument required." ) + elseif(NOT TARGET ${OODP_TARGET} ) + message( FATAL_ERROR "OverwriteOutputDirectoryProperties: TARGET '${OVERWRITE_OUTPUT_DIRECTORY_PROPERTIES_TARGET}' is not a valid target" ) + endif() + # Set the various target properties to current cmake global variable + set_target_properties(${OODP_TARGET} + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" + PDB_OUTPUT_DIRECTORY "${CMAKE_PDB_OUTPUT_DIRECTORY}" + ) +endfunction() \ No newline at end of file