blob: b44f915ab7169a59569b894154ec2a00da863cf4 [file] [log] [blame]
Carmelo Casconeacf12252017-06-05 01:05:43 -04001#!/usr/bin/env bash
2# -----------------------------------------------------------------------------
3# Builds and installs all tools needed for developing and testing P4 support in
4# ONOS.
5#
Brian O'Connor73f8bd72017-06-16 15:27:49 -07006# Tested on Ubuntu 14.04 and 16.04.
Carmelo Casconeacf12252017-06-05 01:05:43 -04007#
8# Recommended minimum system requirements:
9# 4 GB of RAM
10# 2 cores
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040011# 8 GB free hard drive space (~4 GB to build everything)
Carmelo Casconeacf12252017-06-05 01:05:43 -040012# -----------------------------------------------------------------------------
13
14# Exit on errors.
15set -e
16
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040017BUILD_DIR=~/p4tools
Andrea Campanella8bcd5862017-12-11 11:34:45 +010018BMV2_COMMIT="d930fa2818d8ed71f3bf36eaf3f3687d9b68184f"
19PI_COMMIT="13d611a9c655938676ebcde2bd5653b461f46ca7"
Carmelo Cascone65cd9e32018-01-09 16:45:20 -040020P4C_COMMIT="cb8f118ed2861986cd20a3e34f18ee7ae3862233"
Andrea Campanella8bcd5862017-12-11 11:34:45 +010021PROTOBUF_COMMIT="tags/v3.2.0"
Carmelo Casconeb1936bd2017-12-12 14:42:27 -080022GRPC_COMMIT="tags/v1.3.2"
Carmelo Casconeacf12252017-06-05 01:05:43 -040023
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040024NUM_CORES=`grep -c ^processor /proc/cpuinfo`
Carmelo Casconeacf12252017-06-05 01:05:43 -040025
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040026function do_requirements {
27 sudo apt update
Brian O'Connor73f8bd72017-06-16 15:27:49 -070028 sudo apt-get install -y --no-install-recommends \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040029 autoconf \
30 automake \
31 bison \
Carmelo Casconefb76b042017-07-17 19:42:00 -040032 build-essential \
33 cmake \
34 cpp \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040035 curl \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040036 flex \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040037 git \
38 libboost-dev \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070039 libboost-program-options-dev \
40 libboost-system-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040041 libboost-filesystem-dev \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070042 libboost-thread-dev \
43 libboost-filesystem-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040044 libboost-program-options-dev \
45 libboost-system-dev \
46 libboost-test-dev \
47 libboost-thread-dev \
48 libc6-dev \
49 libevent-dev \
50 libffi-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040051 libfl-dev \
52 libgc-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040053 libgc1c2 \
54 libgflags-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040055 libgmp-dev \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070056 libjudy-dev \
57 libpcap-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040058 libgmp10 \
59 libgmpxx4ldbl \
60 libjudy-dev \
61 libpcap-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040062 libreadline6 \
63 libreadline6-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040064 libssl-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040065 libtool \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040066 make \
Carmelo Casconefb76b042017-07-17 19:42:00 -040067 mktemp \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040068 pkg-config \
69 python \
Carmelo Casconefb76b042017-07-17 19:42:00 -040070 python-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040071 python-ipaddr \
Carmelo Casconefb76b042017-07-17 19:42:00 -040072 python-pip \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040073 python-scapy \
Carmelo Casconeee090712017-12-14 23:01:03 -080074 python-setuptools \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040075 tcpdump \
Carmelo Casconefb76b042017-07-17 19:42:00 -040076 wget \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040077 unzip
Carmelo Cascone87ac7372017-09-15 01:58:59 +020078
79 sudo pip install setuptools cffi
Brian O'Connor73f8bd72017-06-16 15:27:49 -070080}
81
82function do_requirements_1404 {
83 sudo apt install -y python-software-properties software-properties-common
84 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020085 sudo add-apt-repository -y ppa:george-edison55/cmake-3.x
Brian O'Connor73f8bd72017-06-16 15:27:49 -070086 sudo apt update
87 sudo apt install -y \
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020088 cmake \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070089 dpkg-dev \
90 g++-4.9 \
91 gcc-4.9 \
92 libboost-iostreams-dev
Carmelo Casconeacf12252017-06-05 01:05:43 -040093
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040094 # Needed for p4c.
95 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
96 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
97}
Carmelo Casconeacf12252017-06-05 01:05:43 -040098
Brian O'Connor73f8bd72017-06-16 15:27:49 -070099function do_requirements_1604 {
100 sudo apt-get update
101 sudo apt-get install -y --no-install-recommends \
102 ca-certificates \
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200103 cmake \
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700104 g++ \
105 libboost-iostreams1.58-dev
106}
107
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400108function do_protobuf {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400109 cd ${BUILD_DIR}
110 if [ ! -d protobuf ]; then
111 git clone https://github.com/google/protobuf.git
112 fi
113 cd protobuf
114 git fetch
115 git checkout ${PROTOBUF_COMMIT}
Carmelo Casconefb76b042017-07-17 19:42:00 -0400116
117 export CFLAGS="-Os"
118 export CXXFLAGS="-Os"
119 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400120 ./autogen.sh
121 ./configure --prefix=/usr
122 make -j${NUM_CORES}
123 sudo make install
124 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400125 unset CFLAGS CXXFLAGS LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400126}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400127
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400128function do_grpc {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400129 cd ${BUILD_DIR}
130 if [ ! -d grpc ]; then
131 git clone https://github.com/grpc/grpc.git
132 fi
133 cd grpc
134 git fetch
135 git checkout ${GRPC_COMMIT}
136 git submodule update --init
Carmelo Casconeacf12252017-06-05 01:05:43 -0400137
Carmelo Casconefb76b042017-07-17 19:42:00 -0400138 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400139 make -j${NUM_CORES}
140 sudo make install
141 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400142 unset LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400143}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400144
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200145function checkout_bmv2 {
146 cd ${BUILD_DIR}
147 if [ ! -d bmv2 ]; then
148 git clone https://github.com/p4lang/behavioral-model.git bmv2
149 fi
150 cd bmv2
151 git fetch
152 git checkout ${BMV2_COMMIT}
153}
154
155function do_pi_bmv2_deps {
156 checkout_bmv2
157 # From bmv2's install_deps.sh.
158 # Nanomsg is required also by p4runtime.
159 tmpdir=`mktemp -d -p .`
160 cd ${tmpdir}
161 bash ../travis/install-thrift.sh
162 bash ../travis/install-nanomsg.sh
163 sudo ldconfig
164 bash ../travis/install-nnpy.sh
165 cd ..
166 sudo rm -rf $tmpdir
167}
168
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400169function do_p4runtime {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400170 cd ${BUILD_DIR}
171 if [ ! -d p4runtime ]; then
172 git clone https://github.com/p4lang/PI.git p4runtime
173 fi
174 cd p4runtime
175 git fetch
176 git checkout ${PI_COMMIT}
177 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400178
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400179 ./autogen.sh
Carmelo Cascone28f37782017-08-29 20:11:21 +0200180 ./configure --with-proto 'CXXFLAGS=-O0 -g'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400181 make -j${NUM_CORES}
182 sudo make install
183 sudo ldconfig
184}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400185
Carmelo Casconefb76b042017-07-17 19:42:00 -0400186function do_bmv2 {
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200187 checkout_bmv2
Carmelo Casconeaaf6d982017-07-29 19:52:58 -0400188
Carmelo Casconefb76b042017-07-17 19:42:00 -0400189 ./autogen.sh
Carmelo Cascone28f37782017-08-29 20:11:21 +0200190 ./configure --enable-debugger --with-pi 'CXXFLAGS=-O0 -g'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400191 make -j${NUM_CORES}
192 sudo make install
193 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400194
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400195 # Simple_switch_grpc target
196 cd targets/simple_switch_grpc
197 ./autogen.sh
Carmelo Cascone28f37782017-08-29 20:11:21 +0200198 ./configure 'CXXFLAGS=-O0 -g'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400199 make -j${NUM_CORES}
200 sudo make install
201 sudo ldconfig
202}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400203
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400204function do_p4c {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400205 cd ${BUILD_DIR}
206 if [ ! -d p4c ]; then
207 git clone https://github.com/p4lang/p4c.git
208 fi
209 cd p4c
210 git fetch
211 git checkout ${P4C_COMMIT}
212 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400213
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200214 mkdir -p build
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400215 cd build
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200216 cmake ..
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400217 make -j${NUM_CORES}
218 sudo make install
219 sudo ldconfig
220}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400221
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400222function check_commit {
223 if [ ! -e $2 ]; then
224 return 0 # true
225 fi
226 if [[ $(< $2) != "$1" ]]; then
227 return 0 # true
228 fi
229 return 1 # false
230}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400231
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700232# The following is borrowed from Mininet's util/install.sh
233function version_ge {
234 # sort -V sorts by *version number*
235 latest=`printf "$1\n$2" | sort -V | tail -1`
236 # If $1 is latest version, then $1 >= $2
237 [ "$1" == "$latest" ]
238}
239
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400240MUST_DO_ALL=false
241DID_REQUIREMENTS=false
242function check_and_do {
243 # Check if the latest built commit is the same we are trying to build now, or if all projects
244 # must be built. If true builds this project.
245 commit_id="$1"
246 proj_dir="$2"
247 func_name="$3"
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200248 simple_name="$4"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400249 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
250 echo "#"
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200251 echo "# Building ${simple_name} (${commit_id})"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400252 echo "#"
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700253 # Print commands used to install to aid debugging
254 set -x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400255 if ! ${DID_REQUIREMENTS} = true; then
256 do_requirements
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700257 #TODO consider other Linux distros; presently this script assumes that it is running on Ubuntu
258 RELEASE=`lsb_release -rs`
259 if version_ge $RELEASE 16.04; then
260 do_requirements_1604
261 elif version_ge $RELEASE 14.04; then
262 do_requirements_1404
263 else
264 echo "Ubuntu version $RELEASE is not supported"
265 exit 1
266 fi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400267 DID_REQUIREMENTS=true
268 fi
269 eval ${func_name}
270 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
271 # Build all next projects as they might depend on this one.
272 MUST_DO_ALL=true
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700273 # Disable printing to reduce output
274 set +x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400275 else
276 echo "${proj_dir} is up to date (commit ${commit_id})"
277 fi
278}
279
280mkdir -p ${BUILD_DIR}
281cd ${BUILD_DIR}
282# In dependency order.
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200283check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf protobuf
284check_and_do ${GRPC_COMMIT} grpc do_grpc grpc
285check_and_do ${BMV2_COMMIT} bmv2 do_pi_bmv2_deps bmv2-deps
286check_and_do ${PI_COMMIT} p4runtime do_p4runtime p4runtime
287check_and_do ${BMV2_COMMIT} bmv2 do_bmv2 bmv2
288check_and_do ${P4C_COMMIT} p4c do_p4c p4c
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400289
290echo "Done!"