xcodebuild generating empty compile_commands.json
Asked Answered
S

6

7

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]

Shellyshelman answered 22/8, 2014 at 9:54 Comment(0)
A
7

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
Aam answered 14/3, 2017 at 15:56 Comment(1)
Thanks for the answer but the question was asked at the time xcode 5Shellyshelman
P
7

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

Precessional answered 26/7, 2022 at 9:33 Comment(1)
Amazing, thanks for posting this!Metz
T
6

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.

Thoracoplasty answered 7/4, 2017 at 15:12 Comment(0)
C
1

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

Clunk answered 28/8, 2014 at 7:51 Comment(0)
K
1

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

Knorr answered 9/7, 2023 at 19:33 Comment(0)
A
-1
  1. try clean the build first, then build again
  2. make sure no white space in the file name or path
Aeriel answered 30/9, 2014 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.