Skip to content

Commit

Permalink
add google-benchmark library
Browse files Browse the repository at this point in the history
  • Loading branch information
LanderlYoung committed Dec 3, 2023
1 parent b4c1c19 commit 893d496
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
4 changes: 3 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ set(SCRIPTX_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

# import googletest
include(cmake/gtest/CMakeLists.txt)
# import google benchmark
include(cmake/benchmark/CMakeLists.txt)

target_sources(UnitTests PRIVATE
src/gtest_main.cc
Expand Down Expand Up @@ -103,7 +105,7 @@ target_compile_definitions(ScriptX PUBLIC ${DEVOPS_LIBS_MARCO})
# if you want to close rtti feature for ScriptX and your project
#target_compile_options(ScriptX PRIVATE -fno-rtti)
#target_compile_options(UnitTests PRIVATE -fno-rtti)
target_link_libraries(UnitTests gtest ScriptX)
target_link_libraries(UnitTests gtest benchmark ScriptX)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using clang or gcc
Expand Down
8 changes: 8 additions & 0 deletions test/cmake/TestEnv.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ if ("${SCRIPTX_TEST_GOOGLE_TEST}" STREQUAL "")
endif ()
endif ()

if ("${SCRIPTX_TEST_GOOGLE_BENCHMARK}" STREQUAL "")
if (NOT "$ENV{SCRIPTX_TEST_GOOGLE_BENCHMARK}" STREQUAL "")
set(SCRIPTX_TEST_GOOGLE_BENCHMARK "$ENV{SCRIPTX_TEST_GOOGLE_BENCHMARK}")
else ()
set(SCRIPTX_TEST_GOOGLE_BENCHMARK ${CMAKE_CURRENT_BINARY_DIR}/benchmark-src)
endif ()
endif ()

if ("${SCRIPTX_TEST_BUILD_ONLY}" STREQUAL "")
if (NOT "$ENV{SCRIPTX_TEST_BUILD_ONLY}" STREQUAL "")
set(SCRIPTX_TEST_BUILD_ONLY "$ENV{SCRIPTX_TEST_BUILD_ONLY}")
Expand Down
40 changes: 40 additions & 0 deletions test/cmake/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# https://github.com/google/benchmark#installation

set(DEPENDENCY_BENCHMARK_BRANCH main)
if (NOT EXISTS ${SCRIPTX_TEST_GOOGLE_BENCHMARK})
# Download and unpack benchmark at configure time
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in benchmark-download/CMakeLists.txt)
execute_process(COMMAND
${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/benchmark-download
)
if (result)
message(FATAL_ERROR "CMake step for benchmark failed: ${result}")
endif ()

execute_process(COMMAND
${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/benchmark-download
)
if (result)
message(FATAL_ERROR "Build step for benchmark failed: ${result}")
endif ()
elseif (("${SCRIPTX_TEST_FORCE_UPDATE_DEPS}" STREQUAL ON) OR ("$ENV{SCRIPTX_TEST_FORCE_UPDATE_DEPS}" STREQUAL ON))
message(STATUS "update SCRIPTX_TEST_GOOGLE_BENCHMARK ${SCRIPTX_TEST_GOOGLE_BENCHMARK}")
execute_process(COMMAND
git fetch
WORKING_DIRECTORY ${SCRIPTX_TEST_GOOGLE_BENCHMARK}
)
execute_process(COMMAND
git checkout --force origin/${DEPENDENCY_BENCHNARK_BRANCH}
WORKING_DIRECTORY ${SCRIPTX_TEST_GOOGLE_TEST}
)
endif ()

# Add benchmark directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${SCRIPTX_TEST_GOOGLE_BENCHMARK}
${CMAKE_CURRENT_BINARY_DIR}/benchmark-build
EXCLUDE_FROM_ALL)
16 changes: 16 additions & 0 deletions test/cmake/benchmark/CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 2.8.2)

project(benchmark-download NONE)

include(ExternalProject)
ExternalProject_Add(benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG ${DEPENDENCY_BENCHMARK_BRANCH}
GIT_SHALLOW 1
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/benchmark-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/benchmark-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

0 comments on commit 893d496

Please sign in to comment.