blob: 65179ef2299752d65c377d2c979d2d4a0f36cab9 [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#
6# Tested on Ubuntu 14.04.
7#
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
Carmelo Casconefb76b042017-07-17 19:42:00 -040029 #FIXME the following two lines seem like they are not required, at least for 16.04
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040030 sudo apt install -y python-software-properties software-properties-common
31 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
32 sudo apt update
Carmelo Casconefb76b042017-07-17 19:42:00 -040033 #FIXME 14.04 needs special gcc, 16.04 can use apt-get default
34 sudo apt install -y --no-install-recommends \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040035 autoconf \
36 automake \
37 bison \
Carmelo Casconefb76b042017-07-17 19:42:00 -040038 build-essential \
39 cmake \
40 cpp \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040041 curl \
42 dpkg-dev \
43 flex \
44 g++-4.9 \
45 gcc-4.9 \
46 git \
47 libboost-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040048 libboost-filesystem-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040049 libboost-iostreams-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040050 libboost-program-options-dev \
51 libboost-system-dev \
52 libboost-test-dev \
53 libboost-thread-dev \
54 libc6-dev \
55 libevent-dev \
56 libffi-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040057 libfl-dev \
58 libgc-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040059 libgc1c2 \
60 libgflags-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040061 libgmp-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040062 libgmp10 \
63 libgmpxx4ldbl \
64 libjudy-dev \
65 libpcap-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040066 libreadline6 \
67 libreadline6-dev \
Carmelo Casconefb76b042017-07-17 19:42:00 -040068 libssl-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040069 libtool \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040070 make \
Carmelo Casconefb76b042017-07-17 19:42:00 -040071 mktemp \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040072 pkg-config \
73 python \
Carmelo Casconefb76b042017-07-17 19:42:00 -040074 python-dev \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040075 python-ipaddr \
Carmelo Casconefb76b042017-07-17 19:42:00 -040076 python-pip \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040077 python-scapy \
78 tcpdump \
Carmelo Casconefb76b042017-07-17 19:42:00 -040079 wget \
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040080 unzip
Carmelo Casconeacf12252017-06-05 01:05:43 -040081
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040082 # Needed for p4c.
83 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
84 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
85}
Carmelo Casconeacf12252017-06-05 01:05:43 -040086
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040087function do_protobuf {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040088 cd ${BUILD_DIR}
89 if [ ! -d protobuf ]; then
90 git clone https://github.com/google/protobuf.git
91 fi
92 cd protobuf
93 git fetch
94 git checkout ${PROTOBUF_COMMIT}
Carmelo Casconefb76b042017-07-17 19:42:00 -040095
96 export CFLAGS="-Os"
97 export CXXFLAGS="-Os"
98 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040099 ./autogen.sh
100 ./configure --prefix=/usr
101 make -j${NUM_CORES}
102 sudo make install
103 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400104 unset CFLAGS CXXFLAGS LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400105}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400106
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400107function do_grpc {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400108 cd ${BUILD_DIR}
109 if [ ! -d grpc ]; then
110 git clone https://github.com/grpc/grpc.git
111 fi
112 cd grpc
113 git fetch
114 git checkout ${GRPC_COMMIT}
115 git submodule update --init
Carmelo Casconeacf12252017-06-05 01:05:43 -0400116
Carmelo Casconefb76b042017-07-17 19:42:00 -0400117 export LDFLAGS="-Wl,-s"
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400118 make -j${NUM_CORES}
119 sudo make install
120 sudo ldconfig
Carmelo Casconefb76b042017-07-17 19:42:00 -0400121 unset LDFLAGS
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400122}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400123
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400124function do_p4runtime {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400125 cd ${BUILD_DIR}
126 if [ ! -d p4runtime ]; then
127 git clone https://github.com/p4lang/PI.git p4runtime
128 fi
129 cd p4runtime
130 git fetch
131 git checkout ${PI_COMMIT}
132 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400133
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400134 ./autogen.sh
Carmelo Casconefb76b042017-07-17 19:42:00 -0400135 ./configure --with-proto --without-cli --without-internal-rpc
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400136 make -j${NUM_CORES}
137 sudo make install
138 sudo ldconfig
139}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400140
Carmelo Casconefb76b042017-07-17 19:42:00 -0400141function do_bmv2 {
142 cd ${BUILD_DIR}
143 if [ ! -d bmv2 ]; then
144 git clone https://github.com/p4lang/behavioral-model.git bmv2
145 fi
146 cd bmv2
147 git fetch
148 git checkout ${BMV2_COMMIT}
149
150 ./autogen.sh
151 # FIXME: to build with debugger, we need to install nanomsg first (see bmv2's install_deps.sh)
152 ./configure --without-thrift --without-nanomsg --with-pi
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400153 make -j${NUM_CORES}
154 sudo make install
155 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400156
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400157 # Simple_switch_grpc target
158 cd targets/simple_switch_grpc
159 ./autogen.sh
160 ./configure
161 make -j${NUM_CORES}
162 sudo make install
163 sudo ldconfig
164}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400165
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400166function do_p4c {
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400167 cd ${BUILD_DIR}
168 if [ ! -d p4c ]; then
169 git clone https://github.com/p4lang/p4c.git
170 fi
171 cd p4c
172 git fetch
173 git checkout ${P4C_COMMIT}
174 git submodule update --init --recursive
Carmelo Casconefb76b042017-07-17 19:42:00 -0400175
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400176 ./bootstrap.sh
177 cd build
178 make -j${NUM_CORES}
179 sudo make install
180 sudo ldconfig
181}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400182
Carmelo Casconefb76b042017-07-17 19:42:00 -0400183# FIXME: Can we deprecate this?
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400184function do_p4c_bm {
Carmelo Casconefb76b042017-07-17 19:42:00 -0400185 # old BMv2 compiler
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400186 cd ${BUILD_DIR}
187 if [ ! -d p4c-bmv2 ]; then
188 git clone https://github.com/p4lang/p4c-bm.git p4c-bmv2
189 fi
190 cd p4c-bmv2
191 git fetch
192 git checkout ${P4C_BM_COMMIT}
193 sudo pip install -r requirements.txt
194 sudo python setup.py install
195}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400196
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400197function check_commit {
198 if [ ! -e $2 ]; then
199 return 0 # true
200 fi
201 if [[ $(< $2) != "$1" ]]; then
202 return 0 # true
203 fi
204 return 1 # false
205}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400206
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400207MUST_DO_ALL=false
208DID_REQUIREMENTS=false
209function check_and_do {
210 # Check if the latest built commit is the same we are trying to build now, or if all projects
211 # must be built. If true builds this project.
212 commit_id="$1"
213 proj_dir="$2"
214 func_name="$3"
215 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
216 echo "#"
217 echo "# Building ${proj_dir} (${commit_id})"
218 echo "#"
219 if ! ${DID_REQUIREMENTS} = true; then
Carmelo Casconefb76b042017-07-17 19:42:00 -0400220 #FIXME need version check for 14.04 vs 16.04
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400221 do_requirements
222 DID_REQUIREMENTS=true
223 fi
224 eval ${func_name}
225 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
226 # Build all next projects as they might depend on this one.
227 MUST_DO_ALL=true
228 else
229 echo "${proj_dir} is up to date (commit ${commit_id})"
230 fi
231}
232
233mkdir -p ${BUILD_DIR}
234cd ${BUILD_DIR}
235# In dependency order.
236check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf
237check_and_do ${GRPC_COMMIT} grpc do_grpc
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400238check_and_do ${PI_COMMIT} p4runtime do_p4runtime
Carmelo Casconefb76b042017-07-17 19:42:00 -0400239check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400240check_and_do ${P4C_COMMIT} p4c do_p4c
241check_and_do ${P4C_BM_COMMIT} p4c-bmv2 do_p4c_bm
242
243echo "Done!"