Compile a project into LLVM Bitcode

Hi all,

I'm trying to compile projects into LLVM Bitcode files instead of executable binaries, without needing to change the Makefile and the configure script.

What is the simplest way of doing that ?

Thanks

Julien

You can use Clang to compile source to bitcode:

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

Hope that helps,
Philip

You can use Clang to compile source to bitcode:

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

Yes.
My problem is to automatically create .bc files with ./configure && make. Ideally, when compiling a project like "grep", I'd like to obtain a single .bc file containing all the project's functions, instead of the grep binary executable.

I already tried things like
CC=clang CFLAGS=-emit-llvm ./configure

But the ./configure fails (indeed, .bc files are not executable, so configure says the C compiler doesn't work)

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for gawk... (cached) gawk
checking for gcc... clang
checking whether the C compiler works... no
configure: error: in `/local/jhenry/examples/grep-2.8':
configure: error: C compiler cannot create executables
See `config.log' for more details

You can use Clang to compile source to bitcode:

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

Yes.
My problem is to automatically create .bc files with ./configure&&
make. Ideally, when compiling a project like "grep", I'd like to obtain
a single .bc file containing all the project's functions, instead of the
grep binary executable.

There are two approaches that you can take:

1) You can modify libLTO so that it saves into a file the entire bitcode that it analyzes. By default, it just creates the bitcode file in memory, optimizes it, and then generates native code. A small addition can get it to save its results right before it generates native code.

2) If you're doing whole-program analysis or transformation, you can build your analysis/transform directly into libLTO.

-- John T.