Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 1 | <!-- go/cmark --> |
| 2 | <!--* freshness: {owner: 'danilchap' reviewed: '2022-01-17'} *--> |
Danil Chapovalov | 46f5c11 | 2021-05-12 12:05:48 | [diff] [blame] | 3 | |
Artem Titov | a617867 | 2023-01-30 10:51:01 | [diff] [blame] | 4 | # WebRTC coding style guide |
Artem Titov | 0f2ce5c | 2023-01-26 20:18:46 | [diff] [blame] | 5 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 6 | ## General advice |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 7 | |
| 8 | Some older parts of the code violate the style guide in various ways. |
Danil Chapovalov | 80569ea | 2022-01-17 15:11:02 | [diff] [blame] | 9 | If making large changes to such code, consider first cleaning it up in a |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 10 | separate CL. |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 11 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 12 | ## C++ |
Karl Wiberg | bb821e2 | 2017-09-03 03:02:12 | [diff] [blame] | 13 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 14 | WebRTC follows the [Chromium C++ style guide][chr-style] and the |
| 15 | [Google C++ style guide][goog-style]. In cases where they conflict, the Chromium |
| 16 | style guide trumps the Google style guide, and the rules in this file trump them |
Karl Wiberg | e1ed241 | 2017-09-06 09:10:23 | [diff] [blame] | 17 | both. |
Karl Wiberg | bb821e2 | 2017-09-03 03:02:12 | [diff] [blame] | 18 | |
Fanny Linderborg | 0d2dc1f | 2021-07-14 14:02:11 | [diff] [blame] | 19 | [chr-style]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md |
Karl Wiberg | bb821e2 | 2017-09-03 03:02:12 | [diff] [blame] | 20 | [goog-style]: https://google.github.io/styleguide/cppguide.html |
| 21 | |
Karl Wiberg | 180d992 | 2018-03-15 10:21:05 | [diff] [blame] | 22 | ### C++ version |
| 23 | |
Mirko Bonadei | 28cd164 | 2021-12-24 08:55:22 | [diff] [blame] | 24 | WebRTC is written in C++17, but with some restrictions: |
Karl Wiberg | 180d992 | 2018-03-15 10:21:05 | [diff] [blame] | 25 | |
Mirko Bonadei | 28cd164 | 2021-12-24 08:55:22 | [diff] [blame] | 26 | * We only allow the subset of C++17 (language and library) that is not banned by |
Joe Mason | 4893dbe | 2021-09-16 23:11:56 | [diff] [blame] | 27 | Chromium; see the [list of banned C++ features in Chromium][chr-style-cpp]. |
Mirko Bonadei | 28cd164 | 2021-12-24 08:55:22 | [diff] [blame] | 28 | * We only allow the subset of C++17 that is also valid C++20; otherwise, users |
| 29 | would not be able to compile WebRTC in C++20 mode. |
Karl Wiberg | 180d992 | 2018-03-15 10:21:05 | [diff] [blame] | 30 | |
Danil Chapovalov | 8ad0be0 | 2022-01-24 12:58:30 | [diff] [blame] | 31 | [chr-style-cpp]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md |
Karl Wiberg | 180d992 | 2018-03-15 10:21:05 | [diff] [blame] | 32 | |
Karl Wiberg | c3af97d | 2018-08-27 02:26:18 | [diff] [blame] | 33 | ### Abseil |
| 34 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 35 | You may use a subset of the utilities provided by the [Abseil][abseil] library |
| 36 | when writing WebRTC C++ code; see the |
| 37 | [instructions on how to use Abseil in WebRTC](abseil-in-webrtc.md). |
Karl Wiberg | c3af97d | 2018-08-27 02:26:18 | [diff] [blame] | 38 | |
| 39 | [abseil]: https://abseil.io/about/ |
| 40 | |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 41 | ### <a name="h-cc-pairs"></a>`.h` and `.cc` files come in pairs |
| 42 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 43 | `.h` and `.cc` files should come in pairs, with the same name (except for the |
| 44 | file type suffix), in the same directory, in the same build target. |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 45 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 46 | * If a declaration in `path/to/foo.h` has a definition in some `.cc` file, it |
| 47 | should be in `path/to/foo.cc`. |
| 48 | * If a definition in `path/to/foo.cc` file has a declaration in some `.h` file, |
| 49 | it should be in `path/to/foo.h`. |
| 50 | * Omit the `.cc` file if it would have been empty, but still list the `.h` file |
| 51 | in a build target. |
| 52 | * Omit the `.h` file if it would have been empty. (This can happen with unit |
| 53 | test `.cc` files, and with `.cc` files that define `main`.) |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 54 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 55 | See also the |
| 56 | [examples and exceptions on how to treat `.h` and `.cpp` files](style-guide/h-cc-pairs.md). |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 57 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 58 | This makes the source code easier to navigate and organize, and precludes some |
| 59 | questionable build system practices such as having build targets that don't pull |
| 60 | in definitions for everything they declare. |
Karl Wiberg | 08c5cb0 | 2018-03-14 11:23:12 | [diff] [blame] | 61 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 62 | ### `TODO` comments |
Danil Chapovalov | 375eff4 | 2019-12-11 11:45:08 | [diff] [blame] | 63 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 64 | Follow the [Google styleguide for `TODO` comments][goog-style-todo]. When |
Emil Lundmark | 11174e7 | 2022-07-01 08:00:11 | [diff] [blame] | 65 | referencing a WebRTC bug, prefer using the URL form (excluding the scheme part): |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 66 | |
| 67 | ```cpp |
Danil Chapovalov | 375eff4 | 2019-12-11 11:45:08 | [diff] [blame] | 68 | // TODO(bugs.webrtc.org/12345): Delete the hack when blocking bugs are resolved. |
| 69 | ``` |
| 70 | |
Emil Lundmark | 11174e7 | 2022-07-01 08:00:11 | [diff] [blame] | 71 | The short form used in commit messages, e.g. `webrtc:12345`, is discouraged. |
| 72 | |
Danil Chapovalov | 375eff4 | 2019-12-11 11:45:08 | [diff] [blame] | 73 | [goog-style-todo]: https://google.github.io/styleguide/cppguide.html#TODO_Comments |
| 74 | |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 75 | ### Deprecation |
| 76 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 77 | Annotate the declarations of deprecated functions and classes with the |
Harald Alvestrand | 832657f | 2022-04-27 08:59:32 | [diff] [blame] | 78 | [`[[deprecated]]` attribute][DEPRECATED] to cause an error when they're used |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 79 | inside WebRTC and a compiler warning when they're used by dependant projects. |
| 80 | Like so: |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 81 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 82 | ```cpp |
Harald Alvestrand | 832657f | 2022-04-27 08:59:32 | [diff] [blame] | 83 | [[deprecated("bugs.webrtc.org/12345")]] |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 84 | std::pony PonyPlz(const std::pony_spec& ps); |
| 85 | ``` |
| 86 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 87 | NOTE 1: The annotation goes on the declaration in the `.h` file, not the |
| 88 | definition in the `.cc` file! |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 89 | |
| 90 | NOTE 2: In order to have unit tests that use the deprecated function without |
| 91 | getting errors, do something like this: |
| 92 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 93 | ```cpp |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 94 | std::pony DEPRECATED_PonyPlz(const std::pony_spec& ps); |
Harald Alvestrand | 832657f | 2022-04-27 08:59:32 | [diff] [blame] | 95 | [[deprecated("bugs.webrtc.org/12345")]] |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 96 | inline std::pony PonyPlz(const std::pony_spec& ps) { |
| 97 | return DEPRECATED_PonyPlz(ps); |
| 98 | } |
| 99 | ``` |
| 100 | |
| 101 | In other words, rename the existing function, and provide an inline wrapper |
| 102 | using the original name that calls it. That way, callers who are willing to |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 103 | call it using the `DEPRECATED_`-prefixed name don't get the warning. |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 104 | |
Harald Alvestrand | 832657f | 2022-04-27 08:59:32 | [diff] [blame] | 105 | NOTE 3: Occasionally, with long descriptions, `git cl format` will do the wrong |
| 106 | thing with the attribute. In that case, you can use the |
| 107 | [`ABSL_DEPRECATED` macro][ABSL_DEPRECATED], which is formatted in a more |
| 108 | readable way. |
| 109 | |
| 110 | [DEPRECATED]: https://en.cppreference.com/w/cpp/language/attributes/deprecated |
Tony Herre | b0ed120 | 2021-07-22 15:40:44 | [diff] [blame] | 111 | [ABSL_DEPRECATED]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/attributes.h?q=ABSL_DEPRECATED |
Danil Chapovalov | 7013b3b | 2021-02-22 13:31:26 | [diff] [blame] | 112 | |
Karl Wiberg | e1ed241 | 2017-09-06 09:10:23 | [diff] [blame] | 113 | ### ArrayView |
Karl Wiberg | bb821e2 | 2017-09-03 03:02:12 | [diff] [blame] | 114 | |
Karl Wiberg | e1ed241 | 2017-09-06 09:10:23 | [diff] [blame] | 115 | When passing an array of values to a function, use `rtc::ArrayView` |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 116 | whenever possible—that is, whenever you're not passing ownership of |
| 117 | the array, and don't allow the callee to change the array size. |
Karl Wiberg | e1ed241 | 2017-09-06 09:10:23 | [diff] [blame] | 118 | |
| 119 | For example, |
| 120 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 121 | | instead of | use | |
| 122 | |-------------------------------------|----------------------| |
| 123 | | `const std::vector<T>&` | `ArrayView<const T>` | |
| 124 | | `const T* ptr, size_t num_elements` | `ArrayView<const T>` | |
| 125 | | `T* ptr, size_t num_elements` | `ArrayView<T>` | |
Karl Wiberg | e1ed241 | 2017-09-06 09:10:23 | [diff] [blame] | 126 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 127 | See the [source code for `rtc::ArrayView`](api/array_view.h) for more detailed |
| 128 | docs. |
Karl Wiberg | fb4e677 | 2017-09-04 09:27:33 | [diff] [blame] | 129 | |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 130 | ### sigslot |
| 131 | |
Harald Alvestrand | f703ed1 | 2021-04-19 13:28:21 | [diff] [blame] | 132 | SIGSLOT IS DEPRECATED. |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 133 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 134 | Prefer `webrtc::CallbackList`, and manage thread safety yourself. |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 135 | |
Harald Alvestrand | f703ed1 | 2021-04-19 13:28:21 | [diff] [blame] | 136 | ### Smart pointers |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 137 | |
Harald Alvestrand | f703ed1 | 2021-04-19 13:28:21 | [diff] [blame] | 138 | The following smart pointer types are recommended: |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 139 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 140 | * `std::unique_ptr` for all singly-owned objects |
| 141 | * `rtc::scoped_refptr` for all objects with shared ownership |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 142 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 143 | Use of `std::shared_ptr` is *not permitted*. It is banned in the Chromium style |
Danil Chapovalov | 80569ea | 2022-01-17 15:11:02 | [diff] [blame] | 144 | guide (overriding the Google style guide). See the |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 145 | [list of banned C++ library features in Chromium][chr-std-shared-ptr] for more |
| 146 | information. |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 147 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 148 | In most cases, one will want to explicitly control lifetimes, and therefore use |
| 149 | `std::unique_ptr`, but in some cases, for instance where references have to |
| 150 | exist both from the API users and internally, with no way to invalidate pointers |
| 151 | held by the API user, `rtc::scoped_refptr` can be appropriate. |
Taylor Brandstetter | efc5fbd | 2017-12-08 19:42:15 | [diff] [blame] | 152 | |
Danil Chapovalov | 8ad0be0 | 2022-01-24 12:58:30 | [diff] [blame] | 153 | [chr-std-shared-ptr]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md#shared-pointers-banned |
Karl Wiberg | 0d44625 | 2018-03-19 22:27:35 | [diff] [blame] | 154 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 155 | ### `std::bind` |
Karl Wiberg | 0d44625 | 2018-03-19 22:27:35 | [diff] [blame] | 156 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 157 | Don't use `std::bind`—there are pitfalls, and lambdas are almost as succinct and |
| 158 | already familiar to modern C++ programmers. |
Karl Wiberg | 0d44625 | 2018-03-19 22:27:35 | [diff] [blame] | 159 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 160 | ### `std::function` |
| 161 | |
| 162 | `std::function` is allowed, but remember that it's not the right tool for every |
| 163 | occasion. Prefer to use interfaces when that makes sense, and consider |
| 164 | `rtc::FunctionView` for cases where the callee will not save the function |
| 165 | object. |
Karl Wiberg | 0d44625 | 2018-03-19 22:27:35 | [diff] [blame] | 166 | |
Taylor Brandstetter | 212a206 | 2017-10-12 18:22:48 | [diff] [blame] | 167 | ### Forward declarations |
| 168 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 169 | WebRTC follows the |
| 170 | [Google C++ style guide on forward declarations][goog-forward-declarations]. |
| 171 | In summary: avoid using forward declarations where possible; just `#include` the |
| 172 | headers you need. |
Taylor Brandstetter | 212a206 | 2017-10-12 18:22:48 | [diff] [blame] | 173 | |
| 174 | [goog-forward-declarations]: https://google.github.io/styleguide/cppguide.html#Forward_Declarations |
| 175 | |
Harald Alvestrand | f88487c | 2022-11-04 12:13:04 | [diff] [blame] | 176 | ### RTTI and dynamic_cast |
| 177 | |
| 178 | The Google style guide [permits the use of dynamic_cast](https://google.github.io/styleguide/cppguide.html#Run-Time_Type_Information__RTTI_). |
| 179 | |
| 180 | However, WebRTC does not permit it. WebRTC (and Chrome) is compiled with the |
| 181 | -fno-rtti flag, and the overhead of enabling RTTI it is on the order of 220 |
| 182 | Kbytes (for Android Arm64). |
| 183 | |
| 184 | Use static_cast and take your own steps to ensure type safety. |
| 185 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 186 | ## C |
Karl Wiberg | fb4e677 | 2017-09-04 09:27:33 | [diff] [blame] | 187 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 188 | There's a substantial chunk of legacy C code in WebRTC, and a lot of it is old |
| 189 | enough that it violates the parts of the C++ style guide that also applies to C |
| 190 | (naming etc.) for the simple reason that it pre-dates the use of the current C++ |
Danil Chapovalov | 80569ea | 2022-01-17 15:11:02 | [diff] [blame] | 191 | style guide for this code base. If making large changes to C code, consider |
| 192 | converting the whole thing to C++ first. |
Karl Wiberg | ee3d760 | 2017-09-04 12:46:47 | [diff] [blame] | 193 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 194 | ## Java |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 195 | |
| 196 | WebRTC follows the [Google Java style guide][goog-java-style]. |
| 197 | |
| 198 | [goog-java-style]: https://google.github.io/styleguide/javaguide.html |
| 199 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 200 | ## Objective-C and Objective-C++ |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 201 | |
| 202 | WebRTC follows the |
| 203 | [Chromium Objective-C and Objective-C++ style guide][chr-objc-style]. |
| 204 | |
Fanny Linderborg | 0d2dc1f | 2021-07-14 14:02:11 | [diff] [blame] | 205 | [chr-objc-style]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/objective-c/objective-c.md |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 206 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 207 | ## Python |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 208 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 209 | WebRTC follows [Chromium's Python style][chr-py-style]. |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 210 | |
Fanny Linderborg | 0d2dc1f | 2021-07-14 14:02:11 | [diff] [blame] | 211 | [chr-py-style]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/python/python.md |
Karl Wiberg | 241d710 | 2017-09-08 13:07:15 | [diff] [blame] | 212 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 213 | ## Build files |
Karl Wiberg | ee3d760 | 2017-09-04 12:46:47 | [diff] [blame] | 214 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 215 | The WebRTC build files are written in [GN][gn], and we follow the |
| 216 | [GN style guide][gn-style]. Additionally, there are some |
| 217 | WebRTC-specific rules below; in case of conflict, they trump the Chromium style |
| 218 | guide. |
Karl Wiberg | 91d0ab7 | 2017-09-07 15:05:31 | [diff] [blame] | 219 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 220 | [gn]: https://gn.googlesource.com/gn/ |
| 221 | [gn-style]: https://gn.googlesource.com/gn/+/HEAD/docs/style_guide.md |
Karl Wiberg | 91d0ab7 | 2017-09-07 15:05:31 | [diff] [blame] | 222 | |
Per Kjellander | a7f2d84 | 2018-01-10 15:54:53 | [diff] [blame] | 223 | ### <a name="webrtc-gn-templates"></a>WebRTC-specific GN templates |
Karl Wiberg | 91d0ab7 | 2017-09-07 15:05:31 | [diff] [blame] | 224 | |
Mirko Bonadei | ab397dd | 2022-12-16 10:08:01 | [diff] [blame] | 225 | As shown in the table below, for library targets (`source_set` and |
| 226 | `static_library`), you should default on using `rtc_library` (which abstracts |
| 227 | away the complexity of using the correct target type for Chromium component |
| 228 | builds). |
Karl Wiberg | 91d0ab7 | 2017-09-07 15:05:31 | [diff] [blame] | 229 | |
Mirko Bonadei | ab397dd | 2022-12-16 10:08:01 | [diff] [blame] | 230 | The general rule is for library targets is: |
| 231 | 1. Use `rtc_library`. |
| 232 | 2. If the library is a header only target use `rtc_source_set`. |
| 233 | 3. If you really need to generate a static library, use `rtc_static_library` |
| 234 | (same for shared libraries, in such case use `rtc_shared_library`). |
| 235 | |
| 236 | To ensure that all our [GN targets][gn-target] are built with the same |
| 237 | configuration, only use the following [GN templates][gn-templ]. |
| 238 | |
| 239 | | instead of | use | |
| 240 | |------------------|-----------------------------------------------------------------------------------------| |
| 241 | | `executable` | `rtc_executable` | |
| 242 | | `shared_library` | `rtc_shared_library` | |
| 243 | | `source_set` | `rtc_source_set` (only for header only libraries, for everything else use `rtc_library` | |
| 244 | | `static_library` | `rtc_static_library` (use `rtc_library` unless you really need `rtc_static_library` | |
| 245 | | `test` | `rtc_test` | |
Karl Wiberg | 91d0ab7 | 2017-09-07 15:05:31 | [diff] [blame] | 246 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 247 | |
| 248 | [gn-templ]: https://gn.googlesource.com/gn/+/HEAD/docs/language.md#Templates |
| 249 | [gn-target]: https://gn.googlesource.com/gn/+/HEAD/docs/language.md#Targets |
Karl Wiberg | 91d0ab7 | 2017-09-07 15:05:31 | [diff] [blame] | 250 | |
Per Kjellander | a7f2d84 | 2018-01-10 15:54:53 | [diff] [blame] | 251 | ### Target visibility and the native API |
| 252 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 253 | The [WebRTC-specific GN templates](#webrtc-gn-templates) declare build targets |
| 254 | whose default `visibility` allows all other targets in the WebRTC tree (and no |
| 255 | targets outside the tree) to depend on them. |
Per Kjellander | a7f2d84 | 2018-01-10 15:54:53 | [diff] [blame] | 256 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 257 | Prefer to restrict the `visibility` if possible: |
Per Kjellander | a7f2d84 | 2018-01-10 15:54:53 | [diff] [blame] | 258 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 259 | * If a target is used by only one or a tiny number of other targets, prefer to |
| 260 | list them explicitly: `visibility = [ ":foo", ":bar" ]` |
Per Kjellander | a7f2d84 | 2018-01-10 15:54:53 | [diff] [blame] | 261 | * If a target is used only by targets in the same `BUILD.gn` file: |
| 262 | `visibility = [ ":*" ]`. |
| 263 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 264 | Setting `visibility = [ "*" ]` means that targets outside the WebRTC tree can |
| 265 | depend on this target; use this only for build targets whose headers are part of |
| 266 | the [native WebRTC API](native-api.md). |
Per Kjellander | a7f2d84 | 2018-01-10 15:54:53 | [diff] [blame] | 267 | |
Karl Wiberg | ee3d760 | 2017-09-04 12:46:47 | [diff] [blame] | 268 | ### Conditional compilation with the C preprocessor |
| 269 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 270 | Avoid using the C preprocessor to conditionally enable or disable pieces of |
| 271 | code. But if you can't avoid it, introduce a GN variable, and then set a |
| 272 | preprocessor constant to either 0 or 1 in the build targets that need it: |
Karl Wiberg | ee3d760 | 2017-09-04 12:46:47 | [diff] [blame] | 273 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 274 | ```gn |
Karl Wiberg | ee3d760 | 2017-09-04 12:46:47 | [diff] [blame] | 275 | if (apm_debug_dump) { |
| 276 | defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ] |
| 277 | } else { |
| 278 | defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ] |
| 279 | } |
| 280 | ``` |
| 281 | |
| 282 | In the C, C++, or Objective-C files, use `#if` when testing the flag, |
| 283 | not `#ifdef` or `#if defined()`: |
| 284 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 285 | ```c |
Karl Wiberg | ee3d760 | 2017-09-04 12:46:47 | [diff] [blame] | 286 | #if WEBRTC_APM_DEBUG_DUMP |
| 287 | // One way. |
| 288 | #else |
| 289 | // Or another. |
| 290 | #endif |
| 291 | ``` |
| 292 | |
Fanny Linderborg | 70efbb8 | 2021-04-23 14:59:29 | [diff] [blame] | 293 | When combined with the `-Wundef` compiler option, this produces compile time |
| 294 | warnings if preprocessor symbols are misspelled, or used without corresponding |
| 295 | build rules to set them. |