summaryrefslogtreecommitdiffstats
path: root/build_fpga_toolchain.sh
blob: 222ee1f9406cff1a1105ed2df1619d19fe5c4a91 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh
#
# FPGA toolchain install script.
# v1.1
# This script installs a full Open Source FPGA toolchain to a user directory.
#
# Author: Michael Buesch <m@bues.ch>
#
# This code is Public Domain.
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
# USE OR PERFORMANCE OF THIS SOFTWARE.
#

die()
{
	echo "$*" >&2
	exit 1
}

show_help()
{
	echo "Usage: build_fpga_toolchain.sh <OPTIONS> [INSTALLDIR]"
	echo
	echo "Options:"
	echo " -j|--jobs NR                  Set the number of build jobs to run in parallel."
	echo "                               Default: Number of CPUs"
	echo " -h|--help                     Print help."
	echo
	echo
	echo "Install toolchain to $HOME/fpga-toolchain"
	echo "  ./build_fpga_toolchain.sh"
	echo
	echo "Install toolchain to another destination"
	echo "  ./build_fpga_toolchain.sh /home/user/directory"
	echo
	echo "The script will create a temporary directory during build to build all tools:"
	echo "  ./fpga-toolchain-build-tmp"
	echo
	echo "The following tools are needed to build the toolchain:"
	echo "- gcc"
	echo "- clang"
	echo "- python3"
	echo "- cmake"
	echo "- git"
	echo "- schedtool"
	echo "Please ensure that all of these tools are installed in the system."
}

parse_args()
{
	# Defaults:
	PARALLEL="$(getconf _NPROCESSORS_ONLN)"
	BUILDDIR="./fpga-toolchain-build-tmp"
	INSTALLDIR="$HOME/fpga-toolchain"

	# Source repositories:
	REPO_ICESTORM="https://github.com/cliffordwolf/icestorm.git"
	REPO_NEXTPNR="https://github.com/YosysHQ/nextpnr.git"
	REPO_YOSYS="https://github.com/YosysHQ/yosys.git"
	REPO_TINYPROG="https://github.com/tinyfpga/TinyFPGA-Bootloader.git"

	# Parse command line options
	while [ $# -ge 1 ]; do
		[ "$(printf '%s' "$1" | cut -c1)" != "-" ] && break

		case "$1" in
		-h|--help)
			show_help
			exit 0
			;;
		-j|--jobs)
			shift
			PARALLEL="$1"
			[ -z "$PARALLEL" -o -n "$(printf '%s' "$PARALLEL" | tr -d '[0-9]')" ] &&\
				die "--jobs: '$PARALLEL' is not a positive integer number."
			;;
		*)
			echo "Unknown option: $1"
			exit 1
			;;
		esac
		shift
	done

	if [ $# -ge 1 -a -n "$1" ]; then
		# User defined INSTALLDIR
		INSTALLDIR="$1"
	fi
}

checkprog()
{
	local prog="$1"
	which "$prog" >/dev/null ||\
		die "$prog is not installed. Please install it by use of the distribution package manager (apt, apt-get, rpm, etc...)"
}

check_build_environment()
{
	[ "$(id -u)" = "0" ] && die "Do not run this as root!"
	checkprog gcc
	checkprog clang
	checkprog python3
	checkprog cmake
	checkprog git
	checkprog schedtool
}

cleanup()
{
	rm -rf "$BUILDDIR" || die "Failed to cleanup BUILDDIR"
}

prepare()
{
	# Resolve paths.
	BUILDDIR="$(realpath -m -s "$BUILDDIR")"
	INSTALLDIR="$(realpath -m -s "$INSTALLDIR")"
	echo "BUILDDIR=$BUILDDIR"
	echo "INSTALLDIR=$INSTALLDIR"
	echo "PARALLEL=$PARALLEL"
	[ -n "$BUILDDIR" -a -n "$INSTALLDIR" ] || die "Failed to resolve directories"
	echo

	# Set priority
	schedtool -D -n19 $$ || die "Failed to reduce process priority"

	# Create the build directories.
	cleanup
	mkdir -p "$BUILDDIR" || die "Failed to create BUILDDIR"
	mkdir -p "$INSTALLDIR" || die "Failed to create INSTALLDIR"

	# Reset the new PATH.
	NEWPATH="\$PATH"
}

