mac下通過xcodebuild使用oclint

openglnewbee發表於2014-07-03

step1 :下載oclint並安裝

下載地址: http://oclint.org/downloads.html

選擇mac os x或者darwin的包,下載到本地。

目錄類似下面:

oclint-release
|-bin
|-lib
|---clang
|-----3.4
|-------include
|-------lib
|---oclint
|-----rules
|-----reporters

安裝官方指南: http://docs.oclint.org/en/dev/intro/installation.html

我們此時在bash(終端terminal)中執行oclint會告訴我們命令找不到,我們需要把它的執行檔案路徑加入到環境變數或者把執行檔案拷貝到系統目錄。

方法1(推薦):把路徑加到環境變數中(實際是.bash_profile或者.bashrc檔案中,關於這兩者有不知道的同學可以搜尋之)

OCLINT_HOME=/path/to/oclint-release   // /path/to/oclint-release 是執行檔案存放路徑,例如:/Users/xxx/Desktop/oclint-0.9.dev.a6ffa25
export PATH=$OCLINT_HOME/bin:$PATH
關於.bash_profile,mac系統裡本身可能不存在,建立和編輯的步驟見下文:
http://blog.csdn.net/openglnewbee/article/details/36663591
關於bashrc,位於/etc目錄下,使用command+shift+g 可以前往/etc,找到bashrc檔案修改許可權後可以編輯。

方法2:把執行檔案拷貝到系統目錄(請自行嘗試)

A few directories are supposed to be in the system PATH already, to mention a few, /usr/local/bin, /usr/bin, /bin, etc. Therefore, it’s also possible to copy the OCLint binaries into one of these folders, and move the dependencies over. As an example, presumes /usr/local/bin is in the PATH (may require root permission).

  1. cp bin/oclint* /usr/local/bin/
  2. cp -rp lib/* /usr/local/lib/

Dependency libraries are required to be put into appropriate directory, because oclint executable searches $(/path/to/bin/oclint)/../lib/clang, $(/path/to/bin/oclint)/../lib/oclint/rules and $(/path/to/bin/oclint)/../lib/oclint/reporters for builtin headers and dynamic libraries by default.

以上兩個方法執行完其中1個我們就可以說oclint已經安裝完成了,此時在bash中執行oclint應該得到如下提示:

oclint: Not enough positional command line arguments specified!
step 2:通過xcodebuild執行oclint命令
1.在bash中通過cd命令進到需要lint的工程路徑下
2.在bash中執行:xcodebuild | tee xcodebuild.log 
(若工程中存在多個不同的target,按照如下格式輸入: xcodebuild  -target selectedTargetName | tee xcodebuild.log,將需要跑的target名字填入到命令中  
 此命令呼叫xcodebuild進行了編譯並把相關日誌資訊輸入到xcodebuild.log,該log檔案是後續步驟的必要條件,生成在當前的工程目錄中.官方解釋如下:
We need to save the xcodebuild output to a log file, by convention, name it xcodebuild.log. We can use xcodebuild <options> | tee xcodebuild.log to pipe every line of the output to xcodebuild.log file.

3.在bash中執行 oclint-xcodebuild xcodebuild.log
此步驟是利用之前的.log檔案通過oclint-xcodebuild的可執行檔案生成了名為compile_commands.json的json格式檔案,該檔案應該包含了多組內容,其中的key分別為directory、command、file,該檔案不應該為空。此檔案是下一步輸入的必要條件。

4. 在bash中執行 oclint-json-compilation-database,此步驟是真正執行了lint,執行完應該在終端輸出結果。
若希望輸出報告到檔案中,執行如下命令:
oclint-json-compilation-database -- -o=report.html

官方參考指南:http://docs.oclint.org/en/dev/guide/xcodebuild.html
注意事項:
1.octool 0.7及以前的版本不支援xcode5.
2.在執行前注意執行clean,也可在step2中的第二步執行如下命令:

xcodebuild -dry-run | tee xcodebuild.log 說明如下:

If a source file has been built by xcodebuild, and it’s not been modified since last build, then it might not be compiled again when you invoke xcodebuild the second time. In other words, if it happens, this file won’t be shown in the log. So we won’t see it in the compile_commands.json. To avoid that, use clean build by removing all build products and intermediate files from the build directory.

However, cleaning and building the entire project takes longer time, especially for those big projects. In this case, if file structure hasn’t been changed, and build settings haven’t been modified, then it’s okay to keep the existing xcodebuild.log and compile_commands.json to save time.

If the xcodebuild build can be guaranteed to be successful with the options specified, then we could also use -dry-run option to build the project without actually running the commands, so that we can still capture the xcodebuild log but with reduced time.





相關文章