blob: a10d0e2a6b0b9a129b318f98f6041f1a3c1174d7 [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 Casconeaaf6d982017-07-29 19:52:58 -040018BMV2_COMMIT="a9b9fb3e30585bf880b252a3e00338ebca67b572"
19PI_COMMIT="b8d937fa72650eb6ec57d2e5755e0c9103fa995f"
Carmelo Cascone569d4ad2017-07-10 16:09:00 -040020P4C_COMMIT="adce375fd961a9e467c7e77ad1bef647ef28e5e8"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040021P4C_BM_COMMIT="8f4abeaa6f8374aaf95ea2aacfc2b750069391b5"
22PROTOBUF_COMMIT="tags/v3.0.2"
23GRPC_COMMIT="tags/v1.3.0"
Carmelo Casconeacf12252017-06-05 01:05:43 -040024
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040025NUM_CORES=`grep -c ^processor /proc/cpuinfo`
Carmelo Casconeacf12252017-06-05 01:05:43 -040026
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040027function do_requirements {
28 sudo apt update
Brian O'Connor73f8bd72017-06-16 15:27:49 -070029 sudo apt-get install -y --no-install-recommends \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040030 autoconf \
31 automake \
32 bison \
Carmelo Casconefb76b042017-07-17 19:42:00 -040033 build-essential \
34 cmake \
35 cpp \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040036 curl \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040037 flex \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040038 git \
39 libboost-dev \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070040 libboost-program-options-dev \
41 libboost-system-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040042 libboost-filesystem-dev \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070043 libboost-thread-dev \
44 libboost-filesystem-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040045 libboost-program-options-dev \
46 libboost-system-dev \
47 libboost-test-dev \
48 libboost-thread-dev \
49 libc6-dev \
50 libevent-dev \
51 libffi-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040052 libfl-dev \
53 libgc-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040054 libgc1c2 \
55 libgflags-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040056 libgmp-dev \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070057 libjudy-dev \
58 libpcap-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040059 libgmp10 \
60 libgmpxx4ldbl \
61 libjudy-dev \
62 libpcap-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040063 libreadline6 \
64 libreadline6-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040065 libssl-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040066 libtool \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040067 make \
Carmelo Casconefb76b042017-07-17 19:42:00 -040068 mktemp \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040069 pkg-config \
70 python \
Carmelo Casconefb76b042017-07-17 19:42:00 -040071 python-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040072 python-ipaddr \
Carmelo Casconefb76b042017-07-17 19:42:00 -040073 python-pip \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040074 python-scapy \
75 tcpdump \
Carmelo Casconefb76b042017-07-17 19:42:00 -040076 wget \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040077 unzip
Brian O'Connor73f8bd72017-06-16 15:27:49 -070078}
79
80function do_requirements_1404 {
81 sudo apt install -y python-software-properties software-properties-common
82 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
83 sudo apt update
84 sudo apt install -y \
85 dpkg-dev \
86 g++-4.9 \
87 gcc-4.9 \
88 libboost-iostreams-dev
Carmelo Casconeacf12252017-06-05 01:05:43 -040089
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040090 # Needed for p4c.
91 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
92 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
93}
Carmelo Casconeacf12252017-06-05 01:05:43 -040094
Brian O'Connor73f8bd72017-06-16 15:27:49 -070095function do_requirements_1604 {
96 sudo apt-get update
97 sudo apt-get install -y --no-install-recommends \
98 ca-certificates \
99 g++ \
100 libboost-iostreams1.58-dev
101}
102
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400103function do_protobuf {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400104 cd ${BUILD_DIR}
105 if [ ! -d protobuf ]; then
106 git clone https://github.com/google/protobuf.git
107 fi
108 cd protobuf
109 git fetch
110 git checkout ${PROTOBUF_COMMIT}
Carmelo Casconefb76b042017-07-17 19:42:00 -0400111
112 export CFLAGS="-Os"
113 export CXXFLAGS="-Os"
114 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400115 ./autogen.sh
116 ./configure --prefix=/usr
117 make -j${NUM_CORES}
118 sudo make install
119 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400120 unset CFLAGS CXXFLAGS LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400121}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400122
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400123function do_grpc {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400124 cd ${BUILD_DIR}
125 if [ ! -d grpc ]; then
126 git clone https://github.com/grpc/grpc.git
127 fi
128 cd grpc
129 git fetch
130 git checkout ${GRPC_COMMIT}
131 git submodule update --init
Carmelo Casconeacf12252017-06-05 01:05:43 -0400132
Carmelo Casconefb76b042017-07-17 19:42:00 -0400133 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400134 make -j${NUM_CORES}
135 sudo make install
136 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400137 unset LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400138}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400139
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400140function do_p4runtime {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400141 cd ${BUILD_DIR}
142 if [ ! -d p4runtime ]; then
143 git clone https://github.com/p4lang/PI.git p4runtime
144 fi
145 cd p4runtime
146 git fetch
147 git checkout ${PI_COMMIT}
148 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400149
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400150 ./autogen.sh
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200151 ./configure --with-proto 'CXXFLAGS=-O0 -ggdb'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400152 make -j${NUM_CORES}
153 sudo make install
154 sudo ldconfig
155}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400156
Carmelo Casconefb76b042017-07-17 19:42:00 -0400157function do_bmv2 {
158 cd ${BUILD_DIR}
159 if [ ! -d bmv2 ]; then
160 git clone https://github.com/p4lang/behavioral-model.git bmv2
161 fi
162 cd bmv2
163 git fetch
164 git checkout ${BMV2_COMMIT}
165
Carmelo Casconeaaf6d982017-07-29 19:52:58 -0400166 # From bmv2's install_deps.sh
167 tmpdir=`mktemp -d -p .`
168 cd ${tmpdir}
169 bash ../travis/install-thrift.sh
170 bash ../travis/install-nanomsg.sh
171 sudo ldconfig
172 bash ../travis/install-nnpy.sh
173 cd ..
174 sudo rm -rf $tmpdir
175
Carmelo Casconefb76b042017-07-17 19:42:00 -0400176 ./autogen.sh
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200177 ./configure --enable-debugger --with-pi 'CXXFLAGS=-O0 -ggdb'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400178 make -j${NUM_CORES}
179 sudo make install
180 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400181
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400182 # Simple_switch_grpc target
183 cd targets/simple_switch_grpc
184 ./autogen.sh
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200185 ./configure 'CXXFLAGS=-O0 -ggdb'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400186 make -j${NUM_CORES}
187 sudo make install
188 sudo ldconfig
189}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400190
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400191function do_p4c {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400192 cd ${BUILD_DIR}
193 if [ ! -d p4c ]; then
194 git clone https://github.com/p4lang/p4c.git
195 fi
196 cd p4c
197 git fetch
198 git checkout ${P4C_COMMIT}
199 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400200
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400201 ./bootstrap.sh
202 cd build
203 make -j${NUM_CORES}
204 sudo make install
205 sudo ldconfig
206}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400207
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400208function check_commit {
209 if [ ! -e $2 ]; then
210 return 0 # true
211 fi
212 if [[ $(< $2) != "$1" ]]; then
213 return 0 # true
214 fi
215 return 1 # false
216}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400217
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700218# The following is borrowed from Mininet's util/install.sh
219function version_ge {
220 # sort -V sorts by *version number*
221 latest=`printf "$1\n$2" | sort -V | tail -1`
222 # If $1 is latest version, then $1 >= $2
223 [ "$1" == "$latest" ]
224}
225
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400226MUST_DO_ALL=false
227DID_REQUIREMENTS=false
228function check_and_do {
229 # Check if the latest built commit is the same we are trying to build now, or if all projects
230 # must be built. If true builds this project.
231 commit_id="$1"
232 proj_dir="$2"
233 func_name="$3"
234 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
235 echo "#"
236 echo "# Building ${proj_dir} (${commit_id})"
237 echo "#"
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700238 # Print commands used to install to aid debugging
239 set -x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400240 if ! ${DID_REQUIREMENTS} = true; then
241 do_requirements
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700242 #TODO consider other Linux distros; presently this script assumes that it is running on Ubuntu
243 RELEASE=`lsb_release -rs`
244 if version_ge $RELEASE 16.04; then
245 do_requirements_1604
246 elif version_ge $RELEASE 14.04; then
247 do_requirements_1404
248 else
249 echo "Ubuntu version $RELEASE is not supported"
250 exit 1
251 fi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400252 DID_REQUIREMENTS=true
253 fi
254 eval ${func_name}
255 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
256 # Build all next projects as they might depend on this one.
257 MUST_DO_ALL=true
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700258 # Disable printing to reduce output
259 set +x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400260 else
261 echo "${proj_dir} is up to date (commit ${commit_id})"
262 fi
263}
264
265mkdir -p ${BUILD_DIR}
266cd ${BUILD_DIR}
267# In dependency order.
268check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf
269check_and_do ${GRPC_COMMIT} grpc do_grpc
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400270check_and_do ${PI_COMMIT} p4runtime do_p4runtime
Carmelo Casconefb76b042017-07-17 19:42:00 -0400271check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400272check_and_do ${P4C_COMMIT} p4c do_p4c
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400273
274echo "Done!"