summaryrefslogtreecommitdiffstats
path: root/libtoprammer/bit/build.sh
blob: b1dc553f9cf93eec6f8d40a31640cd9afaf89847 (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
108
109
110
111
112
#!/bin/bash
# Rebuild FPGA bit files
# Copyright (c) 2010 Michael Buesch <mb@bu3sch.de>
# Licensed under the GNU/GPL v2+

basedir="$PWD/$(dirname $0)"
srcdir="$basedir/src"
bindir="$basedir"
bitparser="python $basedir/../bitfile.py"

function terminate
{
	echo "Interrupted."
	exit 1
}

trap terminate TERM INT

function usage
{
	echo "Usage: build.sh [OPTIONS] [TARGETS]"
	echo
	echo "Options:"
	echo " -h|--help           Show this help text"
	echo " -v|--verbose        Verbose build"
	echo
	echo "Targets:"
	echo "Specify the names of the targets to build, or leave blank to rebuild all."
}

# Parse commandline
verbose=0
nr_targets=0
while [ $# -gt 0 ]; do
	if [ "$1" = "-h" -o "$1" = "--help" ]; then
		usage
		exit 0
	fi
	if [ "$1" = "-v" -o "$1" = "--verbose" ]; then
		verbose=1
		shift
		continue
	fi
	targets[nr_targets]="$1"
	let nr_targets=nr_targets+1
	shift
done

function should_build # $1=target
{
	target="$1"
	[ "$target" = "template" ] && return 1
	[ $nr_targets -eq 0 ] && return 0
	let end=nr_targets-1
	for i in $(seq 0 $end); do
		[ ${targets[i]} = "$target" ] && return 0
	done
	return 1
}

# Check if the payload of two bitfiles matches
function bitfile_is_equal # $1=file1, $2=file2
{
	[ -r $1 -a -r $2 ] || return 1
	sum1="$($bitparser $1 GETPAYLOAD | sha1sum - | cut -d' ' -f1)"
	sum2="$($bitparser $2 GETPAYLOAD | sha1sum - | cut -d' ' -f1)"
	[ "$sum1" = "$sum2" ]
}

for src in $srcdir/*; do
	[ -d "$src" ] || continue

	srcname="$(basename $src)"
	logfile="$bindir/$srcname.build.log"

	should_build $srcname || continue

	echo "Building $srcname..."
	make -C $src/ clean >/dev/null
	if [ $? -ne 0 ]; then
		echo "FAILED to clean $srcname."
		exit 1
	fi
	if [ $verbose -eq 0 ]; then
		make -C $src/ all >$logfile
		if [ $? -ne 0 ]; then
			cat $logfile
			echo "FAILED to build $srcname."
			exit 1
		fi
		cat $logfile | grep WARNING
	else
		make -C $src/ all
	fi

	new="$src/$srcname.bit"
	old="$bindir/$srcname.bit"
	if bitfile_is_equal "$old" "$new"; then
		echo "Bitfile for target $srcname did not change"
	else
		cp -f "$new" "$old"
	fi
	make -C $src/ clean >/dev/null
	if [ $? -ne 0 ]; then
		echo "FAILED to clean $srcname."
		exit 1
	fi
	rm -f $logfile
done
echo "Successfully built all images."

exit 0
bues.ch cgit interface