]> git.xonotic.org Git - xonotic/netradiant.git/blob - easy-builder
easy-builder: improvements
[xonotic/netradiant.git] / easy-builder
1 #! /usr/bin/env bash
2
3 # This script is meant to be kept small and simple
4 # If you think about adding features, it's probably a bad idea
5
6 set -e # exit if a command fails
7 set -o pipefail # Will return the exit status of make if it fails
8
9 project_source_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
10
11 build_dir="${project_source_dir}/build${SUBDIR:+/${SUBDIR}}"
12 install_dir="${project_source_dir}/install${SUBDIR:+/${SUBDIR}}"
13
14 build_type='Release'
15
16 declare -a cmake_opts
17 case "$(uname -s)" in
18         'Linux')
19                 nproc='egrep "^processor" /proc/cpuinfo | wc -l'
20                 ;;
21         'FreeBSD')
22                 nproc='sysctl -n hw.ncpu'
23
24                 if [ -f "$(ls '/usr/local/bin/g++'* | sort | tail -n1)" ]
25                 then
26                         gcc_version="$(ls '/usr/local/bin/g++'* | sort | tail -n1 | sed -e 's/.*[^0-9]\([0-9][0-9]*\)$/\1/')"
27                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_C_COMPILER=/usr/local/bin/gcc${gcc_version}"
28                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_CXX_COMPILER=/usr/local/bin/g++${gcc_version}"
29                 else
30                         printf "WARNING: GCC is recommended: if build fails, install GCC and retry\n" >&2
31                 fi
32                 ;;
33         'Darwin')
34                 nproc='sysctl -n hw.ncpu'
35
36                 if [ -f "$(ls '/usr/local/bin/g++-'* | sort | tail -n1)" ]
37                 then
38                         gcc_version="$(ls '/usr/local/bin/g++-'* | sort | tail -n1 | sed -e 's/.*[^0-9]\([0-9][0-9]*\)$/\1/')"
39                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_C_COMPILER=/usr/local/bin/gcc-${gcc_version}"
40                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_CXX_COMPILER=/usr/local/bin/g++-${gcc_version}"
41                 else
42                         printf "WARNING: GCC is recommended: if build fails, install GCC and retry\n" >&2
43                 fi
44                 ;;
45         'MSYS_NT-'*)
46                 nproc='echo "${NUMBER_OF_PROCESSORS}"'
47                 ;;
48         'CYGWIN_NT-'*|'MINGW'*'_NT-'*)
49                 nproc='echo "${NUMBER_OF_PROCESSORS}"'
50                 printf "WARNING: system is not tested: if build fails, use MSYS2 instead\n" >&2
51                 ;;
52         *)
53                 nproc='true'
54                 printf "WARNING: system is not tested\n" >&2
55                 ;;
56 esac
57
58 if command -v 'nproc' >/dev/null
59 then
60         job_count="$(nproc)"
61 else
62         job_count="$(sh -c "${nproc}")"
63 fi
64
65 job_count="${job_count:-4}"
66
67 declare -a cmake_user_opts
68 while [ ! -z "${1}" ]
69 do
70     case "${1}" in
71     '-j'*)
72         job_count="${1:2}"
73         shift
74         ;;
75     '--debug')
76         build_type='Debug'
77         shift
78         ;;
79     *)
80         cmake_user_opts[${#cmake_user_opts[@]}]="${1}"
81         shift
82         ;;
83     esac
84 done
85
86 declare -a fetch_submodules_cmd
87 if ! [ -f "${project_source_dir}/libs/crunch/inc/crn_decomp.h" ]
88 then
89         fetch_submodules_cmd=(git -C "${project_source_dir}" submodule update --init --recursive)
90 fi
91
92 set -x
93
94 "${fetch_submodules_cmd[@]}"
95
96 cmake \
97         -G'Unix Makefiles' \
98         -S"${project_source_dir}" \
99         -B"${build_dir}" \
100         -D'CMAKE_INSTALL_PREFIX'="${install_dir}" \
101         -D'CMAKE_BUILD_TYPE'="${build_type}" \
102         "${cmake_opts[@]}" \
103         "${cmake_user_opts[@]}"
104
105 cmake \
106         --build "${build_dir}" \
107         -- \
108         -j"${job_count}" \
109         install