Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 1 | Protocol Buffers - Google's data interchange format |
| 2 | =================================================== |
| 3 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 4 | Copyright 2008 Google Inc. |
| 5 | |
| 6 | This directory contains the Objective C Protocol Buffers runtime library. |
| 7 | |
| 8 | Requirements |
| 9 | ------------ |
| 10 | |
Dongjoon Hyun | 7b08d49 | 2016-01-11 14:52:01 -0800 | [diff] [blame] | 11 | The Objective C implementation requires: |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 12 | |
| 13 | - Objective C 2.0 Runtime (32bit & 64bit iOS, 64bit OS X). |
dmaclach | 6f9d488 | 2020-01-23 13:38:16 -0800 | [diff] [blame] | 14 | - Xcode 10.3 (or later). |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 15 | - The library code does *not* use ARC (for performance reasons), but it all can |
| 16 | be called from ARC code. |
| 17 | |
| 18 | Installation |
| 19 | ------------ |
| 20 | |
leovitch | 2804902 | 2018-05-29 21:08:00 +0900 | [diff] [blame] | 21 | The distribution pulled from github includes the sources for both the |
| 22 | compiler (protoc) and the runtime (this directory). After cloning the distribution |
| 23 | and needed submodules ([see the src directory's README](../src/README.md)), |
| 24 | to build the compiler and run the runtime tests, you can use: |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 25 | |
| 26 | $ objectivec/DevTools/full_mac_build.sh |
| 27 | |
| 28 | This will generate the `src/protoc` binary. |
| 29 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 30 | Building |
| 31 | -------- |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 32 | |
| 33 | There are two ways to include the Runtime sources in your project: |
| 34 | |
Thomas Van Lenten | 07f0231 | 2018-02-07 10:39:13 -0500 | [diff] [blame] | 35 | Add `objectivec/*.h`, `objectivec/google/protobuf/*.pbobjc.h`, and |
| 36 | `objectivec/GPBProtocolBuffers.m` to your project. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 37 | |
| 38 | *or* |
| 39 | |
Thomas Van Lenten | 07f0231 | 2018-02-07 10:39:13 -0500 | [diff] [blame] | 40 | Add `objectivec/*.h`, `objectivec/google/protobuf/*.pbobjc.h`, |
| 41 | `objectivec/google/protobuf/*.pbobjc.m`, and `objectivec/*.m` except for |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 42 | `objectivec/GPBProtocolBuffers.m` to your project. |
| 43 | |
| 44 | |
| 45 | If the target is using ARC, remember to turn off ARC (`-fno-objc-arc`) for the |
| 46 | `.m` files. |
| 47 | |
Thomas Van Lenten | 07f0231 | 2018-02-07 10:39:13 -0500 | [diff] [blame] | 48 | The files generated by `protoc` for the `*.proto` files (`*.pbobjc.h` and |
| 49 | `*.pbobjc.m`) are then also added to the target. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 50 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 51 | Usage |
| 52 | ----- |
| 53 | |
| 54 | The objects generated for messages should work like any other Objective C |
| 55 | object. They are mutable objects, but if you don't change them, they are safe |
| 56 | to share between threads (similar to passing an NSMutableDictionary between |
| 57 | threads/queues; as long as no one mutates it, things are fine). |
| 58 | |
| 59 | There are a few behaviors worth calling out: |
| 60 | |
| 61 | A property that is type NSString\* will never return nil. If the value is |
| 62 | unset, it will return an empty string (@""). This is inpart to align things |
| 63 | with the Protocol Buffers spec which says the default for strings is an empty |
| 64 | string, but also so you can always safely pass them to isEqual:/compare:, etc. |
| 65 | and have deterministic results. |
| 66 | |
| 67 | A property that is type NSData\* also won't return nil, it will return an empty |
| 68 | data ([NSData data]). The reasoning is the same as for NSString not returning |
| 69 | nil. |
| 70 | |
| 71 | A property that is another GPBMessage class also will not return nil. If the |
| 72 | field wasn't already set, you will get a instance of the correct class. This |
| 73 | instance will be a temporary instance unless you mutate it, at which point it |
| 74 | will be attached to its parent object. We call this pattern *autocreators*. |
| 75 | Similar to NSString and NSData properties it makes things a little safer when |
| 76 | using them with isEqual:/etc.; but more importantly, this allows you to write |
| 77 | code that uses Objective C's property dot notation to walk into nested objects |
| 78 | and access and/or assign things without having to check that they are not nil |
| 79 | and create them each step along the way. You can write this: |
| 80 | |
| 81 | ``` |
| 82 | - (void)updateRecord:(MyMessage *)msg { |
| 83 | ... |
| 84 | // Note: You don't have to check subMessage and otherMessage for nil and |
| 85 | // alloc/init/assign them back along the way. |
| 86 | msg.subMessage.otherMessage.lastName = @"Smith"; |
| 87 | ... |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | If you want to check if a GPBMessage property is present, there is always as |
| 92 | `has\[NAME\]` property to go with the main property to check if it is set. |
| 93 | |
| 94 | A property that is of an Array or Dictionary type also provides *autocreator* |
| 95 | behavior and will never return nil. This provides all the same benefits you |
| 96 | see for the message properties. Again, you can write: |
| 97 | |
| 98 | ``` |
| 99 | - (void)updateRecord:(MyMessage *)msg { |
| 100 | ... |
| 101 | // Note: Just like above, you don't have to check subMessage and otherMessage |
| 102 | // for nil and alloc/init/assign them back along the way. You also don't have |
| 103 | // to create the siblingsArray, you can safely just append to it. |
| 104 | [msg.subMessage.otherMessage.siblingsArray addObject:@"Pat"]; |
| 105 | ... |
| 106 | } |
| 107 | ``` |
| 108 | |
| 109 | If you are inspecting a message you got from some other place (server, disk, |
| 110 | etc), you may want to check if the Array or Dictionary has entries without |
| 111 | causing it to be created for you. For this, there is always a `\[NAME\]_Count` |
| 112 | property also provided that can return zero or the real count, but won't trigger |
| 113 | the creation. |
| 114 | |
| 115 | For primitive type fields (ints, floats, bools, enum) in messages defined in a |
| 116 | `.proto` file that use *proto2* syntax there are conceptual differences between |
| 117 | having an *explicit* and *default* value. You can always get the value of the |
| 118 | property. In the case that it hasn't been set you will get the default. In |
| 119 | cases where you need to know whether it was set explicitly or you are just |
| 120 | getting the default, you can use the `has\[NAME\]` property. If the value has |
| 121 | been set, and you want to clear it, you can set the `has\[NAME\]` to `NO`. |
| 122 | *proto3* syntax messages do away with this concept, thus the default values are |
| 123 | never included when the message is encoded. |
| 124 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 125 | The Objective C classes/enums can be used from Swift code. |
| 126 | |
Thomas Van Lenten | a2a3399 | 2016-06-14 13:23:37 -0400 | [diff] [blame] | 127 | Objective C Generator Proto File Options |
| 128 | ---------------------------------------- |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 129 | |
| 130 | **objc_class_prefix=\<prefix\>** (no default) |
| 131 | |
Thomas Van Lenten | cf12bff | 2021-06-11 16:58:51 -0400 | [diff] [blame] | 132 | This options allow you to provide a custom prefix for all the symbols generated |
| 133 | from a proto file (classes (from message), enums, the Root for extension |
| 134 | support). |
| 135 | |
Dimitris Koutsogiorgas | 89c4031 | 2022-02-25 09:06:20 -0800 | [diff] [blame] | 136 | If not set, the generation options `package_to_prefix_mappings_path` and |
Dimitris Koutsogiorgas | a112c4a | 2022-02-11 11:05:29 -0800 | [diff] [blame] | 137 | `use_package_as_prefix` (documented below) controls what is used instead. Since |
| 138 | Objective C uses a global namespace for all of its classes, there can be collisions. |
| 139 | `use_package_as_prefix=yes` should avoid collisions since proto package are used to |
| 140 | scope/name things in other languages, but this option can be used to get shorter |
| 141 | names instead. Convention is to base the explicit prefix on the proto package. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 142 | |
Thomas Van Lenten | a2a3399 | 2016-06-14 13:23:37 -0400 | [diff] [blame] | 143 | Objective C Generator `protoc` Options |
| 144 | -------------------------------------- |
| 145 | |
| 146 | When generating Objective C code, `protoc` supports a `--objc_opt` argument; the |
| 147 | argument is comma-delimited name/value pairs (_key=value,key2=value2_). The |
| 148 | _keys_ are used to change the behavior during generation. The currently |
| 149 | supported keys are: |
| 150 | |
Nathan Wong | 3be6110 | 2016-06-27 22:56:34 +0100 | [diff] [blame] | 151 | * `generate_for_named_framework`: The `value` used for this key will be used |
Thomas Van Lenten | a2a3399 | 2016-06-14 13:23:37 -0400 | [diff] [blame] | 152 | when generating the `#import` statements in the generated code. Instead |
| 153 | of being plain `#import "some/path/file.pbobjc.h"` lines, they will be |
| 154 | framework based, i.e. - `#import <VALUE/file.pbobjc.h>`. |
| 155 | |
Thomas Van Lenten | 8c20e55 | 2016-06-17 10:31:05 -0400 | [diff] [blame] | 156 | _NOTE:_ If this is used with `named_framework_to_proto_path_mappings_path`, |
| 157 | then this is effectively the _default_ to use for everything that wasn't |
| 158 | mapped by the other. |
| 159 | |
| 160 | * `named_framework_to_proto_path_mappings_path`: The `value` used for this key |
| 161 | is a path to a file containing the listing of framework names and proto |
| 162 | files. The generator uses this to decide if another proto file referenced |
| 163 | should use a framework style import vs. a user level import |
| 164 | (`#import <FRAMEWORK/file.pbobjc.h>` vs `#import "dir/file.pbobjc.h"`). |
| 165 | |
| 166 | The format of the file is: |
| 167 | * An entry is a line of `frameworkName: file.proto, dir/file2.proto`. |
| 168 | * Comments start with `#`. |
| 169 | * A comment can go on a line after an entry. |
| 170 | (i.e. - `frameworkName: file.proto # comment`) |
| 171 | |
| 172 | Any number of files can be listed for a framework, just separate them with |
| 173 | commas. |
| 174 | |
Peter Newman | e2cc2de | 2020-08-10 19:08:25 +0100 | [diff] [blame] | 175 | There can be multiple lines listing the same frameworkName in case it has a |
Thomas Van Lenten | 8c20e55 | 2016-06-17 10:31:05 -0400 | [diff] [blame] | 176 | lot of proto files included in it; and having multiple lines makes things |
| 177 | easier to read. |
| 178 | |
Thomas Van Lenten | 9f546ba | 2020-06-09 11:19:15 -0400 | [diff] [blame] | 179 | * `runtime_import_prefix`: The `value` used for this key to be used as a |
| 180 | prefix on `#import`s of runtime provided headers in the generated files. |
| 181 | When integrating ObjC protos into a build system, this can be used to avoid |
| 182 | having to add the runtime directory to the header search path since the |
| 183 | generate `#import` will be more complete. |
| 184 | |
Thomas Van Lenten | bb4302e | 2022-02-28 16:32:04 -0500 | [diff] [blame] | 185 | * `package_to_prefix_mappings_path`: The `value` used for this key is a |
Dimitris Koutsogiorgas | 89c4031 | 2022-02-25 09:06:20 -0800 | [diff] [blame] | 186 | path to a file containing a list of proto packages and prefixes. |
Dimitris Koutsogiorgas | a112c4a | 2022-02-11 11:05:29 -0800 | [diff] [blame] | 187 | The generator will use this to locate which ObjC class prefix to use when |
| 188 | generating sources _unless_ the `objc_class_prefix` file option is set. |
| 189 | This option can be useful if multiple apps consume a common set of |
| 190 | proto files but wish to use a different prefix for the generated sources |
| 191 | between them. This option takes precedent over the `use_package_as_prefix` |
| 192 | option. |
| 193 | |
| 194 | The format of the file is: |
| 195 | * An entry is a line of "package=prefix". |
| 196 | * Comments start with `#`. |
| 197 | * A comment can go on a line after a expected package/prefix pair. |
| 198 | (i.e. - "package=prefix # comment") |
| 199 | * For files that do NOT have a proto package (not recommended), an |
| 200 | entry can be made as "no_package:PATH=prefix", where PATH is the |
| 201 | path for the .proto file. |
| 202 | |
Thomas Van Lenten | cf12bff | 2021-06-11 16:58:51 -0400 | [diff] [blame] | 203 | * `use_package_as_prefix` and `proto_package_prefix_exceptions_path`: The |
| 204 | `value` for `use_package_as_prefix` can be `yes` or `no`, and indicates |
| 205 | if a prefix should be derived from the proto package for all the symbols |
| 206 | for files that don't have the `objc_class_prefix` file option (mentioned |
| 207 | above). This helps ensure the symbols are more unique and means there is |
| 208 | less chance of ObjC class name collisions. |
| 209 | |
| 210 | To help in migrating code to using this support, |
| 211 | `proto_package_prefix_exceptions_path` can be used to provide the path |
| 212 | to a file that contains proto package names (one per line, comments allowed |
| 213 | if prefixed with `#`). These package won't get the derived prefix, allowing |
| 214 | migrations to the behavior one proto package at a time across a code base. |
| 215 | |
| 216 | `use_package_as_prefix` currently defaults to `no` (existing behavior), but |
| 217 | in the future (as a breaking change), that is likely to change since it |
| 218 | helps prepare folks before they end up using a lot of protos and getting a |
| 219 | lot of collisions. |
| 220 | |
Thomas Van Lenten | bb4302e | 2022-02-28 16:32:04 -0500 | [diff] [blame] | 221 | * `headers_use_forward_declarations`: The `value` for this can be `yes` or |
| 222 | `no`, and indicates if the generated headers use forward declarations for |
| 223 | Message and Enum types from other .proto files or if the files should be |
| 224 | imported into the generated header instead. |
| 225 | |
| 226 | By using forward declarations, less code is likely to recompile when the |
| 227 | files do change, but Swift generally doesn't like forward declarations and |
| 228 | will fail to include properties when the concrete definition of the type is |
| 229 | known at import time. If your proto usages span modules, this can be a |
| 230 | problem. |
| 231 | |
| 232 | `headers_use_forward_declarations` currently defaults to `yes` (existing |
| 233 | behavior), but in a future release, that default may change to provide |
| 234 | better Swift support by default. |
| 235 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 236 | Contributing |
| 237 | ------------ |
| 238 | |
| 239 | Please make updates to the tests along with changes. If just changing the |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 240 | runtime, the Xcode projects can be used to build and run tests. If your change |
| 241 | also requires changes to the generated code, |
| 242 | `objectivec/DevTools/full_mac_build.sh` can be used to easily rebuild and test |
| 243 | changes. Passing `-h` to the script will show the addition options that could |
| 244 | be useful. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 245 | |
| 246 | Documentation |
| 247 | ------------- |
| 248 | |
| 249 | The complete documentation for Protocol Buffers is available via the |
| 250 | web at: |
| 251 | |
Josh Kelley | 8aaf4d2 | 2021-05-17 17:53:27 -0400 | [diff] [blame] | 252 | https://developers.google.com/protocol-buffers/ |