build-protoc.sh 7.4 KB

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