build-protoc.sh 7.7 KB
Newer Older
1 2
#!/bin/bash

3 4
# Builds protoc executable into target/protoc.exe; optionally build protoc
# plugins into target/protoc-gen-*.exe
5
# To be run from Maven.
Jisi Liu's avatar
Jisi Liu committed
6
# Usage: build-protoc.sh <OS> <ARCH> <TARGET>
7
# <OS> and <ARCH> are ${os.detected.name} and ${os.detected.arch} from os-maven-plugin
Jisi Liu's avatar
Jisi Liu committed
8
# <TARGET> can be "protoc" or "protoc-gen-javalite"
9 10 11 12 13 14
#
# The script now supports cross-compiling windows and linux-arm64 in linux-x86
# environment. Required packages:
# - Windows: i686-w64-mingw32-gcc (32bit) and x86_64-w64-mingw32-gcc (64bit)
# - Arm64: g++-aarch64-linux-gnu

15 16
OS=$1
ARCH=$2
17
MAKE_TARGET=$3
18

19
if [[ $# < 3 ]]; then
20 21 22 23
  echo "No arguments provided. This script is intended to be run from Maven."
  exit 1
fi

24 25 26 27 28 29 30 31 32 33
case $MAKE_TARGET in
  protoc-gen-javalite)
    ;;
  protoc)
    ;;
  *)
    echo "Target ""$TARGET"" invalid."
    exit 1
esac

34 35 36 37 38 39 40 41 42 43
# Under Cygwin, bash doesn't have these in PATH when called from Maven which
# runs in Windows version of Java.
export PATH="/bin:/usr/bin:$PATH"

############################################################################
# Helper functions
############################################################################
E_PARAM_ERR=98
E_ASSERT_FAILED=99

44
# Usage:
45 46
fail()
{
47 48
  echo "ERROR: $1"
  echo
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  exit $E_ASSERT_FAILED
}

# Usage: assertEq VAL1 VAL2 $LINENO
assertEq ()
{
  lineno=$3
  if [ -z "$lineno" ]; then
    echo "lineno not given"
    exit $E_PARAM_ERR
  fi

  if [[ "$1" != "$2" ]]; then
    echo "Assertion failed:  \"$1\" == \"$2\""
    echo "File \"$0\", line $lineno"    # Give name of file and line number.
    exit $E_ASSERT_FAILED
  fi
}
67 68 69 70 71

# Checks the artifact is for the expected architecture
# Usage: checkArch <path-to-protoc>
checkArch ()
{
72
  echo
73
  echo "Checking file format ..."
74 75
  if [[ "$OS" == windows || "$OS" == linux ]]; then
    format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
76
    echo Format=$format
77 78 79 80 81
    if [[ "$OS" == linux ]]; then
      if [[ "$ARCH" == x86_32 ]]; then
        assertEq $format "elf32-i386" $LINENO
      elif [[ "$ARCH" == x86_64 ]]; then
        assertEq $format "elf64-x86-64" $LINENO
82 83
      elif [[ "$ARCH" == aarch_64 ]]; then
        assertEq $format "elf64-little" $LINENO
84 85
      elif [[ "$ARCH" == ppcle_64 ]]; then
        assertEq $format "elf64-powerpcle" $LINENO
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      else
        fail "Unsupported arch: $ARCH"
      fi
    else
      # $OS == windows
      if [[ "$ARCH" == x86_32 ]]; then
        assertEq $format "pei-i386" $LINENO
      elif [[ "$ARCH" == x86_64 ]]; then
        assertEq $format "pei-x86-64" $LINENO
      else
        fail "Unsupported arch: $ARCH"
      fi
    fi
  elif [[ "$OS" == osx ]]; then
    format="$(file -b "$1" | grep -o "[^ ]*$")"
101
    echo Format=$format
102 103 104 105 106 107 108
    if [[ "$ARCH" == x86_32 ]]; then
      assertEq $format "i386" $LINENO
    elif [[ "$ARCH" == x86_64 ]]; then
      assertEq $format "x86_64" $LINENO
    else
      fail "Unsupported arch: $ARCH"
    fi
109
  else
110
    fail "Unsupported system: $OS"
111
  fi
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  echo
}

# Checks the dependencies of the artifact. Artifacts should only depend on
# system libraries.
# Usage: checkDependencies <path-to-protoc>
checkDependencies ()
{
  if [[ "$OS" == windows ]]; then
    dump_cmd='objdump -x '"$1"' | fgrep "DLL Name"'
    white_list="KERNEL32\.dll\|msvcrt\.dll"
  elif [[ "$OS" == linux ]]; then
    dump_cmd='ldd '"$1"
    if [[ "$ARCH" == x86_32 ]]; then
      white_list="linux-gate\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux\.so\.2"
    elif [[ "$ARCH" == x86_64 ]]; then
      white_list="linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2"
129 130
    elif [[ "$ARCH" == ppcle_64 ]]; then
      white_list="linux-vdso64\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|libz\.so\.1\|ld64\.so\.2"
131 132 133
    elif [[ "$ARCH" == aarch_64 ]]; then
      dump_cmd='objdump -p '"$1"' | grep NEEDED'
      white_list="libpthread\.so\.0\|libc\.so\.6\|ld-linux-aarch64\.so\.1"
134 135
    fi
  elif [[ "$OS" == osx ]]; then
136
    dump_cmd='otool -L '"$1"' | fgrep dylib'
137
    white_list="libz\.1\.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib"
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  fi
  if [[ -z "$white_list" || -z "$dump_cmd" ]]; then
    fail "Unsupported platform $OS-$ARCH."
  fi
  echo "Checking for expected dependencies ..."
  eval $dump_cmd | grep -i "$white_list" || fail "doesn't show any expected dependencies"
  echo "Checking for unexpected dependencies ..."
  eval $dump_cmd | grep -i -v "$white_list"
  ret=$?
  if [[ $ret == 0 ]]; then
    fail "found unexpected dependencies (listed above)."
  elif [[ $ret != 1 ]]; then
    fail "Error when checking dependencies."
  fi  # grep returns 1 when "not found", which is what we expect
  echo "Dependencies look good."
  echo
154
}
155 156
############################################################################

