From 98bb6a0b7ba38899ba3b4b6a927d813dbaf3f24c Mon Sep 17 00:00:00 2001 From: DCHii <13780064348@163.com> Date: Wed, 8 May 2024 10:53:18 +0800 Subject: [PATCH] add UT execution script --- .gitmodules | 5 ++++- CMakeLists.txt | 7 +++++++ Common.cmake | 22 ++++++++-------------- build.sh | 21 ++++++++++++++++++++- build/common.sh | 33 +++++++++++++++++++++++++++++++++ third_party/googletest | 1 + 6 files changed, 73 insertions(+), 16 deletions(-) create mode 160000 third_party/googletest diff --git a/.gitmodules b/.gitmodules index fa50f14..e0fc235 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "elfin-parser"] path = third_party/elfin-parser - url = https://gitee.com/openeuler/elfin-parser.git \ No newline at end of file + url = https://gitee.com/openeuler/elfin-parser.git +[submodule "googletest"] + path = third_party/googletest + url = https://gitee.com/mirrors/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 761fc49..742034c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,9 @@ set(CMAKE_INSTALL_PREFIX "${PROJECT_TOP_DIR}/output/" CACHE PATH "Installation d cmake_minimum_required (VERSION 3.12.0) set(CMAKE_CXX_STANDARD 11) +if (INCLUDE_TEST) + set(CMAKE_CXX_STANDARD 14) +endif() set(CMAKE_C_STANDARD 11) if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8.5) message(FATAL_ERROR "GCC 4.8.5 or newer required") @@ -35,4 +38,8 @@ include(${CMAKE_CURRENT_LIST_DIR}/Common.cmake) add_subdirectory(symbol) add_subdirectory(pmu) +if (INCLUDE_TEST) + add_subdirectory(test) +endif() + set(CMAKE_EXPORT_COMPILE_COMMANDS True) diff --git a/Common.cmake b/Common.cmake index 2aac046..7572398 100644 --- a/Common.cmake +++ b/Common.cmake @@ -33,19 +33,13 @@ set_property(TARGET dwarf_static PROPERTY IMPORTED_LOCATION ${THIRD_PARTY}/local include_directories(${THIRD_PARTY}/elfin-parser/dwarf) include_directories(${THIRD_PARTY}/elfin-parser/elf) -find_library(gtest_main_path libgtest_main.a /usr/local/lib64 /usr/local/lib) -if (${gtest_main_path} STREQUAL "gtest_main_path-NOTFOUND") - message (STATUS "required gtest_main library but not found!") -else() - message (STATUS "gtest_main library found in ${gtest_main_path}") -endif() +include_directories(${THIRD_PARTY}/googletest/googletest/include) add_library(gtest_main STATIC IMPORTED) -set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION ${gtest_main_path}) -find_library(gtest_path libgtest.a /usr/local/lib64 /usr/local/lib) -if (${gtest_path} STREQUAL "gtest_path-NOTFOUND") - message (STATUS "required gtest library but not found!") -else() - message (STATUS "gtest library found in ${gtest_path}") -endif() +set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION ${THIRD_PARTY}/local/googletest/lib64/libgtest_main.a) add_library(gtest STATIC IMPORTED) -set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${gtest_path}) +set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${THIRD_PARTY}/local/googletest/lib64/libgtest.a) +add_library(gmock_main STATIC IMPORTED) +set_property(TARGET gmock_main PROPERTY IMPORTED_LOCATION ${THIRD_PARTY}/local/googletest/lib64/libgmock_main.a) +add_library(gmock STATIC IMPORTED) +set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${THIRD_PARTY}/local/googletest/lib64/libgmock.a) +find_library(gtest_main_path libgtest_main.a /usr/local/lib64 /usr/local/lib) diff --git a/build.sh b/build.sh index ae8924a..511c3e3 100644 --- a/build.sh +++ b/build.sh @@ -32,6 +32,16 @@ else creat_dir ${THIRD_PARTY}/local fi +# 默认情况下不编译测试用例 +INCLUDE_TEST=OFF +echo "$1" + +# 如果第一个参数是 "test",则设置 INCLUDE_TEST 为 ON +if [[ "$1" == "test" ]]; then + build_googletest $THIRD_PARTY + INCLUDE_TEST=ON +fi + # build libprof.so libraries including libprocfs.so libprocfs.a libpmu.so libpmu.a libtrace.so libtrace.so function build_elfin() { local cmake_target_dir=$THIRD_PARTY/local/elfin-parser @@ -62,16 +72,25 @@ function build_elfin() { build_libprof() { cd $BUILD_DIR - cmake .. + cmake -DINCLUDE_TEST=$INCLUDE_TEST .. make -j ${cpu_core_num} make install echo "build_libprof success" } +function build_test() +{ + if [ "$INCLUDE_TEST" = "ON" ]; then + execute_binary "$PROJECT_DIR" + fi +} + main() { build_elfin build_libprof + build_test } +# bash build.sh test来获取UT main $@ diff --git a/build/common.sh b/build/common.sh index e19365e..75f3abc 100644 --- a/build/common.sh +++ b/build/common.sh @@ -71,4 +71,37 @@ function build_sqlite3(){ popd rm -rf $open_source_dir/sqlite_src/sqlite-src-3370200 echo "install sqlite3 path: $cmake_target_dir" +} + +function execute_binary() { + test_case_dir=("test_perf" "test_symbol") + test_case_name=("test_perf" "test_symbol") + # for instance. + # test_case_exclude =("TestCount.LLCacheMissRatio TestSPE.SpeProcCollectSubProc TestSPE.SpeProcCollectTwoThreads" "") + test_case_exclude=( + "" + "" + ) + test_prefix="$1"/_build/test/ + # 遍历数组 + for i in "${!test_case_dir[@]}"; do + dir="${test_case_dir[$i]}" + exe="${test_case_name[$i]}" + cd "${test_prefix}${dir}" + exclude="${test_case_exclude[$i]}" + + # 构建命令字符串 + command="./$exe --gtest_filter=-" + if [ -n "$exclude" ]; then + for ex in $exclude; do + command="$command$ex:" + done + # 移除末尾的冒号 + command=${command%:} + fi + + # 执行命令 + echo "执行命令: $command" + eval "$command" + done } \ No newline at end of file diff --git a/third_party/googletest b/third_party/googletest new file mode 160000 index 0000000..58d77fa --- /dev/null +++ b/third_party/googletest @@ -0,0 +1 @@ +Subproject commit 58d77fa8070e8cec2dc1ed015d66b454c8d78850 -- Gitee