blob: af318611c96a07959dc45c2ee369b7139993172e [file] [log] [blame]
zhangkun83da3c3f82015-03-11 18:03:31 -07001#!/bin/bash
2
3# Check that the codegen artifacts are of correct architecture and don't have
4# unexpected dependencies.
5# To be run from Gradle.
6# Usage: check-artifact <OS> <ARCH>
7# <OS> and <ARCH> are ${os.detected.name} and ${os.detected.arch} from
8# osdetector-gradle-plugin
9OS=$1
10ARCH=$2
11
12if [[ $# < 2 ]]; then
13 echo "No arguments provided. This script is intended to be run from Gradle."
14 exit 1
15fi
16
17# Under Cygwin, bash doesn't have these in PATH when called from Gradle which
18# runs in Windows version of Java.
19export PATH="/bin:/usr/bin:$PATH"
20
21E_PARAM_ERR=98
22E_ASSERT_FAILED=99
23
24# Usage: fail ERROR_MSG
25fail()
26{
27 echo "ERROR: $1"
28 echo
29 exit $E_ASSERT_FAILED
30}
31
32# Usage: assertEq VAL1 VAL2 $LINENO
33assertEq ()
34{
35 lineno=$3
36 if [ -z "$lineno" ]; then
37 echo "lineno not given"
38 exit $E_PARAM_ERR
39 fi
40
41 if [[ "$1" != "$2" ]]; then
42 echo "Assertion failed: \"$1\" == \"$2\""
43 echo "File \"$0\", line $lineno" # Give name of file and line number.
44 exit $E_ASSERT_FAILED
45 fi
46}
47
48# Checks the artifact is for the expected architecture
49# Usage: checkArch <path-to-protoc>
50checkArch ()
51{
52 echo
53 echo "Checking format of $1"
54 if [[ "$OS" == windows || "$OS" == linux ]]; then
55 format="$(objdump -f "$1" | grep -o "file format .*$" | grep -o "[^ ]*$")"
56 echo Format=$format
57 if [[ "$OS" == linux ]]; then
58 if [[ "$ARCH" == x86_32 ]]; then
59 assertEq $format "elf32-i386" $LINENO
60 elif [[ "$ARCH" == x86_64 ]]; then
61 assertEq $format "elf64-x86-64" $LINENO
62 else
63 fail "Unsupported arch: $ARCH"
64 fi
65 else
66 # $OS == windows
67 if [[ "$ARCH" == x86_32 ]]; then
68 assertEq $format "pei-i386" $LINENO
69 elif [[ "$ARCH" == x86_64 ]]; then
70 assertEq $format "pei-x86-64" $LINENO
71 else
72 fail "Unsupported arch: $ARCH"
73 fi
74 fi
75 elif [[ "$OS" == osx ]]; then
76 format="$(file -b "$1" | grep -o "[^ ]*$")"
77 echo Format=$format
78 if [[ "$ARCH" == x86_32 ]]; then
79 assertEq $format "i386" $LINENO
80 elif [[ "$ARCH" == x86_64 ]]; then
81 assertEq $format "x86_64" $LINENO
82 else
83 fail "Unsupported arch: $ARCH"
84 fi
85 else
86 fail "Unsupported system: $OS"
87 fi
88 echo
89}
90
91# Checks the dependencies of the artifact. Artifacts should only depend on
92# system libraries.
93# Usage: checkDependencies <path-to-protoc>
94checkDependencies ()
95{
96 echo "Checking dependencies of $1"
97 if [[ "$OS" == windows ]]; then
98 dump_cmd='objdump -x '"$1"' | fgrep "DLL Name"'
Kun Zhang6da8f472016-11-28 17:45:11 -080099 white_list="KERNEL32\.dll\|msvcrt\.dll\|USER32\.dll"
zhangkun83da3c3f82015-03-11 18:03:31 -0700100 elif [[ "$OS" == linux ]]; then
101 dump_cmd='ldd '"$1"
102 if [[ "$ARCH" == x86_32 ]]; then
103 white_list="linux-gate\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux\.so\.2"
104 elif [[ "$ARCH" == x86_64 ]]; then
105 white_list="linux-vdso\.so\.1\|libpthread\.so\.0\|libm\.so\.6\|libc\.so\.6\|ld-linux-x86-64\.so\.2"
106 fi
107 elif [[ "$OS" == osx ]]; then
108 dump_cmd='otool -L '"$1"' | fgrep dylib'
109 white_list="libz\.1\.dylib\|libc++.1.dylib\|libstdc++\.6\.dylib\|libSystem\.B\.dylib"
110 fi
111 if [[ -z "$white_list" || -z "$dump_cmd" ]]; then
112 fail "Unsupported platform $OS-$ARCH."
113 fi
114 echo "Checking for expected dependencies ..."
115 eval $dump_cmd | grep -i "$white_list" || fail "doesn't show any expected dependencies"
116 echo "Checking for unexpected dependencies ..."
117 eval $dump_cmd | grep -i -v "$white_list"
118 ret=$?
119 if [[ $ret == 0 ]]; then
120 fail "found unexpected dependencies (listed above)."
121 elif [[ $ret != 1 ]]; then
122 fail "Error when checking dependencies."
123 fi # grep returns 1 when "not found", which is what we expect
124 echo "Dependencies look good."
125 echo
126}
127
Eric Andersonbf429132016-01-29 17:01:33 -0800128FILE="build/artifacts/java_plugin/protoc-gen-grpc-java.exe"
zhangkun83da3c3f82015-03-11 18:03:31 -0700129checkArch "$FILE" && checkDependencies "$FILE"