init.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bash
  2. # 首次环境:装编译依赖、cmake 编译、建库建表并启动
  3. # 用法: ./init.sh [config.ini]
  4. set -euo pipefail
  5. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. cd "$ROOT"
  7. CONFIG="${1:-config/config.ini}"
  8. BUILD_TYPE="${BUILD_TYPE:-Release}"
  9. JOBS="$(nproc 2>/dev/null || echo 4)"
  10. if [[ ! -f "$CONFIG" ]]; then
  11. echo "config not found: $CONFIG" >&2
  12. exit 1
  13. fi
  14. APT_PACKAGES=(
  15. build-essential
  16. cmake
  17. libcurl4-openssl-dev
  18. libssl-dev
  19. libmysqlcppconn-dev
  20. zlib1g-dev
  21. )
  22. missing=()
  23. for pkg in "${APT_PACKAGES[@]}"; do
  24. if ! dpkg -s "$pkg" >/dev/null 2>&1; then
  25. missing+=("$pkg")
  26. fi
  27. done
  28. if (( ${#missing[@]} > 0 )); then
  29. echo "install packages: ${missing[*]}"
  30. if [[ "$(id -u)" -eq 0 ]]; then
  31. apt-get update
  32. apt-get install -y "${missing[@]}"
  33. else
  34. sudo apt-get update
  35. sudo apt-get install -y "${missing[@]}"
  36. fi
  37. fi
  38. if [[ ! -e /usr/local/include/util/json.h && ! -e /usr/local/include/ylib/util/json.h ]]; then
  39. echo "ylib headers not found under /usr/local/include" >&2
  40. exit 1
  41. fi
  42. if [[ ! -e /usr/local/lib/libylib.a && ! -e /usr/local/lib/libylib.so && ! -e /usr/local/lib/libylib_d.a ]]; then
  43. echo "ylib library not found under /usr/local/lib" >&2
  44. exit 1
  45. fi
  46. if [[ ! -e /usr/local/lib/libhpsocket.a && ! -e /usr/local/lib/libhpsocket.so ]]; then
  47. echo "hpsocket library not found under /usr/local/lib" >&2
  48. exit 1
  49. fi
  50. cmake -S . -B build -DCMAKE_BUILD_TYPE="$BUILD_TYPE"
  51. cmake --build build -j"$JOBS"
  52. if [[ ! -x ./build/server ]]; then
  53. echo "build/server not found" >&2
  54. exit 1
  55. fi
  56. exec ./build/server --init-db "$CONFIG"