2015-03-25 23:07:50 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2016-07-26 23:30:35 +00:00
|
|
|
# Builds protoc executable into target/protoc.exe; optionally build protoc
|
|
|
|
# plugins into target/protoc-gen-*.exe
|
2015-04-01 23:23:15 +00:00
|
|
|
# To be run from Maven.
|
2016-07-26 23:35:28 +00:00
|
|
|
# Usage: build-protoc.sh <OS> <ARCH> <TARGET>
|
2015-04-01 23:23:15 +00:00
|
|
|
# <OS> and <ARCH> are ${os.detected.name} and ${os.detected.arch} from os-maven-plugin
|
2016-07-26 23:51:30 +00:00
|
|
|
# <TARGET> can be "protoc" or "protoc-gen-javalite"
|
2017-11-10 01:16:42 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2015-04-01 23:23:15 +00:00
|
|
|
OS=$1
|
|
|
|
ARCH=$2
|
2016-07-26 23:30:35 +00:00
|
|
|
MAKE_TARGET=$3
|
2015-04-01 23:23:15 +00:00
|
|
|
|
2016-07-26 23:30:35 +00:00
|
|
|
if [[ $# < 3 ]]; then
|
2015-04-02 20:14:29 +00:00
|
|
|
echo "No arguments provided. This script is intended to be run from Maven."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2016-07-26 23:30:35 +00:00
|
|
|
case $MAKE_TARGET in
|
|
|
|
protoc-gen-javalite)
|
|
|
|
;;
|
|
|
|
protoc)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Target ""$TARGET"" invalid."
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
|
2015-04-01 23:23:15 +00:00
|
|
|
# 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
|
|
|
|
|
2015-04-02 20:14:29 +00:00
|
|
|
# Usage:
|
2015-04-01 23:23:15 +00:00
|
|
|
fail()
|
|
|
|
{
|
2015-04-08 03:43:20 +00:00
|
|
|
echo "ERROR: $1"
|
|
|
|
echo
|
2015-04-01 23:23:15 +00:00
|
|
|
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
|
|
|
|
}
|
2015-04-02 20:14:29 +00:00
|
|
|
|
|
|
|
# Checks the artifact is for the expected architecture
|
|
|
|
# Usage: checkArch <path-to-protoc>
|
|
|
|
checkArch ()
|
|
|
|
{
|
2015-04-08 03:43:20 +00:00
|
|
|
echo
|
2015-04-08 04:06:37 +00:00
|
|
|
echo "Checking file format ..."
|
2015-04-02 20:14:29 +00:00
|
|
|
if [[ "$OS" == windows || "$OS" == linux ]]; then
|
|
|
|
format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
|
2015-04-08 03:43:20 +00:00
|
|
|
echo Format=$format
|
2015-04-02 20:14:29 +00:00
|
|
|
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
|
2017-11-10 01:16:42 +00:00
|
|
|
elif [[ "$ARCH" == aarch_64 ]]; then
|
|
|
|
assertEq $format "elf64-little" $LINENO
|
2018-03-22 13:25:50 +00:00
|
|
|
elif [[ "$ARCH" == ppcle_64 ]]; then
|
|
|
|
assertEq $format "elf64-powerpcle" $LINENO
|
2015-04-02 20:14:29 +00:00
|
|
|
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 "[^ ]*$")"
|
2015-04-08 03:43:20 +00:00
|
|
|
echo Format=$format
|
2015-04-06 21:31:29 +00:00
|
|
|
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
|
2015-04-02 20:14:29 +00:00
|
|
|
else
|
2015-04-08 03:43:20 +00:00
|
|
|
fail "Unsupported system: $OS"
|
2015-04-02 20:14:29 +00:00
|
|
|
fi
|
2015-04-08 03:43:20 +00:00
|
|
|
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"
|
2018-03-22 13:25:50 +00:00
|
|
|
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"
|
2017-11-10 01:16:42 +00:00
|
|
|
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"
|
2015-04-08 03:43:20 +00:00
|
|
|
fi
|
|
|
|
elif [[ "$OS" == osx ]]; then
|
2015-04-08 04:06:37 +00:00
|
|
|
dump_cmd='otool -L '"$1"' | fgrep dylib'
|
2015-04-08 07:14:36 +00:00
|
|
|
white_list="libz\.1\.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib"
|
2015-04-08 03:43:20 +00:00
|
|
|
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
|
2015-04-02 20:14:29 +00:00
|
|
|
}
|
2015-04-01 23:23:15 +00:00
|
|
|
############################################################################
|
|
|
|
|
2016-07-26 23:30:35 +00:00
|
|
|
echo "Building protoc, OS=$OS ARCH=$ARCH TARGET=$TARGET"
|
2015-04-01 23:23:15 +00:00
|
|
|
|
2015-04-02 20:14:29 +00:00
|
|
|
# Nested double quotes are unintuitive, but it works.
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
|
2015-04-01 01:26:28 +00:00
|
|
|
WORKING_DIR=$(pwd)
|
2015-04-01 23:23:15 +00:00
|
|
|
CONFIGURE_ARGS="--disable-shared"
|
|
|
|
|
2016-07-28 19:07:54 +00:00
|
|
|
TARGET_FILE=target/$MAKE_TARGET.exe
|
2015-04-01 23:23:15 +00:00
|
|
|
if [[ "$OS" == windows ]]; then
|
|
|
|
MAKE_TARGET="${MAKE_TARGET}.exe"
|
|
|
|
fi
|
|
|
|
|
2015-04-02 20:14:29 +00:00
|
|
|
# Override the default value set in configure.ac that has '-g' which produces
|
|
|
|
# huge binary.
|
|
|
|
CXXFLAGS="-DNDEBUG"
|
2015-04-08 03:43:20 +00:00
|
|
|
LDFLAGS=""
|
2015-04-02 20:14:29 +00:00
|
|
|
|
2015-04-01 23:23:15 +00:00
|
|
|
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
|
2015-04-17 00:30:07 +00:00
|
|
|
elif [[ "$(uname)" == MINGW64* ]]; then
|
|
|
|
assertEq "$OS" windows $LINENO
|
|
|
|
assertEq "$ARCH" x86_64 $LINENO
|
2015-04-01 23:23:15 +00:00
|
|
|
elif [[ "$(uname)" == Linux* ]]; then
|
2015-04-08 03:43:20 +00:00
|
|
|
if [[ "$OS" == linux ]]; then
|
|
|
|
if [[ "$ARCH" == x86_64 ]]; then
|
|
|
|
CXXFLAGS="$CXXFLAGS -m64"
|
|
|
|
elif [[ "$ARCH" == x86_32 ]]; then
|
|
|
|
CXXFLAGS="$CXXFLAGS -m32"
|
2017-11-10 01:16:42 +00:00
|
|
|
elif [[ "$ARCH" == aarch_64 ]]; then
|
|
|
|
CONFIGURE_ARGS="$CONFIGURE_ARGS --host=aarch64-linux-gnu"
|
2018-03-22 13:25:50 +00:00
|
|
|
elif [[ "$ARCH" == ppcle_64 ]]; then
|
|
|
|
CXXFLAGS="$CXXFLAGS -m64"
|
2015-04-08 03:43:20 +00:00
|
|
|
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
|
2015-04-01 23:23:15 +00:00
|
|
|
else
|
2015-04-08 03:43:20 +00:00
|
|
|
fail "Cannot build $OS on $(uname)"
|
2015-04-01 23:23:15 +00:00
|
|
|
fi
|
|
|
|
elif [[ "$(uname)" == Darwin* ]]; then
|
|
|
|
assertEq "$OS" osx $LINENO
|
2015-04-08 07:14:36 +00:00
|
|
|
# Make the binary compatible with OSX 10.7 and later
|
|
|
|
CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.7"
|
2015-04-06 21:31:29 +00:00
|
|
|
if [[ "$ARCH" == x86_64 ]]; then
|
|
|
|
CXXFLAGS="$CXXFLAGS -m64"
|
|
|
|
elif [[ "$ARCH" == x86_32 ]]; then
|
|
|
|
CXXFLAGS="$CXXFLAGS -m32"
|
|
|
|
else
|
|
|
|
fail "Unsupported arch: $ARCH"
|
|
|
|
fi
|
2015-04-01 23:23:15 +00:00
|
|
|
else
|
|
|
|
fail "Unsupported system: $(uname)"
|
|
|
|
fi
|
2015-04-01 01:26:28 +00:00
|
|
|
|
2015-03-31 23:28:57 +00:00
|
|
|
# Statically link libgcc and libstdc++.
|
2015-04-02 01:02:36 +00:00
|
|
|
# -s to produce stripped binary.
|
2017-08-15 20:15:37 +00:00
|
|
|
if [[ "$OS" == windows ]]; then
|
2017-08-15 19:27:46 +00:00
|
|
|
# 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.
|
2015-04-08 03:43:20 +00:00
|
|
|
LDFLAGS="$LDFLAGS -static-libgcc -static-libstdc++ -s"
|
2015-04-02 01:02:36 +00:00
|
|
|
fi
|
2015-03-31 21:07:13 +00:00
|
|
|
|
2015-04-08 03:43:20 +00:00
|
|
|
export CXXFLAGS LDFLAGS
|
|
|
|
|
2015-04-01 23:23:15 +00:00
|
|
|
cd "$WORKING_DIR"/.. && ./configure $CONFIGURE_ARGS &&
|
2016-07-28 19:07:54 +00:00
|
|
|
cd src && make clean && make $MAKE_TARGET &&
|
2015-04-01 01:26:28 +00:00
|
|
|
cd "$WORKING_DIR" && mkdir -p target &&
|
2016-07-28 19:07:54 +00:00
|
|
|
cp ../src/$MAKE_TARGET $TARGET_FILE ||
|
2015-04-08 17:39:21 +00:00
|
|
|
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
|