build.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env bash
  2. set -e
  3. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  4. function command_exists {
  5. hash "$1" 2>/dev/null ;
  6. }
  7. function cmake_install {
  8. if [[ `uname -s` == "Darwin" ]]; then
  9. homebrew_cmake_install
  10. else
  11. linux_cmake_install
  12. fi
  13. }
  14. function homebrew_cmake_install {
  15. if command_exists brew; then
  16. brew install cmake
  17. else
  18. echo "Homebrew was not found installed in your system."
  19. echo "Go to http://mxcl.github.com/homebrew/ and follow the instructions."
  20. echo "Or install CMake somehow and retry."
  21. exit 1
  22. fi
  23. }
  24. function python_finder {
  25. python_library="-DPYTHON_LIBRARY="
  26. python_include="-DPYTHON_INCLUDE_DIR="
  27. # The CMake 'FindPythonLibs' Module does not work properly.
  28. # So we are forced to do its job for it.
  29. python_prefix=$(python-config --prefix | sed 's/^[ \t]*//')
  30. if [ -f "${python_prefix}/Python" ]; then
  31. python_library+="${python_prefix}/Python"
  32. python_include+="${python_prefix}/Headers"
  33. else
  34. which_python=$(python -c 'import sys;print(sys.version)' | sed 's/^[ \t]*//')
  35. which_python="python${which_python:0:3}"
  36. lib_python="${python_prefix}/lib/lib${which_python}"
  37. if [ -f "${lib_python}.a" ]; then
  38. python_library+="${lib_python}.a"
  39. # This check is for for CYGWIN
  40. elif [ -f "${lib_python}.dll.a" ]; then
  41. python_library+="${lib_python}.dll.a"
  42. else
  43. python_library+="${lib_python}.dylib"
  44. fi
  45. python_include+="${python_prefix}/include/${which_python}"
  46. fi
  47. echo "${python_library} ${python_include}"
  48. }
  49. function num_cores {
  50. if command_exists nproc; then
  51. num_cpus=$(nproc)
  52. else
  53. num_cpus=1
  54. if [[ `uname -s` == "Linux" ]]; then
  55. num_cpus=$(grep -c ^processor /proc/cpuinfo)
  56. else
  57. # Works on Mac and FreeBSD
  58. num_cpus=$(sysctl -n hw.ncpu)
  59. fi
  60. fi
  61. echo $num_cpus
  62. }
  63. function install {
  64. build_dir=`mktemp -d -t ycm_build.XXXXXX`
  65. pushd $build_dir
  66. if [[ `uname -s` == "Darwin" ]]; then
  67. cmake -G "Unix Makefiles" $(python_finder) "$@" . $SCRIPT_DIR/cpp
  68. else
  69. cmake -G "Unix Makefiles" "$@" . $SCRIPT_DIR/cpp
  70. fi
  71. make -j $(num_cores) ycm_support_libs
  72. popd
  73. rm -rf $build_dir
  74. }
  75. function testrun {
  76. build_dir=`mktemp -d -t ycm_build.XXXXXX`
  77. pushd $build_dir
  78. cmake -G "Unix Makefiles" "$@" . $SCRIPT_DIR/cpp
  79. make -j $(num_cores) ycm_core_tests
  80. cd ycm/tests
  81. LD_LIBRARY_PATH=$SCRIPT_DIR ./ycm_core_tests
  82. popd
  83. rm -rf $build_dir
  84. }
  85. function linux_cmake_install {
  86. echo "Please install CMake using your package manager and retry."
  87. exit 1
  88. }
  89. function usage {
  90. echo "Usage: $0 [--clang-completer [--system-libclang]] [--omnisharp-completer]"
  91. exit 0
  92. }
  93. function check_third_party_libs {
  94. libs_present=true
  95. for folder in third_party/*; do
  96. num_files_in_folder=$(find $folder -maxdepth 1 -mindepth 1 | wc -l)
  97. if [[ $num_files_in_folder -eq 0 ]]; then
  98. libs_present=false
  99. fi
  100. done
  101. if ! $libs_present; then
  102. echo "Some folders in ./third_party are empty; you probably forgot to run:"
  103. printf "\n\tgit submodule update --init --recursive\n\n"
  104. exit 1
  105. fi
  106. }
  107. cmake_args=""
  108. omnisharp_completer=false
  109. for flag in $@; do
  110. case "$flag" in
  111. --clang-completer)
  112. cmake_args="-DUSE_CLANG_COMPLETER=ON"
  113. ;;
  114. --system-libclang)
  115. cmake_args="$cmake_args -DUSE_SYSTEM_LIBCLANG=ON"
  116. ;;
  117. --omnisharp-completer)
  118. omnisharp_completer=true
  119. ;;
  120. *)
  121. usage
  122. ;;
  123. esac
  124. done
  125. if [[ $cmake_args == *-DUSE_SYSTEM_LIBCLANG=ON* ]] && \
  126. [[ $cmake_args != *-DUSE_CLANG_COMPLETER=ON* ]]; then
  127. usage
  128. fi
  129. check_third_party_libs
  130. if ! command_exists cmake; then
  131. echo "CMake is required to build YouCompleteMe."
  132. cmake_install
  133. fi
  134. if [ -z "$YCM_TESTRUN" ]; then
  135. install $cmake_args $EXTRA_CMAKE_ARGS
  136. else
  137. testrun $cmake_args $EXTRA_CMAKE_ARGS
  138. fi
  139. if $omnisharp_completer; then
  140. buildcommand="msbuild"
  141. if ! command_exists msbuild; then
  142. buildcommand="msbuild.exe"
  143. if ! command_exists msbuild.exe; then
  144. buildcommand="xbuild"
  145. if ! command_exists xbuild; then
  146. echo "msbuild or xbuild is required to build Omnisharp"
  147. exit 1
  148. fi
  149. fi
  150. fi
  151. # TODO: change to the following when omnisharp in own third_party:
  152. # $SCRIPT_DIR"/third_party/OmniSharpServer"
  153. build_dir=$SCRIPT_DIR"/../OmniSharpServer"
  154. cd $build_dir
  155. $buildcommand
  156. cd $ycm_dir
  157. fi