Cocoapods integrated project with OCLint

Posted on Updated on

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.

Setting Up the Project :
1. Download the latest version of oclint from :
http://oclint.org/downloads.html

2. Extract the downloaded file,rename it with a proper name. For eg : we have named it as ‘ oclintrelease ‘.
We are using the following path : $Home/oclintrelease
In case you are using another path kindly modify the path.

3. In Project Navigator, select the project and add a new target in the project, and choose Aggregate as the template.

aggregateoption

4. Name the new target, here we simply call it “OCLint“.
OCLINT name

5. Add a new build phase in the target we just created.Select + option and then choose Run Script option for the phase type.
Run Script

6. Add the following script into your script editor.

OCLINT_HOME=/Users/Dheeraj/Downloads/oclintrelease
export PATH=$OCLINT_HOME/bin:$PATH

hash oclint &> /dev/null
if [ $? -eq 1 ]; then
echo >&2 "oclint not found, analyzing stopped"
exit 1
fi

cd ${TARGET_TEMP_DIR}

if [ ! -f compile_commands.json ]; then
echo "[*] compile_commands.json not found, possibly clean was performed"
echo "Workspace Path : ${MY_WORKSPACE}"
echo "[*] starting xcodebuild to rebuild the project.."
# clean previous output

if [ -f xcodebuild.log ]; then
rm xcodebuild.log
echo "Oclint Clean performed"
fi

cd ${SRCROOT}

xcodebuild clean

#build xcodebuild.log
xcodebuild ONLY_ACTIVE_ARCH=NO -workspace ${PROJECT_NAME}.xcworkspace -scheme ${PROJECT_NAME} -configuration Debug clean build| tee ${TARGET_TEMP_DIR}/xcodebuild.log
#xcodebuild | tee ${TARGET_TEMP_DIR}/xcodebuild.log

echo "[*] transforming xcodebuild.log into compile_commands.json..."
cd ${TARGET_TEMP_DIR}
#transform it into compile_commands.json
oclint-xcodebuild

fi

echo "[*] starting analyzing"
cd ${TARGET_TEMP_DIR}

oclint-json-compilation-database -e /Users/Dheeraj/Desktop/sampleCocoaPods/Pods/ -v oclint_args "-report-type html -o /Users/Dheeraj/NewHTMLREPORT.html" | sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/'

It will exclude all the Pods related files.

If you want to include Pods file as well then replace last line in script by :

oclint-json-compilation-database -v oclint_args "-report-type html -o /Users/Dheeraj/NewHTMLREPORT.html" | sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/'

7. Choose the correct build scheme, here we choose OCLint.
select scheme

8. Click to build, or use the shortcut Command+B.
see build version

9. A report would be generated to the specified path(i.e OCLINT_HOME) with a name report.html.

Notes :
1. If you want to exclude folders from your Project then : oclint-json-compilation-database -e
into last line of script.
2. Clean and build the project.
3. Wait for the project to build as it takes long time to build the project.
4. Please try first with a short sample Application including cocoapods and once you have generated report for sample application then integrate script into your real application as building with OCLint takes a lot of time to generate the report.

Important reference Links :
1. Oclint Website : http://oclint.org/
2. Oclint with Xcode : http://docs.oclint.org/en/dev/guide/xcode.html
3. Stack Overflow answer : http://stackoverflow.com/a/30521758/3141464

One thought on “Cocoapods integrated project with OCLint

    SutoCom said:
    May 29, 2015 at 10:04 am

    Reblogged this on SutoCom Solutions.

Leave a comment