xcodebuild generating empty compile_commands.json

6.9k Views Asked by At

I am using following commands to use the oclint with xcode 5-

Step1: xcodebuild -target OClintDemo -configuration Debug -scheme OClintDemo -sdk iphonesimulator
Step2: OClintDemo jenkins$ xcodebuild -sdk iphonesimulator | tee xcodebuild.log
Step3: oclint-xcodebuild xcodebuild.log
Step4: oclint-json-compilation-database -- -o=report.html

but i am getting compile_commands.json empty file, and report.html contains following- OCLint Report Summary: TotalFiles=0 FilesWithViolations=0 P1=0 P2=0 P3=0 [OCLint (http://oclint.org) v0.7]

6

There are 6 best solutions below

0
On

you try this script

#xctool oclint
xctool  -workspace iWeidao.xcworkspace \
        -scheme iWeidao \
        -reporter json-compilation-database:compile_commands.json clean build

oclint-json-compilation-database -v oclint_args "-report-type html -o report.html -rc=LONG_LINE=120"
open compile_commands.json
open report.html

here https://github.com/facebook/xctool/issues/270

0
On

I just want to combine the answers from earlier ones. There are 2 ways

  1. cd path-to-project-file xcodebuild clean build | xcpretty --report json-compilation-database --output compile_commands.json

  2. cd path-to-project-file xcodebuild test -project ProjectName.xcodeproj -scheme ProjectTests -destination 'platform=OS X,arch=x86_64' OTHER_CFLAGS="\$(inherited) -gen-cdb-fragment-path \$(PROJECT_DIR)/compilation-database" cd compilation-database sed -e '1s/^/[\'$'\n''/' -e '$s/,$/\'$'\n'']/' *.json > ../compile_commands.json

1
On

I have found a way to generate a JSON compilation database from an Xcode project without relying on external tools.

Tools that rely on processing xcodebuild output are all deprecated (oclint-xcodebuild, xctool) or buggy at best (xcpretty), as output format is an implementation detail and subject to change.

The following approach was tested with Xcode 13.4.1 on macOS 12 Monterey and Apple Silicon M1 Pro.

In Xcode Build Settings the following compiler flag can be set:
OTHER_CFLAGS = $(inherited) -gen-cdb-fragment-path $(PROJECT_DIR)/CompilationDatabase

Alternatively, the flag can be passed when invoking xcodebuild:
xcrun xcodebuid clean build -project TestProject.xcodeproj -target TestTarget -configuration Debug OTHER_CFLAGS="\$(inherited) -gen-cdb-fragment-path \$(PROJECT_DIR)/CompilationDatabase"

This instructs clang to emit a fragment of the compilation database for each compilation. These fragments can easily be combined into the final compilation database by using the following command:
sed -e '1s/^/[\'$'\n''/' -e '$s/,$/\'$'\n'']/' *.json > compile_commands.json

Of course it's recommended to validate fragments, but that cannot be done without external tools.

For full details regarding compiler flags, validation of fragments, and references, check out the gist:
Generate a JSON Compilation Database from an Xcode project

0
On
  1. try clean the build first, then build again
  2. make sure no white space in the file name or path
1
On

Xcode 8 does not support xctool, you can use xcpretty. Your xcodebuild command should be like this

If you are using a workspace

xcodebuild -workspace WORKSPACE_NAME.xcworkspace -scheme SELECTED_SCHEME | xcpretty -r json-compilation-database --output compile_commands.json

For a single project

xcodebuild -project PROJECT_NAME.xcodeproj | xcpretty -r json-compilation-database --output compile_commands.json
0
On

Here's how I got a proper compile_commands.json file (Xcode 8.3)

Clean your build

xcodebuild clean -workspace WORKSPACE.xcworkspace/ -scheme "SCHEME"

This ensures xcodebuild recompiles all of your files, which would then make them appear in the compilation database.

Build and generate compile_commands.json:

xcodebuild -workspace WORKSPACE.xcworkspace/ -scheme "SCHEME" | xcpretty -r json-compilation-database -o compile_commands.json

You should obviously replace the workspace and scheme with your projects values. You can also run this against a project by using -project instead of -workspace.