下載并安裝
$ brew install cmake ninja
2、clone OCLint項目
$ git clone https://github.com/oclint/oclint
3、進(jìn)入oclint-scripts目錄,執(zhí)行make命令
$ ./make
成功之后會出現(xiàn)build文件夾,里面有個oclint-release就是編譯成功的oclint工具。
設(shè)置oclint工具的環(huán)境變量
設(shè)置環(huán)境變量的目的是為了我們能夠快捷訪問。然后我們需要配置PATH環(huán)境變量,注意OCLint_PATH的路徑為你存放oclint-release的路徑。將其添加到.zshrc,或者.bash_profile文件末尾:
OCLint_PATH=/Users/你的本地用戶路徑/oclint/build/oclint-release
export PATH=$OCLint_PATH/bin:$PATH
執(zhí)行source .zshrc,刷新環(huán)境變量,然后驗(yàn)證oclint是否安裝成功:
$ oclint --version
OCLint (http://oclint.org/):
OCLint version 0.15.
Built May 19 2020 (11:48:49).
出現(xiàn)這個介紹就說明我們已經(jīng)完成了安裝。
1.進(jìn)入項目更目錄
cd /Users/admin/Desktop/OClintTest
查看項目基本信息
xcodebuild -list
輸出為
Information about project "OClintTest":
Targets:
OClintTest
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
OClintTest
這里顯示了對應(yīng)項目的Schemes
2.編譯項目
先clean指定項目OClint,因?yàn)榧闪藀od,所以使用OClint.xcworkspace;然后再Debug 編譯項目了;最后通過xcpretty,使用 -r json-compilation-database 可以生成指定格式的數(shù)據(jù)。編譯成功后,會在項目的文件夾下出現(xiàn) compile_commands.json 文件
xcodebuild -scheme OClintTest -workspace OClintTest.xcworkspace clean && xcodebuild -scheme OClintTest -workspace OClintTest.xcworkspace -configuration Debug | xcpretty -r json-compilation-database -o compile_commands.json
每次編譯之前需要 clean 這時候就會生成根目錄下就會生成compile_commands.json文件,我們需要把compile_commands.json生成html報表。使用 oclint-json-compilation-database 命令對上一步生成的json數(shù)據(jù)進(jìn)行分析,對項目代碼進(jìn)行分析,最終生成report.html文件。OCLint目前支持輸出html,json,xml,pmd,Xcode格式文件。
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html
如果項目工程太大,整個 lint 會比較耗時,所幸 oclint 支持針對某個代碼文件夾進(jìn)行 lint
oclint-json-compilation-database -i 需要靜態(tài)分析的文件夾或文件 -- -report-type html -o oclintReport.html 其他的參數(shù)
有時候錯誤信息比較多利用下面的腳本則可以將報錯信息寫入 log 文件,方便查看
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html 2>&1 | tee 1.log
此時會在根目錄下生成oclintReport.html文件
參考連接:
注:報錯oclint: error: compilation contains multiple jobs:
修改project和target中COMPILER_INDEX_STORE_ENABLE為NO
參考連接:oclint遇到錯誤及解決辦法