build-protoc.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/bin/bash
  2. # Builds protoc executable into target/<OS>/<ARCH>/protoc.exe; optionally builds
  3. # protoc plugins into target/<OS>/<ARCH>/protoc-gen-*.exe
  4. #
  5. # Usage: ./build-protoc.sh <OS> <ARCH> <TARGET>
  6. #
  7. # <TARGET> can be "protoc" or "protoc-gen-javalite". Supported <OS> <ARCH>
  8. # combinations:
  9. # HOST <OS> <ARCH> <COMMENT>
  10. # cygwin windows x86_32 Requires: i686-w64-mingw32-gcc
  11. # cygwin windows x86_64 Requires: x86_64-w64-mingw32-gcc
  12. # linux linux aarch_64 Requires: g++-aarch64-linux-gnu
  13. # linux linux x86_32
  14. # linux linux x86_64
  15. # linux windows x86_32 Requires: i686-w64-mingw32-gcc
  16. # linux windows x86_64 Requires: x86_64-w64-mingw32-gcc
  17. # macos osx x86_32
  18. # macos osx x86_64
  19. # mingw windows x86_32
  20. # mingw windows x86_64
  21. #
  22. # Before running this script, make sure you have generated the configure script
  23. # in the parent directory (i.e., run ./autogen.sh there).
  24. OS=$1
  25. ARCH=$2
  26. MAKE_TARGET=$3
  27. if [[ $# < 3 ]]; then
  28. echo "Not enough arguments provided."
  29. exit 1
  30. fi
  31. case $MAKE_TARGET in
  32. protoc-gen-javalite)
  33. ;;
  34. protoc)
  35. ;;
  36. *)
  37. echo "Target ""$MAKE_TARGET"" invalid."
  38. exit 1
  39. esac
  40. # Under Cygwin, bash doesn't have these in PATH when called from Maven which
  41. # runs in Windows version of Java.
  42. export PATH="/bin:/usr/bin:$PATH"
  43. ############################################################################
  44. # Helper functions
  45. ############################################################################
  46. E_PARAM_ERR=98
  47. E_ASSERT_FAILED=99
  48. # Usage:
  49. fail()
  50. {
  51. echo "ERROR: $1"
  52. echo
  53. exit $E_ASSERT_FAILED
  54. }
  55. # Usage: assertEq VAL1 VAL2 $LINENO
  56. assertEq ()
  57. {
  58. lineno=$3
  59. if [ -z "$lineno" ]; then
  60. echo "lineno not given"
  61. exit $E_PARAM_ERR
  62. fi
  63. if [[ "$1" != "$2" ]]; then
  64. echo "Assertion failed: \"$1\" == \"$2\""
  65. echo "File \"$0\", line $lineno" # Give name of file and line number.
  66. exit $E_ASSERT_FAILED
  67. fi
  68. }
  69. # Checks the artifact is for the expected architecture
  70. # Usage: checkArch <path-to-protoc>
  71. checkArch ()
  72. {
  73. echo
  74. echo "Checking file format ..."
  75. if [[ "$OS" == windows || "$OS" == linux ]]; then
  76. format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
  77. echo Format=$format
  78. if [[ "$OS" == linux ]]; then
  79. host_machine="$(uname -m)";
  80. if [[ "$ARCH" == x86_32 ]]; then
  81. assertEq $format "elf32-i386" $LINENO
  82. elif [[ "$ARCH" == x86_64 ]]; then
  83. assertEq $format "elf64-x86-64" $LINENO
  84. elif [[ "$ARCH" == aarch_64 ]]; then
  85. assertEq $format "elf64-little" $LINENO
  86. elif [[ "$ARCH" == ppcle_64 ]]; then
  87. if [[ $host_machine == ppc64le ]];then
  88. assertEq $format "elf64-powerpcle" $LINENO
  89. else
  90. assertEq $format "elf64-little" $LINENO
  91. fi
  92. else
  93. fail "Unsupported arch: $ARCH"
  94. fi
  95. else
  96. # $OS == windows
  97. if [[ "$ARCH" == x86_32 ]]; then
  98. assertEq $format "pei-i386" $LINENO
  99. elif [[ "$ARCH" == x86_64 ]]; then
  100. assertEq $format "pei-x86-64" $LINENO
  101. else
  102. fail "Unsupported arch: $ARCH"
  103. fi
  104. fi
  105. elif [[ "$OS" == osx ]]; then
  106. format="$(file -b "$1" | grep -o "[^ ]*$")"
  107. echo Format=$format
  108. if [[ "$ARCH" == x86_32 ]]; then
  109. assertEq $format "i386" $LINENO
  110. elif [[ "$ARCH" == x86_64 ]]; then
  111. assertEq $format "x86_64" $LINENO
  112. else
  113. fail "Unsupported arch: $ARCH"
  114. fi
  115. else
  116. fail "Unsupported system: $OS"
  117. fi
  118. echo
  119. }
  120. # Checks the dependencies of the artifact. Artifacts should only depend on
  121. # system libraries.
  122. # Usage: checkDependencies <path-to-protoc>
  123. checkDependencies ()
  124. {
  125. if [[ "$OS" == windows ]]; then
  126. dump_cmd='objdump -x '"$1"' | fgrep "DLL Name"'
  127. white_list="KERNEL32\.dll\|msvcrt\.dll"
  128. elif [[ "$OS" == linux ]]; then
  129. host_machine="$(uname -m)";
  130. dump_cmd='ldd '"$1"
  131. if [[ "$ARCH" == x86_32 ]]; then
  132. white_list="linux-gate\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux\.so\.2"
  133. elif [[ "$ARCH" == x86_64 ]]; then
  134. white_list="linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2"
  135. elif [[ "$ARCH" == ppcle_64 ]]; then
  136. if [[ $host_machine != ppc64le ]];then
  137. dump_cmd='objdump -p '"$1"' | grep NEEDED'
  138. fi
  139. white_list="linux-vdso64\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|libz\.so\.1\|ld64\.so\.2"
  140. elif [[ "$ARCH" == aarch_64 ]]; then
  141. dump_cmd='objdump -p '"$1"' | grep NEEDED'
  142. white_list="libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-aarch64\.so\.1"
  143. fi
  144. elif [[ "$OS" == osx ]]; then
  145. dump_cmd='otool -L '"$1"' | fgrep dylib'
  146. white_list="libz\.1\.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib"
  147. fi
  148. if [[ -z "$white_list" || -z "$dump_cmd" ]]; then
  149. fail "Unsupported platform $OS-$ARCH."
  150. fi
  151. echo "Checking for expected dependencies ..."
  152. eval $dump_cmd | grep -i "$white_list" || fail "doesn't show any expected dependencies"
  153. echo "Checking for unexpected dependencies ..."
  154. eval $dump_cmd | grep -i -v "$white_list"
  155. ret=$?
  156. if [[ $ret == 0 ]]; then
  157. fail "found unexpected dependencies (listed above)."
  158. elif [[ $ret != 1 ]]; then
  159. fail "Error when checking dependencies."
  160. fi # grep returns 1 when "not found", which is what we expect
  161. echo "Dependencies look good."
  162. echo
  163. }
  164. ############################################################################
  165. echo "Building protoc, OS=$OS ARCH=$ARCH TARGET=$MAKE_TARGET"
  166. CONFIGURE_ARGS="--disable-shared"
  167. if [[ "$OS" == windows ]]; then
  168. MAKE_TARGET="${MAKE_TARGET}.exe"
  169. fi
  170. # Override the default value set in configure.ac that has '-g' which produces
  171. # huge binary.
  172. CXXFLAGS="-DNDEBUG"
  173. LDFLAGS=""
  174. if [[ "$(uname)" == CYGWIN* ]]; then
  175. assertEq "$OS" windows $LINENO
  176. # Use mingw32 compilers because executables produced by Cygwin compiler
  177. # always have dependency on Cygwin DLL.
  178. if [[ "$ARCH" == x86_64 ]]; then
  179. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
  180. elif [[ "$ARCH" == x86_32 ]]; then
  181. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-pc-mingw32"
  182. else
  183. fail "Unsupported arch by CYGWIN: $ARCH"
  184. fi
  185. elif [[ "$(uname)" == MINGW32* ]]; then
  186. assertEq "$OS" windows $LINENO
  187. assertEq "$ARCH" x86_32 $LINENO
  188. elif [[ "$(uname)" == MINGW64* ]]; then
  189. assertEq "$OS" windows $LINENO
  190. assertEq "$ARCH" x86_64 $LINENO
  191. elif [[ "$(uname)" == Linux* ]]; then
  192. if [[ "$OS" == linux ]]; then
  193. if [[ "$ARCH" == x86_64 ]]; then
  194. CXXFLAGS="$CXXFLAGS -m64"
  195. elif [[ "$ARCH" == x86_32 ]]; then
  196. CXXFLAGS="$CXXFLAGS -m32"
  197. elif [[ "$ARCH" == aarch_64 ]]; then
  198. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=aarch64-linux-gnu"
  199. elif [[ "$ARCH" == ppcle_64 ]]; then
  200. CXXFLAGS="$CXXFLAGS -m64"
  201. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=powerpc64le-linux-gnu"
  202. else
  203. fail "Unsupported arch: $ARCH"
  204. fi
  205. elif [[ "$OS" == windows ]]; then
  206. # Cross-compilation for Windows
  207. CONFIGURE_ARGS="$CONFIGURE_ARGS"
  208. if [[ "$ARCH" == x86_64 ]]; then
  209. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
  210. elif [[ "$ARCH" == x86_32 ]]; then
  211. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-w64-mingw32"
  212. else
  213. fail "Unsupported arch: $ARCH"
  214. fi
  215. else
  216. fail "Cannot build $OS on $(uname)"
  217. fi
  218. elif [[ "$(uname)" == Darwin* ]]; then
  219. assertEq "$OS" osx $LINENO
  220. # Make the binary compatible with OSX 10.7 and later
  221. CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.7"
  222. if [[ "$ARCH" == x86_64 ]]; then
  223. CXXFLAGS="$CXXFLAGS -m64"
  224. elif [[ "$ARCH" == x86_32 ]]; then
  225. CXXFLAGS="$CXXFLAGS -m32"
  226. else
  227. fail "Unsupported arch: $ARCH"
  228. fi
  229. else
  230. fail "Unsupported system: $(uname)"
  231. fi
  232. # Statically link libgcc and libstdc++.
  233. # -s to produce stripped binary.
  234. if [[ "$OS" == windows ]]; then
  235. # Also static link libpthread required by mingw64
  236. LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -s"
  237. elif [[ "$OS" != osx ]]; then
  238. # And they don't work under Mac.
  239. LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -s"
  240. fi
  241. export CXXFLAGS LDFLAGS
  242. # Nested double quotes are unintuitive, but it works.
  243. cd "$(dirname "$0")"
  244. WORKING_DIR="$(pwd)"
  245. BUILD_DIR="build/$OS/$ARCH"
  246. TARGET_FILE="target/$OS/$ARCH/$MAKE_TARGET.exe"
  247. mkdir -p "$BUILD_DIR" && cd "$BUILD_DIR" &&
  248. ../../../../configure $CONFIGURE_ARGS &&
  249. cd src && make $MAKE_TARGET -j8 &&
  250. cd "$WORKING_DIR" && mkdir -p $(dirname $TARGET_FILE) &&
  251. cp $BUILD_DIR/src/$MAKE_TARGET $TARGET_FILE ||
  252. exit 1
  253. if [[ "$OS" == osx ]]; then
  254. # Since Mac linker doesn't accept "-s", we need to run strip
  255. strip $TARGET_FILE || exit 1
  256. fi
  257. checkArch $TARGET_FILE && checkDependencies $TARGET_FILE