blob: 63ac02af6c3a332a6b9763aacb968e56b237b8b7 [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 Casconefb76b042017-07-17 19:42:00 -040018BMV2_COMMIT="654e9c3794273b481284d8bafecfc74c7ffd643d"
19PI_COMMIT="8051d54c9c3415ca800db232a226189fd17ad60a"
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 Casconefb76b042017-07-17 19:42:00 -0400151 ./configure --with-proto --without-cli --without-internal-rpc
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
166 ./autogen.sh
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700167 # TODO: to build with debugger, we need to install nanomsg first (see bmv2's install_deps.sh)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400168 ./configure --without-thrift --without-nanomsg --with-pi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400169 make -j${NUM_CORES}
170 sudo make install
171 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400172
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400173 # Simple_switch_grpc target
174 cd targets/simple_switch_grpc
175 ./autogen.sh
176 ./configure
177 make -j${NUM_CORES}
178 sudo make install
179 sudo ldconfig
180}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400181
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400182function do_p4c {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400183 cd ${BUILD_DIR}
184 if [ ! -d p4c ]; then
185 git clone https://github.com/p4lang/p4c.git
186 fi
187 cd p4c
188 git fetch
189 git checkout ${P4C_COMMIT}
190 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400191
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400192 ./bootstrap.sh
193 cd build
194 make -j${NUM_CORES}
195 sudo make install
196 sudo ldconfig
197}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400198
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400199function check_commit {
200 if [ ! -e $2 ]; then
201 return 0 # true
202 fi
203 if [[ $(< $2) != "$1" ]]; then
204 return 0 # true
205 fi
206 return 1 # false
207}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400208
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700209# The following is borrowed from Mininet's util/install.sh
210function version_ge {
211 # sort -V sorts by *version number*
212 latest=`printf "$1\n$2" | sort -V | tail -1`
213 # If $1 is latest version, then $1 >= $2
214 [ "$1" == "$latest" ]
215}
216
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400217MUST_DO_ALL=false
218DID_REQUIREMENTS=false
219function check_and_do {
220 # Check if the latest built commit is the same we are trying to build now, or if all projects
221 # must be built. If true builds this project.
222 commit_id="$1"
223 proj_dir="$2"
224 func_name="$3"
225 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
226 echo "#"
227 echo "# Building ${proj_dir} (${commit_id})"
228 echo "#"
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700229 # Print commands used to install to aid debugging
230 set -x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400231 if ! ${DID_REQUIREMENTS} = true; then
232 do_requirements
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700233 #TODO consider other Linux distros; presently this script assumes that it is running on Ubuntu
234 RELEASE=`lsb_release -rs`
235 if version_ge $RELEASE 16.04; then
236 do_requirements_1604
237 elif version_ge $RELEASE 14.04; then
238 do_requirements_1404
239 else
240 echo "Ubuntu version $RELEASE is not supported"
241 exit 1
242 fi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400243 DID_REQUIREMENTS=true
244 fi
245 eval ${func_name}
246 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
247 # Build all next projects as they might depend on this one.
248 MUST_DO_ALL=true
Brian O'Connor73f8bd72017-06-16 15:27:49 -0700249 # Disable printing to reduce output
250 set +x
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400251 else
252 echo "${proj_dir} is up to date (commit ${commit_id})"
253 fi
254}
255
256mkdir -p ${BUILD_DIR}
257cd ${BUILD_DIR}
258# In dependency order.
259check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf
260check_and_do ${GRPC_COMMIT} grpc do_grpc
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400261check_and_do ${PI_COMMIT} p4runtime do_p4runtime
Carmelo Casconefb76b042017-07-17 19:42:00 -0400262check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400263check_and_do ${P4C_COMMIT} p4c do_p4c
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400264
265echo "Done!"