Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify additional clang options for Xcode project?

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.

like image 757
BergP Avatar asked Nov 21 '13 09:11

BergP


People also ask

Should I use Clang or GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

What is the difference between Clang and LLVM?

Here Clang is the frontend and LLVM is the backend. LLVM defines a common intermediate representation (IR) based on the single static assignment (SSA) form. This makes many optimizations to be easily performed on the IR.

What is cc1 Clang?

clang -cc1 is the frontend, clang is the driver. The driver invokes the frontend with options appropriate for your system. To see these options, run: $ clang -### -c hello.c. Some clang command line options are driver-only options, some are frontend-only options.

What is Clang in Xcode?

In Xcode, the LLVM compiler uses the Clang front end (a C-based languages project on LLVM.org) to parse source code and turn it into an interim format. Then the LLVM code generation layer (back end) turns that interim format into final machine code.


1 Answers

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

like image 108
Volodymyr Sapsai Avatar answered Sep 18 '22 08:09

Volodymyr Sapsai