build-protoc.sh 6.5 KB

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