filipdutescu / modern-cpp-template

A template for modern C++ projects using CMake, Clang-Format, CI, unit testing and more, with support for downstream inclusion.
The Unlicense
1.73k stars 215 forks source link

How to run GoogleTest and Static analyzers in local? #19

Closed stoneboy100200 closed 3 years ago

stoneboy100200 commented 3 years ago

Hello, I'd like to know how to run GTest and cppcheck in my local, not github. Thanks

stoneboy100200 commented 3 years ago

another question: may I run GTest and cppcheck in gitlab by modern-cpp-template. Thanks very much!

filipdutescu commented 3 years ago

Thank you very much for creating this issue and asking you questions! I am happy to respond to both!

Hello, I'd like to know how to run GTest and cppcheck in my local, not github. Thanks

I am not sure I follow. GTest is enabled by default and can be run locally by following the instructions from the README.md, in the Running the tests section.

In order to run Cppcheck locally, you need to go into cmake/StandardSettings.cmake and make the following changes in the "Static Analyzers section":

#
# Static analyzers
#
# Currently supporting: Clang-Tidy, Cppcheck.

# Change this option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable static analysis with Clang-Tidy." ON)
# and this option(${PROJECT_NAME}_ENABLE_CPPCHECK "Enable static analysis with Cppcheck." OFF) to:

option(${PROJECT_NAME}_ENABLE_CLANG_TIDY "Enable static analysis with Clang-Tidy." OFF)
option(${PROJECT_NAME}_ENABLE_CPPCHECK "Enable static analysis with Cppcheck." ON)

another question: may I run GTest and cppcheck in gitlab by modern-cpp-template. Thanks very much!

I have not used Gitlab before, but I see no reason why it would not work. You will however need to create your on CI workflow files, in order to work with Gitlab, as the ones provided are made for GitHub.

Please feel free to ask more questions if you have any or close the issue if the answers provided satisfy you.

stoneboy100200 commented 3 years ago

Many thanks for your reply. I found it seems cppcheck can not run, due to that CMakeLists.txt not include StaticAnalyzers.cmake

stoneboy100200 commented 3 years ago

Many thanks for your reply. I found it seems cppcheck can not run, due to that CMakeLists.txt not include StaticAnalyzers.cmake

It could run if I add this sentence in CMakeLists.txt: include(cmake/StaticAnalyzers.cmake)

Could you please add a CI workkflow file for Gitlab?

filipdutescu commented 3 years ago

It could run if I add this sentence in CMakeLists.txt: include(cmake/StaticAnalyzers.cmake)

Much obliged for reporting this bug! Will make sure that the next release has this (alongside a couple other bugs I found) fixed! Glad you got it to work thought!

Could you please add a CI workkflow file for Gitlab?

At a first glance, Gitlab's CI/CD pipelines seem to be a paid feature. Due to both this and the fact that I could not test on GitHub the workflows I will not add other CI files to the template. This would also open the door in the future for others to ask for such files to be included for their preferred CI service, adding unnecessary dependencies to the project.

That said, I recommend you take a look at Jenkins or Travis-CI as alternatives. Both are really popular, with many tutorials and it should be really easy to integrate with the template, taking the existing files (under .github/workflows/) as examples.

stoneboy100200 commented 3 years ago

Do this template support compiling the debugversion and release versions? How to config it?

stoneboy100200 commented 3 years ago

I have another question: cmake --build . --target install (This is compile command) If I'd like to re-compile the project, what command could I run?

filipdutescu commented 3 years ago

Both your questions are related to CMake, rather than my template. Please take a look at their docs as well, since I assume the user of my template knows CMake already.

To answer your questions, you can specify if you want to build for Debug, Release or a different version by running:

cmake --build . --target install --config Debug
# or
cmake --build . --target install --config Release

To recompile just rerun the commands above. Sometimes, this is not sufficient however and you will see that no updates were made in the final result. In those cases, remove the entire build/ directory and rerun the commands presented in the README.md.

stoneboy100200 commented 3 years ago

Thanks very much!

filipdutescu commented 3 years ago

My pleasure! Hope this let's you start working on awesome C++ projects!

stoneboy100200 commented 3 years ago

Hi, could you tell me how to run code coverage in local? It seems only enable some compile parameters for GCC, but how to run it?

stoneboy100200 commented 3 years ago

May I commit code? I'd like to fix bug mentioned yesterday. It seems could not read from remote repository: Permission to filipdutescu/modern-cpp-template.git denied to stoneboy100200.

stoneboy100200 commented 3 years ago

Hi, in this file: test/CMakeLists.txt

