build-protoc.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. exit $E_ASSERT_FAILED
  25. }
  26. # Usage: assertEq VAL1 VAL2 $LINENO
  27. assertEq ()
  28. {
  29. lineno=$3
  30. if [ -z "$lineno" ]; then
  31. echo "lineno not given"
  32. exit $E_PARAM_ERR
  33. fi
  34. if [[ "$1" != "$2" ]]; then
  35. echo "Assertion failed: \"$1\" == \"$2\""
  36. echo "File \"$0\", line $lineno" # Give name of file and line number.
  37. exit $E_ASSERT_FAILED
  38. fi
  39. }
  40. # Checks the artifact is for the expected architecture
  41. # Usage: checkArch <path-to-protoc>
  42. checkArch ()
  43. {
  44. if [[ "$OS" == windows || "$OS" == linux ]]; then
  45. format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
  46. if [[ "$OS" == linux ]]; then
  47. if [[ "$ARCH" == x86_32 ]]; then
  48. assertEq $format "elf32-i386" $LINENO
  49. elif [[ "$ARCH" == x86_64 ]]; then
  50. assertEq $format "elf64-x86-64" $LINENO
  51. else
  52. fail "Unsupported arch: $ARCH"
  53. fi
  54. else
  55. # $OS == windows
  56. if [[ "$ARCH" == x86_32 ]]; then
  57. assertEq $format "pei-i386" $LINENO
  58. elif [[ "$ARCH" == x86_64 ]]; then
  59. assertEq $format "pei-x86-64" $LINENO
  60. else
  61. fail "Unsupported arch: $ARCH"
  62. fi
  63. fi
  64. elif [[ "$OS" == osx ]]; then
  65. format="$(file -b "$1" | grep -o "[^ ]*$")"
  66. assertEq $format "x86_64" $LINENO
  67. else
  68. fail "Unsupported system: $(uname)"
  69. fi
  70. }
  71. ############################################################################
  72. echo "Building protoc, OS=$OS ARCH=$ARCH"
  73. # Nested double quotes are unintuitive, but it works.
  74. cd "$(dirname "$0")"
  75. WORKING_DIR=$(pwd)
  76. CONFIGURE_ARGS="--disable-shared"
  77. MAKE_TARGET="protoc"
  78. if [[ "$OS" == windows ]]; then
  79. MAKE_TARGET="${MAKE_TARGET}.exe"
  80. fi
  81. # Override the default value set in configure.ac that has '-g' which produces
  82. # huge binary.
  83. CXXFLAGS="-DNDEBUG"
  84. if [[ "$(uname)" == CYGWIN* ]]; then
  85. assertEq "$OS" windows $LINENO
  86. # Use mingw32 compilers because executables produced by Cygwin compiler
  87. # always have dependency on Cygwin DLL.
  88. if [[ "$ARCH" == x86_64 ]]; then
  89. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
  90. elif [[ "$ARCH" == x86_32 ]]; then
  91. CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-pc-mingw32"
  92. else
  93. fail "Unsupported arch by CYGWIN: $ARCH"
  94. fi
  95. elif [[ "$(uname)" == MINGW32* ]]; then
  96. assertEq "$OS" windows $LINENO
  97. assertEq "$ARCH" x86_32 $LINENO
  98. elif [[ "$(uname)" == Linux* ]]; then
  99. assertEq "$OS" linux $LINENO
  100. if [[ "$ARCH" == x86_64 ]]; then
  101. CXXFLAGS="$CXXFLAGS -m64"
  102. elif [[ "$ARCH" == x86_32 ]]; then
  103. CXXFLAGS="$CXXFLAGS -m32"
  104. else
  105. fail "Unsupported arch: $ARCH"
  106. fi
  107. elif [[ "$(uname)" == Darwin* ]]; then
  108. assertEq "$OS" osx $LINENO
  109. else
  110. fail "Unsupported system: $(uname)"
  111. fi
  112. export CXXFLAGS
  113. # Statically link libgcc and libstdc++.
  114. # -s to produce stripped binary.
  115. # And they don't work under Mac.
  116. if [[ "$OS" != osx ]]; then
  117. export LDFLAGS="-static-libgcc -static-libstdc++ -s"
  118. fi
  119. TARGET_FILE=target/protoc.exe
  120. cd "$WORKING_DIR"/.. && ./configure $CONFIGURE_ARGS &&
  121. cd src && make clean && make $MAKE_TARGET &&
  122. cd "$WORKING_DIR" && mkdir -p target &&
  123. (cp ../src/protoc $TARGET_FILE || cp ../src/protoc.exe $TARGET_FILE) &&
  124. checkArch $TARGET_FILE