1

I am building LLVM 6 on Ubuntu 16.04. I want lld too. But no matter what I do, lld doesn't build/install.

Followed the following instruction from here. Still sifting through first and second :)

  1. Read the documentation.

  2. Read the documentation.

  3. Remember that you were warned twice about reading the documentation.

    In particular, the relative paths specified are important.

  4. Checkout LLVM:

    cd where-you-want-llvm-to-live
    svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
    
  5. Checkout Clang:

    cd where-you-want-llvm-to-live
    cd llvm/tools
    svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
    
  6. Checkout Extra Clang Tools [Optional]:

    cd where-you-want-llvm-to-live
    cd llvm/tools/clang/tools
    svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
    
  7. Checkout LLD linker [Optional]:

    cd where-you-want-llvm-to-live
    cd llvm/tools
    svn co http://llvm.org/svn/llvm-project/lld/trunk lld
    

Didn't download the other optional packages. Since svn download was not working properly (svn: E000104: Error running context: Connection reset by peer), I downloaded respective zip for LLVM, Clang, clang-extra and lld from their github mirrors. Since, I didn't use svn some renaming of the folders had to be done (for e.g. clang-tools-extra-master to just extra).

Then execute the following, after cding into the parent folder of the llvm source folder,

$ mkdir build; cd build
$ cmake ../llvm
$ make

After waiting for +8 hours, I get all the programs (clang, lli, llc etc.) except lld. I am expecting it to appear in the folder build/bin.

