blob: fb8a8532d04c2ad8acb341950e60925a15a5fbc2 [file] [log] [blame]
Thomas Van Lenten13a41242016-09-01 11:45:50 -04001#!/bin/bash -eu
Thomas Van Lentend846b0b2015-06-08 16:24:57 -04002# Invoked by the Xcode projects to build the protos needed for the unittests.
3
Thomas Van Lenten4b3da9f2021-07-09 17:13:23 -04004readonly OUTPUT_DIR="${PROJECT_DERIVED_FILE_DIR}/protos"
5
Thomas Van Lenten13a41242016-09-01 11:45:50 -04006# -----------------------------------------------------------------------------
Thomas Van Lentend846b0b2015-06-08 16:24:57 -04007# Helper for bailing.
8die() {
9 echo "Error: $1"
10 exit 2
11}
12
Thomas Van Lenten13a41242016-09-01 11:45:50 -040013# -----------------------------------------------------------------------------
Thomas Van Lentend846b0b2015-06-08 16:24:57 -040014# 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
Thomas Van Lenten13a41242016-09-01 11:45:50 -040028# -----------------------------------------------------------------------------
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050029# The objc unittest specific proto files.
30
31OBJC_TEST_PROTO_FILES=(
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040032 objectivec/Tests/any_test.proto
33 objectivec/Tests/map_proto2_unittest.proto
34 objectivec/Tests/map_unittest.proto
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050035 objectivec/Tests/unittest_cycle.proto
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050036 objectivec/Tests/unittest_deprecated_file.proto
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040037 objectivec/Tests/unittest_deprecated.proto
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050038 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
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040045 objectivec/Tests/unittest_import_public.proto
46 objectivec/Tests/unittest_import.proto
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040047 objectivec/Tests/unittest_mset.proto
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050048 objectivec/Tests/unittest_objc_options.proto
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040049 objectivec/Tests/unittest_objc_startup.proto
50 objectivec/Tests/unittest_objc.proto
51 objectivec/Tests/unittest_preserve_unknown_enum.proto
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050052 objectivec/Tests/unittest_runtime_proto2.proto
53 objectivec/Tests/unittest_runtime_proto3.proto
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040054 objectivec/Tests/unittest.proto
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050055)
56
57OBJC_EXTENSIONS=( .pbobjc.h .pbobjc.m )
58
59# -----------------------------------------------------------------------------
60# Ensure the output dir exists
Thomas Van Lentenb46c0ee2022-08-23 13:56:37 -040061mkdir -p "${OUTPUT_DIR}"
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050062
63# -----------------------------------------------------------------------------
64# Move to the top of the protobuf directories and ensure there is a protoc
65# binary to use.
66cd "${SRCROOT}/.."
Mike Kruskaled5c57a2022-08-10 22:51:29 -070067readonly PROTOC="bazel-bin/protoc"
Thomas Van Lentenb46c0ee2022-08-23 13:56:37 -040068[[ -x "${PROTOC}" ]] || \
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050069 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
Peter Newmane2cc2de2020-08-10 19:08:25 +010074# Check to if all the output files exist (in case a new one got added).
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050075
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -040076for PROTO_FILE in "${OBJC_TEST_PROTO_FILES[@]}"; do
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050077 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
Thomas Van Lenten28bc3ba2022-08-23 17:23:28 -040084 if [[ ! -f "${OUTPUT_DIR}/${DIR}/${OBJC_NAME}${EXT}" ]]; then
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050085 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 \
Thomas Van Lentenb46c0ee2022-08-23 13:56:37 -040097 objectivec/Tests/*.proto "${PROTOC}" \
Thomas Van Lentenedf69e52020-01-22 17:56:06 -050098 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# -----------------------------------------------------------------------------
Thomas Van Lenten3e0d3992022-08-23 13:26:25 -0400127# Generate the Objective C specific testing protos.
Thomas Van Lentenedf69e52020-01-22 17:56:06 -0500128
Thomas Van Lenten5cf61b72022-08-29 16:37:36 -0400129"${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 \
Thomas Van Lentenbc9880b2022-08-23 14:05:47 -0400136 "${OBJC_TEST_PROTO_FILES[@]}"