1#!/usr/local/bin/bash
2#
3# build-linux.sh is the master script we use to control the multi-step build
4# processes.
5
6help_message()
7{
8    cat << EOHELP
9Usage: $0 [options] <command>
10
11Commands:
12
13    cmake           Run cmake only.
14
15    build           Run the builds without cleans.
16    install         Install built assets.
17
18    clean           Clean all the builds.
19    clean-all       Clean all the builds and remove generated files.
20
21    uninstall       Uninstall Surge.
22
23Options:
24
25    -h, --help              Show help.
26    -v, --verbose           Verbose output.
27    -p, --project=PROJECT   Select a specific PROJECT, which can be either
28                            vst2, vst3, lv2 or headless.
29    -d, --debug             Use a debug version.
30    -l, --local             Install/uninstall built assets under /home instead
31                            of /usr
32EOHELP
33}
34
35RED=`tput setaf 1`
36GREEN=`tput setaf 2`
37NC=`tput sgr0`
38DEF_BUILD_DIR="buildlin"
39
40UNAME_M=`uname -m`
41if [[ "$UNAME_M" =~ ^arm ]]; then
42   ARM=1
43fi
44if [[ "$UNAME_M" =~ ^aarch ]]; then
45   ARM=1
46fi
47
48if [[ ! -z $ARM ]]; then
49   CMAKE_EXTRA_ARGS="-DARM_NATIVE=native"
50   DEF_BUILD_DIR="buildlin-${UNAME_M}"
51   echo "ARM build activated. Using ARM_NATIVE=native settings; building in ${DEF_BUILD_DIR}"
52fi
53
54
55prerequisite_check()
56{
57    if [ ! -f vst3sdk/LICENSE.txt ]; then
58        echo
59        echo ${RED}ERROR: You have not gotten the submodules required to build Surge. Run the following command to get them.${NC}
60        echo
61        echo git submodule update --init --recursive
62        echo
63        exit 1
64    fi
65}
66
67run_cmake()
68{
69    cmake . -B${DEF_BUILD_DIR} ${CMAKE_EXTRA_ARGS}
70}
71
72run_cmake_if()
73{
74    if [[ ! -f ${DEF_BUILD_DIR}/CMakeCache.txt ]]; then
75        run_cmake
76    fi
77    if [[ CMakeLists.txt -nt ${DEF_BUILD_DIR}/CMakeCache.txt ]]; then
78        run_cmake
79    fi
80}
81
82run_clean()
83{
84    if [[ -d ${DEF_BUILD_DIR} ]]; then
85	cmake --build ${DEF_BUILD_DIR} --target clean --config Release
86    fi
87}
88
89run_build()
90{
91    local flavor=$1
92    cmake --build ${DEF_BUILD_DIR} --config Release --target $flavor --parallel 2
93
94    build_suc=$?
95    if [[ $build_suc = 0 ]]; then
96        echo ${GREEN}Build of ${flavor} succeeded${NC}
97    else
98        echo
99        echo ${RED}** Build of ${flavor} failed**${NC}
100
101        exit 2
102    fi
103}
104
105run_builds()
106{
107    if [ ! -z "$option_vst2" ]; then
108        run_cmake_if
109        run_build "Surge-VST2-Packaged"
110    fi
111
112    if [ ! -z "$option_vst3" ]; then
113        run_cmake_if
114        run_build "Surge-VST3-Packaged"
115    fi
116
117    if [ ! -z "$option_lv2" ]; then
118        run_cmake_if
119        run_build "Surge-LV2-Packaged"
120    fi
121
122    if [ ! -z "$option_headless" ]; then
123        run_cmake_if
124        run_build "surge-headless"
125    fi
126}
127
128run_install()
129{
130    echo "Installing presets"
131    rsync -r --delete "resources/data/" $data_path
132
133    if [ ! -z "$option_vst2" ]; then
134        echo "Installing VST2"
135        rsync -r -delete $vst2_src_path \
136                         $vst2_dest_path/$dest_plugin_name
137    fi
138
139    if [ ! -z "$option_vst3" ]; then
140        echo "Installing VST3"
141        # No dest plugin name here since we are a bundle
142        rsync -r --delete $vst3_src_path \
143                          $vst3_dest_path
144    fi
145
146    if [ ! -z "$option_lv2" ]; then
147        echo "Installing LV2"
148        # No dest plugin name here since we are a bundle
149        rsync -r --delete $lv2_src_path \
150                          $lv2_dest_path
151    fi
152
153    if [ ! -z "$option_headless" ] && [ -d "$headless_dest_path" ]; then
154        echo "Installing Headless"
155        rsync -r --delete $headless_src_path \
156                          $headless_dest_path/$dest_headless_name
157    fi
158}
159
160run_clean_builds()
161{
162    run_clean
163}
164
165run_clean_all()
166{
167    run_clean_builds
168
169    echo "Cleaning additional assets"
170    rm -rf ${DEF_BUILD_DIR}
171}
172
173run_uninstall()
174{
175    rm -rvf $data_path
176
177    if [ ! -z "$option_vst2" ]; then
178        rm -vf $vst2_dest_path/$dest_plugin_name
179    fi
180
181    if [ ! -z "$option_vst3" ]; then
182	rm -vf $vst3_dest_path/Surge.vst3/Contents/x86_64-linux/$dest_plugin_name
183	rmdir -v $vst3_dest_path/Surge.vst3/Contents/x86_64-linux $vst3_dest_path/Surge.vst3/Contents $vst3_dest_path/Surge.vst3
184    fi
185
186    if [ ! -z "$option_lv2" ]; then
187        rm -vf $lv2_dest_path/$lv2_bundle_name/$dest_plugin_name
188        rm -vf $lv2_dest_path/$lv2_bundle_name/*.ttl
189        test -d $lv2_dest_path/$lv2_bundle_name && rmdir $lv2_dest_path/$lv2_bundle_name
190    fi
191
192    if [ ! -z "$option_headless" ]; then
193	rm -vf $headless_dest_path/$dest_headless_name
194    fi
195}
196
197prerequisite_check
198
199ARGS=$(getopt -o hvp:dl --long help,verbose,project:,debug,local -n "$0" -- "$@") \
200    || exit 1
201eval set -- "$ARGS"
202
203while true ; do
204    case "$1" in
205        -h|--help) option_help=1 ; shift ;;
206        -v|--verbose) option_verbose=1 ; shift ;;
207        -p|--project)
208            case "$2" in
209                "") shift 2 ;;
210                *) option_project=$2 ; shift 2 ;;
211            esac ;;
212        -d|--debug) option_debug=1 ; shift ;;
213        -l|--local) option_local=1 ; shift ;;
214        --) shift ; break ;;
215        *) break ;;
216    esac
217done
218
219if [[ ! -z "$option_help" ]]; then
220    help_message
221    exit 0
222fi
223
224if [ -z "$option_project" ] || [ "$option_project" == "vst2" ]; then
225    if [ -e "surge-vst2.make" ] || [ ! -z "$VST2SDK_DIR" ]; then
226        option_vst2=1
227    fi
228fi
229
230if [ -z "$option_project" ] || [ "$option_project" == "vst3" ]; then
231    option_vst3=1
232fi
233
234if [ -z "$option_project" ] || [ "$option_project" == "lv2" ]; then
235    option_lv2=1
236fi
237
238if [ -z "$option_project" ] || [ "$option_project" == "headless" ]; then
239    option_headless=1
240fi
241
242if [ -z "$option_debug" ]; then
243    config="config=release_x64"
244    vst2_src_path="${DEF_BUILD_DIR}/surge_products/Surge.so"
245    vst3_src_path="${DEF_BUILD_DIR}/surge_products/Surge.vst3"
246    lv2_bundle_name="Surge.lv2"
247    lv2_src_path="${DEF_BUILD_DIR}/surge_products/$lv2_bundle_name"
248    headless_src_path="${DEF_BUILD_DIR}/surge-headless"
249    dest_plugin_name="Surge.so"
250    dest_headless_name="Surge-Headless"
251else
252    config="config=debug_x64"
253    vst2_src_path="target/vst2/Debug/Surge-Debug.so"
254    vst3_src_path="target/vst3/Debug/Surge-Debug.so"
255    lv2_bundle_name="Surge.lv2"
256    lv2_src_path="target/lv2/Debug/$lv2_bundle_name"
257    headless_src_path="target/headless/Debug/Surge-Debug"
258    dest_plugin_name="Surge-Debug.so"
259    dest_headless_name="Surge-Headless-Debug"
260fi
261
262if [[ ! -z "$option_local" ]]; then
263    vst2_dest_path="$HOME/.vst"
264    vst3_dest_path="$HOME/.vst3"
265    lv2_dest_path="$HOME/.lv2"
266    headless_dest_path="$HOME/bin"
267    data_path="$HOME/.local/share/surge"
268else
269    vst2_dest_path="/usr/lib/vst"
270    vst3_dest_path="/usr/lib/vst3"
271    lv2_dest_path="/usr/lib/lv2"
272    headless_dest_path="/usr/bin"
273    data_path="/usr/share/surge"
274fi
275
276case $1 in
277    cmake)
278        run_cmake
279        ;;
280    build)
281        run_builds
282        ;;
283    install)
284        run_install
285        ;;
286    clean)
287        run_clean_builds
288        ;;
289    clean-all)
290        run_clean_all
291        ;;
292    uninstall)
293        run_uninstall
294        ;;
295    *)
296        echo $0: missing operand
297        echo Try \'$0 --help\' for more information.
298        ;;
299esac
300