summarylogtreecommitdiffstats
path: root/compile.bash
blob: 9a20852afa3037489de0c449d1fb35595f4ee3f2 (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
#!/usr/bin/env bash
set -e

if command -v schedtool >/dev/null 2>&1; then
	# Set current shell and all descendents as SCHED_BATCH, see schedtool(8)
	schedtool -B $$
	prefix_cmd='schedtool -B -n20 -e '
fi

venv() {
	virtualenv ".venv" -p python3
	. ".venv/bin/activate"
}

prepare() {
	if [[ -n "$USE_VENV" ]]; then
		venv
	fi
	echo "Installing autobuild..."
	pip install --upgrade certifi --quiet
	pip3 install --upgrade llbase --quiet
	if command -v autobuild >/dev/null 2>&1 && [[ "$(autobuild --version)" == "autobuild 9.0.0" ]]; then
		pip3 uninstall --yes autobuild --quiet
	fi
	pip3 install --no-cache --upgrade autobuild --quiet
}

build() {
	# we have a lot of files, relax ulimit to help performance
	if [[ -n "$USE_VENV" ]]; then
		. ".venv/bin/activate"
	fi
	ulimit -n 20000
	build_jobs=$(nproc)
	if [[ ${build_jobs} -gt 1 ]]; then
		mempercorekb=$((4 * 1048576))
		# The viewer requires an average of 4GB of memory per core to link
		requiredmemorykb=$(($(nproc) * mempercorekb))
		# Source: Total Used Free
		free_output="$(free --kilo --total | tail -n+2| tr -s ' ')"
		totalmemorykb=$(grep Total <<< "$free_output" | cut -d ' ' -f 2)
		availablememorykb=$(cut -d ' ' -f 7 <<< "$free_output")
		echo "Total memory:         $totalmemorykb (includes swap)"
		echo "Available memory:     $availablememorykb"
		echo "Required memory:      $requiredmemorykb"
		if [[ ${requiredmemorykb} -gt ${totalmemorykb} ]]; then
			echo "Estimated required memory to build with all cores: $((requiredmemorykb/1024/1024)) GB"
			echo "Available memory on this system: $((availablememorykb/1024/1024)) GB"
			echo "Warning: Not enough physical memory to build with all cores, adjusting"
			if [[ ${requiredmemorykb} -gt ${availablememorykb} ]]; then
				jobs=1
				echo "Allocating build jobs according to available memory (including swap) (${availablememorykb}/${requiredmemorykb})..."
				# FIXME: Goes one iteration beyond what it should
				while [[ $((jobs * mempercorekb)) -lt ${availablememorykb} ]]; do
					jobs=$((jobs+1))
					echo -e "${jobs} jobs would consume $(((jobs * mempercorekb)/1024/1024))GB"
				done
				# Back off one job count. Not sure why I have to do this but
				# the loop is doing one extra iteration.
				jobs=$((jobs-1))
				build_jobs=${jobs}
				echo "Computed job count: ${build_jobs}"
			fi
		fi
	fi
	export AUTOBUILD_CPU_COUNT=$build_jobs
	AL_ARCH_FLAGS=${AL_ARCH_FLAGS:-'-march=native -mtune=native -w'}
	AL_CMAKE_CONFIG=(
		-DLL_TESTS:BOOL=OFF
		-DDISABLE_FATAL_WARNINGS=ON
		-DUSE_LTO:BOOL=OFF
		-DVIEWER_CHANNEL="Alchemy Test"
		-DCMAKE_C_FLAGS="$AL_ARCH_FLAGS"
		-DCMAKE_CXX_FLAGS="$AL_ARCH_FLAGS"
		-DCMAKE_C_COMPILER="${CC:-gcc}"
		-DCMAKE_CXX_COMPILER="${CXX:-g++}"
		)
	# I could not find the documentation on how to handle BUILDENV/OPTION in
	# makepkg.conf. If you are reading this and know where it is,
	# please send it my way.
	# if [[ -n "$AL_NO_CCACHE" ]] || ! command -v ccache 2 > /dev/null 2>&1; then
		# echo "ccache disabled"
		# AL_CMAKE_CONFIG+=(-UCMAKE_CXX_COMPILER_LAUNCHER)
	# else
		# echo "ccache available and enabled"
		# export CCACHE_SLOPPINESS="file_macro,locale,time_macros"
		# export CCACHE_NOHASHDIR="true"
		# AL_CMAKE_CONFIG+=(-DCMAKE_CXX_COMPILER_LAUNCHER=ccache)
	# fi
	$prefix_cmd autobuild configure -A 64 -c ReleaseOS -- "${AL_CMAKE_CONFIG[@]}"
	echo "Building with ${AUTOBUILD_CPU_COUNT} jobs (adjusted)"
	$prefix_cmd autobuild build -A64 -c ReleaseOS --no-configure
}

cleanbuild()
{
	rm -rf build-linux-64
	git pull --prune
	build
}
if [[ -n "$1" ]]; then
	$1
else
	echo "Running unattended batch build..."
	prepare
	build
fi