blob: 327a165c10867702db4b3ad1f2492207d311471a [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 Cascone6e4f7eb2017-08-21 23:00:43 +020018BMV2_COMMIT="0f7f05e6ad88640f68b34cd0cae50214bb4140c5"
19PI_COMMIT="1202bfc3d76d88e274c7ec3156a06d206e0ee068"
20P4C_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
Brian O'Connor73f8bd72017-06-16 15:27:49 -070077}
78
79function do_requirements_1404 {
80 sudo apt install -y python-software-properties software-properties-common
81 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020082 sudo add-apt-repository -y ppa:george-edison55/cmake-3.x
Brian O'Connor73f8bd72017-06-16 15:27:49 -070083 sudo apt update
84 sudo apt install -y \
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +020085 cmake \
Brian O'Connor73f8bd72017-06-16 15:27:49 -070086 dpkg-dev \
87 g++-4.9 \
88 gcc-4.9 \
89 libboost-iostreams-dev
Carmelo Casconeacf12252017-06-05 01:05:43 -040090
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040091 # Needed for p4c.
92 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
93 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
94}
Carmelo Casconeacf12252017-06-05 01:05:43 -040095
Brian O'Connor73f8bd72017-06-16 15:27:49 -070096function do_requirements_1604 {
97 sudo apt-get update
98 sudo apt-get install -y --no-install-recommends \
99 ca-certificates \
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200100 cmake \
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700101 g++ \
102 libboost-iostreams1.58-dev
103}
104
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400105function do_protobuf {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400106 cd ${BUILD_DIR}
107 if [ ! -d protobuf ]; then
108 git clone https://github.com/google/protobuf.git
109 fi
110 cd protobuf
111 git fetch
112 git checkout ${PROTOBUF_COMMIT}
Carmelo Casconefb76b042017-07-17 19:42:00 -0400113
114 export CFLAGS="-Os"
115 export CXXFLAGS="-Os"
116 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400117 ./autogen.sh
118 ./configure --prefix=/usr
119 make -j${NUM_CORES}
120 sudo make install
121 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400122 unset CFLAGS CXXFLAGS LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400123}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400124
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400125function do_grpc {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400126 cd ${BUILD_DIR}
127 if [ ! -d grpc ]; then
128 git clone https://github.com/grpc/grpc.git
129 fi
130 cd grpc
131 git fetch
132 git checkout ${GRPC_COMMIT}
133 git submodule update --init
Carmelo Casconeacf12252017-06-05 01:05:43 -0400134
Carmelo Casconefb76b042017-07-17 19:42:00 -0400135 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400136 make -j${NUM_CORES}
137 sudo make install
138 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400139 unset LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400140}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400141
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400142function do_p4runtime {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400143 cd ${BUILD_DIR}
144 if [ ! -d p4runtime ]; then
145 git clone https://github.com/p4lang/PI.git p4runtime
146 fi
147 cd p4runtime
148 git fetch
149 git checkout ${PI_COMMIT}
150 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400151
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400152 ./autogen.sh
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200153 ./configure --with-proto 'CXXFLAGS=-O0 -ggdb'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400154 make -j${NUM_CORES}
155 sudo make install
156 sudo ldconfig
157}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400158
Carmelo Casconefb76b042017-07-17 19:42:00 -0400159function do_bmv2 {
160 cd ${BUILD_DIR}
161 if [ ! -d bmv2 ]; then
162 git clone https://github.com/p4lang/behavioral-model.git bmv2
163 fi
164 cd bmv2
165 git fetch
166 git checkout ${BMV2_COMMIT}
167
Carmelo Casconeaaf6d982017-07-29 19:52:58 -0400168 # From bmv2's install_deps.sh
169 tmpdir=`mktemp -d -p .`
170 cd ${tmpdir}
171 bash ../travis/install-thrift.sh
172 bash ../travis/install-nanomsg.sh
173 sudo ldconfig
174 bash ../travis/install-nnpy.sh
175 cd ..
176 sudo rm -rf $tmpdir
177
Carmelo Casconefb76b042017-07-17 19:42:00 -0400178 ./autogen.sh
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200179 ./configure --enable-debugger --with-pi 'CXXFLAGS=-O0 -ggdb'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400180 make -j${NUM_CORES}
181 sudo make install
182 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400183
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400184 # Simple_switch_grpc target
185 cd targets/simple_switch_grpc
186 ./autogen.sh
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200187 ./configure 'CXXFLAGS=-O0 -ggdb'
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400188 make -j${NUM_CORES}
189 sudo make install
190 sudo ldconfig
191}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400192
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400193function do_p4c {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400194 cd ${BUILD_DIR}
195 if [ ! -d p4c ]; then
196 git clone https://github.com/p4lang/p4c.git
197 fi
198 cd p4c
199 git fetch
200 git checkout ${P4C_COMMIT}
201 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400202
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200203 mkdir -p build
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400204 cd build
Carmelo Cascone6e4f7eb2017-08-21 23:00:43 +0200205 cmake ..
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400206 make -j${NUM_CORES}
207 sudo make install
208 sudo ldconfig
209}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400210
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400211function check_commit {
212 if [ ! -e $2 ]; then
213 return 0 # true
214 fi
215 if [[ $(< $2) != "$1" ]]; then
216 return 0 # true
217 fi
218 return 1 # false
219}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400220
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700221# The following is borrowed from Mininet's util/install.sh
222function version_ge {
223 # sort -V sorts by *version number*
224 latest=`printf "$1\n$2" | sort -V | tail -1`
225 # If $1 is latest version, then $1 >= $2
226 [ "$1" == "$latest" ]
227}
228
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400229MUST_DO_ALL=false
230DID_REQUIREMENTS=false
231function check_and_do {
232 # Check if the latest built commit is the same we are trying to build now, or if all projects
233 # must be built. If true builds this project.
234 commit_id="$1"
235 proj_dir="$2"
236 func_name="$3"
237 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
238 echo "#"
239 echo "# Building ${proj_dir} (${commit_id})"
240 echo "#"
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700241 # Print commands used to install to aid debugging
242 set -x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400243 if ! ${DID_REQUIREMENTS} = true; then
244 do_requirements
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700245 #TODO consider other Linux distros; presently this script assumes that it is running on Ubuntu
246 RELEASE=`lsb_release -rs`
247 if version_ge $RELEASE 16.04; then
248 do_requirements_1604
249 elif version_ge $RELEASE 14.04; then
250 do_requirements_1404
251 else
252 echo "Ubuntu version $RELEASE is not supported"
253 exit 1
254 fi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400255 DID_REQUIREMENTS=true
256 fi
257 eval ${func_name}
258 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
259 # Build all next projects as they might depend on this one.
260 MUST_DO_ALL=true
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700261 # Disable printing to reduce output
262 set +x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400263 else
264 echo "${proj_dir} is up to date (commit ${commit_id})"
265 fi
266}
267
268mkdir -p ${BUILD_DIR}
269cd ${BUILD_DIR}
270# In dependency order.
271check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf
272check_and_do ${GRPC_COMMIT} grpc do_grpc
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400273check_and_do ${PI_COMMIT} p4runtime do_p4runtime
Carmelo Casconefb76b042017-07-17 19:42:00 -0400274check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400275check_and_do ${P4C_COMMIT} p4c do_p4c
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400276
277echo "Done!"