build-protoc.sh 6.8 KB

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