How to create LLVM bit code for a file using compilation database?

I want to create LLVM bit code for a large number of C source files for which I have a compilation database . Is there a way to cause clangit to read the compilation database and use the appropriate flags?

Background

For toy programs, the LLVM bit code generation command is simple:

clang -emit-llvm -c foo.c -o foo.bc

However, source files in large projects require large amounts of additional compilation flags, including -Iand -D, and something else.

I want to write a script that iterates over a large number of source files and calls clang -emit-llvm ...for each to generate LLVM bitcode. The difficulty is that each command clang -emit-llvm ...must have flags specific to this source file. I have a compilation database for these source files that perfectly captures the flags needed for each individual source file. Is there a way to make the clang -emit-llvm ...information on the basis of my compilation?

One solution that I thought of is to independently analyze the compilation database and find the corresponding entry for each source file and change the entry commandto (a) include -emit-llvmand (b) change -o foo.oto -o foo.bc, and then run the command. It might work, but it seems a bit hacked.

+4
source share
1 answer

Instead of analyzing the compilation database yourself, you can rely on the Python binding for this. Judging by the binding test set , you can do something like:

cdb = CompilationDatabase.fromDirectory(kInputsDir)
cmds = cdb.getAllCompileCommands()

and then slightly refresh the contents cmds.

0
source

All Articles