oclint踩坑记

3,065 阅读1分钟

安装oclint

  1. 下载资源
  2. 配置环境变量
  3. 验证 安装参考官方文档,不赘述
  4. 脚本配置
source ~/.zshrc
cd ${SRCROOT}
xcodebuild clean
xcodebuild -workspace testPrivatePod.xcworkspace -scheme testPrivatePod | xcpretty -r json-compilation-database
oclint-json-compilation-database -- -report-type xcode

使用:cmd+b编译 报错

Error: compile_commands.json not found at xxxx

脚本修改

xcodebuild -workspace testPrivatePod.xcworkspace -scheme testPrivatePod | xcpretty -r json-compilation-database

继续报错

accessing build database "/Users/xxx/Library/Developer/Xcode/DerivedData/testPrivatePod-dkrzybvaowacztfjutpjkzqosbrf/Build/Intermediates.noindex/XCBuildData/build.db": database is locked Possibly there are two concurrent builds running in the same filesystem location.

添加条件

-UseModernBuildSystem=NO

如果去掉仍旧报上面的错误

继续报错

Could not delete `/build` because it was not created by the build system

编译在项目目录下面生成了build文件夹,手动删除即可

重新编译仍旧报错

Error: compile_commands.json not found at xxxx

在根目录手动创建一个这样的文件,也可以配置如下,表示输出一个文件

xcodebuild -workspace testPrivatePod.xcworkspace -scheme testPrivatePod -UseModernBuildSystem=NO | xcpretty -r json-compilation-database  --output compile_commands.json

cmd+b

仍旧报错

LLVM ERROR: CommonOptionsParser: failed to parse command-line arguments. [CommonOptionsParser]: oclint: Not enough positional command line arguments specified!
Must specify at least 1 positional argument: See: /Users/xxx/oclint-21.03/bin/oclint --help

看起来是参数有问题,说是至少需要一个参数,但是代码都是从官网copy的,究竟哪里错了呢?

注释下面这一行并编译,不报错

oclint-json-compilation-database -exclude Pods -- -report-type xcode

所以应该是这行命令出现了问题

更换为别人说好用的命令

unset LLVM_TARGET_TRIPLE_SUFFIX

# Rules
LINT_LONG_LINE=300
LINT_LONG_VARIABLE_NAME=64
LINT_LONG_METHOD=150

LINT_RULES="-rc LONG_LINE=${LINT_LONG_LINE} \
    -rc LONG_VARIABLE_NAME=${LINT_LONG_VARIABLE_NAME} \
    -rc LONG_METHOD=${LINT_LONG_METHOD}"

# Threshold.
LINT_PRIORITY_1_THRESHOLD=0
LINT_PRIORITY_2_THRESHOLD=20
LINT_PRIORITY_3_THRESHOLD=30
LINT_THRESHOLD = "-max-priority-1=${LINT_PRIORITY_1_THRESHOLD} \
    -max-priority-2=${LINT_PRIORITY_2_THRESHOLD} \
    -max-priority-3=${LINT_PRIORITY_3_THRESHOLD}"

# Excludes
# you can use grep-like regular expressions syntax,
LINT_EXCLUDES="Pods|lib"

oclint-json-compilation-database \
    -exclude ${LINT_EXCLUDES} \
    -- \
    -report-type xcode \
    ${LINT_RULES} \
    ${LINT_THRESHOLD} \

尝试解决方案:执行oclint之前清除缓存

....
rm -rf ~/Library/Developer/Xcode/DerivedData/

oclint-json-compilation-database \
    -exclude ${LINT_EXCLUDES} \
    -- \
    -report-type xcode \
    ${LINT_RULES} \
    ${LINT_THRESHOLD} \

没有用,错误还多了

尝试解决方案:导出分析日志到html

oclint-json-compilation-database -e Pods -- --verbose -rc CYCLOMATIC_COMPLEXITY=10 -rc LONG_CLASS=1000 -report-type html -o oclint.html

仍旧报错参数问题

注释oclint的命令,又莫名其妙的报下面的错

invalid byte sequence in US-ASCII 

解决方案,执行脚本之前设置语言

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

worked

打开oclint命令 报错

/Users/xxxx/Desktop/其他/myDemo/tododemoios/oclint:1:1: compilation contains multiple jobs:

解决方案将buildsetting中的所有COMPILER_INDEX_STORE_ENABLE 属性设置成NO,podfile更新如下

  post_install do |installer|
      installer.pods_project.targets.each do |target|
          target.build_configurations.each do |config|
              config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO"
          end
      end
  end

pod install,重新编译,报错

oclint: error: violations exceed threshold

错误太多超过阈值 修改命令如下

oclint-json-compilation-database -e Pods -- -report-type xcode -max-priority-1=9999 -max-priority-2=9999 -max-priority-3=9999

大功告成

目前是xcode里面报警告

尝试导出到html并打开

oclint-json-compilation-database -e Pods -- -report-type html -o oclint.html -max-priority-1=9999 -max-priority-2=9999 -max-priority-3=9999

open oclint.html

成功