2
0

compile_clang.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/bash
  2. #-------------------------------------------------------------------------------------------------------
  3. # Copyright (C) Microsoft. All rights reserved.
  4. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. #-------------------------------------------------------------------------------------------------------
  6. LLVM_VERSION="5.0.0"
  7. DEFAULT_COLOR='\033[0m'
  8. ERROR_COLOR='\033[0;31m'
  9. GREEN_COLOR='\033[0;32m'
  10. CC_URL="git://sourceware.org/git/binutils-gdb.git\nhttp://llvm.org/releases/${LLVM_VERSION}/\n"
  11. if [[ $1 == '-y' || $1 == '-Y' ]]; then
  12. ALWAYS_YES=1
  13. fi
  14. WARN_LICENSE () {
  15. echo -e "${ERROR_COLOR}"
  16. echo -e "----------------------------------------------------------------"
  17. echo -e "${DEFAULT_COLOR}"
  18. echo -e "This script will download LLVM/CLANG and LLVM Gold Bintools from\n${CC_URL}\n"
  19. echo "These software are licensed to you by its publisher(s), not Microsoft."
  20. echo "Microsoft is not responsible for the software."
  21. echo "Your installation and use of the software is subject to the publisher's terms available here:"
  22. echo -e "http://llvm.org/docs/DeveloperPolicy.html#license\nhttp://llvm.org/docs/GoldPlugin.html#licensing"
  23. echo -e "${ERROR_COLOR}"
  24. echo -e "----------------------------------------------------------------\n"
  25. echo -e "${GREEN_COLOR}If you don't agree, press Ctrl+C to terminate${DEFAULT_COLOR}"
  26. WAIT_QUESTION="Hit ENTER to continue (or wait 20 seconds)"
  27. if [[ $ALWAYS_YES == 1 ]]; then
  28. echo "$WAIT_QUESTION : Y"
  29. else
  30. read -t 20 -p "$WAIT_QUESTION"
  31. fi
  32. echo -e "\nWell, this will take some time... [and free memory 2GB+]\n"
  33. }
  34. WARN_PACKAGE () {
  35. echo -e "\n${GREEN_COLOR}"
  36. echo -e "----------------------------------------------------------------${DEFAULT_COLOR}"
  37. echo "This script requires (texinfo texi2html csh gawk automake libtool libtool-bin bison flex ncurses-devel)"
  38. echo "Automated installation of these requirements is supported with apt-get and yum only."
  39. echo ""
  40. echo "If you don't have these packages are installed, press Ctrl+C to terminate"
  41. echo -e "${GREEN_COLOR}----------------------------------------------------------------"
  42. echo -e "${DEFAULT_COLOR}"
  43. WAIT_QUESTION="Hit ENTER to continue (or wait 20 seconds)"
  44. if [[ $ALWAYS_YES == 1 ]]; then
  45. echo "$WAIT_QUESTION : Y"
  46. else
  47. read -t 20 -p "$WAIT_QUESTION"
  48. fi
  49. }
  50. DOWNLOAD_HELPER() {
  51. WGET_CTR=1
  52. while [ 1 ]; do
  53. wget --no-dns-cache --tries=3 --retry-connrefused --waitretry=3 $1 >/dev/null 2>&1
  54. if [[ $? == 0 ]]; then
  55. break;
  56. else
  57. if [[ $WGET_CTR == 3 ]]; then
  58. echo "Failed to download $1"
  59. exit 1
  60. fi
  61. WGET_CTR=$(($WGET_CTR + 1))
  62. echo "${WGET_CTR}. try...."
  63. fi
  64. done;
  65. }
  66. ROOT=${PWD}/cc-toolchain/
  67. GOLD_PLUGIN=""
  68. if [ ! -d ./cc-toolchain/src/llvm/projects/compiler-rt ]; then
  69. rm -rf cc-toolchain
  70. mkdir cc-toolchain
  71. cd cc-toolchain
  72. mkdir src
  73. mkdir bin
  74. cd src
  75. if [[ "$OSTYPE" =~ "darwin" ]]; then # osx
  76. echo "This script is not prepared for OSX"
  77. exit 0
  78. else
  79. WARN_LICENSE
  80. apt-get -v >/dev/null 2>&1
  81. if [ $? == 0 ]; then # debian
  82. apt-get install -y apt-file texinfo texi2html csh gawk automake libtool \
  83. libtool-bin bison flex libncurses5-dev
  84. if [ $? != 0 ]; then
  85. WARN_PACKAGE
  86. fi
  87. else
  88. yum -v >/dev/null 2>&1
  89. if [ $? == 0 ]; then # redhat
  90. yum install -y texinfo texi2html csh gawk automake libtool libtool-bin bison flex ncurses-devel
  91. else
  92. WARN_PACKAGE
  93. fi
  94. fi
  95. fi
  96. mkdir lto_utils
  97. cd lto_utils
  98. echo "Downloading LLVM Gold Plugin"
  99. git clone --depth 1 git://sourceware.org/git/binutils-gdb.git binutils >/dev/null 2>&1
  100. mkdir binutils_compile; cd binutils_compile
  101. LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${ROOT}/build/lib/"
  102. ../binutils/configure --enable-gold --enable-plugins --disable-werror --prefix="${ROOT}/build"
  103. make -j4
  104. make install
  105. if [ $? != 0 ]; then
  106. exit 1
  107. fi
  108. echo -e "\n\n\n\n"
  109. cd "${ROOT}/src/"
  110. echo "Downloading LLVM ${LLVM_VERSION}"
  111. DOWNLOAD_HELPER "http://llvm.org/releases/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz"
  112. tar xf "llvm-${LLVM_VERSION}.src.tar.xz"
  113. if [ $? == 0 ]; then
  114. rm "llvm-${LLVM_VERSION}.src.tar.xz"
  115. mv "llvm-${LLVM_VERSION}.src" llvm
  116. else
  117. exit 1
  118. fi
  119. cd llvm/tools/
  120. echo "Downloading Clang ${LLVM_VERSION}"
  121. DOWNLOAD_HELPER "http://llvm.org/releases/${LLVM_VERSION}/cfe-${LLVM_VERSION}.src.tar.xz"
  122. tar xf "cfe-${LLVM_VERSION}.src.tar.xz"
  123. if [ $? == 0 ]; then
  124. mv "cfe-${LLVM_VERSION}.src" clang
  125. rm "cfe-${LLVM_VERSION}.src.tar.xz"
  126. else
  127. exit 1
  128. fi
  129. mkdir -p ../projects/
  130. cd ../projects/
  131. echo "Downloading Compiler-RT ${LLVM_VERSION}"
  132. DOWNLOAD_HELPER "http://llvm.org/releases/${LLVM_VERSION}/compiler-rt-${LLVM_VERSION}.src.tar.xz"
  133. tar xf "compiler-rt-${LLVM_VERSION}.src.tar.xz"
  134. if [ $? == 0 ]; then
  135. mv "compiler-rt-${LLVM_VERSION}.src" compiler-rt
  136. rm "compiler-rt-${LLVM_VERSION}.src.tar.xz"
  137. else
  138. exit 1
  139. fi
  140. fi
  141. GOLD_PLUGIN=-DLLVM_BINUTILS_INCDIR="${ROOT}/src/lto_utils/binutils/include"
  142. mkdir -p "${ROOT}/build"
  143. cd "${ROOT}/src/llvm"
  144. mkdir -p build_
  145. cd build_
  146. cmake ../ -DCMAKE_INSTALL_PREFIX="${ROOT}/build" -DCMAKE_BUILD_TYPE=Release ${GOLD_PLUGIN}
  147. if [ $? != 0 ]; then
  148. cd ..
  149. rm -rf build_
  150. mkdir build_
  151. exit 1
  152. fi
  153. make -j4 install
  154. if [ $? == 0 ]; then
  155. echo -e "Done!\n./build.sh args are given below;\n\n"
  156. # Create a local bfd-plugins and copy LLVMgold.so into there.
  157. mkdir -p "${ROOT}/build/lib/bfd-plugins/"
  158. cp "${ROOT}/build/lib/LLVMgold.so" "${ROOT}/build/lib/bfd-plugins/"
  159. echo "--cxx=${ROOT}/build/bin/clang++ --cc=${ROOT}/build/bin/clang"
  160. fi