blob: fb88e7e397785fde08de0c81a9c2b5af71be0c9d [file] [log] [blame]
Carmelo Casconeb7e618d2018-01-12 18:31:33 -08001#!/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 and 16.04.
7#
8# Recommended minimum system requirements:
9# 4 GB of RAM
10# 2 cores
11# 8 GB free hard drive space (~4 GB to build everything)
12# -----------------------------------------------------------------------------
13
14# Exit on errors.
15set -e
16
17BUILD_DIR=~/p4tools
18BMV2_COMMIT="3f1d8d7893d7cf1657285c8aacbb4af5c6d22620"
19PI_COMMIT="0325da7746efe192935e8969fd08eed68d654c98"
20P4C_COMMIT="4c0d629ce2492294ff4108c910f8e6be44112c68"
21PROTOBUF_COMMIT="tags/v3.2.0"
22GRPC_COMMIT="tags/v1.3.2"
23LIBYANG_COMMIT="v0.14-r1"
24SYSREPO_COMMIT="v0.7.2"
Carmelo Cascone76b3ee62018-01-30 15:45:27 -080025P4RT_TEST_COMMIT="master"
Carmelo Casconeb7e618d2018-01-12 18:31:33 -080026
27NUM_CORES=`grep -c ^processor /proc/cpuinfo`
28
29function do_requirements {
30 sudo apt update
31 sudo apt-get install -y --no-install-recommends \
32 autoconf \
33 automake \
34 bison \
35 build-essential \
36 cmake \
37 cpp \
38 curl \
39 flex \
40 git \
41 libavl-dev \
42 libboost-dev \
43 libboost-program-options-dev \
44 libboost-system-dev \
45 libboost-filesystem-dev \
46 libboost-thread-dev \
47 libboost-filesystem-dev \
48 libboost-program-options-dev \
49 libboost-system-dev \
50 libboost-test-dev \
51 libboost-thread-dev \
52 libc6-dev \
53 libev-dev \
54 libevent-dev \
55 libffi-dev \
56 libfl-dev \
57 libgc-dev \
58 libgc1c2 \
59 libgflags-dev \
60 libgmp-dev \
61 libgmp10 \
62 libgmpxx4ldbl \
63 libjudy-dev \
64 libpcap-dev \
65 libpcre3-dev \
66 libprotobuf-c-dev \
67 libreadline6 \
68 libreadline6-dev \
69 libssl-dev \
70 libtool \
71 make \
72 mktemp \
73 pkg-config \
74 protobuf-c-compiler \
75 python \
76 python-dev \
77 python-ipaddr \
78 python-pip \
79 python-scapy \
80 python-setuptools \
81 tcpdump \
82 wget \
83 unzip
84
85 sudo pip install setuptools cffi grpcio
86}
87
88function do_requirements_1404 {
89 sudo apt install -y python-software-properties software-properties-common
90 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
91 sudo add-apt-repository -y ppa:george-edison55/cmake-3.x
92 sudo apt update
93 sudo apt install -y \
94 dpkg-dev \
95 g++-4.9 \
96 gcc-4.9 \
97 libboost-iostreams-dev
98
99 # Needed for p4c.
100 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
101 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
102}
103
104function do_requirements_1604 {
105 sudo apt-get update
106 sudo apt-get install -y --no-install-recommends \
107 ca-certificates \
108 g++ \
109 libboost-iostreams1.58-dev
110}
111
112function do_protobuf {
113 cd ${BUILD_DIR}
114 if [ ! -d protobuf ]; then
115 git clone https://github.com/google/protobuf.git
116 fi
117 cd protobuf
118 git fetch
119 git checkout ${PROTOBUF_COMMIT}
120
121 export CFLAGS="-Os"
122 export CXXFLAGS="-Os"
123 export LDFLAGS="-Wl,-s"
124 ./autogen.sh
125 ./configure --prefix=/usr
126 make -j${NUM_CORES}
127 sudo make install
128 sudo ldconfig
129 unset CFLAGS CXXFLAGS LDFLAGS
130}
131
132function do_grpc {
133 cd ${BUILD_DIR}
134 if [ ! -d grpc ]; then
135 git clone https://github.com/grpc/grpc.git
136 fi
137 cd grpc
138 git fetch
139 git checkout ${GRPC_COMMIT}
140 git submodule update --init
141
142 export LDFLAGS="-Wl,-s"
143 make -j${NUM_CORES}
144 sudo make install
145 sudo ldconfig
146 unset LDFLAGS
147}
148
149function do_libyang {
150 cd ${BUILD_DIR}
151 if [ ! -d libyang ]; then
152 git clone https://github.com/CESNET/libyang.git
153 fi
154 cd libyang
155 git fetch
156 git checkout ${LIBYANG_COMMIT}
157
158 mkdir build
159 cd build
160 cmake ..
161 make -j${NUM_CORES}
162 sudo make install
163 sudo ldconfig
164}
165
166function do_sysrepo {
167 cd ${BUILD_DIR}
168 if [ ! -d sysrepo ]; then
169 git clone https://github.com/sysrepo/sysrepo.git
170 fi
171 cd sysrepo
172 git fetch
173 git checkout ${SYSREPO_COMMIT}
174
175 mkdir build
176 cd build
177 cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_EXAMPLES=Off \
178 -DCALL_TARGET_BINS_DIRECTLY=Off ..
179 make -j${NUM_CORES}
180 sudo make install
181 sudo ldconfig
182}
183
184function checkout_bmv2 {
185 cd ${BUILD_DIR}
186 if [ ! -d bmv2 ]; then
187 git clone https://github.com/p4lang/behavioral-model.git bmv2
188 fi
189 cd bmv2
190 git fetch
191 git checkout ${BMV2_COMMIT}
192}
193
194function do_pi_bmv2_deps {
195 checkout_bmv2
196 # From bmv2's install_deps.sh.
197 # Nanomsg is required also by p4runtime.
198 tmpdir=`mktemp -d -p .`
199 cd ${tmpdir}
200 bash ../travis/install-thrift.sh
201 bash ../travis/install-nanomsg.sh
202 sudo ldconfig
203 bash ../travis/install-nnpy.sh
204 cd ..
205 sudo rm -rf $tmpdir
206}
207
208function do_p4runtime {
209 cd ${BUILD_DIR}
210 if [ ! -d p4runtime ]; then
211 git clone https://github.com/p4lang/PI.git p4runtime
212 fi
213 cd p4runtime
214 git fetch
215 git checkout ${PI_COMMIT}
216 git submodule update --init --recursive
217
218 ./autogen.sh
219 ./configure --with-proto --with-sysrepo 'CXXFLAGS=-O0 -g'
220 make -j${NUM_CORES}
221 sudo make install
222 sudo ldconfig
223
224 sudo proto/sysrepo/install_yangs.sh
225}
226
227function do_bmv2 {
228 checkout_bmv2
229
230 ./autogen.sh
231 ./configure --enable-debugger --with-pi 'CXXFLAGS=-O0 -g'
232 make -j${NUM_CORES}
233 sudo make install
234 sudo ldconfig
235
236 # Simple_switch_grpc target
237 cd targets/simple_switch_grpc
238 ./autogen.sh
239 ./configure --with-sysrepo --with-thrift 'CXXFLAGS=-O0 -g'
240 make -j${NUM_CORES}
241 sudo make install
242 sudo ldconfig
243}
244
245function do_p4c {
246 cd ${BUILD_DIR}
247 if [ ! -d p4c ]; then
248 git clone https://github.com/p4lang/p4c.git
249 fi
250 cd p4c
251 git fetch
252 git checkout ${P4C_COMMIT}
253 git submodule update --init --recursive
254
255 mkdir -p build
256 cd build
257 cmake ..
258 make -j${NUM_CORES}
259 sudo make install
260 sudo ldconfig
261}
262
Carmelo Cascone76b3ee62018-01-30 15:45:27 -0800263function do_p4rt_test {
264 cd ${BUILD_DIR}
265 if [ ! -d p4rt-test ]; then
266 git clone https://github.com/TakeshiTseng/P4-runtime-test-tool.git p4rt-test
267 fi
268 cd p4rt-test
269 git pull origin master
270
271 sudo rm -f /usr/local/bin/p4rt-test
272 sudo ln -s ${BUILD_DIR}/p4rt-test/main.py /usr/local/bin/p4rt-test
273}
274
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800275function check_commit {
276 if [ ! -e $2 ]; then
277 return 0 # true
278 fi
279 if [[ $(< $2) != "$1" ]]; then
280 return 0 # true
281 fi
282 return 1 # false
283}
284
285# The following is borrowed from Mininet's util/install.sh
286function version_ge {
287 # sort -V sorts by *version number*
288 latest=`printf "$1\n$2" | sort -V | tail -1`
289 # If $1 is latest version, then $1 >= $2
290 [ "$1" == "$latest" ]
291}
292
293MUST_DO_ALL=false
294DID_REQUIREMENTS=false
295function check_and_do {
296 # Check if the latest built commit is the same we are trying to build now,
297 # or if all projects must be built. If true builds this project.
298 commit_id="$1"
299 proj_dir="$2"
300 func_name="$3"
301 simple_name="$4"
302 if ${MUST_DO_ALL} = true \
Carmelo Cascone76b3ee62018-01-30 15:45:27 -0800303 || ${commit_id} = "master" \
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800304 || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
305 echo "#"
306 echo "# Building ${simple_name} (${commit_id})"
307 echo "#"
308 # Print commands used to install to aid debugging
309 set -x
310 if ! ${DID_REQUIREMENTS} = true; then
311 do_requirements
312 # TODO consider other Linux distros; presently this script assumes
313 # that it is running on Ubuntu.
314 RELEASE=`lsb_release -rs`
315 if version_ge $RELEASE 16.04; then
316 do_requirements_1604
317 elif version_ge $RELEASE 14.04; then
318 do_requirements_1404
319 else
320 echo "Ubuntu version $RELEASE is not supported"
321 exit 1
322 fi
323 DID_REQUIREMENTS=true
324 fi
325 eval ${func_name}
326 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
327 # Build all next projects as they might depend on this one.
328 MUST_DO_ALL=true
329 # Disable printing to reduce output
330 set +x
331 else
332 echo "${proj_dir} is up to date (commit ${commit_id})"
333 fi
334}
335
336mkdir -p ${BUILD_DIR}
337cd ${BUILD_DIR}
338# In dependency order.
339check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf protobuf
340check_and_do ${GRPC_COMMIT} grpc do_grpc grpc
341check_and_do ${LIBYANG_COMMIT} libyang do_libyang libyang
342check_and_do ${SYSREPO_COMMIT} sysrepo do_sysrepo sysrepo
343check_and_do ${BMV2_COMMIT} bmv2 do_pi_bmv2_deps bmv2-deps
344check_and_do ${PI_COMMIT} p4runtime do_p4runtime p4runtime
345check_and_do ${BMV2_COMMIT} bmv2 do_bmv2 bmv2
346check_and_do ${P4C_COMMIT} p4c do_p4c p4c
Carmelo Cascone76b3ee62018-01-30 15:45:27 -0800347check_and_do ${P4RT_TEST_COMMIT} p4rt-test do_p4rt_test p4rt-test
Carmelo Casconeb7e618d2018-01-12 18:31:33 -0800348
349echo "Done!"