if (${CMAKE_PROJECT_NAME}_ENABLE_CODE_COVERAGE)
  target_compile_options(${CMAKE_PROJECT_NAME} PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
  target_link_options(${CMAKE_PROJECT_NAME} PUBLIC -fprofile-arcs -ftest-coverage)
  verbose_message("Code coverage is enabled and provided with GCC.")
endif()

target is ${CMAKE_PROJECT_NAME}, not ${PROJECT_NAME}, so the coverage is just for CMAKE_PROJECT_NAME, not test project(${CMAKE_PROJECT_NAME}Tests), is right? If I'd like to get coverage for ${CMAKE_PROJECT_NAME}Tests, I should change ${CMAKE_PROJECT_NAME} to ${PROJECT_NAME}, right?

stoneboy100200 commented 3 years ago

Another issue: in test/CMakeLists.txt, it could not output debug info by verbose_message, but message is ok.

filipdutescu commented 3 years ago

Hi, could you tell me how to run code coverage in local? It seems only enable some compile parameters for GCC, but how to run it?

I am not sure why you'd ever want ro run coverage locally. But if you really wanted, there is an option in cmake/StandardSettings.cmake you can turn on.

May I commit code? I'd like to fix bug mentioned yesterday. It seems could not read from remote repository: Permission to filipdutescu/modern-cpp-template.git denied to stoneboy100200.

Yes, you can. But I think you need to fork the project first, work on the fork and then create a PR. Please also follow the instructions in CONTRIBUTING and the ones present in the PR template before submitting your code.

Regarding the test project coverage, you are correct, changing the name would have you cover the test project, but I cannot see any reason you would want to do that.

Another issue: in test/CMakeLists.txt, it could not output debug info by verbose_message, but message is ok.

Did you turn verbose messages off? For me it works.

stoneboy100200 commented 3 years ago

Regarding the test project coverage, you are correct, changing the name would have you cover the test project, but I cannot see any reason you would want to do that.

In my opinion, there some reasons: 1.my program runs on ARM platform, it dependent upon aarch64 librarys; 2. I'd like to test for assigning files; 3. There are many test cases in test project, if I run the project (not test project), maybe the program coverage may not be comprehensive, please let me know if my opinion is wrong. Thanks.

Did you turn verbose messages off? For me it works.

verbose messages is ON: option(${PROJECT_NAME}_VERBOSE_OUTPUT "Enable verbose output, allowing for a better understanding of each step taken." ON) it so strange that it could output normally in CMakeLists.txt (root folder), but it could not output in test/CMakeLists.txt. You could also try it.

filipdutescu commented 3 years ago

In my opinion, there some reasons: 1.my program runs on ARM platform, it dependent upon aarch64 librarys; 2. I'd like to test for assigning files; 3. There are many test cases in test project, if I run the project (not test project), maybe the program coverage may not be comprehensive, please let me know if my opinion is wrong. Thanks.

  1. I fail to see how your project running on the ARM platform would mean you want to see the coverage of tests locally.
  2. I do not think coverage of the test project (meaning how much of the code written in the test project is tested) would help with that.
  3. Coverage is dictated by the tests you write, not the how many files there are in a project or how they are written.

Another observation is that the compiler flags you saw dictate that the resulting program should contain a mechanism which allows another project (in this example the tests project) to count how many of and which lines of code are executed in it. For example, having a two functions in your main project and making tests only for one in the test project would result in 50% coverage.

it could output normally in CMakeLists.txt (root folder), but it could not output in test/CMakeLists.txt. You could also try it.

Thanks for reporting, I think I know why. I'll work on a release around weekend which should address both the issues you've identified as well as a couple of others I already know of.

stoneboy100200 commented 3 years ago

Another observation is that the compiler flags you saw dictate that the resulting program should contain a mechanism which allows another project (in this example the tests project) to count how many of and which lines of code are executed in it. For example, having a two functions in your main project and making tests only for one in the test project would result in 50% coverage.

Add some information: I use gcov and lcov to get the result of coverage in Ubuntu(not github).

Thanks for reporting, I think I know why. I'll work on a release around weekend which should address both the issues you've identified as well as a couple of others I already know of.

I forgot there is another issue :) verbose_message only output one file name:

verbose_message("Found the following header files:")
verbose_message(${headers})

output is:

-- Found the following header files: -- include/recorder/recorder.h

acturally, I have many header files in my project. message() will output correct result:

-- Found the following header files: -- include/recorder/recorder.h include/recorder/recorder.hinclude/recorder/recorder_manager.hinclude/recorder/camera/front_cam_recorder.hinclude/recorder/gnss/gnss_recorder.hinclude/recorder/imu/imu_recorder.hinclude/recorder/perception/perception_recorder.hinclude/recorder/vio/vio_recorder.hinclude/recorder/warning/warning_recorder.hinclude/recorder/common/config_parser.hinclude/recorder/common/loader.hinclude/recorder/common/nodes_fact.hinclude/recorder/common/timer.hinclude/recorder/common/topic_base.hinclude/recorder/common/topics_fact.hinclude/recorder/common/utils.h

filipdutescu commented 3 years ago

Add some information: I use gcov and lcov to get the result of coverage in Ubuntu(not github).

This does not change my arguments, as Github doesn't have the option to process coverage anyways. I think you might misunderstand what coverage is or how you should use it, based on your statements. It only tells you how much of the code you've written is tested. That is why you need to have it on the main project, because that is what you want covered.

acturally, I have many header files in my project. message() will output correct result

Weird indeed. Will look into it.

stoneboy100200 commented 3 years ago

This does not change my arguments, as Github doesn't have the option to process coverage anyways. I think you might misunderstand what coverage is or how you should use it, based on your statements. It only tells you how much of the code you've written is tested. That is why you need to have it on the main project, because that is what you want covered.

It's nice talking to you. Maybe I was wrong.

filipdutescu commented 3 years ago

It's nice talking to you. Maybe I was wrong.

A pleasure on my part as well. I think that what you are trying to do could maybe find its place in a test, rather than coverage. If not testable, maybe you can rethink the problem to try and make it testable. Good luck!

filipdutescu commented 3 years ago

I will reopen as I have yet to fix the reported bugs.

david-bakin commented 3 years ago

Actually, FYI, GitLab CI/CD is not a paid feature - though the free tier has limited minutes-per-month. (And you can buy more minutes of CI/CD without having a subscription.)

I'm not suggesting you change your mind about including GitLab workflows ... just, FYI, as I said.

filipdutescu commented 3 years ago

I have resolved all mentioned issues, as far as I can tell, in the latest commits