I have also tried the option cmake -DLLVM_ENABLE_PROJECTS=llvm/tools/lld ../llvm. (The form -DLLVM_ENABLE_PROJECTS=lld doesn't work and I found this place discussing about the same ENABLE issue.)

I have already tried things with some tweaking three times. Since it takes so much time to compile, I decided to take your help.

A separate issue: While building, linking phase takes 12 GB RAM + 8.8 GB swap space on my laptop!! Does LLVM building really require >20 GB of ram ? (I had closed all other foreground apps, especially firefox)

Please let me know if any more information is needed.

codeman48
  • 1,330
  • 11
  • 17
  • 2
    Regarding the RAM use of the linking phase: this is due to debug information and static linking, see this answer for different approaches to mitigate the problem https://stackoverflow.com/a/44808601 – PaulR Dec 07 '17 at 14:40
  • @PaulR thanks for the link. Will give it a try whenever I build next. I forgot to mention that I used `make -j 2`, hence parallel linking could be another culprit as the link indicates. – codeman48 Dec 07 '17 at 14:58
  • 1
    Now that I think about it, my guess would be that LLVM_PARALLEL_LINK_JOBS mentioned in the post will only work with ninja. But ninja works nicely otherwise, so you could give it a try. – PaulR Dec 07 '17 at 15:01

1 Answers1

1

First of all, I think that the LLVM_ENABLE_PROJECTS option is useful if you use a "flat" source directory layout. In your case, if you performed the checkouts as you wrote, then it's not of much use.

Moreover, what @PaulR wrote about using ninja is a very good suggestion. It's faster and very useful when restarting builds and it spawns as many separate compilation jobs concurrently as it can depending on the nproc without having to specify it explicitly.

For faster build and shorter link times, I'd also suggest enabling a shared libs enabled build using the BUILD_SHARED_LIBS option.

Some general good advice can be found here, which can be summarized as:

  1. use clang to build clang (a bootstrapped or second-stage build).
  2. use the gold linker instead ld for faster linking times.
  3. prefer a build producing shared libraries.

You can skip point 1 for now (although you could install an initial llvm/clang from the system package manager)

Lastly, you could limit the compilation for the specific target backend you currently require using the LLVM_TARGETS_TO_BUILD option.

I've built trunk recently (including lld) with this cmake configuration:

CC=gcc CXX=g++ \
cmake -G Ninja \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=On \
  -DBUILD_SHARED_LIBS=On \

  -DLLVM_ENABLE_ASSERTIONS=On \
  -DLLVM_TARGETS_TO_BUILD="X86" \
  -DLLVM_ENABLE_SPHINX=Off \
  -DLLVM_ENABLE_THREADS=On \
  -DLLVM_INSTALL_UTILS=On \

  -DCMAKE_BUILD_TYPE=Debug \
  [path-to-source-root-dir]

You can add the following flags if you need to build libcxx:

[...]
-DLLVM_ENABLE_LIBCXX=On \
-DLIBCXX_ENABLE_EXCEPTIONS=On \
-DLIBCXX_ENABLE_RTTI=On \
[...]

For a bootstrapped build using another llvm/clang and libc++ you can augment the above command as (having that clang in your $PATH environment variable):

LLVM_TOOLCHAIN_LIB_DIR=$(llvm-config --libdir)

LD_FLAGS=""
LD_FLAGS="${LD_FLAGS} -Wl,-L ${LLVM_TOOLCHAIN_LIB_DIR}"
LD_FLAGS="${LD_FLAGS} -Wl,-rpath-link ${LLVM_TOOLCHAIN_LIB_DIR}"
LD_FLAGS="${LD_FLAGS} -lc++ -lc++abi"

CXX_FLAGS=""
CXX_FLAGS="${CXX_FLAGS} -stdlib=libc++ -pthread"

CC=clang CXX=clang++ \
cmake -G Ninja \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=On \
  -DBUILD_SHARED_LIBS=On \
  -DLLVM_ENABLE_LIBCXX=On \
  -DLLVM_ENABLE_ASSERTIONS=On \
  -DLLVM_TARGETS_TO_BUILD="X86" \
  -DLLVM_ENABLE_SPHINX=Off \
  -DLLVM_ENABLE_THREADS=On \
  -DLLVM_INSTALL_UTILS=On \
  -DLIBCXX_ENABLE_EXCEPTIONS=On \
  -DLIBCXX_ENABLE_RTTI=On \
  -DCMAKE_BUILD_TYPE=Debug \
  -DCMAKE_CXX_FLAGS="${CXX_FLAGS}" \
  -DCMAKE_SHARED_LINKER_FLAGS="${LD_FLAGS}" \
  -DCMAKE_MODULE_LINKER_FLAGS="${LD_FLAGS}" \
  -DCMAKE_EXE_LINKER_FLAGS="${LD_FLAGS}" \
  -DCMAKE_POLICY_DEFAULT_CMP0056=NEW \
  -DCMAKE_POLICY_DEFAULT_CMP0058=NEW \
  [path-to-source-root-dir]

Moreover, another relevant SO question of interest can be found here.

As you wrote, reading the documentation, experimenting and reading the documentation again is the way to go.

compor
  • 2,239
  • 1
  • 19
  • 29
  • I tried the first cmake configration you gave (without the flags for libcxx) in a fresh build folder, but I get linker error continuously. Though I tried running `ninja` countless times (since each invocation did link some files) it doesn't move beyond [2/200] mark. The error is: `/home/codeman/mydata/local/packages-live/clang-llvm6/llvm/include/llvm/Analysis/DominanceFrontier.h:122: undefined reference to 'llvm::DominanceFrontierBase::DominanceFrontierBase()' collect2: error: ld returned 1 exit status` – codeman48 Dec 11 '17 at 09:50
  • 2
    things break sometimes when you work on trunk. are you willing to try with `LLVM` 5.0.0. I'll try to find the git refs of when I've built it last. – compor Dec 11 '17 at 11:21
  • The command worked on LLVM 5.0.0 release, with clang, clang-extra and lld !! With ninja the whole thing got built in 2 hours! (will compare it with `make -j 4` soon). @compor you were right LLVM Trunk must be buggy at this point. I followed the same steps and got `lld` where it needed it to be. Thanks! – codeman48 Dec 12 '17 at 11:30
  • 1
    The difference between `ninja` and `make` will be most palpable when you modify and rebuild. Last built `LLVM`trunk (6.0.0) that worked for me is "a694e228c261eec9940ea99cf60de8da4453bf8e" (if still interested), but that's a moving target since there are other projects as well. Anyhow, if you absolutely need trunk, play around with different checkouts. – compor Dec 12 '17 at 12:09