Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build llvm and clang on cygwin

Can I build the LLVM and clang from source code on cygwin?

How long to build LLVM and Clang? Because I try to build them on VirtualBox, it takes about 2 hours.

like image 940
Han Bing Avatar asked Nov 08 '12 20:11

Han Bing


People also ask

Is Clang part of LLVM?

Clang operates in tandem with the LLVM compiler back end and has been a subproject of LLVM 2.6 and later. As with LLVM, it is free and open-source software under the Apache License 2.0 software license. Its contributors include Apple, Microsoft, Google, ARM, Sony, Intel, and AMD.

Is LLVM same as Clang?

LLVM is a backend compiler meant to build compilers on top of it. It deals with optimizations and production of code adapted to the target architecture. CLang is a front end which parses C, C++ and Objective C code and translates it into a representation suitable for LLVM.


2 Answers

Having spent some amount of effort on this, I figure it might be worthwhile to share some of my findings. I managed to build Clang + LLVM 3.4 on Cygwin 1.7.28 (x86).

Cygwin-specific

Fixing the machine integer types

A recent update to Cygwin seems to have broken one of the system headers used by Clang. An incomplete patch was provided on the Cygwin mailing list. Here's the full patch.

Include directories for the C++ standard library

Clang++ (as of 3.4) still hardcodes the include directories for the C++ standard library (it relies on the ones provided by GCC). If the paths are incorrect (as is often the case on Cygwin), you'll encounter issues when including any C++ header (such as <iostream>). Two possible ways to fix this:

  • You can add the correct paths to the code. This is rather inflexible since you'd have to recompile Clang++ every time this changes and this takes a long time on Cygwin. If you want to do this, you can find the code at clang/lib/Frontend/InitHeaderSearch.cpp on line 389.

  • Alternatively, you can create a symbolic link where Clang expects them.

    1. Identify the directories searched by Clang++ by executing:

      echo | clang++ -v -x c++ -
      

      You should find something along the lines of

      ignoring nonexistent directory "/usr/lib/gcc/i686-pc-cygwin/X.Y.Z/include/c++"
      

      There'll probably be several different version numbers X.Y.Z. Pick any of them, and then create a symbolic link to the correct location:

      ln -s A.B.C /usr/lib/gcc/i686-pc-cygwin/X.Y.Z
      

      where A.B.C is the version of GCC that's currently installed.

Colored diagnostics

If you want colored error messages from Clang, be sure to install the ncurses development package (libncurses-devel) with Cygwin Setup beforehand. Without it, Clang will fallback to its colorless mode because LLVM wouldn't be able to detect whether the terminal supports colors.

You can still force colors to appear with -fcolor-diagnostics, but GCC's linker will choke on this flag, which makes it very inconvenient to use.

System-independent

This is the easiest (and longest) part, and for the most part everything in LLVM + Clang's documentation applies here.

  1. Download the source. Even if you only need Clang, you'll still need the LLVM source code.

  2. After extracting both Clang and LLVM, move the Clang's directory into the tools subdirectory of LLVM.

    mv clang-X.Y llvm-X.Y/tools/clang
    
  3. Begin by configuring:

    ./configure --enable-optimized
    

    Clang prefers to choose itself as the compiler, so if you don't have a working Clang installation yet, use the CC and CXX environment variables to force it to use GCC instead.

  4. If everything goes alright, you can start building & installing, which will build both clang and llvm:

    make && make install
    
like image 169
Rufflewind Avatar answered Oct 04 '22 06:10

Rufflewind


Clang (x86, x86_64) and LLVM (x86, x86_64) are currently available as Cygwin packages.

Installing the Cygwin package would reduce much of your trouble

like image 37
Shenal Silva Avatar answered Oct 04 '22 07:10

Shenal Silva