]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - easy-builder
bundle: make daemonmap buildable and bundlable like other tools as submodule
[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 install_target='install/strip'
15 build_type='Release'
16
17 declare -a cmake_opts
18 case "$(uname -s)" in
19         'Linux')
20                 nproc='egrep "^processor" /proc/cpuinfo | wc -l'
21                 ;;
22         'FreeBSD')
23                 nproc='sysctl -n hw.ncpu'
24
25                 if [ -f "$(ls '/usr/local/bin/g++'* | sort | tail -n1)" ]
26                 then
27                         gcc_version="$(ls '/usr/local/bin/g++'* | sort | tail -n1 | sed -e 's/.*[^0-9]\([0-9][0-9]*\)$/\1/')"
28                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_C_COMPILER=/usr/local/bin/gcc${gcc_version}"
29                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_CXX_COMPILER=/usr/local/bin/g++${gcc_version}"
30                 else
31                         printf "WARNING: GCC is recommended: if build fails, install GCC and retry\n" >&2
32                 fi
33                 ;;
34         'Darwin')
35                 nproc='sysctl -n hw.ncpu'
36
37                 if [ -f "$(ls '/usr/local/bin/g++-'* | sort | tail -n1)" ]
38                 then
39                         gcc_version="$(ls '/usr/local/bin/g++-'* | sort | tail -n1 | sed -e 's/.*[^0-9]\([0-9][0-9]*\)$/\1/')"
40                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_C_COMPILER=/usr/local/bin/gcc-${gcc_version}"
41                         cmake_opts[${#cmake_opts[@]}]="-DCMAKE_CXX_COMPILER=/usr/local/bin/g++-${gcc_version}"
42                 else
43                         printf "WARNING: GCC is recommended: if build fails, install GCC and retry\n" >&2
44                 fi
45                 ;;
46         'MSYS_NT-'*)
47                 nproc='echo "${NUMBER_OF_PROCESSORS}"'
48                 ;;
49         'CYGWIN_NT-'*|'MINGW'*'_NT-'*)
50                 nproc='echo "${NUMBER_OF_PROCESSORS}"'
51                 printf "WARNING: system is not tested: if build fails, use MSYS2 instead\n" >&2
52                 ;;
53         *)
54                 nproc='true'
55                 printf "WARNING: system is not tested\n" >&2
56                 ;;
57 esac
58
59 if command -v 'nproc' >/dev/null
60 then
61         job_count="$(nproc)"
62 else
63         job_count="$(sh -c "${nproc}")"
64 fi
65
66 job_count="${job_count:-4}"
67
68 declare -a cmake_user_opts
69 while [ ! -z "${1}" ]
70 do
71         case "${1}" in
72         '-j'*)
73                 job_count="${1:2}"
74                 shift
75                 ;;
76         '--debug')
77                 install_target='install'
78                 build_type='Debug'
79                 shift
80                 ;;
81         *)
82         cmake_user_opts[${#cmake_user_opts[@]}]="${1}"
83         shift
84                 ;;
85         esac
86 done
87
88 declare -a fetch_submodules_cmd
89 for submodule_file in 'libs/crunch/inc/crn_decomp.h' \
90         'tools/unvanquished/daemonmap/tools/quake3/q3map2/main.c'
91 do
92         if ! [ -f "${project_source_dir}/${submodule_file}" ]
93         then
94                 fetch_submodules_cmd=(git -C "${project_source_dir}" submodule update --init --recursive)
95         fi
96 done
97
98 set -x
99
100 "${fetch_submodules_cmd[@]}"
101
102 cmake \
103         -G'Unix Makefiles' \
104         -S"${project_source_dir}" \
105         -B"${build_dir}" \
106         -D'CMAKE_INSTALL_PREFIX'="${install_dir}" \
107         -D'CMAKE_BUILD_TYPE'="${build_type}" \
108         "${cmake_opts[@]}" \
109         "${cmake_user_opts[@]}" \
110         "${project_source_dir}"
111
112 cmake \
113         --build "${build_dir}" \
114         -- \
115         -j"${job_count}" \
116         "${install_target}"