Skip to content
This repository has been archived by the owner on Feb 12, 2025. It is now read-only.

Commit

Permalink
Add code analysis and compilation optimization tools (#832)
Browse files Browse the repository at this point in the history
Integration of new cpp tools in cmake, including static code analysis, compilation cache and code coverage. 

Changes:
* feat: setup code coverage on unit tests
* feat: set different targets for code coverage depending on test types
* doc: improve doc on code coverage
* test: remove commented content from cmake file
* conf: add additional tools for code analysis and compilation optimization
Changes include the addition of ccache, cotire, clang-tidy and include-what-you-use.
* ci: add cmake-scripts git submodule update
  • Loading branch information
jcoatelen-ledger authored Dec 2, 2021
1 parent 528f2c0 commit 77db363
Show file tree
Hide file tree
Showing 273 changed files with 5,091 additions and 204 deletions.
1 change: 1 addition & 0 deletions .circleci/init_submodules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ git submodule sync
#Otherwise can't init submodules
echo "========> Update all submodules "
#No need for those
git submodule update -- cmake-scripts || echo "===========Cmake-scripts submodule already updated"
git submodule update -- djinni || echo "===========Djinni submodule already updated"
git submodule update -- toolchains/polly || echo "===========Polly submodule already updated"
#git submodule update -- tools/gyp || echo "===========gyp submodule already updated"
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
[submodule "core/src/wallet/algorand/thirdparty/msgpack-c"]
path = core/src/wallet/algorand/thirdparty/msgpack-c
url = https://github.com/ledgerHQ/msgpack-c.git
[submodule "cmake/cmake-scripts"]
path = cmake/cmake-scripts
url = https://github.com/StableCoder/cmake-scripts
29 changes: 24 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,30 @@ if(NIX_BUILD)
set(SYS_OPENSSL ON CACHE BOOL "Building with Nix implies using system openssl" FORCE)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Update Cmake Module Path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-modules")
include(UseBackportedModules)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-scripts")

# General internal functions
function (unsetListOfVarsStartingWith _prefix)
get_cmake_property(_vars VARIABLES)
string (REGEX MATCHALL "(^|;)${_prefix}[A-Za-z0-9_]*" _matchedVars "${_vars}")
FOREACH(var ${_matchedVars})
unset (${var} CACHE)
ENDFOREACH()
endfunction()

# Include cmake scripts
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/CCache.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/Coverage.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/Cotire.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-scripts/c++-standards.cmake")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-scripts/tools.cmake")

cxx_14()
clang_tidy("-checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*,cppcoreguidelines-*,modernize-*") # add cmake conf option CLANG_TIDY=ON to activate
include_what_you_use() # add cmake conf option IWYU=ON to activate

# The project version number.
set(VERSION_MAJOR 4 CACHE STRING "Project major version number.")
Expand All @@ -27,9 +48,6 @@ mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build)
list(APPEND INCLUDE_DIRECTORIES core/test/include/)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Setup BOOST
set(BOOST_INCLUDEDIR "${CMAKE_CURRENT_SOURCE_DIR}/core/lib/boost/")

Expand All @@ -53,6 +71,8 @@ if(IS_IOS GREATER_EQUAL 0 OR TARGET_JNI OR ANDROID)
set(BUILD_TESTS OFF CACHE BOOL "Cannot run tests for these options" FORCE)
endif()

enable_testing()

add_subdirectory(doc)
add_subdirectory(core)

Expand All @@ -63,4 +83,3 @@ add_subdirectory(core)
# > Issue is specific to 10.10.x, 10.9.5 and > 10.10.x are fine
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X version to target for deployment: 10.9" FORCE)

enable_testing()
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,28 @@ if you want to run only one specific unit test. (e.g. the test case `BitcoinLike
You can generate the Doxygen documentation by running the `doc` target (for instance, `make doc`
with makefiles).

## Use Code Coverage
:warning: Only available on Linux for now (with gcc).

0. Make sure to have `lcov` installed
```sh
sudo apt-get install -y lcov
```
1. Set the `CODE_COVERAGE` cmake option to `ON`
2. Compute the coverage, in build directory:
* Based on unit tests only: `cmake --build . --config Debug --target coverage_unit`
* Based on all tests: `cmake --build . --config Debug --target coverage_all`
3. Open the report: `./coverage/index.html`

## Use optional compilation checks

* [clang-tidy](https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clang-tidy/index.html) checks can be activated by setting `CLANG_TIDY` cmake option to `ON`
* [include-what-you-use](https://github.com/include-what-you-use/include-what-you-use) checks can be activated by setting `IWYU` cmake option to `ON`

## Use optional compilation optimization

* [ccache](https://github.com/ccache/ccache) compilation optimization can be activated by setting `CCACHE` cmake option to `ON`

## Binding to node.js

The library can be compiled and integrated as an node module in a pretty straightforward way. You
Expand Down
14 changes: 14 additions & 0 deletions cmake/CCache.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
option (CCACHE "Use ccache compilation optimization" OFF)

find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
message(STATUS "ccache found: ${CCACHE_FOUND}")
if(CCACHE)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
else(CCACHE)
message(STATUS "ccache NOT ENABLED via 'CCACHE' variable!")
endif(CCACHE)
else(CCACHE_FOUND)
message(STATUS "ccache not found")
endif(CCACHE_FOUND)
8 changes: 8 additions & 0 deletions cmake/Cotire.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
option (COTIRE "Use Cotire compilation optimization" OFF)
if(COTIRE)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cotire")
set (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES "-j")
include(cotire)
else()
unsetListOfVarsStartingWith("COTIRE_")
endif()
28 changes: 28 additions & 0 deletions cmake/Coverage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
option (CODE_COVERAGE "Enable profiling and coverage report analysis" OFF)

if(CODE_COVERAGE)
if(CMAKE_COMPILER_IS_GNUCXX) # NOT WIN32
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/codecoverage")
include(CodeCoverage)
append_coverage_compiler_flags()

set(COVERAGE_EXCLUDES
"/usr/*"
"${PROJECT_BINARY_DIR}/*"
"${PROJECT_SOURCE_DIR}/core/lib/*"
"${PROJECT_SOURCE_DIR}/core/test/*"
)

setup_target_for_coverage_lcov(
NAME coverage_unit
EXECUTABLE ctest -T Test -L unit
BASE_DIRECTORY "${PROJECT_SOURCE_DIR}/core/src"
)
setup_target_for_coverage_lcov(
NAME coverage_all
EXECUTABLE ctest -T Test
BASE_DIRECTORY "${PROJECT_SOURCE_DIR}/core/src"
)

endif()
endif()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions cmake/cmake-scripts
Submodule cmake-scripts added at 774a07
Loading

0 comments on commit 77db363

Please sign in to comment.