From bd706b5d5fdc3eb919be113b3441edf8ae33c9f2 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/googletest.cmake | 4 +++- cmake/set_output_directories.cmake | 33 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cmake/googletest.cmake b/cmake/googletest.cmake index 9f8c24f6a..92d4443fb 100644 --- a/cmake/googletest.cmake +++ b/cmake/googletest.cmake @@ -25,10 +25,12 @@ if(NOT googletest_POPULATED) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL) CMAKE_SET_TARGET_FOLDER("gtest" "Tests/Dependencies") - # Suppress warnigns from this target. + # Suppress warnigns and overwrite output directories + include(${CMAKE_CURRENT_LIST_DIR}/set_output_directories.cmake) include(${CMAKE_CURRENT_LIST_DIR}/warnings.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