From e7df3b3c73d29c2cd6f01b796b3f0f17e6ba3352 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Sun, 24 Nov 2024 23:28:59 +0100 Subject: [PATCH] [External] Adding Google benchmark option in CMake --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ cmake_modules/KratosGBenchmark.cmake | 13 +++++++++++++ kratos/CMakeLists.txt | 15 +++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 cmake_modules/KratosGBenchmark.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index f54c5b3f8c07..63d467db94dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,9 @@ endif() # If no test policy enable by default option(KRATOS_BUILD_TESTING "KRATOS_BUILD_TESTING defines if the C++ tests are compiled. These increase compilation time, but if not set only python tests can be executed. Default setting is ON" ON) +# If no benchmark policy enable by default +option(KRATOS_BUILD_BENCHMARK "KRATOS_BUILD_BENCHMARK defines if the C++ benchmarks (Google benchmark) are compiled. These increase compilation time. Default setting is OFF" OFF) + # If no pch policy disable by default option(KRATOS_USE_PCH "KRATOS_USE_PCH defines if pch will be used during the compilation. This may will decrease compilation for clean build or if core components are touched. Default setting is OFF" OFF) @@ -288,6 +291,7 @@ SET(INSTALL_PYTHON_FILES ON) # To be removed when all applications are po include(install_function) include(KratosDependencies) include(KratosGTest) +include(KratosGBenchmark) include(FetchContent) # Logger configuration @@ -320,6 +324,28 @@ if(KRATOS_BUILD_TESTING MATCHES ON) set_directory_properties(PROPERTIES COMPILE_OPTIONS "${kratos_root_compile_options}") endif(KRATOS_BUILD_TESTING MATCHES ON) +# Benchmarking +if(KRATOS_BUILD_BENCHMARK MATCHES ON) + # retrieve a copy of the current directory's `COMPILE_OPTIONS` + get_directory_property(kratos_root_compile_options COMPILE_OPTIONS) + + # Disable warnings (needed by centos. We should all love centos, it clearly needs some affection) + add_compile_options(-w) + + FetchContent_Declare( + googlebenchmark + URL https://github.com/google/benchmark/archive/v1.9.0.zip + ) + # For Windows: Prevent overriding the parent project's compiler/linker settings + set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE) + set(CMAKE_INSTALL_LIBDIR "libs") # In Linux default dir is lib + set(CMAKE_INSTALL_BINDIR "libs") # In Win default dir is bin + FetchContent_MakeAvailable(googlebenchmark) + + # restore the current directory's old `COMPILE_OPTIONS` + set_directory_properties(PROPERTIES COMPILE_OPTIONS "${kratos_root_compile_options}") +endif(KRATOS_BUILD_BENCHMARK MATCHES ON) + ################### PYBIND11 # Try to use python executable from env variable diff --git a/cmake_modules/KratosGBenchmark.cmake b/cmake_modules/KratosGBenchmark.cmake new file mode 100644 index 000000000000..1f51efaf4fbb --- /dev/null +++ b/cmake_modules/KratosGBenchmark.cmake @@ -0,0 +1,13 @@ +# This function automatically configures a given application to build its benchmarks +macro(kratos_add_benchmarks) + set(options USE_MPI USE_CUSTOM_MAIN) + set(oneValueArgs TARGET WORKING_DIRECTORY) + set(multiValueArgs SOURCES) + + cmake_parse_arguments(KRATOS_ADD_BENCHMARK "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(KRATOS_ADD_BENCHMARK_SOURCES) + include(GoogleBenchmark) + endif(KRATOS_ADD_BENCHMARK_SOURCES) + +endmacro(kratos_add_benchmarks) \ No newline at end of file diff --git a/kratos/CMakeLists.txt b/kratos/CMakeLists.txt index 825fb49776b4..282ba4101292 100644 --- a/kratos/CMakeLists.txt +++ b/kratos/CMakeLists.txt @@ -87,6 +87,21 @@ if(${KRATOS_BUILD_TESTING} MATCHES ON) gtest_discover_tests(KratosCoreTest DISCOVERY_MODE PRE_TEST) endif(${KRATOS_BUILD_TESTING} MATCHES ON) +## Kratos benchmark sources. Disabled by default +if(${KRATOS_BUILD_BENCHMARK} MATCHES ON) + file(GLOB_RECURSE KRATOS_BENCHMARK_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/benchmarks/*.cpp + ) + + foreach(file ${KRATOS_BENCHMARK_SOURCES}) + get_filename_component(filename ${file} NAME_WE) + add_executable(${filename} ${file}) + target_link_libraries(${filename} PUBLIC KratosCore benchmark::benchmark) + set_target_properties(${filename} PROPERTIES COMPILE_DEFINITIONS "KRATOS_BENCHMARK=IMPORT,API") + install(TARGETS ${filename} DESTINATION benchmark) + endforeach(file ${KRATOS_BENCHMARK_SOURCES}) +endif(${KRATOS_BUILD_BENCHMARK} MATCHES ON) + ## Define KratosVersion object add_library(KratosVersion OBJECT ${KRATOS_VERSION_SOURCES}) set_target_properties(KratosVersion PROPERTIES COMPILE_DEFINITIONS "KRATOS_VERSION=IMPORT,API")