Building a Build Server – Checking Code Complexity

During the previous week, we covered setup of two tools integrating code quality checks into Jenkins (dupFinder and InspectCode), both contained in the ReSharper Command Line Tools package. In this article, we are going to further improve the code quality check of our build server by adding a code complexity checker, CCM.

CCM calculates a code metric on the codebase called cyclomatic complexity. This metric is basically measuring the count of possible different code-paths to go through the method. To read more about cyclomatic complexity, visit the cyclomatic complexity article on Wikipedia.

Little disclaimer: CCM unfortunately does not support the Visual Basic.NET language. I’ll do a post on other tools, when I find other ones that can easily integrate with Jenkins.

Installing CCM

Open the Cygwin terminal and execute the following commands:

mkdir /tmp/CCM

cd /tmp/CCM

wget https://github.com/jonasblunck/ccm/releases/download/v1.1.11/ccm.1.1.11.zip

unzip ccm.1.1.11.zip

rm ccm.1.1.11.zip

cd /cygdrive/c

mv /tmp/CCM /cygdrive/c

icacls CCM /T /Q /C /RESET

After the download and unpack of the CCM package, we need to start it once before usage. The reason behind this is that on newer operating systems, the .NET package used to build the executable is not installed by default. To do this, open a command prompt and issue the following command:

C:\CCM\CCM.exe

If you see the console output starting with the following text, you have the tool and the dependencies installed correctly:

Argument error.

 

ccm will analyze you code base and provide cyclomatic complexity metrics.

Supported languages are c/c++ (.c, .cpp, .h, .hpp), c# (.cs) and javascript (.js)

 

 

Usage:

  ccm [config-file]

  ccm [path-to-analyze] [/xml] [/v] [/ignorecases] [/threshold=5] [/nummetrics=10]

On the other hand, if you see the window below, you are missing the .NET Framework required to execute the binary. By clicking on the Download and install this feature option in the dialog, windows will install the required framework for you. This was the situation in my case, when I started the executable on Windows 10 Home. If you fail to do this, Jenkins will give you a cryptic error during the build.

Configure the CCM execution for the project

To configure how CCM will analyze the project we need to add a configuration file into the codebase. We can find one in our favorite code to analyze in the root directory called ccmconfig.xml. A complete documentation exists about the possible configuration options on Jonas’s GitHub page for CCM (see link of the CCM application above). He is the best authority on it, as he is the developer of the CCM application.

Installing necessary plugins

The plugin required to process the build result for this task is called CCM Plug-in and you can add it by selecting the Manage Jenkins / Manage Plugins menu from the main Jenkins page.

Integrating CCM into Jenkins

To try out this tool, we are going to use the repository at https://github.com/atzimler/CodeDuplicates.git.

The test build project has the following build configuration:

Source Code Management

Use Git source code management with

Repositories

Repository URL

https://github.com/atzimler/CodeDuplicates.git

   

Branches to build

Branch specifier (blank for ‘any’)

*/master

Repository browser:

(Auto)

Build

Execute Windows batch command

Command

C:\CCM\CCM.exe ccmconfig.xml >ccm.xml

Post-build Actions

Publish CCM analysis results

CCM results

ccm.xml

Two small things to note at the end:

  1. Even though the CCM results field in the CCM plugin configuration options mentions that if no value is set, then it will use the default **/ccm.xml, that does not seem to happen in plugin version 3.1.
  2. The plugin version 3.1 does not contain option for adjusting the threshold levels. It seems that the priorities are set as the following:

What to ignore

You set the value for ignore in the configuration file (of CCM in your source code).

Low

0-10

Normal

11-20

High

21 and above

 

The threshold level configuration seems like one accepted approach on the Internet, though I would prefer being able to set these. Probably I will tackle both issues later by editing the plugin itself. As currently I am working on a project where we use Visual Basic.NET this can wait, as first I will need to find a CCM tool for that language. If you know any modification of the CCM plugin that allows the configuration and possibly correct the first problem, please share it.

you can connect with me on LinkedIn or Twitter. Share if you found this article useful.

By subscribing to the email list, you will be notified about new posts.
Loading
No comments yet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress. Designed by WooThemes