blob: b8d2eb8762ab2de09d9a1500588df90c5d1dc6e7 [file] [log] [blame]
zhangkun83488162d2015-03-25 16:07:50 -07001#!/bin/bash
2
Feng Xiao474fd312018-07-13 12:28:17 -07003# Builds protoc executable into target/<OS>/<ARCH>/protoc.exe; optionally builds
4# protoc plugins into target/<OS>/<ARCH>/protoc-gen-*.exe
Jisi Liub879abc2017-11-09 17:16:42 -08005#
Feng Xiao474fd312018-07-13 12:28:17 -07006# Usage: ./build-protoc.sh <OS> <ARCH> <TARGET>
7#
8# <TARGET> can be "protoc" or "protoc-gen-javalite". Supported <OS> <ARCH>
9# combinations:
10# HOST <OS> <ARCH> <COMMENT>
11# cygwin windows x86_32 Requires: i686-w64-mingw32-gcc
12# cygwin windows x86_64 Requires: x86_64-w64-mingw32-gcc
13# linux linux aarch_64 Requires: g++-aarch64-linux-gnu
14# linux linux x86_32
15# linux linux x86_64
16# linux windows x86_32 Requires: i686-w64-mingw32-gcc
17# linux windows x86_64 Requires: x86_64-w64-mingw32-gcc
18# macos osx x86_32
19# macos osx x86_64
20# mingw windows x86_32
21# mingw windows x86_64
22#
23# Before running this script, make sure you have generated the configure script
24# in the parent directory (i.e., run ./autogen.sh there).
Jisi Liub879abc2017-11-09 17:16:42 -080025
Kun Zhangc8eda8e2015-04-01 16:23:15 -070026OS=$1
27ARCH=$2
Jisi Liub1aac0b2016-07-26 16:30:35 -070028MAKE_TARGET=$3
Kun Zhangc8eda8e2015-04-01 16:23:15 -070029
Jisi Liub1aac0b2016-07-26 16:30:35 -070030if [[ $# < 3 ]]; then
Feng Xiao474fd312018-07-13 12:28:17 -070031 echo "Not enough arguments provided."
Kun Zhangb00a5d72015-04-02 13:14:29 -070032 exit 1
33fi
34
Jisi Liub1aac0b2016-07-26 16:30:35 -070035case $MAKE_TARGET in
36 protoc-gen-javalite)
37 ;;
38 protoc)
39 ;;
40 *)
Feng Xiao9209a412018-07-15 18:16:40 -070041 echo "Target ""$MAKE_TARGET"" invalid."
Jisi Liub1aac0b2016-07-26 16:30:35 -070042 exit 1
43esac
44
Kun Zhangc8eda8e2015-04-01 16:23:15 -070045# Under Cygwin, bash doesn't have these in PATH when called from Maven which
46# runs in Windows version of Java.
47export PATH="/bin:/usr/bin:$PATH"
48
49############################################################################
50# Helper functions
51############################################################################
52E_PARAM_ERR=98
53E_ASSERT_FAILED=99
54
Kun Zhangb00a5d72015-04-02 13:14:29 -070055# Usage:
Kun Zhangc8eda8e2015-04-01 16:23:15 -070056fail()
57{
Kun Zhang6f2bc192015-04-07 20:43:20 -070058 echo "ERROR: $1"
59 echo
Kun Zhangc8eda8e2015-04-01 16:23:15 -070060 exit $E_ASSERT_FAILED
61}
62
63# Usage: assertEq VAL1 VAL2 $LINENO
64assertEq ()
65{
66 lineno=$3
67 if [ -z "$lineno" ]; then
68 echo "lineno not given"
69 exit $E_PARAM_ERR
70 fi
71
72 if [[ "$1" != "$2" ]]; then
73 echo "Assertion failed: \"$1\" == \"$2\""
74 echo "File \"$0\", line $lineno" # Give name of file and line number.
75 exit $E_ASSERT_FAILED
76 fi
77}
Kun Zhangb00a5d72015-04-02 13:14:29 -070078
79# Checks the artifact is for the expected architecture
80# Usage: checkArch <path-to-protoc>
81checkArch ()
82{
Kun Zhang6f2bc192015-04-07 20:43:20 -070083 echo
Kun Zhang5c265fa2015-04-07 21:06:37 -070084 echo "Checking file format ..."
Kun Zhangb00a5d72015-04-02 13:14:29 -070085 if [[ "$OS" == windows || "$OS" == linux ]]; then
86 format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
Kun Zhang6f2bc192015-04-07 20:43:20 -070087 echo Format=$format
Kun Zhangb00a5d72015-04-02 13:14:29 -070088 if [[ "$OS" == linux ]]; then
nashimus1f7837a2018-07-06 20:03:25 -060089 host_machine="$(uname -m)";
Kun Zhangb00a5d72015-04-02 13:14:29 -070090 if [[ "$ARCH" == x86_32 ]]; then
91 assertEq $format "elf32-i386" $LINENO
92 elif [[ "$ARCH" == x86_64 ]]; then
93 assertEq $format "elf64-x86-64" $LINENO
Jisi Liub879abc2017-11-09 17:16:42 -080094 elif [[ "$ARCH" == aarch_64 ]]; then
95 assertEq $format "elf64-little" $LINENO
pravin-dsilva36ba04b2018-03-22 18:55:50 +053096 elif [[ "$ARCH" == ppcle_64 ]]; then
nashimus1f7837a2018-07-06 20:03:25 -060097 if [[ $host_machine == ppc64le ]];then
98 assertEq $format "elf64-powerpcle" $LINENO
99 else
100 assertEq $format "elf64-little" $LINENO
101 fi
Kun Zhangb00a5d72015-04-02 13:14:29 -0700102 else
103 fail "Unsupported arch: $ARCH"
104 fi
105 else
106 # $OS == windows
107 if [[ "$ARCH" == x86_32 ]]; then
108 assertEq $format "pei-i386" $LINENO
109 elif [[ "$ARCH" == x86_64 ]]; then
110 assertEq $format "pei-x86-64" $LINENO
111 else
112 fail "Unsupported arch: $ARCH"
113 fi
114 fi
115 elif [[ "$OS" == osx ]]; then
116 format="$(file -b "$1" | grep -o "[^ ]*$")"
Kun Zhang6f2bc192015-04-07 20:43:20 -0700117 echo Format=$format
Kun Zhangc5a2a7c2015-04-06 14:31:29 -0700118 if [[ "$ARCH" == x86_32 ]]; then
119 assertEq $format "i386" $LINENO
120 elif [[ "$ARCH" == x86_64 ]]; then
121 assertEq $format "x86_64" $LINENO
122 else
123 fail "Unsupported arch: $ARCH"
124 fi
Kun Zhangb00a5d72015-04-02 13:14:29 -0700125 else
Kun Zhang6f2bc192015-04-07 20:43:20 -0700126 fail "Unsupported system: $OS"
Kun Zhangb00a5d72015-04-02 13:14:29 -0700127 fi
Kun Zhang6f2bc192015-04-07 20:43:20 -0700128 echo
129}
130
131# Checks the dependencies of the artifact. Artifacts should only depend on
132# system libraries.
133# Usage: checkDependencies <path-to-protoc>
134checkDependencies ()
135{
136 if [[ "$OS" == windows ]]; then
137 dump_cmd='objdump -x '"$1"' | fgrep "DLL Name"'
138 white_list="KERNEL32\.dll\|msvcrt\.dll"
139 elif [[ "$OS" == linux ]]; then
nashimus1f7837a2018-07-06 20:03:25 -0600140 host_machine="$(uname -m)";
Kun Zhang6f2bc192015-04-07 20:43:20 -0700141 dump_cmd='ldd '"$1"
142 if [[ "$ARCH" == x86_32 ]]; then
143 white_list="linux-gate\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux\.so\.2"
144 elif [[ "$ARCH" == x86_64 ]]; then
145 white_list="linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2"
pravin-dsilva36ba04b2018-03-22 18:55:50 +0530146 elif [[ "$ARCH" == ppcle_64 ]]; then
nashimus1f7837a2018-07-06 20:03:25 -0600147 if [[ $host_machine != ppc64le ]];then
148 dump_cmd='objdump -p '"$1"' | grep NEEDED'
149 fi
pravin-dsilva36ba04b2018-03-22 18:55:50 +0530150 white_list="linux-vdso64\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|libz\.so\.1\|ld64\.so\.2"
Jisi Liub879abc2017-11-09 17:16:42 -0800151 elif [[ "$ARCH" == aarch_64 ]]; then
152 dump_cmd='objdump -p '"$1"' | grep NEEDED'
Adam Cozzette6fc2bac2018-07-06 12:38:14 -0700153 white_list="libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-aarch64\.so\.1"
Kun Zhang6f2bc192015-04-07 20:43:20 -0700154 fi
155 elif [[ "$OS" == osx ]]; then
Kun Zhang5c265fa2015-04-07 21:06:37 -0700156 dump_cmd='otool -L '"$1"' | fgrep dylib'
Kun Zhang62903ec2015-04-08 00:14:36 -0700157 white_list="libz\.1\.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib"
Kun Zhang6f2bc192015-04-07 20:43:20 -0700158 fi
159 if [[ -z "$white_list" || -z "$dump_cmd" ]]; then
160 fail "Unsupported platform $OS-$ARCH."
161 fi
162 echo "Checking for expected dependencies ..."
163 eval $dump_cmd | grep -i "$white_list" || fail "doesn't show any expected dependencies"
164 echo "Checking for unexpected dependencies ..."
165 eval $dump_cmd | grep -i -v "$white_list"
166 ret=$?
167 if [[ $ret == 0 ]]; then
168 fail "found unexpected dependencies (listed above)."
169 elif [[ $ret != 1 ]]; then
170 fail "Error when checking dependencies."
171 fi # grep returns 1 when "not found", which is what we expect
172 echo "Dependencies look good."
173 echo
Kun Zhangb00a5d72015-04-02 13:14:29 -0700174}
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700175############################################################################
176
Feng Xiao9209a412018-07-15 18:16:40 -0700177echo "Building protoc, OS=$OS ARCH=$ARCH TARGET=$MAKE_TARGET"
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700178
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700179CONFIGURE_ARGS="--disable-shared"
180
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700181if [[ "$OS" == windows ]]; then
182 MAKE_TARGET="${MAKE_TARGET}.exe"
183fi
184
Kun Zhangb00a5d72015-04-02 13:14:29 -0700185# Override the default value set in configure.ac that has '-g' which produces
186# huge binary.
187CXXFLAGS="-DNDEBUG"
Kun Zhang6f2bc192015-04-07 20:43:20 -0700188LDFLAGS=""
Kun Zhangb00a5d72015-04-02 13:14:29 -0700189
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700190if [[ "$(uname)" == CYGWIN* ]]; then
191 assertEq "$OS" windows $LINENO
192 # Use mingw32 compilers because executables produced by Cygwin compiler
193 # always have dependency on Cygwin DLL.
194 if [[ "$ARCH" == x86_64 ]]; then
195 CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
196 elif [[ "$ARCH" == x86_32 ]]; then
197 CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-pc-mingw32"
198 else
199 fail "Unsupported arch by CYGWIN: $ARCH"
200 fi
201elif [[ "$(uname)" == MINGW32* ]]; then
202 assertEq "$OS" windows $LINENO
203 assertEq "$ARCH" x86_32 $LINENO
Kun Zhang90a7ed62015-04-16 17:30:07 -0700204elif [[ "$(uname)" == MINGW64* ]]; then
205 assertEq "$OS" windows $LINENO
206 assertEq "$ARCH" x86_64 $LINENO
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700207elif [[ "$(uname)" == Linux* ]]; then
Kun Zhang6f2bc192015-04-07 20:43:20 -0700208 if [[ "$OS" == linux ]]; then
209 if [[ "$ARCH" == x86_64 ]]; then
210 CXXFLAGS="$CXXFLAGS -m64"
211 elif [[ "$ARCH" == x86_32 ]]; then
212 CXXFLAGS="$CXXFLAGS -m32"
Jisi Liub879abc2017-11-09 17:16:42 -0800213 elif [[ "$ARCH" == aarch_64 ]]; then
214 CONFIGURE_ARGS="$CONFIGURE_ARGS --host=aarch64-linux-gnu"
pravin-dsilva36ba04b2018-03-22 18:55:50 +0530215 elif [[ "$ARCH" == ppcle_64 ]]; then
216 CXXFLAGS="$CXXFLAGS -m64"
nashimus1f7837a2018-07-06 20:03:25 -0600217 CONFIGURE_ARGS="$CONFIGURE_ARGS --host=powerpc64le-linux-gnu"
Kun Zhang6f2bc192015-04-07 20:43:20 -0700218 else
219 fail "Unsupported arch: $ARCH"
220 fi
221 elif [[ "$OS" == windows ]]; then
222 # Cross-compilation for Windows
Kun Zhang6f2bc192015-04-07 20:43:20 -0700223 CONFIGURE_ARGS="$CONFIGURE_ARGS"
224 if [[ "$ARCH" == x86_64 ]]; then
225 CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
226 elif [[ "$ARCH" == x86_32 ]]; then
227 CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-w64-mingw32"
228 else
229 fail "Unsupported arch: $ARCH"
230 fi
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700231 else
Kun Zhang6f2bc192015-04-07 20:43:20 -0700232 fail "Cannot build $OS on $(uname)"
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700233 fi
234elif [[ "$(uname)" == Darwin* ]]; then
235 assertEq "$OS" osx $LINENO
Kun Zhang62903ec2015-04-08 00:14:36 -0700236 # Make the binary compatible with OSX 10.7 and later
237 CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.7"
Kun Zhangc5a2a7c2015-04-06 14:31:29 -0700238 if [[ "$ARCH" == x86_64 ]]; then
239 CXXFLAGS="$CXXFLAGS -m64"
240 elif [[ "$ARCH" == x86_32 ]]; then
241 CXXFLAGS="$CXXFLAGS -m32"
242 else
243 fail "Unsupported arch: $ARCH"
244 fi
Kun Zhangc8eda8e2015-04-01 16:23:15 -0700245else
246 fail "Unsupported system: $(uname)"
247fi
Kun Zhangae9177d2015-03-31 18:26:28 -0700248
Kun Zhange4f1f932015-03-31 16:28:57 -0700249# Statically link libgcc and libstdc++.
Kun Zhang87b85012015-04-01 18:02:36 -0700250# -s to produce stripped binary.
Jisi Liud9098342017-08-15 13:15:37 -0700251if [[ "$OS" == windows ]]; then
Jisi Liufa086c82017-08-15 12:27:46 -0700252 # Also static link libpthread required by mingw64
253 LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -s"
254elif [[ "$OS" != osx ]]; then
255 # And they don't work under Mac.
Kun Zhang6f2bc192015-04-07 20:43:20 -0700256 LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -s"
Kun Zhang87b85012015-04-01 18:02:36 -0700257fi
Kun Zhang36093ca2015-03-31 22:07:13 +0100258
Kun Zhang6f2bc192015-04-07 20:43:20 -0700259export CXXFLAGS LDFLAGS
260
Feng Xiao474fd312018-07-13 12:28:17 -0700261# Nested double quotes are unintuitive, but it works.
262cd "$(dirname "$0")"
263
264WORKING_DIR="$(pwd)"
265BUILD_DIR="build/$OS/$ARCH"
266TARGET_FILE="target/$OS/$ARCH/$MAKE_TARGET.exe"
267
268mkdir -p "$BUILD_DIR" && cd "$BUILD_DIR" &&
269 ../../../../configure $CONFIGURE_ARGS &&
270 cd src && make $MAKE_TARGET -j8 &&
271 cd "$WORKING_DIR" && mkdir -p $(dirname $TARGET_FILE) &&
272 cp $BUILD_DIR/src/$MAKE_TARGET $TARGET_FILE ||
Kun Zhang1c126122015-04-08 10:39:21 -0700273 exit 1
274
275if [[ "$OS" == osx ]]; then
276 # Since Mac linker doesn't accept "-s", we need to run strip
277 strip $TARGET_FILE || exit 1
278fi
279
280checkArch $TARGET_FILE && checkDependencies $TARGET_FILE