xref: /aosp_15_r20/external/grpc-grpc-java/buildscripts/kokoro/unix.sh (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Worker#!/bin/bash
2*e07d83d3SAndroid Build Coastguard Worker
3*e07d83d3SAndroid Build Coastguard Worker# This file is used for both Linux and MacOS builds.
4*e07d83d3SAndroid Build Coastguard Worker# For Linux, this script is called inside a docker container with
5*e07d83d3SAndroid Build Coastguard Worker# the correct environment for releases.
6*e07d83d3SAndroid Build Coastguard Worker# To run locally:
7*e07d83d3SAndroid Build Coastguard Worker#  ./buildscripts/kokoro/unix.sh
8*e07d83d3SAndroid Build Coastguard Worker# For x86 32 arch:
9*e07d83d3SAndroid Build Coastguard Worker#  ARCH=x86_32 ./buildscripts/kokoro/unix.sh
10*e07d83d3SAndroid Build Coastguard Worker# For aarch64 arch:
11*e07d83d3SAndroid Build Coastguard Worker#  ARCH=aarch_64 ./buildscripts/kokoro/unix.sh
12*e07d83d3SAndroid Build Coastguard Worker# For ppc64le arch:
13*e07d83d3SAndroid Build Coastguard Worker#  ARCH=ppcle_64 ./buildscripts/kokoro/unix.sh
14*e07d83d3SAndroid Build Coastguard Worker# For s390x arch:
15*e07d83d3SAndroid Build Coastguard Worker#  ARCH=s390_64 ./buildscripts/kokoro/unix.sh
16*e07d83d3SAndroid Build Coastguard Worker
17*e07d83d3SAndroid Build Coastguard Worker# This script assumes `set -e`. Removing it may lead to undefined behavior.
18*e07d83d3SAndroid Build Coastguard Workerset -exu -o pipefail
19*e07d83d3SAndroid Build Coastguard Worker
20*e07d83d3SAndroid Build Coastguard Worker# It would be nicer to use 'readlink -f' here but osx does not support it.
21*e07d83d3SAndroid Build Coastguard Workerreadonly GRPC_JAVA_DIR="$(cd "$(dirname "$0")"/../.. && pwd)"
22*e07d83d3SAndroid Build Coastguard Worker
23*e07d83d3SAndroid Build Coastguard Worker# cd to the root dir of grpc-java
24*e07d83d3SAndroid Build Coastguard Workercd $(dirname $0)/../..
25*e07d83d3SAndroid Build Coastguard Worker
26*e07d83d3SAndroid Build Coastguard Worker# TODO(zpencer): always make sure we are using Oracle jdk8
27*e07d83d3SAndroid Build Coastguard Workerif [[ -f /usr/libexec/java_home ]]; then
28*e07d83d3SAndroid Build Coastguard Worker    JAVA_HOME=$(/usr/libexec/java_home -v"1.8.0")
29*e07d83d3SAndroid Build Coastguard Workerfi
30*e07d83d3SAndroid Build Coastguard Worker
31*e07d83d3SAndroid Build Coastguard Worker# ARCH is x86_64 unless otherwise specified.
32*e07d83d3SAndroid Build Coastguard WorkerARCH="${ARCH:-x86_64}"
33*e07d83d3SAndroid Build Coastguard Worker
34*e07d83d3SAndroid Build Coastguard Workercat <<EOF >> gradle.properties
35*e07d83d3SAndroid Build Coastguard Worker# defaults to -Xmx512m -XX:MaxMetaspaceSize=256m
36*e07d83d3SAndroid Build Coastguard Worker# https://docs.gradle.org/current/userguide/build_environment.html#sec:configuring_jvm_memory
37*e07d83d3SAndroid Build Coastguard Worker# Increased due to java.lang.OutOfMemoryError: Metaspace failures, "JVM heap
38*e07d83d3SAndroid Build Coastguard Worker# space is exhausted", and to increase build speed
39*e07d83d3SAndroid Build Coastguard Workerorg.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=1024m
40*e07d83d3SAndroid Build Coastguard WorkerEOF
41*e07d83d3SAndroid Build Coastguard Worker
42*e07d83d3SAndroid Build Coastguard WorkerARCH="$ARCH" buildscripts/make_dependencies.sh
43*e07d83d3SAndroid Build Coastguard Worker
44*e07d83d3SAndroid Build Coastguard Worker# Set properties via flags, do not pollute gradle.properties
45*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS="${GRADLE_FLAGS:-}"
46*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS+=" -PtargetArch=$ARCH"
47*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS+=" -Pcheckstyle.ignoreFailures=false"
48*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS+=" -PfailOnWarnings=true"
49*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS+=" -PerrorProne=true"
50*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS+=" -PskipAndroid=true"
51*e07d83d3SAndroid Build Coastguard WorkerGRADLE_FLAGS+=" -Dorg.gradle.parallel=true"
52*e07d83d3SAndroid Build Coastguard Workerexport GRADLE_OPTS="-Dorg.gradle.jvmargs='-Xmx1g'"
53*e07d83d3SAndroid Build Coastguard Worker
54*e07d83d3SAndroid Build Coastguard Worker# Make protobuf discoverable by :grpc-compiler
55*e07d83d3SAndroid Build Coastguard Workerexport LD_LIBRARY_PATH=/tmp/protobuf/lib
56*e07d83d3SAndroid Build Coastguard Workerexport LDFLAGS=-L/tmp/protobuf/lib
57*e07d83d3SAndroid Build Coastguard Workerexport CXXFLAGS="-I/tmp/protobuf/include"
58*e07d83d3SAndroid Build Coastguard Worker
59*e07d83d3SAndroid Build Coastguard Worker./gradlew grpc-compiler:clean $GRADLE_FLAGS
60*e07d83d3SAndroid Build Coastguard Worker
61*e07d83d3SAndroid Build Coastguard Workerif [[ -z "${SKIP_TESTS:-}" ]]; then
62*e07d83d3SAndroid Build Coastguard Worker  # Ensure all *.proto changes include *.java generated code
63*e07d83d3SAndroid Build Coastguard Worker  ./gradlew assemble generateTestProto publishToMavenLocal $GRADLE_FLAGS
64*e07d83d3SAndroid Build Coastguard Worker
65*e07d83d3SAndroid Build Coastguard Worker  if [[ -z "${SKIP_CLEAN_CHECK:-}" && ! -z $(git status --porcelain) ]]; then
66*e07d83d3SAndroid Build Coastguard Worker    git status
67*e07d83d3SAndroid Build Coastguard Worker    echo "Error Working directory is not clean. Forget to commit generated files?"
68*e07d83d3SAndroid Build Coastguard Worker    exit 1
69*e07d83d3SAndroid Build Coastguard Worker  fi
70*e07d83d3SAndroid Build Coastguard Worker  # Run tests
71*e07d83d3SAndroid Build Coastguard Worker  ./gradlew build :grpc-all:jacocoTestReport $GRADLE_FLAGS
72*e07d83d3SAndroid Build Coastguard Worker  pushd examples
73*e07d83d3SAndroid Build Coastguard Worker  ./gradlew build $GRADLE_FLAGS
74*e07d83d3SAndroid Build Coastguard Worker  # --batch-mode reduces log spam
75*e07d83d3SAndroid Build Coastguard Worker  mvn verify --batch-mode
76*e07d83d3SAndroid Build Coastguard Worker  popd
77*e07d83d3SAndroid Build Coastguard Worker  for f in examples/example-*
78*e07d83d3SAndroid Build Coastguard Worker  do
79*e07d83d3SAndroid Build Coastguard Worker     pushd "$f"
80*e07d83d3SAndroid Build Coastguard Worker     ../gradlew build $GRADLE_FLAGS
81*e07d83d3SAndroid Build Coastguard Worker     if [ -f "pom.xml" ]; then
82*e07d83d3SAndroid Build Coastguard Worker       # --batch-mode reduces log spam
83*e07d83d3SAndroid Build Coastguard Worker       mvn verify --batch-mode
84*e07d83d3SAndroid Build Coastguard Worker     fi
85*e07d83d3SAndroid Build Coastguard Worker     popd
86*e07d83d3SAndroid Build Coastguard Worker  done
87*e07d83d3SAndroid Build Coastguard Worker  # TODO(zpencer): also build the GAE examples
88*e07d83d3SAndroid Build Coastguard Workerfi
89*e07d83d3SAndroid Build Coastguard Worker
90*e07d83d3SAndroid Build Coastguard WorkerLOCAL_MVN_TEMP=$(mktemp -d)
91*e07d83d3SAndroid Build Coastguard Worker# Note that this disables parallel=true from GRADLE_FLAGS
92*e07d83d3SAndroid Build Coastguard Workerif [[ -z "${ALL_ARTIFACTS:-}" ]]; then
93*e07d83d3SAndroid Build Coastguard Worker  if [[ "$ARCH" = "aarch_64" || "$ARCH" = "ppcle_64" || "$ARCH" = "s390_64" ]]; then
94*e07d83d3SAndroid Build Coastguard Worker    GRADLE_FLAGS+=" -x grpc-compiler:generateTestProto -x grpc-compiler:generateTestLiteProto"
95*e07d83d3SAndroid Build Coastguard Worker    GRADLE_FLAGS+=" -x grpc-compiler:testGolden -x grpc-compiler:testLiteGolden"
96*e07d83d3SAndroid Build Coastguard Worker    GRADLE_FLAGS+=" -x grpc-compiler:testDeprecatedGolden -x grpc-compiler:testDeprecatedLiteGolden"
97*e07d83d3SAndroid Build Coastguard Worker  fi
98*e07d83d3SAndroid Build Coastguard Worker  ./gradlew grpc-compiler:build grpc-compiler:publish $GRADLE_FLAGS \
99*e07d83d3SAndroid Build Coastguard Worker    -Dorg.gradle.parallel=false -PrepositoryDir=$LOCAL_MVN_TEMP
100*e07d83d3SAndroid Build Coastguard Workerelse
101*e07d83d3SAndroid Build Coastguard Worker  ./gradlew publish :grpc-core:versionFile $GRADLE_FLAGS \
102*e07d83d3SAndroid Build Coastguard Worker    -Dorg.gradle.parallel=false -PrepositoryDir=$LOCAL_MVN_TEMP
103*e07d83d3SAndroid Build Coastguard Worker  pushd examples/example-hostname
104*e07d83d3SAndroid Build Coastguard Worker  ../gradlew jibBuildTar $GRADLE_FLAGS
105*e07d83d3SAndroid Build Coastguard Worker  popd
106*e07d83d3SAndroid Build Coastguard Worker
107*e07d83d3SAndroid Build Coastguard Worker  readonly OTHER_ARTIFACT_DIR="${OTHER_ARTIFACT_DIR:-$GRPC_JAVA_DIR/artifacts}"
108*e07d83d3SAndroid Build Coastguard Worker  mkdir -p "$OTHER_ARTIFACT_DIR"
109*e07d83d3SAndroid Build Coastguard Worker  cp core/build/version "$OTHER_ARTIFACT_DIR"/
110*e07d83d3SAndroid Build Coastguard Worker  cp examples/example-hostname/build/example-hostname.* "$OTHER_ARTIFACT_DIR"/
111*e07d83d3SAndroid Build Coastguard Workerfi
112*e07d83d3SAndroid Build Coastguard Worker
113*e07d83d3SAndroid Build Coastguard Workerreadonly MVN_ARTIFACT_DIR="${MVN_ARTIFACT_DIR:-$GRPC_JAVA_DIR/mvn-artifacts}"
114*e07d83d3SAndroid Build Coastguard Worker
115*e07d83d3SAndroid Build Coastguard Workermkdir -p "$MVN_ARTIFACT_DIR"
116*e07d83d3SAndroid Build Coastguard Workercp -r "$LOCAL_MVN_TEMP"/* "$MVN_ARTIFACT_DIR"/
117