blob: f3573a1eec61ba0449d633fe86b1231ce1315293 [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 Cascone569d4ad2017-07-10 16:09:00 -040018BMV2_COMMIT="41c1f2ed487ff086a1d582069e8bc68cab8ffdb0"
19PI_COMMIT="f110570ca0c8ddc5feaf5028b08a0f477ceb4eea"
20P4C_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
29 sudo apt install -y python-software-properties software-properties-common
30 sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
31 sudo apt update
32 sudo apt install -y \
33 autoconf \
34 automake \
35 bison \
36 curl \
37 dpkg-dev \
38 flex \
39 g++-4.9 \
40 gcc-4.9 \
41 git \
42 libboost-dev \
43 libboost-iostreams-dev \
44 libfl-dev \
45 libgc-dev \
46 libgmp-dev \
47 libreadline6 \
48 libreadline6-dev \
49 libtool \
50 libc6-dev \
51 make \
52 pkg-config \
53 python \
54 python-ipaddr \
55 python-scapy \
56 tcpdump \
57 unzip
Carmelo Casconeacf12252017-06-05 01:05:43 -040058
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040059 # Needed for p4c.
60 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
61 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
62}
Carmelo Casconeacf12252017-06-05 01:05:43 -040063
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040064function do_protobuf {
65 # Protobuf
66 cd ${BUILD_DIR}
67 if [ ! -d protobuf ]; then
68 git clone https://github.com/google/protobuf.git
69 fi
70 cd protobuf
71 git fetch
72 git checkout ${PROTOBUF_COMMIT}
73 ./autogen.sh
74 ./configure --prefix=/usr
75 make -j${NUM_CORES}
76 sudo make install
77 sudo ldconfig
78}
Carmelo Casconeacf12252017-06-05 01:05:43 -040079
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040080function do_grpc {
81 # gRPC
82 cd ${BUILD_DIR}
83 if [ ! -d grpc ]; then
84 git clone https://github.com/grpc/grpc.git
85 fi
86 cd grpc
87 git fetch
88 git checkout ${GRPC_COMMIT}
89 git submodule update --init
90 make -j${NUM_CORES}
91 sudo make install
92 sudo ldconfig
93}
Carmelo Casconeacf12252017-06-05 01:05:43 -040094
Carmelo Cascone7e8f8322017-06-05 13:31:47 -040095function do_bmv2 {
96 # BMv2 part 1 (without PI)
97 cd ${BUILD_DIR}
98 if [ ! -d bmv2 ]; then
99 git clone https://github.com/p4lang/behavioral-model.git bmv2
100 fi
101 cd bmv2
102 git fetch
103 git checkout ${BMV2_COMMIT}
104 ./install_deps.sh
105 ./autogen.sh
106 ./configure --enable-debugger
107 make -j${NUM_CORES}
108 sudo make install
109 sudo ldconfig
110}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400111
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400112function do_p4runtime {
113 # P4Runtime (PI)
114 cd ${BUILD_DIR}
115 if [ ! -d p4runtime ]; then
116 git clone https://github.com/p4lang/PI.git p4runtime
117 fi
118 cd p4runtime
119 git fetch
120 git checkout ${PI_COMMIT}
121 git submodule update --init --recursive
122 ./autogen.sh
123 ./configure --with-bmv2 --with-proto
124 make -j${NUM_CORES}
125 sudo make install
126 sudo ldconfig
127}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400128
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400129function do_bmv2_with_pi {
130 # BMv2 part 2 (with PI)
131 cd ${BUILD_DIR}/bmv2
132 ./configure --enable-debugger --with-pi
133 make -j${NUM_CORES}
134 sudo make install
135 sudo ldconfig
Carmelo Casconeacf12252017-06-05 01:05:43 -0400136
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400137 # Simple_switch_grpc target
138 cd targets/simple_switch_grpc
139 ./autogen.sh
140 ./configure
141 make -j${NUM_CORES}
142 sudo make install
143 sudo ldconfig
144}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400145
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400146function do_p4c {
147 # p4c
148 cd ${BUILD_DIR}
149 if [ ! -d p4c ]; then
150 git clone https://github.com/p4lang/p4c.git
151 fi
152 cd p4c
153 git fetch
154 git checkout ${P4C_COMMIT}
155 git submodule update --init --recursive
156 ./bootstrap.sh
157 cd build
158 make -j${NUM_CORES}
159 sudo make install
160 sudo ldconfig
161}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400162
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400163function do_p4c_bm {
164 # p4c-bmv2 (old BMv2 compiler)
165 cd ${BUILD_DIR}
166 if [ ! -d p4c-bmv2 ]; then
167 git clone https://github.com/p4lang/p4c-bm.git p4c-bmv2
168 fi
169 cd p4c-bmv2
170 git fetch
171 git checkout ${P4C_BM_COMMIT}
172 sudo pip install -r requirements.txt
173 sudo python setup.py install
174}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400175
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400176function check_commit {
177 if [ ! -e $2 ]; then
178 return 0 # true
179 fi
180 if [[ $(< $2) != "$1" ]]; then
181 return 0 # true
182 fi
183 return 1 # false
184}
Carmelo Casconeacf12252017-06-05 01:05:43 -0400185
Carmelo Cascone7e8f8322017-06-05 13:31:47 -0400186MUST_DO_ALL=false
187DID_REQUIREMENTS=false
188function check_and_do {
189 # Check if the latest built commit is the same we are trying to build now, or if all projects
190 # must be built. If true builds this project.
191 commit_id="$1"
192 proj_dir="$2"
193 func_name="$3"
194 if ${MUST_DO_ALL} = true || check_commit ${commit_id} ${proj_dir}/.last_built_commit; then
195 echo "#"
196 echo "# Building ${proj_dir} (${commit_id})"
197 echo "#"
198 if ! ${DID_REQUIREMENTS} = true; then
199 do_requirements
200 DID_REQUIREMENTS=true
201 fi
202 eval ${func_name}
203 echo ${commit_id} > ${BUILD_DIR}/${proj_dir}/.last_built_commit
204 # Build all next projects as they might depend on this one.
205 MUST_DO_ALL=true
206 else
207 echo "${proj_dir} is up to date (commit ${commit_id})"
208 fi
209}
210
211mkdir -p ${BUILD_DIR}
212cd ${BUILD_DIR}
213# In dependency order.
214check_and_do ${PROTOBUF_COMMIT} protobuf do_protobuf
215check_and_do ${GRPC_COMMIT} grpc do_grpc
216check_and_do ${BMV2_COMMIT} bmv2 do_bmv2
217check_and_do ${PI_COMMIT} p4runtime do_p4runtime
218check_and_do ${BMV2_COMMIT} bmv2 do_bmv2_with_pi
219check_and_do ${P4C_COMMIT} p4c do_p4c
220check_and_do ${P4C_BM_COMMIT} p4c-bmv2 do_p4c_bm
221
222echo "Done!"