Thomas Van Lenten | 13a4124 | 2016-09-01 11:45:50 -0400 | [diff] [blame] | 1 | #!/bin/bash -eu |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 2 | # Invoked by the Xcode projects to build the protos needed for the unittests. |
| 3 | |
Thomas Van Lenten | 4b3da9f | 2021-07-09 17:13:23 -0400 | [diff] [blame] | 4 | readonly OUTPUT_DIR="${PROJECT_DERIVED_FILE_DIR}/protos" |
| 5 | |
Thomas Van Lenten | 13a4124 | 2016-09-01 11:45:50 -0400 | [diff] [blame] | 6 | # ----------------------------------------------------------------------------- |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 7 | # Helper for bailing. |
| 8 | die() { |
| 9 | echo "Error: $1" |
| 10 | exit 2 |
| 11 | } |
| 12 | |
Thomas Van Lenten | 13a4124 | 2016-09-01 11:45:50 -0400 | [diff] [blame] | 13 | # ----------------------------------------------------------------------------- |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 14 | # What to do. |
| 15 | case "${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 | ;; |
| 26 | esac |
| 27 | |
Thomas Van Lenten | 13a4124 | 2016-09-01 11:45:50 -0400 | [diff] [blame] | 28 | # ----------------------------------------------------------------------------- |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 29 | # The objc unittest specific proto files. |
| 30 | |
| 31 | OBJC_TEST_PROTO_FILES=( |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 32 | objectivec/Tests/any_test.proto |
| 33 | objectivec/Tests/map_proto2_unittest.proto |
| 34 | objectivec/Tests/map_unittest.proto |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 35 | objectivec/Tests/unittest_cycle.proto |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 36 | objectivec/Tests/unittest_deprecated_file.proto |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 37 | objectivec/Tests/unittest_deprecated.proto |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 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 |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 45 | objectivec/Tests/unittest_import_public.proto |
| 46 | objectivec/Tests/unittest_import.proto |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 47 | objectivec/Tests/unittest_mset.proto |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 48 | objectivec/Tests/unittest_objc_options.proto |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 49 | objectivec/Tests/unittest_objc_startup.proto |
| 50 | objectivec/Tests/unittest_objc.proto |
| 51 | objectivec/Tests/unittest_preserve_unknown_enum.proto |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 52 | objectivec/Tests/unittest_runtime_proto2.proto |
| 53 | objectivec/Tests/unittest_runtime_proto3.proto |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 54 | objectivec/Tests/unittest.proto |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | OBJC_EXTENSIONS=( .pbobjc.h .pbobjc.m ) |
| 58 | |
| 59 | # ----------------------------------------------------------------------------- |
| 60 | # Ensure the output dir exists |
Thomas Van Lenten | b46c0ee | 2022-08-23 13:56:37 -0400 | [diff] [blame] | 61 | mkdir -p "${OUTPUT_DIR}" |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 62 | |
| 63 | # ----------------------------------------------------------------------------- |
| 64 | # Move to the top of the protobuf directories and ensure there is a protoc |
| 65 | # binary to use. |
| 66 | cd "${SRCROOT}/.." |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 67 | readonly PROTOC="bazel-bin/protoc" |
Thomas Van Lenten | b46c0ee | 2022-08-23 13:56:37 -0400 | [diff] [blame] | 68 | [[ -x "${PROTOC}" ]] || \ |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 69 | die "Could not find the protoc binary; make sure you have built it (objectivec/DevTools/full_mac_build.sh -h)." |
| 70 | |
| 71 | # ----------------------------------------------------------------------------- |
| 72 | RUN_PROTOC=no |
| 73 | |
Peter Newman | e2cc2de | 2020-08-10 19:08:25 +0100 | [diff] [blame] | 74 | # Check to if all the output files exist (in case a new one got added). |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 75 | |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 76 | for PROTO_FILE in "${OBJC_TEST_PROTO_FILES[@]}"; do |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 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 |
Thomas Van Lenten | 28bc3ba | 2022-08-23 17:23:28 -0400 | [diff] [blame] | 84 | if [[ ! -f "${OUTPUT_DIR}/${DIR}/${OBJC_NAME}${EXT}" ]]; then |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 85 | RUN_PROTOC=yes |
| 86 | fi |
| 87 | done |
| 88 | done |
| 89 | |
| 90 | # If we haven't decided to run protoc because of a missing file, check to see if |
| 91 | # an input has changed. |
| 92 | if [[ "${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 Lenten | b46c0ee | 2022-08-23 13:56:37 -0400 | [diff] [blame] | 97 | objectivec/Tests/*.proto "${PROTOC}" \ |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 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 |
| 112 | fi |
| 113 | |
| 114 | if [[ "${RUN_PROTOC}" != "yes" ]] ; then |
| 115 | # Up to date. |
| 116 | exit 0 |
| 117 | fi |
| 118 | |
| 119 | # ----------------------------------------------------------------------------- |
| 120 | # Prune out all the files from previous generations to ensure we only have |
| 121 | # current ones. |
| 122 | find "${OUTPUT_DIR}" \ |
| 123 | -type f -name "*.pbobjc.[hm]" -print0 \ |
| 124 | | xargs -0 rm -rf |
| 125 | |
| 126 | # ----------------------------------------------------------------------------- |
Thomas Van Lenten | 3e0d399 | 2022-08-23 13:26:25 -0400 | [diff] [blame] | 127 | # Generate the Objective C specific testing protos. |
Thomas Van Lenten | edf69e5 | 2020-01-22 17:56:06 -0500 | [diff] [blame] | 128 | |
Thomas Van Lenten | 5cf61b7 | 2022-08-29 16:37:36 -0400 | [diff] [blame] | 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 \ |
Thomas Van Lenten | bc9880b | 2022-08-23 14:05:47 -0400 | [diff] [blame] | 136 | "${OBJC_TEST_PROTO_FILES[@]}" |