shell_include.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # shellcheck shell=bash
  2. # shellcheck disable=SC2034
  3. # SC2034: "Variable appears unused. Verify it or export it."
  4. # Those are intentional here, as the file is meant to be included elsewhere.
  5. # NOTE: If using another privilege escalation binary make sure it is configured or has the appropriate flag
  6. # to keep the current environment variables in the launched process (in sudo's case this is achieved
  7. # through the -E flag described in sudo(8).
  8. die() {
  9. echo "die: $*"
  10. exit 1
  11. }
  12. exit_if_running_as_root() {
  13. if [ "$(id -u)" -eq 0 ]; then
  14. die "$*"
  15. fi
  16. }
  17. # Usage: check_program_version_at_least <Display Name> <Program Name> <Version String>
  18. check_program_version_at_least()
  19. {
  20. echo -n "Checking for $1 version at least $3... "
  21. if ! command -v "$2" > /dev/null 2>&1; then
  22. echo "ERROR: Cannot find $2 ($1)"
  23. return 1
  24. fi
  25. v=$("$2" --version 2>&1 | grep -E -o '[0-9]+\.[0-9\.]+[a-z]*' | head -n1)
  26. if printf '%s\n' "$3" "$v" | sort --version-sort --check &>/dev/null; then
  27. echo "ok, found $v"
  28. return 0;
  29. else
  30. echo "ERROR: found version $v, too old!"
  31. return 1;
  32. fi
  33. }
  34. get_number_of_processing_units() {
  35. number_of_processing_units="nproc"
  36. SYSTEM_NAME="$(uname -s)"
  37. if [ "$SYSTEM_NAME" = "OpenBSD" ]; then
  38. number_of_processing_units="sysctl -n hw.ncpuonline"
  39. elif [ "$SYSTEM_NAME" = "FreeBSD" ]; then
  40. number_of_processing_units="sysctl -n hw.ncpu"
  41. elif [ "$SYSTEM_NAME" = "Darwin" ]; then
  42. number_of_processing_units="sysctl -n hw.ncpu"
  43. fi
  44. ($number_of_processing_units)
  45. }
  46. get_top_dir() {
  47. git rev-parse --show-toplevel
  48. }
  49. ensure_ladybird_source_dir() {
  50. if [ -z "$LADYBIRD_SOURCE_DIR" ] || [ ! -d "$LADYBIRD_SOURCE_DIR" ]; then
  51. LADYBIRD_SOURCE_DIR="$(get_top_dir)"
  52. export LADYBIRD_SOURCE_DIR
  53. fi
  54. }
  55. get_build_dir() {
  56. ensure_ladybird_source_dir
  57. # Note: Keep in sync with buildDir defaults in CMakePresets.json
  58. case "$1" in
  59. "default")
  60. BUILD_DIR="${LADYBIRD_SOURCE_DIR}/Build/ladybird"
  61. ;;
  62. "Debug")
  63. BUILD_DIR="${LADYBIRD_SOURCE_DIR}/Build/ladybird-debug"
  64. ;;
  65. "Sanitizer")
  66. BUILD_DIR="${LADYBIRD_SOURCE_DIR}/Build/ladybird-sanitizers"
  67. ;;
  68. *)
  69. echo "Unknown BUILD_PRESET: '$1'" >&2
  70. exit 1
  71. ;;
  72. esac
  73. echo "${BUILD_DIR}"
  74. }