Manage Fortran Code Quality with SonarQube and i-Code CNES

Angeliki Ts
9 min readSep 28, 2019

--

Introduction

Fortran is a general-purpose, compiled imperative programming language that is especially suited to numeric computation and scientific computing. Fortran is rarely used today in industry. In the field of high performance computing (HPC), of which large scale numerical simulation is a subset, there are only two languages in use today, C++ and Fortran.

Code quality is an approximation of how useful and maintainable a specific piece of code is. Good code is high quality. It’s clean code. It stands the test of time. Bad code is low quality. It won’t last long. Essentially, good code quality means that the code:

  1. Does what it should.
  2. Follows a consistent style.
  3. Is easy to understand.
  4. Has been well-documented.
  5. Can be tested.

Measuring quality helps you understand where you’re at. After you’ve measured, you can take steps to improve overall quality.

SonarQube is an automatic code review tool to detect bugs, vulnerabilities and code smells in your code. It can integrate with your existing workflow to enable continuous code inspection across your project branches and pull requests.

i-Code CNES is a static code analysis tool to help developers write code compliant with CNES coding rules for Fortran 77, Fortran 90 and Shell.

In this guide, you will install i-code CNES plugin, learn some tricky stuffs of the plugin, analyse a fortran project and then learn some more tricky stuffs.

Prerequisites

Before you begin this guide you’ll need the following:

  1. Operating System Ubuntu 18.04
  2. Pre-installed SonarQube 7.3 (tutorial link below)
  3. Pre-installed Sonar-Scanner (tutorial link below)
  4. Python 3

First of all, you will need to install SonarQube and Sonar-Scanner in your Linux Machine. You can find an installation guide for SonarQube and Sonar-Scanner here.

Step 1— Download and Install i-Code CNES Sonar Plugin

After you install SonarQube, open a browser and type http://localhost:9000 on the URL. Port 9000 is the default for SonarQube, so if you chose another one, replace 9000 with the port number you chose.

You can download the i-code CNES sonar plugin from Marketplace inside the SonarQube Web Interface.

Click on Login with the default credentials username: admin and password: admin (or your personal credentials). Afterwards, choose Administration and Marketplace as you can see in the picture below. In search, type “Sonar i-Code CNES” and click install. The plugin is not pre-installed inside SonarQube. Once the installation is over, it will ask you to restart the SonarQube Server.

To check that everything is fine go to Quality Profiles and check if Fortran 77 and Fortran 90 are shown. The i-Code Sonar Plugin is installed successfully.

Step 2— Download i-Code CNES Command Line

i-Code CNES executes separately from SonarQube and produces a xml or res file. Then, in sonar-project.properties you need to specify the folder in which the xml or res file is saved so that SonarQube reads from there the analysis. The plugin we downloaded in Step 1 is responsible for reading this res file and the command line tool is responsible for doing the analysis.

According to the documentation, there are three ways to use i-Code, as command line tool, as i-code ide and as eclipse plugin. In this tutorial, we will use the command line tool, you can download it here. Choose the i-Code CNES 3.1.0 from releases. Then, from Assets download the i-CodeCNES-3.1.0-CLI-linux.gtk.x86_64.zip.

Unzip the file in your preferable directory either with right click and Extract here or with the following commands. Don’t forget to change the folder with your preferable directory. Open a terminal and type:

sudo apt-get install unzipunzip  i-CodeCNES-3.1.0-CLI-linux.gtk.x86_64.zip -d folder

You’re done. The command line tool doesn’t need installation. In the next step, we will see how to use it.

Step 3— Execute i-Code CNES Command Line

As a command line tool you will need to use a terminal and move to the directory in which i-Code is saved (first command). Then, type the second command to see the available options.

cd Downloads/i-CodeCNES-3.1.0-CLI-linux.gtk.x86_64/icode
./icode -help

If there is a permission error, change the permissions with the following commands. Go one folder back (cd ..) in order to change the permissions in all files inside the folder (with *).

