blob: 38ba91ba36908da394c4d5c812fb2d3a6a82747e [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
Carmelo Cascone99474f12017-09-06 22:11:39 +020018BMV2_COMMIT="4eeb8dad8e8f062636f9d0d8296aa7f288c6f6dd"
19PI_COMMIT="9fc50cd0a0187eb1346272524d4b8bafb51bb513"
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020020P4C_COMMIT="55067fd0e5f9e25fef06e58e49033da3493f796d"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040021PROTOBUF_COMMIT="tags/v3.0.2"
22GRPC_COMMIT="tags/v1.3.0"
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 \
74 tcpdump \
Carmelo Casconefb76b042017-07-17 19:42:00 -040075 wget \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040076 unzip
Carmelo Cascone87ac7372017-09-15 01:58:59 +020077
78 sudo pip install setuptools cffi
Brian O'Connor73f8bd72017-06-16 15:27:49 -070079}
80
81function do_requirements_1404 {
82 sudo apt install -y python-software-properties software-properties-common
83 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020084 sudo add-apt-repository -y ppa:george-edison55/cmake-3.x
Brian O'Connor73f8bd72017-06-16 15:27:49 -070085 sudo apt update
86 sudo apt install -y \
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020087 cmake \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070088 dpkg-dev \
89 g++-4.9 \
90 gcc-4.9 \
91 libboost-iostreams-dev
Carmelo Casconeacf12252017-06-05 01:05:43 -040092
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040093 # Needed for p4c.
94 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
95 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
96}
Carmelo Casconeacf12252017-06-05 01:05:43 -040097
Brian O'Connor73f8bd72017-06-16 15:27:49 -070098function do_requirements_1604 {
99 sudo apt-get update
100 sudo apt-get install -y --no-install-recommends \
101 ca-certificates \
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200102 cmake \
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700103 g++ \
104 libboost-iostreams1.58-dev
105}
106
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400107function do_protobuf {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400108 cd ${BUILD_DIR}
109 if [ ! -d protobuf ]; then
110 git clone https://github.com/google/protobuf.git
111 fi
112 cd protobuf
113 git fetch
114 git checkout ${PROTOBUF_COMMIT}
Carmelo Casconefb76b042017-07-17 19:42:00 -0400115
116 export CFLAGS="-Os"
117 export CXXFLAGS="-Os"
118 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400119 ./autogen.sh
120 ./configure --prefix=/usr
121 make -j${NUM_CORES}
122 sudo make install
123 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400124 unset CFLAGS CXXFLAGS LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400125}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400126
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400127function do_grpc {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400128 cd ${BUILD_DIR}
129 if [ ! -d grpc ]; then
130 git clone https://github.com/grpc/grpc.git
131 fi
132 cd grpc
133 git fetch
134 git checkout ${GRPC_COMMIT}
135 git submodule update --init
Carmelo Casconeacf12252017-06-05 01:05:43 -0400136
Carmelo Casconefb76b042017-07-17 19:42:00 -0400137 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400138 make -j${NUM_CORES}
139 sudo make install
140 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400141 unset LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400142}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400143
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200144function checkout_bmv2 {
145 cd ${BUILD_DIR}
146 if [ ! -d bmv2 ]; then
147 git clone https://github.com/p4lang/behavioral-model.git bmv2
148 fi
149 cd bmv2
150 git fetch
151 git checkout ${BMV2_COMMIT}
152}
153
154function do_pi_bmv2_deps {
155 checkout_bmv2
156 # From bmv2's install_deps.sh.
157 # Nanomsg is required also by p4runtime.
158 tmpdir=`mktemp -d -p .`
159 cd ${tmpdir}
160 bash ../travis/install-thrift.sh
161 bash ../travis/install-nanomsg.sh
162 sudo ldconfig
163 bash ../travis/install-nnpy.sh
164 cd ..
165 sudo rm -rf $tmpdir
166}
167
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400168function do_p4runtime {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400169 cd ${BUILD_DIR}
170 if [ ! -d p4runtime ]; then
171 git clone https://github.com/p4lang/PI.git p4runtime
172 fi
173 cd p4runtime
174 git fetch
175 git checkout ${PI_COMMIT}
176 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400177
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400178 ./autogen.sh
Carmelo Cascone28f37782017-08-29 20:11:21 +0200179 ./configure --with-proto 'CXXFLAGS=-O0 -g'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400180 make -j${NUM_CORES}
181 sudo make install
182 sudo ldconfig
183}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400184
Carmelo Casconefb76b042017-07-17 19:42:00 -0400185function do_bmv2 {
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200186 checkout_bmv2
Carmelo Casconeaaf6d982017-07-29 19:52:58 -0400187
Carmelo Casconefb76b042017-07-17 19:42:00 -0400188 ./autogen.sh
Carmelo Cascone28f37782017-08-29 20:11:21 +0200189 ./configure --enable-debugger --with-pi 'CXXFLAGS=-O0 -g'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400190 make -j${NUM_CORES}
191 sudo make install
192 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400193
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400194 # Simple_switch_grpc target
195 cd targets/simple_switch_grpc
196 ./autogen.sh
Carmelo Cascone28f37782017-08-29 20:11:21 +0200197 ./configure 'CXXFLAGS=-O0 -g'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400198 make -j${NUM_CORES}
199 sudo make install
200 sudo ldconfig
201}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400202
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400203function do_p4c {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400204 cd ${BUILD_DIR}
205 if [ ! -d p4c ]; then
206 git clone https://github.com/p4lang/p4c.git
207 fi
208 cd p4c
209 git fetch
210 git checkout ${P4C_COMMIT}
211 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400212
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200213 mkdir -p build
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400214 cd build
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200215 cmake ..
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400216 make -j${NUM_CORES}
217 sudo make install
218 sudo ldconfig
219}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400220
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400221function check_commit {
222 if [ ! -e $2 ]; then
223 return 0 # true
224 fi
225 if [[ $(< $2) != "$1" ]]; then
226 return 0 # true
227 fi
228 return 1 # false
229}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400230
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700231# The following is borrowed from Mininet's util/install.sh
232function version_ge {
233 # sort -V sorts by *version number*
234 latest=`printf "$1\n$2" | sort -V | tail -1`
235 # If $1 is latest version, then $1 >= $2
236 [ "$1" == "$latest" ]
237}
238
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400239MUST_DO_ALL=false
240DID_REQUIREMENTS=false
241function check_and_do {
242 # Check if the latest built commit is the same we are trying to build now, or if all projects
243 # must be built. If true builds this project.
244 commit_id="$1"
245 proj_dir="$2"
246 func_name="$3"
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200247 simple_name="$4"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400248 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
249 echo "#"
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200250 echo "# Building ${simple_name} (${commit_id})"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400251 echo "#"
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700252 # Print commands used to install to aid debugging
253 set -x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400254 if ! ${DID_REQUIREMENTS} = true; then
255 do_requirements
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700256 #TODO consider other Linux distros; presently this script assumes that it is running on Ubuntu
257 RELEASE=`lsb_release -rs`
258 if version_ge $RELEASE 16.04; then
259 do_requirements_1604
260 elif version_ge $RELEASE 14.04; then
261 do_requirements_1404
262 else
263 echo "Ubuntu version $RELEASE is not supported"
264 exit 1
265 fi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400266 DID_REQUIREMENTS=true
267 fi
268 eval ${func_name}
269 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
270 # Build all next projects as they might depend on this one.
271 MUST_DO_ALL=true
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700272 # Disable printing to reduce output
273 set +x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400274 else
275 echo "${proj_dir} is up to date (commit ${commit_id})"
276 fi
277}
278
279mkdir -p ${BUILD_DIR}
280cd ${BUILD_DIR}
281# In dependency order.
Carmelo Cascone87ac7372017-09-15 01:58:59 +0200282check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf protobuf
283check_and_do ${GRPC_COMMIT} grpc do_grpc grpc
284check_and_do ${BMV2_COMMIT} bmv2 do_pi_bmv2_deps bmv2-deps
285check_and_do ${PI_COMMIT} p4runtime do_p4runtime p4runtime
286check_and_do ${BMV2_COMMIT} bmv2 do_bmv2 bmv2
287check_and_do ${P4C_COMMIT} p4c do_p4c p4c
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400288
289echo "Done!"