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# Ensure the output dir exists
30mkdir -p "${OUTPUT_DIR}/google/protobuf"
31
32# -----------------------------------------------------------------------------
33# Move to the top of the protobuf directories and ensure there is a protoc
34# binary to use.
35cd "${SRCROOT}/.."
36[[ -x src/protoc ]] || \
37  die "Could not find the protoc binary; make sure you have built it (objectivec/DevTools/full_mac_build.sh -h)."
38
39# -----------------------------------------------------------------------------
40# See the compiler or proto files have changed.
41RUN_PROTOC=no
42if [[ ! -d "${OUTPUT_DIR}" ]] ; then
43  RUN_PROTOC=yes
44else
45  # Find the newest input file (protos, compiler, and this script).
46  # (these patterns catch some extra stuff, but better to over sample than
47  # under)
48  readonly NewestInput=$(find \
49     src/google/protobuf/*.proto \
50     objectivec/Tests/*.proto \
51     src/.libs src/*.la src/protoc \
52     objectivec/DevTools/compile_testing_protos.sh \
53        -type f -print0 \
54        | xargs -0 stat -f "%m %N" \
55        | sort -n | tail -n1 | cut -f2- -d" ")
56  # Find the oldest output file.
57  readonly OldestOutput=$(find \
58        "${OUTPUT_DIR}" \
59        -type f -name "*pbobjc.[hm]" -print0 \
60        | xargs -0 stat -f "%m %N" \
61        | sort -n -r | tail -n1 | cut -f2- -d" ")
62  # If the newest input is newer than the oldest output, regenerate.
63  if [[ "${NewestInput}" -nt "${OldestOutput}" ]] ; then
64    RUN_PROTOC=yes
65  fi
66fi
67
68if [[ "${RUN_PROTOC}" != "yes" ]] ; then
69  # Up to date.
70  exit 0
71fi
72
73# -----------------------------------------------------------------------------
74# Prune out all the files from previous generations to ensure we only have
75# current ones.
76find "${OUTPUT_DIR}" \
77    -type f -name "*pbobjc.[hm]" -print0 \
78    | xargs -0 rm -rf
79
80# -----------------------------------------------------------------------------
81# Helper to invoke protoc
82compile_protos() {
83  src/protoc                                   \
84    --objc_out="${OUTPUT_DIR}/google/protobuf" \
85    --proto_path=src/google/protobuf/          \
86    --proto_path=src                           \
87    "$@"
88}
89
90# -----------------------------------------------------------------------------
91# Generate most of the proto files that exist in the C++ src tree.  Several
92# are used in the tests, but the extra don't hurt in that they ensure ObjC
93# sources can be generated from them.
94
95CORE_PROTO_FILES=(
96  src/google/protobuf/any_test.proto
97  src/google/protobuf/unittest_arena.proto
98  src/google/protobuf/unittest_custom_options.proto
99  src/google/protobuf/unittest_enormous_descriptor.proto
100  src/google/protobuf/unittest_embed_optimize_for.proto
101  src/google/protobuf/unittest_empty.proto
102  src/google/protobuf/unittest_import.proto
103  src/google/protobuf/unittest_import_lite.proto
104  src/google/protobuf/unittest_lite.proto
105  src/google/protobuf/unittest_mset.proto
106  src/google/protobuf/unittest_mset_wire_format.proto
107  src/google/protobuf/unittest_no_arena.proto
108  src/google/protobuf/unittest_no_arena_import.proto
109  src/google/protobuf/unittest_no_generic_services.proto
110  src/google/protobuf/unittest_optimize_for.proto
111  src/google/protobuf/unittest.proto
112  src/google/protobuf/unittest_import_public.proto
113  src/google/protobuf/unittest_import_public_lite.proto
114  src/google/protobuf/unittest_drop_unknown_fields.proto
115  src/google/protobuf/unittest_preserve_unknown_enum.proto
116  src/google/protobuf/map_lite_unittest.proto
117  src/google/protobuf/map_proto2_unittest.proto
118  src/google/protobuf/map_unittest.proto
119  # The unittest_custom_options.proto extends the messages in descriptor.proto
120  # so we build it in to test extending in general. The library doesn't provide
121  # a descriptor as it doesn't use the classes/enums.
122  src/google/protobuf/descriptor.proto
123)
124
125# Note: there is overlap in package.Message names between some of the test
126# files, so they can't be generated all at once. This works because the overlap
127# isn't linked into a single binary.
128for a_proto in "${CORE_PROTO_FILES[@]}" ; do
129  compile_protos "${a_proto}"
130done
131
132# -----------------------------------------------------------------------------
133# Generate the Objective C specific testing protos.
134compile_protos \
135  --proto_path="objectivec/Tests" \
136  objectivec/Tests/unittest_cycle.proto \
137  objectivec/Tests/unittest_deprecated.proto \
138  objectivec/Tests/unittest_deprecated_file.proto \
139  objectivec/Tests/unittest_extension_chain_a.proto \
140  objectivec/Tests/unittest_extension_chain_b.proto \
141  objectivec/Tests/unittest_extension_chain_c.proto \
142  objectivec/Tests/unittest_extension_chain_d.proto \
143  objectivec/Tests/unittest_extension_chain_e.proto \
144  objectivec/Tests/unittest_extension_chain_f.proto \
145  objectivec/Tests/unittest_extension_chain_g.proto \
146  objectivec/Tests/unittest_runtime_proto2.proto \
147  objectivec/Tests/unittest_runtime_proto3.proto \
148  objectivec/Tests/unittest_objc.proto \
149  objectivec/Tests/unittest_objc_startup.proto \
150  objectivec/Tests/unittest_objc_options.proto
151