cd ..
sudo chmod 755 icode/*

Now, you are ready to do a Fortran code analysis. Or maybe not?

If the code is developed in Windows machine you will first need to do a conversion.

Files created on DOS/Windows machines have different line endings than files created on Unix/Linux. DOS uses carriage return and line feed (“\r\n”) as a line ending, which Unix uses just line feed (“\n”). You need to be careful about transferring files between Windows machines and Unix machines to make sure the line endings are translated properly.

i-code CNES analyse only files with Unix Format so you have to convert a DOS file to UNIX format. Luckily, Linux has the answer and it is pretty simple. A tool called dos2unix.

Open a terminal and type the following command to install the tool.

sudo apt install dos2unix

Now, we will use Python 3 to create a script in order to have an automated way to convert all project files (fortran files) to Unix Format. After the conversion, this script executes i-code command line tool. Run the following command to check your python version. If you have version 2 then upgrade to version 3.

python -V

In the following Python script, set the variable projectPath to the absolute path of the project to be analysed and then set the variable pathIcode to the path in which icode executable is saved. If you want to analyse .f90 files change the fileType too.

Let’s take a look in the script below. First of all, it finds all files that end with fileType inside the projectPath and changes the Dos Format to Unix Format by executing the tool dos2unix. It also saves all the files in a list in case you want to execute i-code file by file. Then, it executes i-code for all files in one execution. If the number of files is too big maybe, you will need to split it to segments and execute i-code for each segment.

# Execute I-code
import sys,os
from os.path import join

projectPath= '/home/user/project'
pathIcode = '/home/user/Downloads/icode/icode'
fileType = '.f'

# Find all files in directory and convert any dos line breaks to linux line breaks
files = []
for r, d, f in os.walk(projectPath):
for file in f:
if fileType in file:
files.append(os.path.join(r, file))
command = 'dos2unix ' + os.path.join(r, file)
os.system(command)
# Execute I-code plugin
command = pathIcode + " " + projectPath + "/**/*" + fileType + " -output " + projectPath + "/icode/results.res *" + fileType
os.system(command)

If case you want a different output, or there is only one directory and no subdirectories inside it you can change the command variable. Check the manual for more options by executing the icode with -help parameter.

Let’s break the command variable. First, we use the pathIcode which is the execution command of the i-code CNES. Then, we put a space and the projectPath followed by /**/* and fileType. This defines to analyse every file (including files in subdirectories) that ends with fileType. After that, we define that the output file will be saved in projectPath inside a folder named icode and the file with the metrics is the results.res (the file that SonarQube will read).

Step 4— Execute SonarQube

In order to execute SonarQube you first need to create a configuration file inside the project folder with a name sonar-project.properties. Each project will need to have its own sonar-project.properties file. This file will need to exist in the folder from which you execute the sonar-runner. An example of this file is given below. In this file you should include the last line so that SonarQube enable i-code CNES plugin. The bold part is the folder in which i-code saved the res file.

sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=my:project

# --- optional properties ---

# defaults to project key
#sonar.projectName=My project
# defaults to 'not provided'
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Defaults to .
#sonar.sources=.

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
sonar.icode.path=/icode/results.res

After that, open a terminal, move to project directory where sonar-project.properties file is and run the next command to start the code quality analysis with SonarQube.

$SONAR_RUNNER_HOME/bin/sonar-runner

When the analysis is done, open the browser and type http://localhost:9000. Select the project and see the analysis results.

Step 5— The ugly truth

The i-code CNES plugin needs ruleset configuration. You can observe that every issue is Major and has remediation time 30 minutes. Take a look here to understand better how SonarQube works and how measures code quality and technical debt. In the picture below there is a screenshot of the Tab Issues.

So, if you need a more accurate measurement of code quality you have to create the ruleset based on your needs. For example (look in the picture above), if you believe that the rule “the code is not indented” is not Major but Minor and the remediation time is not 30 minutes but 2 minutes you should make the above changes. But how? Well, you will need to change a file, create a jar again and then save it in the plugins folder of SonarQube.

Step 6— Ruleset Configuration

Download the code from here. In folder plugin/src/main/resources/rules/ there are 3 xml files. The files contain the rules for fortran77, fortran90 and shell respectively. Let’s say you want to change the rules configurations for fortran77. When you open the file, you will see a <rule> tag for every rule as shown below.

Find the rules you like to change and replace the values in severity and remediationFunctionBaseEffort. The new values is at your discretion. If you are a researcher you can do a Case Study with industrial companies or if you are a developer you can adapt the rules in your company’s needs. For some projects some rules are more important than others. These rules should have a blocker or critical severity and rules that are not important can have severity major or minor. In addition, the experience of the developers has a high impact on the remediation time to solve an issue.

You can do the changes from a java editor (like Eclipse) or from a text editor (like Atom). I prefer Eclipse, so after you do the changes in the xml files, choose Export to jar file (for example sonaricode-1.4.0.jar). You’re almost done! You have completed the configuration of the ruleset and you are ready to replace the old jar file with the new one.

Move the jar file to $SONARQUBE_HOME/extensions/plugins and restart the SonarQube. Move to Step 3 and Step 4 and repeat the execution of i-Code CNES Command Line Tool and sonar-scanner and Ta-Da!!! You are ready to manage fortran code quality with SonarQube and i-Code CNES.

--

--