Tuesday, December 29, 2015

(CI) Jenkins PMD report for Objective-C with OCLint

OCLint is a static code analysis tool for improving quality and reducing defects by inspecting C, C++ and Objective-C code and looking for potential problems like:
  • Possible bugs - empty if/else/try/catch/finally statements
  • Unused code - unused local variables and parameters
  • Complicated code - high cyclomatic complexity, NPath complexity and high NCSS
  • Redundant code - redundant if statement and useless parentheses
  • Code smells - long method and long parameter list
  • Bad practices - inverted logic and parameter reassignment
  • ...

Installation:

  • Step 1: Download "oclint-0.x.x-x86_64-darwin-15.0.0.tar" from the downloads page. Unzip this file and see as below:

  • Step 2: Copying OCLint to System PATH. Open Terminal prompt, and input some commands lines as follows:
  • $ cd /Downloads/oclint-0.10.x
    $ cp bin/oclint* /usr/local/bin/
    $ cp -rp lib/* /usr/local/lib/
    
    Other option: Directly adding to PATH at here.
  • Checking installation: Open a new terminal prompt, and execute oclint directly from there and expect message similar to below:
  • $ oclint
    oclint: Not enough positional command line arguments specified!
    Must specify at least 1 positional arguments: See: oclint -help
    

Using OCLint with xcodebuild:

Suppose we have a project with the name as iOSDesignPatterns:

- Create a build script, as follows:
 #!/bin/sh
# Generate PMD reporter
# Step 1: Create xcodebuild.log
xcodebuild clean -project iOSDesignPatterns.xcodeproj/ -scheme 'iOSDesignPatterns' -configuration Debug
xcodebuild build -target iOSDesignPatterns -configuration Debug -scheme iOSDesignPatterns CLANG_ENABLE_MODULE_DEBUGGING=NO CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO | tee xcodebuild.log
# Step 2: Create compile_commands.json
oclint-xcodebuild
# Step 3: Create pmd_oclint.xml
oclint-json-compilation-database -- -report-type pmd -o pmd_oclint.xml

...and run it with:
$ sh build_pmd.sh

After run it successfully, as below:


Using OCLint with Jenkins:

Create a project on Jenkins and clone that project:
Configuring OCLint and PMD plugin:
- Open Configure page of a project on Jenkins CI.
- For execute a script:: Add a new build step and choose "Execute shell". Set up the command as above ("sh build_pmd.sh"). 
- For export PMD report: Add a new post-build action and choose "Publish PMD analysis result". Enter the output artifact name which is defined in the script ("pmd_oclint.xml").
- The result as below:
Build Jenkins CI: You will get an error as below:
If you want to build successful on Jenkins CI, you will need to add some arguments of oclint-json-compilation-database, as follows:
 # Step 3: Create pmd_oclint.xml
oclint-json-compilation-database -- -max-priority-1 '1' -max-priority-2 '50' -max-priority-3 '100' -report-type pmd -o pmd_oclint.xml

Save that script and build Jenkins CI again. The successful result as below:

Tools: XCode 7.x, OCLint 0.10.1
Ref documents: - OCLint.org

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.