157
echo "Building protoc, OS=$OS ARCH=$ARCH TARGET=$TARGET"
158

159 160 161
# Nested double quotes are unintuitive, but it works.
cd "$(dirname "$0")"

162
WORKING_DIR=$(pwd)
163 164
CONFIGURE_ARGS="--disable-shared"

165
TARGET_FILE=target/$MAKE_TARGET.exe
166 167 168 169
if [[ "$OS" == windows ]]; then
  MAKE_TARGET="${MAKE_TARGET}.exe"
fi

170 171 172
# Override the default value set in configure.ac that has '-g' which produces
# huge binary.
CXXFLAGS="-DNDEBUG"
173
LDFLAGS=""
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188
if [[ "$(uname)" == CYGWIN* ]]; then
  assertEq "$OS" windows $LINENO
  # Use mingw32 compilers because executables produced by Cygwin compiler
  # always have dependency on Cygwin DLL.
  if [[ "$ARCH" == x86_64 ]]; then
    CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
  elif [[ "$ARCH" == x86_32 ]]; then
    CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-pc-mingw32"
  else
    fail "Unsupported arch by CYGWIN: $ARCH"
  fi
elif [[ "$(uname)" == MINGW32* ]]; then
  assertEq "$OS" windows $LINENO
  assertEq "$ARCH" x86_32 $LINENO
Kun Zhang's avatar
Kun Zhang committed
189 190 191
elif [[ "$(uname)" == MINGW64* ]]; then
  assertEq "$OS" windows $LINENO
  assertEq "$ARCH" x86_64 $LINENO
192
elif [[ "$(uname)" == Linux* ]]; then
193 194 195 196 197
  if [[ "$OS" == linux ]]; then
    if [[ "$ARCH" == x86_64 ]]; then
      CXXFLAGS="$CXXFLAGS -m64"
    elif [[ "$ARCH" == x86_32 ]]; then
      CXXFLAGS="$CXXFLAGS -m32"
198 199
    elif [[ "$ARCH" == aarch_64 ]]; then
      CONFIGURE_ARGS="$CONFIGURE_ARGS --host=aarch64-linux-gnu"
200 201
    elif [[ "$ARCH" == ppcle_64 ]]; then
      CXXFLAGS="$CXXFLAGS -m64"
202 203 204 205 206 207 208 209 210 211 212 213 214
    else
      fail "Unsupported arch: $ARCH"
    fi
  elif [[ "$OS" == windows ]]; then
    # Cross-compilation for Windows
    CONFIGURE_ARGS="$CONFIGURE_ARGS"
    if [[ "$ARCH" == x86_64 ]]; then
      CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-w64-mingw32"
    elif [[ "$ARCH" == x86_32 ]]; then
      CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-w64-mingw32"
    else
      fail "Unsupported arch: $ARCH"
    fi
215
  else
216
    fail "Cannot build $OS on $(uname)"
217 218 219
  fi
elif [[ "$(uname)" == Darwin* ]]; then
  assertEq "$OS" osx $LINENO
220 221
  # Make the binary compatible with OSX 10.7 and later
  CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.7"
222 223 224 225 226 227 228
  if [[ "$ARCH" == x86_64 ]]; then
    CXXFLAGS="$CXXFLAGS -m64"
  elif [[ "$ARCH" == x86_32 ]]; then
    CXXFLAGS="$CXXFLAGS -m32"
  else
    fail "Unsupported arch: $ARCH"
  fi
229 230 231
else
  fail "Unsupported system: $(uname)"
fi
232

233
# Statically link libgcc and libstdc++.
Kun Zhang's avatar
Kun Zhang committed
234
# -s to produce stripped binary.
235
if [[ "$OS" == windows ]]; then
236 237 238 239
  # Also static link libpthread required by mingw64
  LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -s"
elif [[ "$OS" != osx ]]; then
  # And they don't work under Mac.
240
  LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -s"
Kun Zhang's avatar
Kun Zhang committed
241
fi
242

243 244
export CXXFLAGS LDFLAGS

245
cd "$WORKING_DIR"/.. && ./configure $CONFIGURE_ARGS &&
246
  cd src && make clean && make $MAKE_TARGET &&
247
  cd "$WORKING_DIR" && mkdir -p target &&
248
  cp ../src/$MAKE_TARGET $TARGET_FILE ||
Kun Zhang's avatar
Kun Zhang committed
249 250 251 252 253 254 255 256
  exit 1

if [[ "$OS" == osx ]]; then
  # Since Mac linker doesn't accept "-s", we need to run strip
  strip $TARGET_FILE || exit 1
fi

checkArch $TARGET_FILE && checkDependencies $TARGET_FILE