• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -eu
2# Invoked by the Xcode projects to build the protos needed for the unittests.
3
4readonly OUTPUT_DIR="${PROJECT_DERIVED_FILE_DIR}/protos"
5
6# -----------------------------------------------------------------------------
7# Helper for bailing.
8die() {
9  echo "Error: $1"
10  exit 2
11}
12
13# -----------------------------------------------------------------------------
14# What to do.
15case "${ACTION}" in
16  "")
17    # Build, fall thru
18    ;;
19  "clean")
20    rm -rf "${OUTPUT_DIR}"
21    exit 0
22    ;;
23  *)
24    die "Unknown action requested: ${ACTION}"
25    ;;
26esac
27
28# -----------------------------------------------------------------------------
29# The objc unittest specific proto files.
30
31OBJC_TEST_PROTO_FILES=(
32  objectivec/Tests/any_test.proto
33  objectivec/Tests/map_proto2_unittest.proto
34  objectivec/Tests/map_unittest.proto
35  objectivec/Tests/unittest_cycle.proto
36  objectivec/Tests/unittest_deprecated_file.proto
37  objectivec/Tests/unittest_deprecated.proto
38  objectivec/Tests/unittest_extension_chain_a.proto
39  objectivec/Tests/unittest_extension_chain_b.proto
40  objectivec/Tests/unittest_extension_chain_c.proto
41  objectivec/Tests/unittest_extension_chain_d.proto
42  objectivec/Tests/unittest_extension_chain_e.proto
43  objectivec/Tests/unittest_extension_chain_f.proto
44  objectivec/Tests/unittest_extension_chain_g.proto
45  objectivec/Tests/unittest_import_public.proto
46  objectivec/Tests/unittest_import.proto
47  objectivec/Tests/unittest_mset.proto
48  objectivec/Tests/unittest_objc_options.proto
49  objectivec/Tests/unittest_objc_startup.proto
50  objectivec/Tests/unittest_objc.proto
51  objectivec/Tests/unittest_preserve_unknown_enum.proto
52  objectivec/Tests/unittest_runtime_proto2.proto
53  objectivec/Tests/unittest_runtime_proto3.proto
54  objectivec/Tests/unittest.proto
55)
56
57OBJC_EXTENSIONS=( .pbobjc.h .pbobjc.m )
58
59# -----------------------------------------------------------------------------
60# Ensure the output dir exists
61mkdir -p "${OUTPUT_DIR}"
62
63# -----------------------------------------------------------------------------
64# Move to the top of the protobuf directories and ensure there is a protoc
65# binary to use.
66cd "${SRCROOT}/.."
67readonly PROTOC="bazel-bin/protoc"
68[[ -x "${PROTOC}" ]] || \
69  die "Could not find the protoc binary; make sure you have built it (objectivec/DevTools/full_mac_build.sh -h)."
70
71# -----------------------------------------------------------------------------
72RUN_PROTOC=no
73
74# Check to if all the output files exist (in case a new one got added).
75
76for PROTO_FILE in "${OBJC_TEST_PROTO_FILES[@]}"; do
77  DIR=${PROTO_FILE%/*}
78  BASE_NAME=${PROTO_FILE##*/}
79  # Drop the extension
80  BASE_NAME=${BASE_NAME%.*}
81  OBJC_NAME=$(echo "${BASE_NAME}" | awk -F _ '{for(i=1; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i,2);}')
82
83  for EXT in "${OBJC_EXTENSIONS[@]}"; do
84    if [[ ! -f "${OUTPUT_DIR}/${DIR}/${OBJC_NAME}${EXT}" ]]; then
85      RUN_PROTOC=yes
86    fi
87  done
88done
89
90# If we haven't decided to run protoc because of a missing file, check to see if
91# an input has changed.
92if [[ "${RUN_PROTOC}" != "yes" ]] ; then
93  # Find the newest input file (protos, compiler, and this script).
94  # (these patterns catch some extra stuff, but better to over sample than
95  # under)
96  readonly NewestInput=$(find \
97     objectivec/Tests/*.proto "${PROTOC}" \
98     objectivec/DevTools/compile_testing_protos.sh \
99        -type f -print0 \
100        | xargs -0 stat -f "%m %N" \
101        | sort -n | tail -n1 | cut -f2- -d" ")
102  # Find the oldest output file.
103  readonly OldestOutput=$(find \
104        "${OUTPUT_DIR}" \
105        -type f -name "*.pbobjc.[hm]" -print0 \
106        | xargs -0 stat -f "%m %N" \
107        | sort -n -r | tail -n1 | cut -f2- -d" ")
108  # If the newest input is newer than the oldest output, regenerate.
109  if [[ "${NewestInput}" -nt "${OldestOutput}" ]] ; then
110    RUN_PROTOC=yes
111  fi
112fi
113
114if [[ "${RUN_PROTOC}" != "yes" ]] ; then
115  # Up to date.
116  exit 0
117fi
118
119# -----------------------------------------------------------------------------
120# Prune out all the files from previous generations to ensure we only have
121# current ones.
122find "${OUTPUT_DIR}" \
123    -type f -name "*.pbobjc.[hm]" -print0 \
124    | xargs -0 rm -rf
125
126# -----------------------------------------------------------------------------
127# Generate the Objective C specific testing protos.
128
129"${PROTOC}"                                                                 \
130  --objc_out="${OUTPUT_DIR}"                                                \
131  --objc_opt=expected_prefixes_path=objectivec/Tests/expected_prefixes.txt  \
132  --objc_opt=prefixes_must_be_registered=yes                                \
133  --objc_opt=require_prefixes=yes                                           \
134  --proto_path=.                                                            \
135  --proto_path=src                                                          \
136  "${OBJC_TEST_PROTO_FILES[@]}"
137