install.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env bash
  2. set -e
  3. function command_exists {
  4. hash "$1" 2>/dev/null ;
  5. }
  6. function cmake_install {
  7. if [[ `uname -s` == "Darwin" ]]; then
  8. homebrew_cmake_install
  9. else
  10. linux_cmake_install
  11. fi
  12. }
  13. function homebrew_cmake_install {
  14. if command_exists brew; then
  15. brew install cmake
  16. else
  17. echo "Homebrew was not found installed in your system."
  18. echo "Go to http://mxcl.github.com/homebrew/ and follow the instructions."
  19. echo "Or install CMake somehow and retry."
  20. exit 1
  21. fi
  22. }
  23. function python_finder {
  24. python_library="-DPYTHON_LIBRARY="
  25. python_include="-DPYTHON_INCLUDE_DIR="
  26. # The CMake 'FindPythonLibs' Module does not work properly.
  27. # So we are forced to do its job for it.
  28. python_prefix=$(python-config --prefix | sed 's/^[ \t]*//')
  29. if [ -f "${python_prefix}/Python" ]; then
  30. python_library+="${python_prefix}/Python"
  31. python_include+="${python_prefix}/Headers"
  32. else
  33. which_python=$(python -c 'import sys;print(sys.version)' | sed 's/^[ \t]*//')
  34. which_python="python${which_python:0:3}"
  35. lib_python="${python_prefix}/lib/lib${which_python}"
  36. if [ -f "${lib_python}.a" ]; then
  37. python_library+="${lib_python}.a"
  38. else
  39. python_library+="${lib_python}.dylib"
  40. fi
  41. python_include+="${python_prefix}/include/${which_python}"
  42. fi
  43. echo "${python_library} ${python_include}"
  44. }
  45. function num_cores {
  46. if command_exists nproc; then
  47. num_cpus=$(nproc)
  48. else
  49. num_cpus=1
  50. if [[ `uname -s` == "Linux" ]]; then
  51. num_cpus=$(grep -c ^processor /proc/cpuinfo)
  52. else
  53. # Works on Mac and FreeBSD
  54. num_cpus=$(sysctl -n hw.ncpu)
  55. fi
  56. fi
  57. echo $num_cpus
  58. }
  59. function install {
  60. ycm_dir=`pwd`
  61. build_dir=`mktemp -d -t ycm_build.XXXXXX`
  62. pushd $build_dir
  63. if [[ `uname -s` == "Darwin" ]]; then
  64. cmake -G "Unix Makefiles" $(python_finder) "$@" . $ycm_dir/cpp
  65. else
  66. cmake -G "Unix Makefiles" "$@" . $ycm_dir/cpp
  67. fi
  68. make -j $(num_cores) ycm_core
  69. popd
  70. rm -rf $build_dir
  71. }
  72. function testrun {
  73. ycm_dir=`pwd`
  74. build_dir=`mktemp -d -t ycm_build.XXXXXX`
  75. pushd $build_dir
  76. cmake -G "Unix Makefiles" "$@" . $ycm_dir/cpp
  77. make -j $(num_cores) ycm_core_tests
  78. cd ycm/tests
  79. LD_LIBRARY_PATH=$ycm_dir/python ./ycm_core_tests
  80. popd
  81. rm -rf $build_dir
  82. }
  83. function linux_cmake_install {
  84. echo "Please install CMake using your package manager and retry."
  85. exit 1
  86. }
  87. function usage {
  88. echo "Usage: $0 [--clang-completer]"
  89. exit 0
  90. }
  91. if [[ $# -gt 1 ]]; then
  92. usage
  93. fi
  94. case "$1" in
  95. --clang-completer)
  96. cmake_args='-DUSE_CLANG_COMPLETER=ON'
  97. ;;
  98. '')
  99. cmake_args=''
  100. ;;
  101. *)
  102. usage
  103. ;;
  104. esac
  105. if ! command_exists cmake; then
  106. echo "CMake is required to build YouCompleteMe."
  107. cmake_install
  108. fi
  109. if [ -z "$YCM_TESTRUN" ]; then
  110. install $cmake_args $EXTRA_CMAKE_ARGS
  111. else
  112. testrun $cmake_args $EXTRA_CMAKE_ARGS
  113. fi