compile_clang.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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="4.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. ROOT=${PWD}/cc-toolchain/
  51. GOLD_PLUGIN=""
  52. if [ ! -d ./cc-toolchain/src/llvm/projects/compiler-rt ]; then
  53. rm -rf cc-toolchain
  54. mkdir cc-toolchain
  55. cd cc-toolchain
  56. mkdir src
  57. mkdir bin
  58. cd src
  59. if [[ "$OSTYPE" =~ "darwin" ]]; then # osx
  60. echo "This script is not prepared for OSX"
  61. exit 0
  62. else
  63. WARN_LICENSE
  64. apt-get -v >/dev/null 2>&1
  65. if [ $? == 0 ]; then # debian
  66. apt-get install -y apt-file texinfo texi2html csh gawk automake libtool \
  67. libtool-bin bison flex libncurses5-dev
  68. if [ $? != 0 ]; then
  69. WARN_PACKAGE
  70. fi
  71. else
  72. yum -v >/dev/null 2>&1
  73. if [ $? == 0 ]; then # redhat
  74. yum install -y texinfo texi2html csh gawk automake libtool libtool-bin bison flex ncurses-devel
  75. else
  76. WARN_PACKAGE
  77. fi
  78. fi
  79. fi
  80. mkdir lto_utils
  81. cd lto_utils
  82. echo "Downloading LLVM Gold Plugin"
  83. git clone --depth 1 git://sourceware.org/git/binutils-gdb.git binutils >/dev/null 2>&1
  84. mkdir binutils_compile; cd binutils_compile
  85. LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${ROOT}/build/lib/"
  86. ../binutils/configure --enable-gold --enable-plugins --disable-werror --prefix="${ROOT}/build"
  87. make -j2
  88. make install
  89. if [ $? != 0 ]; then
  90. exit 1
  91. fi
  92. echo -e "\n\n\n\n"
  93. cd "${ROOT}/src/"
  94. echo "Downloading LLVM ${LLVM_VERSION}"
  95. wget --quiet "http://llvm.org/releases/${LLVM_VERSION}/llvm-${LLVM_VERSION}.src.tar.xz" >/dev/null 2>&1
  96. tar -xf "llvm-${LLVM_VERSION}.src.tar.xz"
  97. if [ $? == 0 ]; then
  98. rm "llvm-${LLVM_VERSION}.src.tar.xz"
  99. mv "llvm-${LLVM_VERSION}.src" llvm
  100. else
  101. exit 1
  102. fi
  103. cd llvm/tools/
  104. echo "Downloading Clang ${LLVM_VERSION}"
  105. wget --quiet "http://llvm.org/releases/${LLVM_VERSION}/cfe-${LLVM_VERSION}.src.tar.xz" >/dev/null 2>&1
  106. tar -xf "cfe-${LLVM_VERSION}.src.tar.xz"
  107. if [ $? == 0 ]; then
  108. mv "cfe-${LLVM_VERSION}.src" clang
  109. rm "cfe-${LLVM_VERSION}.src.tar.xz"
  110. else
  111. exit 1
  112. fi
  113. mkdir -p ../projects/
  114. cd ../projects/
  115. echo "Downloading Compiler-RT ${LLVM_VERSION}"
  116. wget --quiet "http://llvm.org/releases/${LLVM_VERSION}/compiler-rt-${LLVM_VERSION}.src.tar.xz" >/dev/null 2>&1
  117. tar -xf "compiler-rt-${LLVM_VERSION}.src.tar.xz"
  118. if [ $? == 0 ]; then
  119. mv "compiler-rt-${LLVM_VERSION}.src" compiler-rt
  120. rm "compiler-rt-${LLVM_VERSION}.src.tar.xz"
  121. else
  122. exit 1
  123. fi
  124. fi
  125. GOLD_PLUGIN=-DLLVM_BINUTILS_INCDIR="${ROOT}/src/lto_utils/binutils/include"
  126. mkdir -p "${ROOT}/build"
  127. cd "${ROOT}/src/llvm"
  128. mkdir -p build_
  129. cd build_
  130. cmake ../ -DCMAKE_INSTALL_PREFIX="${ROOT}/build" -DCMAKE_BUILD_TYPE=Release ${GOLD_PLUGIN}
  131. if [ $? != 0 ]; then
  132. cd ..
  133. rm -rf build_
  134. mkdir build_
  135. exit 1
  136. fi
  137. make -j4 install
  138. if [ $? == 0 ]; then
  139. echo -e "Done!\n./build.sh args are given below;\n\n"
  140. # Create a local bfd-plugins and copy LLVMgold.so into there.
  141. mkdir -p "${ROOT}/build/lib/bfd-plugins/"
  142. cp "${ROOT}/build/lib/LLVMgold.so" "${ROOT}/build/lib/bfd-plugins/"
  143. echo "--cxx=${ROOT}/build/bin/clang++ --cc=${ROOT}/build/bin/clang"
  144. fi