Projects/Edu/KStars/C++ developer tools with KStars on Linux

From KDE TechBase
< Projects‎ | Edu‎ | KStars
Revision as of 17:59, 15 July 2017 by Ckertesz (talk | contribs)

This page describes how to use open-source C++ tools to hunt down memory errors and improve the code quality.

Reduce compilation time

CCache

Official homepage: https://ccache.samba.org

CCache is a useful tool to cache the compiled object files to avoid recompilation of the same source files. A typical use case is changing git branches when some include files are modified. By default, the compiler would recompile the affected source files after each branch checkout. With CCache, the build system can get the already-compiled object file from a local cache on the disk if available.

Install CCache on Ubuntu Linux:

  sudo apt-get install ccache

Add the -DCCACHE_SUPPORT=ON to the CMake flags to enable this feature. For example:

  cmake . -DCCACHE_SUPPORT=ON

You can build KStars after these steps normally, CMake will put the compiler invocation after the ccache command and the object files will be saved to the local cache when a make command is executed.

Unity Build

Unity Build is a build technique to include the existing C++ files into temporary super C++ files and build these super files with the compiler instead of the original. The advantage of this method is that the compiler is invoked less times and the build time can be reduced dramatically.

In the KStars build system, a cmake flag (-DUNITY_BUILD=ON) is needed to enable this support. For example:

  cmake . -DUNITY_BUILD=ON

To get an idea about the speed-up: the KStars build was cut to 8 min from 21 min on my older laptop.

Code quality tools

Clang Format

Official homepage: https://clang.llvm.org/docs/ClangFormat.html

Clang Format can indent the source codes with a defined style. The config file (.clang-format) is in the root of the KStars Git repository. The clang format can be installed easily in Ubuntu Linux from the LLVM repositories (http://apt.llvm.org/):

  sudo apt-get install clang-format-5.0

The current bleeding-edge Clang Format does not support value indentation (style option: AlignConsecutiveMacros) after #define as of now (June 2017), therefore, a patched package is uploaded to this wiki page which contains the "define patch" from https://reviews.llvm.org/D28462. Unfortunately, the package file is zipped because this wiki does not support to upload .deb files directly: File:Clang-format-package.zip.

When clang-format is installed, a cmake flag (-DFORMAT_CODE=ON) is needed to enable Clang Format support. For example:

  cmake . -DFORMAT_CODE=ON

To format the code, a make command must be executed:

  make clang-format

Sanitizers with Clang

Official homepages:


The two most useful Clang sanitizers are useful to detect memory handling errors (ASan) or undefined behaviors (UBSan). The application/library must be recompiled with special compiler flags to have an instrumented binary. The checks are performed during runtime with minimal slowdown.

When Clang is installed and used to compile KStars under Linux or macOS, a cmake flag (-DCLANG_SANITIZERS=ON) is needed to enable this support. For example:

  cmake . -DCLANG_SANITIZERS=ON

Export these environmental variables before running the recompiled KStars:

  export ASAN_OPTIONS=detect_odr_violation=0
  export UBSAN_OPTIONS=print_stacktrace=1

The reasons for the above things: skip the odr checks which are not useful and print backtraces for the undefined behavior errors.

To get demangled backtraces, at least on Ubuntu, a symlink (llvm-symbolizer) must be created from llvm-symbolizer-x.x because AddressSanitizer will call this binary without versioning in the executable name.

Other sanitizers:

  • MemorySanitizer: basically, a subset of AddressSanitizer checks.
  • Control Flow Sanitizer: big slowdown in compilation time+runtime, but it will detect mostly the same errors like UBSan.

Valgrind

Official homepage: http://valgrind.org/

Valgrind is a memory error checker. There is a suppression file in the repository at tools/valgrind.supp. To start KStars with valgrind, go to the root of the git repository and run this example command:

  valgrind --leak-check=full --show-reachable=yes --track-origins=yes --gen-suppressions=all --suppressions=tools/valgrind.supp kstars