aboutsummaryrefslogtreecommitdiffstats
path: root/phy_fpga/build_toolchain.sh
blob: c8c07f28ca57af8160bd90c8ba82b406d37e0d0a (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
#!/bin/sh
#
# FPGA toolchain install script.
# This script installs the full FPGA toolchain into the
# directory specified as INSTALLDIR.
#


# The toolchain will be installed into this directory:
INSTALLDIR="$HOME/fpga-toolchain"

# The following directory will be used as temporary build directory:
BUILDDIR="./toolchain-build"

# Run at most this many build processes in parallel:
PARALLEL="$(getconf _NPROCESSORS_ONLN)"

# The following tools are needed to build the toolchain:
# - gcc
# - clang
# - python3
# - cmake
# - git
#
# Please ensure that all these tools are installed in the system.



#####################################################################
#####################################################################
#####################################################################


# 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"

BUILD_ICESTORM=1
BUILD_NEXTPNR=1
BUILD_YOSYS=1
BUILD_TINYPROG=1

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

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...)"
}

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

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


# Cleanup
rm -rf "$BUILDDIR" || die "Failed to cleanup BUILDDIR"
mkdir -p "$BUILDDIR" || die "Failed to create BUILDDIR"
mkdir -p "$INSTALLDIR" || die "Failed to create INSTALLDIR"

newpath="\$PATH"


# Project Icestorm
if [ $BUILD_ICESTORM -ne 0 ]; then
	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"
fi


# nextpnr
if [ $BUILD_NEXTPNR -ne 0 ]; then
	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"
fi


# yosys
if [ $BUILD_YOSYS -ne 0 ]; then
	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"
fi


# tinyprog
if [ $BUILD_TINYPROG -ne 0 ]; then
	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"
fi

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