build_icestorm()
{
	echo "Building icestorm..."
	cd "$BUILDDIR" || die "Failed to cd to builddir."
	git clone "$REPO_ICESTORM" "$BUILDDIR/icestorm" || die "Failed to clone icestorm"
	cd "$BUILDDIR/icestorm" || die "Failed to cd to icestorm."
	export PREFIX="$INSTALLDIR/icestorm"
	make -j "$PARALLEL" PREFIX="$PREFIX" || die "Failed to build icestorm"
	rm -rf "$PREFIX" || die "Failed to clean install icestorm"
	make install PREFIX="$PREFIX" || die "Failed to install icestorm"

	NEWPATH="$PREFIX/bin:$NEWPATH"
}

build_nextpnr()
{
	echo "Building nextpnr..."
	cd "$BUILDDIR" || die "Failed to cd to builddir."
	git clone "$REPO_NEXTPNR" "$BUILDDIR/nextpnr" || die "Failed to clone nextpnr"
	mkdir "$BUILDDIR/nextpnr/builddir" || die "Failed to create nextpnr builddir"
	cd "$BUILDDIR/nextpnr/builddir" || die "Failed to cd to nextpnr."
	export PREFIX="$INSTALLDIR/nextpnr"
	cmake -DARCH=ice40 -DICEBOX_ROOT="$INSTALLDIR/icestorm/share/icebox" -DCMAKE_INSTALL_PREFIX="$PREFIX" .. || die "Failed to build nextpnr"
	make -j "$PARALLEL" || die "Failed to build nextpnr"
	rm -rf "$PREFIX" || die "Failed to clean install nextpnr"
	make install || die "Failed to install nextpnr"

	NEWPATH="$PREFIX/bin:$NEWPATH"
}

build_yosys()
{
	echo "Building yosys..."
	cd "$BUILDDIR" || die "Failed to cd to builddir."
	git clone "$REPO_YOSYS" "$BUILDDIR/yosys" || die "Failed to clone yosys"
	cd "$BUILDDIR/yosys" || die "Failed to cd to yosys."
	export PREFIX="$INSTALLDIR/yosys"
	make config-clang PREFIX="$PREFIX" || die "Failed to configure yosys"
	make -j "$PARALLEL" PREFIX="$PREFIX" || die "Failed to build yosys"
	rm -rf "$PREFIX" || die "Failed to clean install yosys"
	make install PREFIX="$PREFIX" || die "Failed to install yosys"

	NEWPATH="$PREFIX/bin:$NEWPATH"
}

build_tinyprog()
{
	echo "Building tinyprog..."
	cd "$BUILDDIR" || die "Failed to cd to builddir."
	git clone "$REPO_TINYPROG" "$BUILDDIR/TinyFPGA-Bootloader" || die "Failed to clone tinyprog"
	cd "$BUILDDIR/TinyFPGA-Bootloader/programmer" || die "Failed to cd to tinyprog."
	export PREFIX="$INSTALLDIR/tinyprog"
	rm -rf "$PREFIX" || die "Failed to clean install tinyprog"
	mkdir -p "$PREFIX/lib" || die "Failed to create tinyprog lib"
	mkdir -p "$PREFIX/bin" || die "Failed to create tinyprog bin"
	cp -r "$BUILDDIR/TinyFPGA-Bootloader/programmer/tinyprog" "$PREFIX/lib/" || die "Failed to install tinyprog"
	cat > "$PREFIX/bin/tinyprog" <<EOF
#!/bin/sh
export PYTHONPATH="$PREFIX/lib/:\$PYTHONPATH"
exec python3 "$PREFIX/lib/tinyprog" "\$@"
EOF
	[ -f "$PREFIX/bin/tinyprog" ] || die "Failed to install tinyprog wrapper"
	chmod 755 "$PREFIX/bin/tinyprog" || die "Failed to chmod tinyprog"

	NEWPATH="$PREFIX/bin:$NEWPATH"
}


parse_args "$@"
check_build_environment
prepare
build_icestorm
build_nextpnr
build_yosys
build_tinyprog
cleanup

echo
echo
echo
echo "Successfully built and installed all FPGA tools to: $INSTALLDIR"
echo "Please add the following line to your $HOME/.bashrc file:"
echo
echo "export PATH=\"$NEWPATH\""
echo
bues.ch cgit interface