build 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. IFS=$'\n\t'
  4. stdout() {
  5. cat <<< "$@"
  6. }
  7. stderr() {
  8. cat <<< "$@" 1>&2
  9. }
  10. prereqs () {
  11. local E_BADARGS=65
  12. if [ $# -eq 0 ]; then
  13. stderr "Usage: $(basename $0) [prerequisite_program] [another_program...]"
  14. return $E_BADARGS
  15. fi
  16. for prog in $@; do
  17. hash $prog 2>&-
  18. if [ $? -ne 0 ]; then
  19. return 1
  20. fi
  21. done
  22. }
  23. usage() {
  24. if [ $# -ne 0 ]; then
  25. stdout $@
  26. fi
  27. stdout "Usage: $(basename $0) [options]"
  28. stdout
  29. stdout "A convenience script to quickly build the library with CMake."
  30. stdout
  31. stdout "Options:"
  32. stdout " [--shared|(--static)] Builds either a static or a shared library"
  33. stdout " [--debug|(--release)] Builds a certain variant of the library"
  34. stdout " -g,--generator name The CMake generator to use ('Unix Makefiles')"
  35. stdout " -o,--output folder The place to output the build files (./output)"
  36. stdout
  37. stdout "Examples:"
  38. stdout " ./build"
  39. stdout " ./build --shared --debug"
  40. stdout " ./build --static --release -o ~/my-output-folder"
  41. }
  42. check() {
  43. local E_BADARGS=65
  44. if [ $# -ne 1 ]; then
  45. stderr "Usage: check prerequisite_program"
  46. return $E_BADARGS
  47. fi
  48. prereqs $1
  49. if [ $? -ne 0 ]; then
  50. stderr "Failed to find `$1` on the command line:"
  51. stderr "Please install it with your package manager"
  52. return 1
  53. fi
  54. }
  55. sanitize() {
  56. local E_BADARGS=65
  57. if [ $# -ne 1 ]; then
  58. stderr "Usage: sanitize string_to_clean"
  59. return $E_BADARGS
  60. fi
  61. echo $(echo "$1" | sed "s|[^A-Za-z]\+|-|g" | tr '[:upper:]' '[:lower:]')
  62. return 0
  63. }
  64. build () {
  65. # Get the build locations
  66. local src_dir=$(cd $(dirname $0); pwd -P)
  67. # Arguments
  68. local E_BADARGS=65
  69. local generator="Unix Makefiles"
  70. local shared=NO
  71. local build_type=Release
  72. local output_dir="${src_dir}/output"
  73. while (( "$#" )); do
  74. case "$1" in
  75. --debug) build_type=Debug;;
  76. --release) build_type=Release;;
  77. --shared) shared=YES;;
  78. --static) shared=NO;;
  79. --output) shift; out="$1";;
  80. -o) shift; output_dir="$1";;
  81. --generator) shift; generator="$1";;
  82. -g) shift; generator="$1";;
  83. --help) usage; return 0;;
  84. --) shift; break;;
  85. -*) usage "Bad argument $1"; return ${E_BADARGS};;
  86. *) break;;
  87. esac
  88. shift
  89. done
  90. # Update the build folder
  91. local build_dir=${output_dir}/build
  92. local install_dir=${output_dir}/install
  93. # Create the build folder
  94. mkdir -p ${build_dir}
  95. # Enter the build folder
  96. cd ${build_dir}
  97. trap 'cd ${src_dir}' INT TERM EXIT
  98. # Do the CMake configuration
  99. check cmake
  100. cmake -G ${generator} -DCMAKE_BUILD_TYPE=${build_type} -DBUILD_SHARED_LIBS:BOOL=${shared} ${src_dir}
  101. # Do the build
  102. if [ "${generator}" = "Unix Makefiles" ]; then
  103. check make
  104. make all test
  105. else
  106. stderr "Unknown build system for ${generator}, go to ${build_dir} and run the correct build program"
  107. fi
  108. # Do the install
  109. cmake -DCMAKE_INSTALL_PREFIX="${install_dir}" -P "${build_dir}/cmake_install.cmake"
  110. # Return to the correct folder
  111. trap - INT TERM EXIT
  112. cd ${src_dir}
  113. # Notify the user
  114. stdout "Built files are available at ${install_dir}"
  115. }
  116. # If the script was not sourced we need to run the function
  117. case "$0" in
  118. *"build")
  119. build "$@"
  120. ;;
  121. esac