linux: do not strip binaries (does not work well with the non-PIE hack)
[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 case "$(uname -s)" in
18         # Stripping is known to make non-PIE Linux netradiant binary unusable.
19         # Maybe that's related to the way we patch rpath?
20         # Building NetRadiant as non-PIE is required because of
21         # a mistake in the mimetype-library that prevents users
22         # to run the application from file managers on Linux.
23         # See: https://gitlab.freedesktop.org/xdg/shared-mime-info/-/issues/11
24         'Linux')
25                 install_target='install'
26                 ;;
27         # Stripping is known to make FreeBSD binaries unusable.
28         # Maybe that's related to the way we patch rpath?
29         'FreeBSD')
30                 install_target='install'
31                 ;;
32 esac
33
34 _job_count=4
35
36 _nproc () {
37         if command -v 'nproc' >/dev/null
38         then
39                 nproc
40         else
41                 case "$(uname -s)" in
42                         'Linux')
43                                 egrep "^processor" /proc/cpuinfo | wc -l
44                                 ;;
45                         'FreeBSD')
46                                 sysctl -n hw.ncpu
47                                 ;;
48                         'Darwin')
49                                 sysctl -n hw.logicalcpu \
50                                 || sysctl -n hw.ncpu
51                                 ;;
52                         'MSYS_NT-'*|'CYGWIN_NT-'*|'MINGW'*'_NT-'*)
53                                 if command -v 'wmic' >/dev/null
54                                 then
55                                         wmic cpu get NumberOfLogicalProcessors/Format:List \
56                                                 | grep -m1 '=' | cut -f2 -d'='
57                                 else
58                                         echo "${NUMBER_OF_PROCESSORS:-${_job_count}}"
59                                 fi
60                                 ;;
61                         *)
62                                 echo "${_job_count}"
63                                 ;;
64                 esac
65         fi
66 }
67
68 job_count="$(_nproc)" 2>/dev/null
69 job_count="${job_count:-${_job_count}}"
70
71 declare -a cmake_user_opts
72 while [ ! -z "${1}" ]
73 do
74         case "${1}" in
75         '-j'*)
76                 job_count="${1:2}"
77                 shift
78                 ;;
79         '--debug')
80                 install_target='install'
81                 build_type='Debug'
82                 shift
83                 ;;
84         *)
85         cmake_user_opts[${#cmake_user_opts[@]}]="${1}"
86         shift
87                 ;;
88         esac
89 done
90
91 declare -a fetch_submodules_cmd
92 for submodule_file in 'libs/crunch/inc/crn_decomp.h' \
93         'tools/unvanquished/daemonmap/tools/quake3/q3map2/main.c'
94 do
95         if ! [ -f "${project_source_dir}/${submodule_file}" ]
96         then
97                 fetch_submodules_cmd=(git -C "${project_source_dir}" submodule update --init --recursive)
98         fi
99 done
100
101 case "$(uname -s)" in
102         'Darwin')
103                 cmake_user_opts[${#cmake_user_opts[@]}]='-DBUILTIN_GTKGLEXT=ON -DBUILTIN_GTKTHEME_MOJAVE=ON'
104                 ;;
105 esac
106
107 task_enter_build_dir () {
108         sync
109         mkdir -pv "${build_dir}"
110         cd "${build_dir}"
111 }
112
113 task_fetch_submodules () {
114         sync
115         "${fetch_submodules_cmd[@]}"
116 }
117
118 task_configure () {
119         sync
120         cmake \
121                 -G'Unix Makefiles' \
122                 -D'CMAKE_INSTALL_PREFIX'="${install_dir}" \
123                 -D'CMAKE_BUILD_TYPE'="${build_type}" \
124                 "${cmake_user_opts[@]}" \
125                 "${project_source_dir}"
126 }
127
128 task_build_builtins () {
129         sync
130         make -j"${job_count}" builtins
131 }
132
133 task_build () {
134         sync
135         make -j"${job_count}"
136 }
137
138 task_install () {
139         sync
140         make "${install_target}"
141 }
142
143 set -x
144
145 task_enter_build_dir
146
147 task_fetch_submodules
148
149 task_configure
150
151 task_build_builtins
152
153 task_build
154
155 task_install