aboutsummaryrefslogtreecommitdiffhomepage
path: root/protoc-artifacts/build-protoc.sh
diff options
context:
space:
mode:
authorGravatar Kun Zhang <zhangkun@google.com>2015-04-02 13:14:29 -0700
committerGravatar Kun Zhang <zhangkun@google.com>2015-04-02 13:14:29 -0700
commitb00a5d7ee089e03ef50ea114b42447c5ed6359b1 (patch)
treeff34f81909f44cbca6de0d57e7d0097a95a199d8 /protoc-artifacts/build-protoc.sh
parent53df3201a396973d73c413c8f8f123332f130da7 (diff)
Document more about cross-compilation; Post-build check for the actual arch of the artifact
Diffstat (limited to 'protoc-artifacts/build-protoc.sh')
-rwxr-xr-xprotoc-artifacts/build-protoc.sh61
1 files changed, 53 insertions, 8 deletions
diff --git a/protoc-artifacts/build-protoc.sh b/protoc-artifacts/build-protoc.sh
index f02ed01b..50c4b349 100755
--- a/protoc-artifacts/build-protoc.sh
+++ b/protoc-artifacts/build-protoc.sh
@@ -7,6 +7,11 @@
OS=$1
ARCH=$2
+if [[ $# < 2 ]]; then
+ echo "No arguments provided. This script is intended to be run from Maven."
+ exit 1
+fi
+
# 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"
@@ -17,6 +22,7 @@ export PATH="/bin:/usr/bin:$PATH"
E_PARAM_ERR=98
E_ASSERT_FAILED=99
+# Usage:
fail()
{
echo "Error: $1"
@@ -38,11 +44,45 @@ assertEq ()
exit $E_ASSERT_FAILED
fi
}
+
+# Checks the artifact is for the expected architecture
+# Usage: checkArch <path-to-protoc>
+checkArch ()
+{
+ if [[ "$OS" == windows || "$OS" == linux ]]; then
+ format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
+ 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
+ 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 "[^ ]*$")"
+ assertEq $format "x86_64" $LINENO
+ else
+ fail "Unsupported system: $(uname)"
+ fi
+}
############################################################################
echo "Building protoc, OS=$OS ARCH=$ARCH"
-cd "$(dirname \"$0\")"
+# Nested double quotes are unintuitive, but it works.
+cd "$(dirname "$0")"
+
WORKING_DIR=$(pwd)
CONFIGURE_ARGS="--disable-shared"
@@ -51,6 +91,10 @@ if [[ "$OS" == windows ]]; then
MAKE_TARGET="${MAKE_TARGET}.exe"
fi
+# Override the default value set in configure.ac that has '-g' which produces
+# huge binary.
+CXXFLAGS="-DNDEBUG"
+
if [[ "$(uname)" == CYGWIN* ]]; then
assertEq "$OS" windows $LINENO
# Use mingw32 compilers because executables produced by Cygwin compiler
@@ -68,11 +112,11 @@ elif [[ "$(uname)" == MINGW32* ]]; then
elif [[ "$(uname)" == Linux* ]]; then
assertEq "$OS" linux $LINENO
if [[ "$ARCH" == x86_64 ]]; then
- CONFIGURE_ARGS="$CONFIGURE_ARGS --host=x86_64-linux-gnu"
+ CXXFLAGS="$CXXFLAGS -m64"
elif [[ "$ARCH" == x86_32 ]]; then
- CONFIGURE_ARGS="$CONFIGURE_ARGS --host=i686-linux-gnu"
+ CXXFLAGS="$CXXFLAGS -m32"
else
- fail "Unsupported arch by CYGWIN: $ARCH"
+ fail "Unsupported arch: $ARCH"
fi
elif [[ "$(uname)" == Darwin* ]]; then
assertEq "$OS" osx $LINENO
@@ -80,9 +124,7 @@ else
fail "Unsupported system: $(uname)"
fi
-# Override the default value set in configure.ac that has '-g' which produces
-# huge binary.
-export CXXFLAGS="-DNDEBUG"
+export CXXFLAGS
# Statically link libgcc and libstdc++.
# -s to produce stripped binary.
@@ -91,7 +133,10 @@ if [[ "$OS" != osx ]]; then
export LDFLAGS="-static-libgcc -static-libstdc++ -s"
fi
+TARGET_FILE=target/protoc.exe
+
cd "$WORKING_DIR"/.. && ./configure $CONFIGURE_ARGS &&
cd src && make clean && make $MAKE_TARGET &&
cd "$WORKING_DIR" && mkdir -p target &&
- (cp ../src/protoc target/protoc.exe || cp ../src/protoc.exe target/protoc.exe)
+ (cp ../src/protoc $TARGET_FILE || cp ../src/protoc.exe $TARGET_FILE) &&
+ checkArch $TARGET_FILE