build-protoc.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. if [[ "$ARCH" == x86_32 ]]; then
  80. assertEq $format "elf32-i386" $LINENO
  81. elif [[ "$ARCH" == x86_64 ]]; then
  82. assertEq $format "elf64-x86-64" $LINENO
  83. elif [[ "$ARCH" == aarch_64 ]]; then
  84. assertEq $format "elf64-little" $LINENO
  85. elif [[ "$ARCH" == ppcle_64 ]]; then
  86. assertEq $format "elf64-powerpcle" $LINENO
  87. else
  88. fail "Unsupported arch: $ARCH"
  89. fi
  90. else
  91. # $OS == windows
  92. if [[ "$ARCH" == x86_32 ]]; then
  93. assertEq $format "pei-i386" $LINENO
  94. elif [[ "$ARCH" == x86_64 ]]; then
  95. assertEq $format "pei-x86-64" $LINENO
  96. else
  97. fail "Unsupported arch: $ARCH"
  98. fi
  99. fi
  100. elif [[ "$OS" == osx ]]; then
  101. format="$(file -b "$1" | grep -o "[^ ]*$")"
  102. echo Format=$format
  103. if [[ "$ARCH" == x86_32 ]]; then
  104. assertEq $format "i386" $LINENO
  105. elif [[ "$ARCH" == x86_64 ]]; then
  106. assertEq $format "x86_64" $LINENO
  107. else
  108. fail "Unsupported arch: $ARCH"
  109. fi
  110. else
  111. fail "Unsupported system: $OS"
  112. fi
  113. echo
  114. }
  115. # Checks the dependencies of the artifact. Artifacts should only depend on
  116. # system libraries.
  117. # Usage: checkDependencies <path-to-protoc>
  118. checkDependencies ()
  119. {
  120. if [[ "$OS" == windows ]]; then
  121. dump_cmd='objdump -x '"$1"' | fgrep "DLL Name"'
  122. white_list="KERNEL32\.dll\|msvcrt\.dll"
  123. elif [[ "$OS" == linux ]]; then
  124. dump_cmd='ldd '"$1"
  125. if [[ "$ARCH" == x86_32 ]]; then
  126. white_list="linux-gate\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux\.so\.2"
  127. elif [[ "$ARCH" == x86_64 ]]; then
  128. white_list="linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2"
  129. elif [[ "$ARCH" == ppcle_64 ]]; then
  130. white_list="linux-vdso64\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|libz\.so\.1\|ld64\.so\.2"
  131. elif [[ "$ARCH" == aarch_64 ]]; then
  132. dump_cmd='objdump -p '"$1"' | grep NEEDED'
  133. white_list="libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-aarch64\.so\.1"
  134. fi
  135. elif [[ "$OS" == osx ]]; then
  136. dump_cmd='otool -L '"$1"' | fgrep dylib'
  137. white_list="libz\.1\.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib"
  138. fi
  139. if [[ -z "$white_list" || -z "$dump_cmd" ]]; then
  140. fail "Unsupported platform $OS-$ARCH."
  141. fi
  142. echo "Checking for expected dependencies ..."
  143. eval $dump_cmd | grep -i "$white_list" || fail "doesn't show any expected dependencies"
  144. echo "Checking for unexpected dependencies ..."
  145. eval $dump_cmd | grep -i -v "$white_list"
  146. ret=$?
  147. if [[ $ret == 0 ]]; then
  148. fail "found unexpected dependencies (listed above)."
  149. elif [[ $ret != 1 ]]; then
  150. fail "Error when checking dependencies."
  151. fi # grep returns 1 when "not found", which is what we expect
  152. echo "Dependencies look good."
  153. echo
  154. }
  155. ############################################################################
  156. echo "Building protoc, OS=$OS ARCH=$ARCH TARGET=$MAKE_TARGET"
  157. CONFIGURE_ARGS="--disable-shared"
  158. if [[ "$OS" == windows ]]; then
  159. MAKE_TARGET="${MAKE_TARGET}.exe"
  160. fi
  161. # Override the default value set in configure.ac that has '-g' which produces
  162. # huge binary.
  163. CXXFLAGS="-DNDEBUG"
  164. LDFLAGS=""
  165. if [[ "$(uname)" == CYGWIN* ]]; then
  166. assertEq "$OS" windows $LINENO
  167. # Use mingw32 compilers because executables produced by Cygwin compiler
  168. # always have dependency on Cygwin DLL.
  169. if [[ "$ARCH" == x86_64 ]]; then
  170. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
  171. elif [[ "$ARCH" == x86_32 ]]; then
  172. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-pc-mingw32"
  173. else
  174. fail "Unsupported arch by CYGWIN: $ARCH"
  175. fi
  176. elif [[ "$(uname)" == MINGW32* ]]; then
  177. assertEq "$OS" windows $LINENO
  178. assertEq "$ARCH" x86_32 $LINENO
  179. elif [[ "$(uname)" == MINGW64* ]]; then
  180. assertEq "$OS" windows $LINENO
  181. assertEq "$ARCH" x86_64 $LINENO
  182. elif [[ "$(uname)" == Linux* ]]; then
  183. if [[ "$OS" == linux ]]; then
  184. if [[ "$ARCH" == x86_64 ]]; then
  185. CXXFLAGS="$CXXFLAGS -m64"
  186. elif [[ "$ARCH" == x86_32 ]]; then
  187. CXXFLAGS="$CXXFLAGS -m32"
  188. elif [[ "$ARCH" == aarch_64 ]]; then
  189. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=aarch64-linux-gnu"
  190. elif [[ "$ARCH" == ppcle_64 ]]; then
  191. CXXFLAGS="$CXXFLAGS -m64"
  192. else
  193. fail "Unsupported arch: $ARCH"
  194. fi
  195. elif [[ "$OS" == windows ]]; then
  196. # Cross-compilation for Windows
  197. CONFIGURE_ARGS="$CONFIGURE_ARGS"
  198. if [[ "$ARCH" == x86_64 ]]; then
  199. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
  200. elif [[ "$ARCH" == x86_32 ]]; then
  201. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-w64-mingw32"
  202. else
  203. fail "Unsupported arch: $ARCH"
  204. fi
  205. else
  206. fail "Cannot build $OS on $(uname)"
  207. fi
  208. elif [[ "$(uname)" == Darwin* ]]; then
  209. assertEq "$OS" osx $LINENO
  210. # Make the binary compatible with OSX 10.7 and later
  211. CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.7"
  212. if [[ "$ARCH" == x86_64 ]]; then
  213. CXXFLAGS="$CXXFLAGS -m64"
  214. elif [[ "$ARCH" == x86_32 ]]; then
  215. CXXFLAGS="$CXXFLAGS -m32"
  216. else
  217. fail "Unsupported arch: $ARCH"
  218. fi
  219. else
  220. fail "Unsupported system: $(uname)"
  221. fi
  222. # Statically link libgcc and libstdc++.
  223. # -s to produce stripped binary.
  224. if [[ "$OS" == windows ]]; then
  225. # Also static link libpthread required by mingw64
  226. LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -s"
  227. elif [[ "$OS" != osx ]]; then
  228. # And they don't work under Mac.
  229. LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -s"
  230. fi
  231. export CXXFLAGS LDFLAGS
  232. # Nested double quotes are unintuitive, but it works.
  233. cd "$(dirname "$0")"
  234. WORKING_DIR="$(pwd)"
  235. BUILD_DIR="build/$OS/$ARCH"
  236. TARGET_FILE="target/$OS/$ARCH/$MAKE_TARGET.exe"
  237. mkdir -p "$BUILD_DIR" && cd "$BUILD_DIR" &&
  238. ../../../../configure $CONFIGURE_ARGS &&
  239. cd src && make $MAKE_TARGET -j8 &&
  240. cd "$WORKING_DIR" && mkdir -p $(dirname $TARGET_FILE) &&
  241. cp $BUILD_DIR/src/$MAKE_TARGET $TARGET_FILE ||
  242. exit 1
  243. if [[ "$OS" == osx ]]; then
  244. # Since Mac linker doesn't accept "-s", we need to run strip
  245. strip $TARGET_FILE || exit 1
  246. fi
  247. checkArch $TARGET_FILE && checkDependencies $TARGET_FILE