#!/bin/bash # Builds protoc executable into target/protoc.exe; optionally build protoc # plugins into target/protoc-gen-*.exe # To be run from Maven. # Usage: build-protoc.sh # and are ${os.detected.name} and ${os.detected.arch} from os-maven-plugin # can be "protoc" or "protoc-gen-javalite" setglobal OS = $1 setglobal ARCH = $2 setglobal MAKE_TARGET = $3 if [[ $# < 3 ]] { echo "No arguments provided. This script is intended to be run from Maven." exit 1 } match $MAKE_TARGET { with protoc-gen-javalite with protoc with * echo "Target ""$TARGET"" invalid." exit 1 } # 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 ############################################################################ setglobal E_PARAM_ERR = '98' setglobal E_ASSERT_FAILED = '99' # Usage: proc fail { echo "ERROR: $1" echo exit $E_ASSERT_FAILED } # Usage: assertEq VAL1 VAL2 $LINENO proc assertEq { setglobal lineno = $3 if test -z $lineno { echo "lineno not given" exit $E_PARAM_ERR } if [[ "$1" != "$2" ]] { echo "Assertion failed: \"$1\" == \"$2\"" echo "File \"$0\", line $lineno" # Give name of file and line number. exit $E_ASSERT_FAILED } } # Checks the artifact is for the expected architecture # Usage: checkArch proc checkArch { echo echo "Checking file format ..." if [[ "$OS" == windows || "$OS" == linux ]] { setglobal format = $[objdump -f $1 | grep -o "file format .*$" | grep -o "[^ ]*$] echo Format=$format if [[ "$OS" == linux ]] { if [[ "$ARCH" == x86_32 ]] { assertEq $format "elf32-i386" $LINENO } elif [[ "$ARCH" == x86_64 ]] { assertEq $format "elf64-x86-64" $LINENO } else { fail "Unsupported arch: $ARCH" } } else { # $OS == windows if [[ "$ARCH" == x86_32 ]] { assertEq $format "pei-i386" $LINENO } elif [[ "$ARCH" == x86_64 ]] { assertEq $format "pei-x86-64" $LINENO } else { fail "Unsupported arch: $ARCH" } } } elif [[ "$OS" == osx ]] { setglobal format = $[file -b $1 | grep -o "[^ ]*$] echo Format=$format if [[ "$ARCH" == x86_32 ]] { assertEq $format "i386" $LINENO } elif [[ "$ARCH" == x86_64 ]] { assertEq $format "x86_64" $LINENO } else { fail "Unsupported arch: $ARCH" } } else { fail "Unsupported system: $OS" } echo } # Checks the dependencies of the artifact. Artifacts should only depend on # system libraries. # Usage: checkDependencies proc checkDependencies { if [[ "$OS" == windows ]] { setglobal dump_cmd = "'objdump -x '"$1"' | fgrep "DLL Name""' setglobal white_list = '"KERNEL32\.dll\|msvcrt\.dll'" } elif [[ "$OS" == linux ]] { setglobal dump_cmd = "'ldd '"$1"" if [[ "$ARCH" == x86_32 ]] { setglobal white_list = '"linux-gate\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux\.so\.2'" } elif [[ "$ARCH" == x86_64 ]] { setglobal white_list = '"linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2'" } } elif [[ "$OS" == osx ]] { setglobal dump_cmd = "'otool -L '"$1"' | fgrep dylib"' setglobal white_list = '"libz\.1\.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib'" } if [[ -z "$white_list" || -z "$dump_cmd" ]] { fail "Unsupported platform $OS-$ARCH." } 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 setglobal ret = $Status if [[ $ret == 0 ]] { fail "found unexpected dependencies (listed above)." } elif [[ $ret != 1 ]] { fail "Error when checking dependencies." } # grep returns 1 when "not found", which is what we expect echo "Dependencies look good." echo } ############################################################################ echo "Building protoc, OS=$OS ARCH=$ARCH TARGET=$TARGET" # Nested double quotes are unintuitive, but it works. cd $[dirname $0] setglobal WORKING_DIR = $[pwd] setglobal CONFIGURE_ARGS = '"--disable-shared'" setglobal TARGET_FILE = "target/$MAKE_TARGET.exe" if [[ "$OS" == windows ]] { setglobal MAKE_TARGET = ""$(MAKE_TARGET).exe"" } # Override the default value set in configure.ac that has '-g' which produces # huge binary. setglobal CXXFLAGS = '"-DNDEBUG'" setglobal LDFLAGS = ''"" if [[ "$(uname)" == CYGWIN* ]] { assertEq $OS windows $LINENO # Use mingw32 compilers because executables produced by Cygwin compiler # always have dependency on Cygwin DLL. if [[ "$ARCH" == x86_64 ]] { setglobal CONFIGURE_ARGS = ""$CONFIGURE_ARGS --host=x86_64-w64-mingw32"" } elif [[ "$ARCH" == x86_32 ]] { setglobal CONFIGURE_ARGS = ""$CONFIGURE_ARGS --host=i686-pc-mingw32"" } else { fail "Unsupported arch by CYGWIN: $ARCH" } } elif [[ "$(uname)" == MINGW32* ]] { assertEq $OS windows $LINENO assertEq $ARCH x86_32 $LINENO } elif [[ "$(uname)" == MINGW64* ]] { assertEq $OS windows $LINENO assertEq $ARCH x86_64 $LINENO } elif [[ "$(uname)" == Linux* ]] { if [[ "$OS" == linux ]] { if [[ "$ARCH" == x86_64 ]] { setglobal CXXFLAGS = ""$CXXFLAGS -m64"" } elif [[ "$ARCH" == x86_32 ]] { setglobal CXXFLAGS = ""$CXXFLAGS -m32"" } else { fail "Unsupported arch: $ARCH" } } elif [[ "$OS" == windows ]] { # Cross-compilation for Windows # TODO(zhangkun83) MinGW 64 always adds dependency on libwinpthread-1.dll, # which is undesirable for repository deployment. setglobal CONFIGURE_ARGS = $CONFIGURE_ARGS if [[ "$ARCH" == x86_64 ]] { setglobal CONFIGURE_ARGS = ""$CONFIGURE_ARGS --host=x86_64-w64-mingw32"" } elif [[ "$ARCH" == x86_32 ]] { setglobal CONFIGURE_ARGS = ""$CONFIGURE_ARGS --host=i686-w64-mingw32"" } else { fail "Unsupported arch: $ARCH" } } else { fail "Cannot build $OS on $[uname]" } } elif [[ "$(uname)" == Darwin* ]] { assertEq $OS osx $LINENO # Make the binary compatible with OSX 10.7 and later setglobal CXXFLAGS = ""$CXXFLAGS -mmacosx-version-min=10.7"" if [[ "$ARCH" == x86_64 ]] { setglobal CXXFLAGS = ""$CXXFLAGS -m64"" } elif [[ "$ARCH" == x86_32 ]] { setglobal CXXFLAGS = ""$CXXFLAGS -m32"" } else { fail "Unsupported arch: $ARCH" } } else { fail "Unsupported system: $[uname]" } # Statically link libgcc and libstdc++. # -s to produce stripped binary. # And they don't work under Mac. if [[ "$OS" != osx ]] { setglobal LDFLAGS = ""$LDFLAGS -static-libgcc -static-libstdc++ -s"" } export CXXFLAGS LDFLAGS cd "$WORKING_DIR"/.. && ./configure $CONFIGURE_ARGS && cd src && make clean && make $MAKE_TARGET && cd $WORKING_DIR && mkdir -p target && cp ../src/$MAKE_TARGET $TARGET_FILE || exit 1 if [[ "$OS" == osx ]] { # Since Mac linker doesn't accept "-s", we need to run strip strip $TARGET_FILE || exit 1 } checkArch $TARGET_FILE && checkDependencies $TARGET_FILE