1ffe3c632Sopenharmony_ci#!/bin/bash -eu
2ffe3c632Sopenharmony_ci# Invoked by the Xcode projects to build the protos needed for the unittests.
3ffe3c632Sopenharmony_ci
4ffe3c632Sopenharmony_cireadonly OUTPUT_DIR="${PROJECT_DERIVED_FILE_DIR}/protos"
5ffe3c632Sopenharmony_ci
6ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
7ffe3c632Sopenharmony_ci# Helper for bailing.
8ffe3c632Sopenharmony_cidie() {
9ffe3c632Sopenharmony_ci  echo "Error: $1"
10ffe3c632Sopenharmony_ci  exit 2
11ffe3c632Sopenharmony_ci}
12ffe3c632Sopenharmony_ci
13ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
14ffe3c632Sopenharmony_ci# What to do.
15ffe3c632Sopenharmony_cicase "${ACTION}" in
16ffe3c632Sopenharmony_ci  "")
17ffe3c632Sopenharmony_ci    # Build, fall thru
18ffe3c632Sopenharmony_ci    ;;
19ffe3c632Sopenharmony_ci  "clean")
20ffe3c632Sopenharmony_ci    rm -rf "${OUTPUT_DIR}"
21ffe3c632Sopenharmony_ci    exit 0
22ffe3c632Sopenharmony_ci    ;;
23ffe3c632Sopenharmony_ci  *)
24ffe3c632Sopenharmony_ci    die "Unknown action requested: ${ACTION}"
25ffe3c632Sopenharmony_ci    ;;
26ffe3c632Sopenharmony_ciesac
27ffe3c632Sopenharmony_ci
28ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
29ffe3c632Sopenharmony_ci# Reusing a bunch of the protos from the protocolbuffers/protobuf tree, this
30ffe3c632Sopenharmony_ci# can include some extras as there is no harm in ensuring work for C++
31ffe3c632Sopenharmony_ci# generation.
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciCORE_PROTO_FILES=(
34ffe3c632Sopenharmony_ci  src/google/protobuf/any_test.proto
35ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_arena.proto
36ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_custom_options.proto
37ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_enormous_descriptor.proto
38ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_embed_optimize_for.proto
39ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_empty.proto
40ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_import.proto
41ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_import_lite.proto
42ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_lite.proto
43ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_mset.proto
44ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_mset_wire_format.proto
45ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_no_generic_services.proto
46ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_optimize_for.proto
47ffe3c632Sopenharmony_ci  src/google/protobuf/unittest.proto
48ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_import_public.proto
49ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_import_public_lite.proto
50ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_drop_unknown_fields.proto
51ffe3c632Sopenharmony_ci  src/google/protobuf/unittest_preserve_unknown_enum.proto
52ffe3c632Sopenharmony_ci  src/google/protobuf/map_lite_unittest.proto
53ffe3c632Sopenharmony_ci  src/google/protobuf/map_proto2_unittest.proto
54ffe3c632Sopenharmony_ci  src/google/protobuf/map_unittest.proto
55ffe3c632Sopenharmony_ci  # The unittest_custom_options.proto extends the messages in descriptor.proto
56ffe3c632Sopenharmony_ci  # so we build it in to test extending in general. The library doesn't provide
57ffe3c632Sopenharmony_ci  # a descriptor as it doesn't use the classes/enums.
58ffe3c632Sopenharmony_ci  src/google/protobuf/descriptor.proto
59ffe3c632Sopenharmony_ci)
60ffe3c632Sopenharmony_ci
61ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
62ffe3c632Sopenharmony_ci# The objc unittest specific proto files.
63ffe3c632Sopenharmony_ci
64ffe3c632Sopenharmony_ciOBJC_TEST_PROTO_FILES=(
65ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_cycle.proto
66ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_deprecated.proto
67ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_deprecated_file.proto
68ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_a.proto
69ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_b.proto
70ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_c.proto
71ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_d.proto
72ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_e.proto
73ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_f.proto
74ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_extension_chain_g.proto
75ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_objc.proto
76ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_objc_startup.proto
77ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_objc_options.proto
78ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_runtime_proto2.proto
79ffe3c632Sopenharmony_ci  objectivec/Tests/unittest_runtime_proto3.proto
80ffe3c632Sopenharmony_ci)
81ffe3c632Sopenharmony_ci
82ffe3c632Sopenharmony_ciOBJC_EXTENSIONS=( .pbobjc.h .pbobjc.m )
83ffe3c632Sopenharmony_ci
84ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
85ffe3c632Sopenharmony_ci# Ensure the output dir exists
86ffe3c632Sopenharmony_cimkdir -p "${OUTPUT_DIR}/google/protobuf"
87ffe3c632Sopenharmony_ci
88ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
89ffe3c632Sopenharmony_ci# Move to the top of the protobuf directories and ensure there is a protoc
90ffe3c632Sopenharmony_ci# binary to use.
91ffe3c632Sopenharmony_cicd "${SRCROOT}/.."
92ffe3c632Sopenharmony_ci[[ -x src/protoc ]] || \
93ffe3c632Sopenharmony_ci  die "Could not find the protoc binary; make sure you have built it (objectivec/DevTools/full_mac_build.sh -h)."
94ffe3c632Sopenharmony_ci
95ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
96ffe3c632Sopenharmony_ciRUN_PROTOC=no
97ffe3c632Sopenharmony_ci
98ffe3c632Sopenharmony_ci# Check to if all the output files exist (incase a new one got added).
99ffe3c632Sopenharmony_ci
100ffe3c632Sopenharmony_cifor PROTO_FILE in "${CORE_PROTO_FILES[@]}" "${OBJC_TEST_PROTO_FILES[@]}"; do
101ffe3c632Sopenharmony_ci  DIR=${PROTO_FILE%/*}
102ffe3c632Sopenharmony_ci  BASE_NAME=${PROTO_FILE##*/}
103ffe3c632Sopenharmony_ci  # Drop the extension
104ffe3c632Sopenharmony_ci  BASE_NAME=${BASE_NAME%.*}
105ffe3c632Sopenharmony_ci  OBJC_NAME=$(echo "${BASE_NAME}" | awk -F _ '{for(i=1; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i,2);}')
106ffe3c632Sopenharmony_ci
107ffe3c632Sopenharmony_ci  for EXT in "${OBJC_EXTENSIONS[@]}"; do
108ffe3c632Sopenharmony_ci    if [[ ! -f "${OUTPUT_DIR}/google/protobuf/${OBJC_NAME}${EXT}" ]]; then
109ffe3c632Sopenharmony_ci      RUN_PROTOC=yes
110ffe3c632Sopenharmony_ci    fi
111ffe3c632Sopenharmony_ci  done
112ffe3c632Sopenharmony_cidone
113ffe3c632Sopenharmony_ci
114ffe3c632Sopenharmony_ci# If we haven't decided to run protoc because of a missing file, check to see if
115ffe3c632Sopenharmony_ci# an input has changed.
116ffe3c632Sopenharmony_ciif [[ "${RUN_PROTOC}" != "yes" ]] ; then
117ffe3c632Sopenharmony_ci  # Find the newest input file (protos, compiler, and this script).
118ffe3c632Sopenharmony_ci  # (these patterns catch some extra stuff, but better to over sample than
119ffe3c632Sopenharmony_ci  # under)
120ffe3c632Sopenharmony_ci  readonly NewestInput=$(find \
121ffe3c632Sopenharmony_ci     src/google/protobuf/*.proto \
122ffe3c632Sopenharmony_ci     objectivec/Tests/*.proto \
123ffe3c632Sopenharmony_ci     src/.libs src/*.la src/protoc \
124ffe3c632Sopenharmony_ci     objectivec/DevTools/compile_testing_protos.sh \
125ffe3c632Sopenharmony_ci        -type f -print0 \
126ffe3c632Sopenharmony_ci        | xargs -0 stat -f "%m %N" \
127ffe3c632Sopenharmony_ci        | sort -n | tail -n1 | cut -f2- -d" ")
128ffe3c632Sopenharmony_ci  # Find the oldest output file.
129ffe3c632Sopenharmony_ci  readonly OldestOutput=$(find \
130ffe3c632Sopenharmony_ci        "${OUTPUT_DIR}" \
131ffe3c632Sopenharmony_ci        -type f -name "*.pbobjc.[hm]" -print0 \
132ffe3c632Sopenharmony_ci        | xargs -0 stat -f "%m %N" \
133ffe3c632Sopenharmony_ci        | sort -n -r | tail -n1 | cut -f2- -d" ")
134ffe3c632Sopenharmony_ci  # If the newest input is newer than the oldest output, regenerate.
135ffe3c632Sopenharmony_ci  if [[ "${NewestInput}" -nt "${OldestOutput}" ]] ; then
136ffe3c632Sopenharmony_ci    RUN_PROTOC=yes
137ffe3c632Sopenharmony_ci  fi
138ffe3c632Sopenharmony_cifi
139ffe3c632Sopenharmony_ci
140ffe3c632Sopenharmony_ciif [[ "${RUN_PROTOC}" != "yes" ]] ; then
141ffe3c632Sopenharmony_ci  # Up to date.
142ffe3c632Sopenharmony_ci  exit 0
143ffe3c632Sopenharmony_cifi
144ffe3c632Sopenharmony_ci
145ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
146ffe3c632Sopenharmony_ci# Prune out all the files from previous generations to ensure we only have
147ffe3c632Sopenharmony_ci# current ones.
148ffe3c632Sopenharmony_cifind "${OUTPUT_DIR}" \
149ffe3c632Sopenharmony_ci    -type f -name "*.pbobjc.[hm]" -print0 \
150ffe3c632Sopenharmony_ci    | xargs -0 rm -rf
151ffe3c632Sopenharmony_ci
152ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
153ffe3c632Sopenharmony_ci# Helper to invoke protoc
154ffe3c632Sopenharmony_cicompile_protos() {
155ffe3c632Sopenharmony_ci  src/protoc                                   \
156ffe3c632Sopenharmony_ci    --objc_out="${OUTPUT_DIR}/google/protobuf" \
157ffe3c632Sopenharmony_ci    --proto_path=src/google/protobuf/          \
158ffe3c632Sopenharmony_ci    --proto_path=src                           \
159ffe3c632Sopenharmony_ci    --experimental_allow_proto3_optional       \
160ffe3c632Sopenharmony_ci    "$@"
161ffe3c632Sopenharmony_ci}
162ffe3c632Sopenharmony_ci
163ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
164ffe3c632Sopenharmony_ci# Generate most of the proto files that exist in the C++ src tree.
165ffe3c632Sopenharmony_ci
166ffe3c632Sopenharmony_ci# Note: there is overlap in package.Message names between some of the test
167ffe3c632Sopenharmony_ci# files, so they can't be generated all at once. This works because the overlap
168ffe3c632Sopenharmony_ci# isn't linked into a single binary.
169ffe3c632Sopenharmony_cifor a_proto in "${CORE_PROTO_FILES[@]}" ; do
170ffe3c632Sopenharmony_ci  compile_protos "${a_proto}"
171ffe3c632Sopenharmony_cidone
172ffe3c632Sopenharmony_ci
173ffe3c632Sopenharmony_ci# -----------------------------------------------------------------------------
174ffe3c632Sopenharmony_ci# Generate the Objective C specific testing protos.
175ffe3c632Sopenharmony_cicompile_protos \
176ffe3c632Sopenharmony_ci  --proto_path="objectivec/Tests" \
177ffe3c632Sopenharmony_ci  "${OBJC_TEST_PROTO_FILES[@]}"
178