11

I have created my custom clang plugin with help of this tutorial and I want to run it automatically on my Xcode iOS project.

I need to run following additional commands on clang,

-Xclang -load \
-Xclang ~/static_analysis/llvm/Debug+Asserts/lib/libPluginExample.so \
-Xclang -plugin -Xclang -example-plugin \

I would like to save all other commands generated by Xcode, because it is difficult to create and pass those commands for every Xcode project. That is the reason why I choose to use clang plugin but not clang tool.

How can I do achieve this?

Or how can I extract compiler flags generated by xcode aoutomtically, to use them in clang tool? (becouse, for correct using tool I need to specify all include directories, and all sources, and all frameworks)

Update:

I have added thous commands in Project

Settings -> Build Phases -> Compile Sources (double click on source)

, but in compile time there is error (plugin is standard example libPrintFunctionNames.dylib from clang sources):

error: unable to load plugin '/Users/...llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib': 'dlopen(/Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib, 9): Symbol not found: __ZN5clang11ASTConsumer21HandleInterestingDeclENS_12DeclGroupRefE Referenced from: /Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib Expected in: flat namespace in /Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib' Command /Applications/Xcode 2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1

I have tried to use libPrintFunctionNames.a instead of libPrintFunctionNames.dylib, but it doe not help.

Maybe the cause is that I built my plugin on separated source files of llvm and clang, and in xcode I use other version of clang. I will check that.

1 Answer 1

6

I would specify additional Clang options in build option OTHER_CFLAGS. You can do so in

target/project Build Settings -> Apple LLVM 5.0 - Custom Compiler Flags -> Other C Flags

Or you can specify OTHER_CFLAGS for xcodebuild, for example,

xcodebuild -scheme SampleProject build OTHER_CFLAGS="-Xclang -load -Xclang /path/to/libPrintFunctionNames.dylib -Xclang -plugin -Xclang print-fns"

xcodebuild is convenient when you don't want to maintain 2 targets which differ only in OTHER_CFLAGS.

But you're right, it looks like you really need to link against the same libraries as clang itself is linked. At least I've downloaded Clang+LLVM 3.3 binaries from http://llvm.org/releases/download.html , built the plugin with downloaded libraries and it works with clang from http://llvm.org, but doesn't work with clang from Xcode - I encounter the following error:

error: unable to find plugin 'print-fns'

I've created Xcode workspace which builds Clang plugin and shows how you can try to use it with default iOS application. You can find it at https://github.com/vsapsai/ClangPluginExample

Sign up to request clarification or add additional context in comments.

2 Comments

I've faced the same issue, but managed to solve via small Xcode hack. railsware.com/blog/2014/02/28/… Here is more detailed explanation about how to make it work with Xcode.
Good point, @AlexDenisov. Another way to force Xcode to use custom compiler is via xcodebuild. The command looks like xcodebuild [..some arguments..] CC=/path/to/custom/clang CXX=/path/to/custom/clang

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.