build-protoc.sh 7.7 KB

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