summaryrefslogtreecommitdiffstats
path: root/src/civetweb/build
blob: 2b423108ffaf5e9015a5ed7d58d4cd97fb1e1477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
set -euo pipefail
IFS=$'\n\t'

stdout() {
  cat <<< "$@"
}

stderr() {
  cat <<< "$@" 1>&2
}

prereqs () {
  local E_BADARGS=65
  if [ $# -eq 0 ]; then
    stderr "Usage: $(basename $0) [prerequisite_program] [another_program...]"
    return $E_BADARGS
  fi
  for prog in $@; do
    hash $prog 2>&-
    if [ $? -ne 0 ]; then
      return 1
    fi
  done
}

usage() {
  if [ $# -ne 0 ]; then
    stdout $@
  fi
  stdout "Usage: $(basename $0) [options]"
  stdout
  stdout "A convenience script to quickly build the library with CMake."
  stdout
  stdout "Options:"
  stdout "  [--shared|(--static)]   Builds either a static or a shared library"
  stdout "  [--debug|(--release)]   Builds a certain variant of the library"
  stdout "  -g,--generator name     The CMake generator to use ('Unix Makefiles')"
  stdout "  -o,--output folder      The place to output the build files (./output)"
  stdout
  stdout "Examples:"
  stdout "  ./build"
  stdout "  ./build --shared --debug"
  stdout "  ./build --static --release -o ~/my-output-folder"
}

check() {
  local E_BADARGS=65
  if [ $# -ne 1 ]; then
    stderr "Usage: check prerequisite_program"
    return $E_BADARGS
  fi
  prereqs $1
  if [ $? -ne 0 ]; then
    stderr "Failed to find `$1` on the command line:"
    stderr "Please install it with your package manager"
    return 1
  fi
}

sanitize() {
  local E_BADARGS=65
  if [ $# -ne 1 ]; then
    stderr "Usage: sanitize string_to_clean"
    return $E_BADARGS
  fi
  echo $(echo "$1" | sed "s|[^A-Za-z]\+|-|g" | tr '[:upper:]' '[:lower:]')
  return 0
}

build () {
  # Get the build locations
  local src_dir=$(cd $(dirname $0); pwd -P)

  # Arguments
  local E_BADARGS=65
  local generator="Unix Makefiles"
  local shared=NO
  local build_type=Release
  local output_dir="${src_dir}/output"
  while (( "$#" )); do
    case "$1" in
      --debug) build_type=Release;;
      --release) build_type=Debug;;
      --shared) shared=YES;;
      --static) shared=NO;;
      --output) shift; out="$1";;
      -o) shift; output_dir="$1";;
      --generator) shift; generator="$1";;
      -g) shift; generator="$1";;
      --help) usage; return 0;;
      --) shift; break;;
      -*) usage "Bad argument $1"; return ${E_BADARGS};;
      *) break;;
    esac
    shift
  done

  # Update the build folder
  local build_dir=${output_dir}/build
  local install_dir=${output_dir}/install

  # Create the build folder
  mkdir -p ${build_dir}

  # Enter the build folder
  cd ${build_dir}
  trap 'cd ${src_dir}' INT TERM EXIT

  # Do the CMake configuration
  check cmake
  cmake -G ${generator} -DCMAKE_BUILD_TYPE=${build_type} -DBUILD_SHARED_LIBS:BOOL=${shared} ${src_dir}

  # Do the build
  if [ "${generator}" = "Unix Makefiles" ]; then
    check make
    make all test
  else
    stderr "Unknown build system for ${generator}, go to ${build_dir} and run the correct build program"
  fi

  # Do the install
  cmake -DCMAKE_INSTALL_PREFIX="${install_dir}" -P "${build_dir}/cmake_install.cmake"

  # Return to the correct folder
  trap - INT TERM EXIT
  cd ${src_dir}

  # Notify the user
  stdout "Built files are available at ${install_dir}"
}

# If the script was not sourced we need to run the function
case "$0" in
  *"build")
    build "$@"
    ;;
esac