diff --git a/.gitignore b/.gitignore index 471dd961b..e62efd940 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,7 @@ vs2017 vs2019 vs2010 vs2013 -vs2015 \ No newline at end of file +vs2015 +cover_report +cover*.info +*.gcno \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu/xmake.lua b/hikyuu_cpp/hikyuu/xmake.lua index 750b913a4..fbf4e37c9 100644 --- a/hikyuu_cpp/hikyuu/xmake.lua +++ b/hikyuu_cpp/hikyuu/xmake.lua @@ -1,5 +1,5 @@ target("hikyuu") - if is_mode("debug") then + if is_mode("debug", "coverage", "asan", "msan", "tsan", "lsan") then set_kind("static") else set_kind("shared") diff --git a/hikyuu_cpp/unit_test/xmake.lua b/hikyuu_cpp/unit_test/xmake.lua index 511bebd46..2b146c2bb 100644 --- a/hikyuu_cpp/unit_test/xmake.lua +++ b/hikyuu_cpp/unit_test/xmake.lua @@ -1,5 +1,41 @@ add_requires("doctest") +function coverage_report(target) + if is_mode("coverage") and not is_plat("windows") and not (linuxos.name() == "ubuntu" and linuxos.version():lt("20.0")) then + -- 如需分支覆盖,须在下面 lcov, genhtml 命令后都加入: --rc lcov_branch_coverage=1 + print("Processing coverage info ...") + + if not os.isfile("cover-init.info") then + -- 初始化并创建基准数据文件 + os.run("lcov -c -i -d ./ -o cover-init.info") + end + + -- 移除之前的结果,否则容易出错 + os.tryrm('cover.info') + os.tryrm('cover-total.info') + + -- 收集当前测试文件运行后产生的覆盖率文件 + os.run("lcov -c -d ./ -o cover.info") + + -- 合并基准数据和执行测试文件后生成的覆盖率数据 + os.exec("lcov -a cover-init.info -a cover.info -o cover-total.info") + + -- 删除统计信息中如下的代码或文件,支持正则 + os.run("lcov --remove cover-total.info '*/usr/include/*' \ + '*/usr/lib/*' '*/usr/lib64/*' '*/usr/local/include/*' '*/usr/local/lib/*' '*/usr/local/lib64/*' \ + '*/test/*' '*/.xmake*' '*/boost/*' '*/ffmpeg/*' \ + '*/cpp/yihua/ocr_module/clipper.*' -o cover-final.info") + + -- 生成的html及相关文件的目录名称,--legend 简单的统计信息说明 + os.exec("genhtml -o cover_report --legend --title 'yhsdk' --prefix=" .. os.projectdir() .. " cover-final.info") + + -- 生成 sonar 可读取报告 + if is_plat("linux") then + os.run("gcovr -r . -e cpp/test -e 'cpp/yihua/ocr_module/clipper.*' --xml -o coverage.xml") + end + end +end + target("unit-test") set_kind("binary") set_default(false) @@ -43,6 +79,7 @@ target("unit-test") -- add files add_files("**.cpp") + after_run(coverage_report) target_end() target("small-test") @@ -82,4 +119,6 @@ target("small-test") -- add files add_files("./hikyuu/hikyuu/**.cpp"); add_files("./hikyuu/test_main.cpp") + + after_run(coverage_report) target_end() \ No newline at end of file diff --git a/setup.py b/setup.py index 4908cc7fe..df33c684c 100644 --- a/setup.py +++ b/setup.py @@ -99,6 +99,7 @@ def save_current_compile_info(compile_info): def build_boost(mode): """ 编译依赖的 boost 库 """ + new_mode = 'release' if mode == 'release' else 'debug' current_boost_root, current_boost_lib = get_boost_envrionment() if current_boost_root == '' or current_boost_lib == '': print("Can't get boost environment!") @@ -230,7 +231,7 @@ def cli(): @click.option('-m', '--mode', default='release', - type=click.Choice(['release', 'debug']), + type=click.Choice(['release', 'debug', 'coverage', 'asan', 'tsan', 'msan', 'lsan']), help='编译模式') def build(verbose, mode, j): """ 执行编译 """ @@ -245,7 +246,7 @@ def build(verbose, mode, j): @click.option('-m', '--mode', default='release', - type=click.Choice(['release', 'debug']), + type=click.Choice(['release', 'debug', 'coverage', 'asan', 'msan', 'tsan', 'lsan']), help='编译模式') @click.option('-case', '--case', default='', help="执行指定的 TestCase") def test(all, compile, verbose, mode, case, j): diff --git a/xmake.lua b/xmake.lua index 6a06fe3b7..cd5d8cc1a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -5,7 +5,7 @@ set_project("hikyuu") add_rules("mode.debug", "mode.release") if not is_plat("windows") then - add_rules("mode.coverage") + add_rules("mode.coverage", "mode.asan", "mode.msan", "mode.tsan", "mode.lsan") end -- version