Getting Started with the LLVM System using MinGW

Written by: Henrik Bach

Overview

The LLVM test suite cannot be run on the MinGW port at this point of time.

All of the llvm tools and the cfe build with some manual modifications at the source code level and will (probely) work as intended - Not tested throughly.

bugpoint is excluded to be be build.

Further information about the LLVM directory structure and tool chain can be found on the main Getting Started page.

Requirements

Before you begin to use the LLVM system, review the requirements given below. This may save you some trouble by knowing ahead of time what hardware and software you will need.

Hardware

Any system that can adequately run MinGW is fine. The LLVM source tree and object files, libraries and executables will consume approximately two GB in debug mode.

Software on Windows

Note, that the installation instructions relates to the older version 1.0 of MinGW for Windows.

From MinGW.org you should install at least MinGW-3.1.0-1+.exe, MSYS-1.0.10+.exe, gcc-core-3.4.1+ and gcc-g++-3.4.1+.

From MinGWiki you should download and install in the MinGW folder (in that order): Bison (rename or exclude the m4.exe version from that package) and Flex.

Do not use MinGW binutils 2.13.90 and 2.15.91, due to a mangler bug in ld. Upgrade to at least to binutils 2.15.94 or build binutils 2.15 from source (http://gnu.ftp.org/gnu/binutils). Building binutlis on the local machine requires dmake and perl 5.8.5+. Build perl with dmake in cmd.exe. Note, if dmake can't find gcc, try set your path to the folder where gcc lives (though you have followed the README.win32 for perl). Then, you should do:

  1. Rename or (re)move old perl utils.
  2. Update your path to perl\bin and perl\bin\arch after mingw in the path.
  3. copy pod2man.bat to pod2man and remove dos specific lines in the file.
  4. configure, make and install binutils.
  5. move old binutils from c:\mingw\mingw32\bin to c:\mingw\mingw32\bin\binutils_old even though your environment reaches the new binutils.
  6. Exit all shells.

If you want to compile the cfe then upgrade to texinfo 4.5+ from http://sourceforge.net/projects/gnuwin32.

If you want to create your own makefiles from autoconf, then you should upgrade to at least autoconf 2.59+ and automake 1.9.2+

If you want to be able to test the llvm tools and cfe (This cannot be achieved yet), you should at least do:

  1. Build and install the latest tclsh from Tcl/Tk.
  2. Make a symbolic link to tclsh with this command: ln -s /mingw/bin/tclsh<vers.>.exe /mingw/bin/tclsh.
  3. Install Expect-NT for Windows NT.
  4. Add /mingw/lib/expect-5.21 to your path.
  5. Install DejaGnu - Follow INSTALL/README instructions - Except test, which unfortunately fails on MinGW.
  6. Disable the reference to exp_debug in runtest.exp.
  7. or

    1. Install the latest TCL and Expect from ActiveState.
    2. Install DejaGnu - Follow INSTALL/README instructions - Except test?, which unfortunately fails on MinGW?
Getting Started to compile LLVM

The remainder of this guide is meant to get you up and running with LLVM using MinGW.

In your .profile add: export CPPFLAGS="-DLLVM_ON_WIN32".

Following guide line follows the style given in <--a href="http://llvm.cs.uiuc.edu/docs/CFEBuildInstrs.html"--> Bootstrapping the LLVM C/C++ Front-End.

Unfortunately, the MSYS bash-shell cannot understand options given on the command line in the abbreviated form: OPTION="opt1 ... optn", but understands options given on the form: OPTION+=opt1 ... OPTION+=optn.

  1. Configure and build the LLVM libraries and tools. In below case we have objdir != srcdir:

     % cd objdir
     % srcdir/configure --prefix=/some/path/you/can/install/to \ 
    --enable-targets=x86 % make tools-only TOOLLINKOPTSB+=-limagehelp TOOLLINKOPTSB+=-lpsapi \
    LDFLAGS+=-Wl,--no-keep-memory -r
  2. Install the tools to your --prefix=/some/path/you/can/install/to and add it to your PATH:

     % make install
    
  3. Unpack the C/C++ front-end source.

  4. Make "build" and "install" directories as siblings of the "src" tree:

     % pwd
     /usr/local/example/cfrontend/src
     % cd ..
     % mkdir build install
     % export CFEINSTALL=/some/path/you/can/install/to
     % cd build
    
  5. Configure, build, and install the GCC front-end:

     % cfe-srcdir/configure --prefix=$CFEINSTALL --disable-threads --disable-nls \ 
    --disable-shared --enable-languages=c,c++ --program-prefix=llvm- \
    --srcdir=../../../src/llvm-gcc-1/llvm-gcc --includedir=/C/MinGW mingw32 % make CFLAGS=-O LIBCFLAGS+=-g LIBCFLAGS+=-O2 LIBCXXFLAGS+=-g \
    LIBCXXFLAGS+=-O2 LIBCXXFLAGS+=-fno-implicit-templates all % make install
  6. Put $CFEINSTALL/bin into your PATH.

  7. Go back into the LLVM source tree proper. Rerun configure, using the same options as the last time. This will cause the configuration to now find the newly built llvm-gcc and llvm-g++ executables.

  8. Rebuild your source tree. This shouldn't cause the whole thing to be rebuilt, but it should build the runtime libraries. After the tree is built, install the runtime libraries into your GCC front-end build tree. These are the commands you need:

     % make TOOLLINKOPTSB+=-ldbghelp TOOLLINKOPTSB+=-lpsapi \ 
    LDFLAGS+=-Wl,--no-keep-memory -r % make -C runtime install-bytecode
  9. Optionally, build a symbol table for the newly installed runtime libraries. Although this step is optional, you are strongly encouraged to do this as the symbol tables will make a significant difference in your link times. Use the llvm-ranlib tool to do this, as follows:

     % cd $CFEINSTALL/lib
     % llvm-ranlib libiberty.a
     % llvm-ranlib libstdc++.a
     % llvm-ranlib libsupc++.a
     % cd $CFEINSTALL/lib/gcc/target-triplet/3.4-llvm
     % llvm-ranlib libgcc.a
     % llvm-ranlib libgcov.a
    
  10. Not all is working yet: Test the newly-installed C frontend by one or more of the following means:

    • running the feature & regression tests via make check
    • compiling and running a "hello, LLVM" program in C and C++.
    • running the tests found in the llvm-test CVS module
An Example Using the LLVM Tool Chain
  1. First, create a simple C file, name it 'hello.c':
       #include <stdio.h>
       int main() {
         printf("hello world\n");
         return 0;
       }
           
  2. Next, compile the C file into a LLVM bytecode file:

    % llvm-gcc hello.c -o hello

    Note that you should have already built the tools and they have to be in your path, at least gccas and gccld.

    This will create two result files: hello and hello.bc. The hello.bc is the LLVM bytecode that corresponds the the compiled program and the library facilities that it required. hello is a simple shell script that runs the bytecode file with lli, making the result directly executable. Note that all LLVM optimizations are enabled by default, so there is no need for a "-O3" switch.

    Note: while you cannot do this step on Windows, you can do it on a Unix system and transfer hello.bc to Windows.

  3. Run the program. To make sure the program ran, execute the following command:

    % lli hello.bc

  4. Use the llvm-dis utility to take a look at the LLVM assembly code:

    % llvm-dis < hello.bc | less

  5. Compile the program to native assembly using the LLC code generator:

    % llc hello.bc -o hello.s

  6. Assemble the native assembly language file into a program:

    Not currently possible, but eventually will use NASMW.

  7. Execute the native code program:

    % ./hello.native

Common Problems

If you are having problems building or using LLVM, or if you have any other general questions about LLVM, please consult the Frequently Asked Questions page.

Links

This document is just an introduction to how to use LLVM to do some simple things... there are many more interesting and complicated things that you can do that aren't documented here (but we'll gladly accept a patch if you want to write something up!). For more information about LLVM, check out:


Valid CSS! Valid HTML 4.01! Henrik Bach
The LLVM Compiler Infrastructure
Last modified: $Date: 2005/02/02 05:46:20 $
Hosted by www.Geocities.ws

1