2023-01-30 10:51:01 +00:00
|
|
|
<!-- go/cmark -->
|
2024-10-22 13:18:14 +02:00
|
|
|
<!--* freshness: {owner: 'danilchap' reviewed: '2024-10-22'} *-->
|
2021-05-12 14:05:48 +02:00
|
|
|
|
2023-01-30 10:51:01 +00:00
|
|
|
# WebRTC coding style guide
|
2023-01-26 20:18:46 +00:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## General advice
|
2017-09-08 15:07:15 +02:00
|
|
|
|
|
|
|
|
Some older parts of the code violate the style guide in various ways.
|
2022-01-17 16:11:02 +01:00
|
|
|
If making large changes to such code, consider first cleaning it up in a
|
2021-04-23 14:59:29 +00:00
|
|
|
separate CL.
|
2017-09-08 15:07:15 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## C++
|
2017-09-03 05:02:12 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
WebRTC follows the [Chromium C++ style guide][chr-style] and the
|
|
|
|
|
[Google C++ style guide][goog-style]. In cases where they conflict, the Chromium
|
|
|
|
|
style guide trumps the Google style guide, and the rules in this file trump them
|
2024-04-17 15:01:40 +02:00
|
|
|
both. In addition to style guides it is recommended to follow
|
|
|
|
|
[best practices][goog-best-practice] when applicable.
|
2017-09-03 05:02:12 +02:00
|
|
|
|
2021-07-14 14:02:11 +00:00
|
|
|
[chr-style]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md
|
2017-09-03 05:02:12 +02:00
|
|
|
[goog-style]: https://google.github.io/styleguide/cppguide.html
|
2024-04-17 15:01:40 +02:00
|
|
|
[goog-best-practice]: https://abseil.io/tips
|
2017-09-03 05:02:12 +02:00
|
|
|
|
2018-03-15 11:21:05 +01:00
|
|
|
### C++ version
|
|
|
|
|
|
2021-12-24 09:55:22 +01:00
|
|
|
WebRTC is written in C++17, but with some restrictions:
|
2018-03-15 11:21:05 +01:00
|
|
|
|
2021-12-24 09:55:22 +01:00
|
|
|
* We only allow the subset of C++17 (language and library) that is not banned by
|
2021-09-16 19:11:56 -04:00
|
|
|
Chromium; see the [list of banned C++ features in Chromium][chr-style-cpp].
|
2021-12-24 09:55:22 +01:00
|
|
|
* We only allow the subset of C++17 that is also valid C++20; otherwise, users
|
|
|
|
|
would not be able to compile WebRTC in C++20 mode.
|
2018-03-15 11:21:05 +01:00
|
|
|
|
2022-01-24 13:58:30 +01:00
|
|
|
[chr-style-cpp]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md
|
2018-03-15 11:21:05 +01:00
|
|
|
|
2018-08-27 04:26:18 +02:00
|
|
|
### Abseil
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
You may use a subset of the utilities provided by the [Abseil][abseil] library
|
|
|
|
|
when writing WebRTC C++ code; see the
|
|
|
|
|
[instructions on how to use Abseil in WebRTC](abseil-in-webrtc.md).
|
2018-08-27 04:26:18 +02:00
|
|
|
|
|
|
|
|
[abseil]: https://abseil.io/about/
|
|
|
|
|
|
2018-03-14 12:23:12 +01:00
|
|
|
### <a name="h-cc-pairs"></a>`.h` and `.cc` files come in pairs
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
`.h` and `.cc` files should come in pairs, with the same name (except for the
|
|
|
|
|
file type suffix), in the same directory, in the same build target.
|
2018-03-14 12:23:12 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
* If a declaration in `path/to/foo.h` has a definition in some `.cc` file, it
|
|
|
|
|
should be in `path/to/foo.cc`.
|
|
|
|
|
* If a definition in `path/to/foo.cc` file has a declaration in some `.h` file,
|
|
|
|
|
it should be in `path/to/foo.h`.
|
|
|
|
|
* Omit the `.cc` file if it would have been empty, but still list the `.h` file
|
|
|
|
|
in a build target.
|
|
|
|
|
* Omit the `.h` file if it would have been empty. (This can happen with unit
|
|
|
|
|
test `.cc` files, and with `.cc` files that define `main`.)
|
2018-03-14 12:23:12 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
See also the
|
2024-05-16 13:38:08 +00:00
|
|
|
[examples and exceptions on how to treat `.h` and `.cc` files](style-guide/h-cc-pairs.md).
|
2018-03-14 12:23:12 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
This makes the source code easier to navigate and organize, and precludes some
|
|
|
|
|
questionable build system practices such as having build targets that don't pull
|
|
|
|
|
in definitions for everything they declare.
|
2018-03-14 12:23:12 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
### `TODO` comments
|
2019-12-11 12:45:08 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Follow the [Google styleguide for `TODO` comments][goog-style-todo]. When
|
2022-07-01 10:00:11 +02:00
|
|
|
referencing a WebRTC bug, prefer using the URL form (excluding the scheme part):
|
2021-04-23 14:59:29 +00:00
|
|
|
|
|
|
|
|
```cpp
|
2023-11-21 22:09:06 +00:00
|
|
|
// TODO: bugs.webrtc.org/12345 - Delete the hack when blocking bugs are resolved.
|
2019-12-11 12:45:08 +01:00
|
|
|
```
|
|
|
|
|
|
2022-07-01 10:00:11 +02:00
|
|
|
The short form used in commit messages, e.g. `webrtc:12345`, is discouraged.
|
|
|
|
|
|
2019-12-11 12:45:08 +01:00
|
|
|
[goog-style-todo]: https://google.github.io/styleguide/cppguide.html#TODO_Comments
|
|
|
|
|
|
2021-02-22 14:31:26 +01:00
|
|
|
### Deprecation
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Annotate the declarations of deprecated functions and classes with the
|
2022-04-27 08:59:32 +00:00
|
|
|
[`[[deprecated]]` attribute][DEPRECATED] to cause an error when they're used
|
2021-04-23 14:59:29 +00:00
|
|
|
inside WebRTC and a compiler warning when they're used by dependant projects.
|
|
|
|
|
Like so:
|
2021-02-22 14:31:26 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
```cpp
|
2022-04-27 08:59:32 +00:00
|
|
|
[[deprecated("bugs.webrtc.org/12345")]]
|
2021-02-22 14:31:26 +01:00
|
|
|
std::pony PonyPlz(const std::pony_spec& ps);
|
|
|
|
|
```
|
|
|
|
|
|
2024-04-17 15:01:40 +02:00
|
|
|
Prefer [ABSL_DEPRECATE_AND_INLINE] to deprecate an inline function definition
|
|
|
|
|
or a type alias. This macro allows to automate inlining the functions's body or
|
|
|
|
|
replacing the type where it is used downstream. e.g.,
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
ABSL_DEPRECATE_AND_INLINE() inline int OldFunc(int x) {
|
|
|
|
|
return NewFunc(x, 0);
|
|
|
|
|
}
|
|
|
|
|
using OldTypeName ABSL_DEPRECATE_AND_INLINE() = NewTypeName;
|
|
|
|
|
```
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
NOTE 1: The annotation goes on the declaration in the `.h` file, not the
|
|
|
|
|
definition in the `.cc` file!
|
2021-02-22 14:31:26 +01:00
|
|
|
|
|
|
|
|
NOTE 2: In order to have unit tests that use the deprecated function without
|
|
|
|
|
getting errors, do something like this:
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
```cpp
|
2021-02-22 14:31:26 +01:00
|
|
|
std::pony DEPRECATED_PonyPlz(const std::pony_spec& ps);
|
2022-04-27 08:59:32 +00:00
|
|
|
[[deprecated("bugs.webrtc.org/12345")]]
|
2021-02-22 14:31:26 +01:00
|
|
|
inline std::pony PonyPlz(const std::pony_spec& ps) {
|
|
|
|
|
return DEPRECATED_PonyPlz(ps);
|
|
|
|
|
}
|
|
|
|
|
```
|
2024-11-26 10:31:30 -08:00
|
|
|
or wrap the test with
|
2025-01-24 10:25:56 +01:00
|
|
|
|
2024-11-26 10:31:30 -08:00
|
|
|
```cpp
|
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
TEST_...
|
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
```
|
2021-02-22 14:31:26 +01:00
|
|
|
|
|
|
|
|
In other words, rename the existing function, and provide an inline wrapper
|
|
|
|
|
using the original name that calls it. That way, callers who are willing to
|
2021-04-23 14:59:29 +00:00
|
|
|
call it using the `DEPRECATED_`-prefixed name don't get the warning.
|
2021-02-22 14:31:26 +01:00
|
|
|
|
2022-04-27 08:59:32 +00:00
|
|
|
NOTE 3: Occasionally, with long descriptions, `git cl format` will do the wrong
|
|
|
|
|
thing with the attribute. In that case, you can use the
|
|
|
|
|
[`ABSL_DEPRECATED` macro][ABSL_DEPRECATED], which is formatted in a more
|
|
|
|
|
readable way.
|
|
|
|
|
|
|
|
|
|
[DEPRECATED]: https://en.cppreference.com/w/cpp/language/attributes/deprecated
|
2021-07-22 15:40:44 +00:00
|
|
|
[ABSL_DEPRECATED]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/attributes.h?q=ABSL_DEPRECATED
|
2024-04-17 15:01:40 +02:00
|
|
|
[ABSL_DEPRECATE_AND_INLINE]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/abseil-cpp/absl/base/macros.h?q=ABSL_DEPRECATE_AND_INLINE
|
2021-02-22 14:31:26 +01:00
|
|
|
|
2017-09-06 11:10:23 +02:00
|
|
|
### ArrayView
|
2017-09-03 05:02:12 +02:00
|
|
|
|
2017-09-06 11:10:23 +02:00
|
|
|
When passing an array of values to a function, use `rtc::ArrayView`
|
2021-04-23 14:59:29 +00:00
|
|
|
whenever possible—that is, whenever you're not passing ownership of
|
|
|
|
|
the array, and don't allow the callee to change the array size.
|
2017-09-06 11:10:23 +02:00
|
|
|
|
|
|
|
|
For example,
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
| instead of | use |
|
|
|
|
|
|-------------------------------------|----------------------|
|
|
|
|
|
| `const std::vector<T>&` | `ArrayView<const T>` |
|
|
|
|
|
| `const T* ptr, size_t num_elements` | `ArrayView<const T>` |
|
|
|
|
|
| `T* ptr, size_t num_elements` | `ArrayView<T>` |
|
2017-09-06 11:10:23 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
See the [source code for `rtc::ArrayView`](api/array_view.h) for more detailed
|
|
|
|
|
docs.
|
2017-09-04 11:27:33 +02:00
|
|
|
|
2023-10-30 12:44:35 +00:00
|
|
|
### Strings
|
|
|
|
|
|
|
|
|
|
WebRTC uses std::string, with content assumed to be UTF-8. Note that this
|
|
|
|
|
has to be verified whenever accepting external input.
|
|
|
|
|
|
2023-11-15 11:04:48 +01:00
|
|
|
For concatenation of strings, use webrtc::StrJoin or rtc::SimpleStringBuilder
|
|
|
|
|
directly.
|
2023-10-30 12:44:35 +00:00
|
|
|
|
|
|
|
|
The following string building tools are NOT recommended:
|
|
|
|
|
* The + operator. See https://abseil.io/tips/3 for why not.
|
|
|
|
|
* absl::StrCat, absl::StrAppend, absl::StrJoin. These are optimized for
|
|
|
|
|
speed, not code size, and have significant code size overhead.
|
|
|
|
|
* strcat. It is too easy to create buffer overflows.
|
|
|
|
|
|
2017-12-08 11:42:15 -08:00
|
|
|
### sigslot
|
|
|
|
|
|
2021-04-19 13:28:21 +00:00
|
|
|
SIGSLOT IS DEPRECATED.
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Prefer `webrtc::CallbackList`, and manage thread safety yourself.
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2021-04-19 13:28:21 +00:00
|
|
|
### Smart pointers
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2021-04-19 13:28:21 +00:00
|
|
|
The following smart pointer types are recommended:
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
* `std::unique_ptr` for all singly-owned objects
|
2024-10-22 13:18:14 +02:00
|
|
|
* `webrtc::scoped_refptr` for all objects with shared ownership
|
2021-04-23 14:59:29 +00:00
|
|
|
|
|
|
|
|
Use of `std::shared_ptr` is *not permitted*. It is banned in the Chromium style
|
2022-01-17 16:11:02 +01:00
|
|
|
guide (overriding the Google style guide). See the
|
2021-04-23 14:59:29 +00:00
|
|
|
[list of banned C++ library features in Chromium][chr-std-shared-ptr] for more
|
|
|
|
|
information.
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
In most cases, one will want to explicitly control lifetimes, and therefore use
|
|
|
|
|
`std::unique_ptr`, but in some cases, for instance where references have to
|
|
|
|
|
exist both from the API users and internally, with no way to invalidate pointers
|
2024-10-22 13:18:14 +02:00
|
|
|
held by the API user, `scoped_refptr` can be appropriate.
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2022-01-24 13:58:30 +01:00
|
|
|
[chr-std-shared-ptr]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md#shared-pointers-banned
|
2017-12-08 11:42:15 -08:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
### `std::bind`
|
2018-03-19 23:27:35 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Don't use `std::bind`—there are pitfalls, and lambdas are almost as succinct and
|
2024-04-17 15:01:40 +02:00
|
|
|
already familiar to modern C++ programmers. See [Avoid std::bind][totw-108] for more.
|
|
|
|
|
|
|
|
|
|
[totw-108]: https://abseil.io/tips/108
|
2018-03-19 23:27:35 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
### `std::function`
|
2018-03-19 23:27:35 +01:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
`std::function` is allowed, but remember that it's not the right tool for every
|
|
|
|
|
occasion. Prefer to use interfaces when that makes sense, and consider
|
|
|
|
|
`rtc::FunctionView` for cases where the callee will not save the function
|
2024-10-22 13:18:14 +02:00
|
|
|
object. Prefer `absl::AnyInvocable` over `std::function` when you can accomplish
|
|
|
|
|
the task by moving the callable instead of copying it.
|
2018-03-19 23:27:35 +01:00
|
|
|
|
2017-10-12 11:22:48 -07:00
|
|
|
### Forward declarations
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
WebRTC follows the
|
|
|
|
|
[Google C++ style guide on forward declarations][goog-forward-declarations].
|
|
|
|
|
In summary: avoid using forward declarations where possible; just `#include` the
|
|
|
|
|
headers you need.
|
2017-10-12 11:22:48 -07:00
|
|
|
|
|
|
|
|
[goog-forward-declarations]: https://google.github.io/styleguide/cppguide.html#Forward_Declarations
|
|
|
|
|
|
2022-11-04 12:13:04 +00:00
|
|
|
### RTTI and dynamic_cast
|
|
|
|
|
|
|
|
|
|
The Google style guide [permits the use of dynamic_cast](https://google.github.io/styleguide/cppguide.html#Run-Time_Type_Information__RTTI_).
|
|
|
|
|
|
|
|
|
|
However, WebRTC does not permit it. WebRTC (and Chrome) is compiled with the
|
|
|
|
|
-fno-rtti flag, and the overhead of enabling RTTI it is on the order of 220
|
|
|
|
|
Kbytes (for Android Arm64).
|
|
|
|
|
|
|
|
|
|
Use static_cast and take your own steps to ensure type safety.
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## C
|
2017-09-04 11:27:33 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
There's a substantial chunk of legacy C code in WebRTC, and a lot of it is old
|
|
|
|
|
enough that it violates the parts of the C++ style guide that also applies to C
|
|
|
|
|
(naming etc.) for the simple reason that it pre-dates the use of the current C++
|
2022-01-17 16:11:02 +01:00
|
|
|
style guide for this code base. If making large changes to C code, consider
|
|
|
|
|
converting the whole thing to C++ first.
|
2017-09-04 14:46:47 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## Java
|
2017-09-08 15:07:15 +02:00
|
|
|
|
|
|
|
|
WebRTC follows the [Google Java style guide][goog-java-style].
|
|
|
|
|
|
|
|
|
|
[goog-java-style]: https://google.github.io/styleguide/javaguide.html
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## Objective-C and Objective-C++
|
2017-09-08 15:07:15 +02:00
|
|
|
|
|
|
|
|
WebRTC follows the
|
|
|
|
|
[Chromium Objective-C and Objective-C++ style guide][chr-objc-style].
|
|
|
|
|
|
2021-07-14 14:02:11 +00:00
|
|
|
[chr-objc-style]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/objective-c/objective-c.md
|
2017-09-08 15:07:15 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## Python
|
2017-09-08 15:07:15 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
WebRTC follows [Chromium's Python style][chr-py-style].
|
2017-09-08 15:07:15 +02:00
|
|
|
|
2024-07-29 15:41:08 +02:00
|
|
|
Chromium's Python style is now using PEP-8 and not all Python code has been migrated.
|
|
|
|
|
For this reason running presubmit on old WebRTC python script might trigger failures.
|
|
|
|
|
The failures can either be fixed are ignored by adding the script to the [PYLINT_OLD_STYLE][old-style-lint] list.
|
|
|
|
|
|
2021-07-14 14:02:11 +00:00
|
|
|
[chr-py-style]: https://chromium.googlesource.com/chromium/src/+/main/styleguide/python/python.md
|
2024-07-29 15:41:08 +02:00
|
|
|
[old-style-lint]: https://source.chromium.org/chromium/_/webrtc/src/+/9b81d2c954128831c62d8a0657c7f955b3c02d32:PRESUBMIT.py;l=50
|
2017-09-08 15:07:15 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
## Build files
|
2017-09-04 14:46:47 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
The WebRTC build files are written in [GN][gn], and we follow the
|
|
|
|
|
[GN style guide][gn-style]. Additionally, there are some
|
|
|
|
|
WebRTC-specific rules below; in case of conflict, they trump the Chromium style
|
|
|
|
|
guide.
|
2017-09-07 17:05:31 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
[gn]: https://gn.googlesource.com/gn/
|
|
|
|
|
[gn-style]: https://gn.googlesource.com/gn/+/HEAD/docs/style_guide.md
|
2017-09-07 17:05:31 +02:00
|
|
|
|
2018-01-10 15:54:53 +00:00
|
|
|
### <a name="webrtc-gn-templates"></a>WebRTC-specific GN templates
|
2017-09-07 17:05:31 +02:00
|
|
|
|
2022-12-16 11:08:01 +01:00
|
|
|
As shown in the table below, for library targets (`source_set` and
|
|
|
|
|
`static_library`), you should default on using `rtc_library` (which abstracts
|
|
|
|
|
away the complexity of using the correct target type for Chromium component
|
|
|
|
|
builds).
|
|
|
|
|
|
|
|
|
|
The general rule is for library targets is:
|
|
|
|
|
1. Use `rtc_library`.
|
|
|
|
|
2. If the library is a header only target use `rtc_source_set`.
|
|
|
|
|
3. If you really need to generate a static library, use `rtc_static_library`
|
|
|
|
|
(same for shared libraries, in such case use `rtc_shared_library`).
|
|
|
|
|
|
|
|
|
|
To ensure that all our [GN targets][gn-target] are built with the same
|
|
|
|
|
configuration, only use the following [GN templates][gn-templ].
|
|
|
|
|
|
|
|
|
|
| instead of | use |
|
|
|
|
|
|------------------|-----------------------------------------------------------------------------------------|
|
|
|
|
|
| `executable` | `rtc_executable` |
|
|
|
|
|
| `shared_library` | `rtc_shared_library` |
|
|
|
|
|
| `source_set` | `rtc_source_set` (only for header only libraries, for everything else use `rtc_library` |
|
|
|
|
|
| `static_library` | `rtc_static_library` (use `rtc_library` unless you really need `rtc_static_library` |
|
|
|
|
|
| `test` | `rtc_test` |
|
2017-09-07 17:05:31 +02:00
|
|
|
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
[gn-templ]: https://gn.googlesource.com/gn/+/HEAD/docs/language.md#Templates
|
|
|
|
|
[gn-target]: https://gn.googlesource.com/gn/+/HEAD/docs/language.md#Targets
|
2017-09-07 17:05:31 +02:00
|
|
|
|
2018-01-10 15:54:53 +00:00
|
|
|
### Target visibility and the native API
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
The [WebRTC-specific GN templates](#webrtc-gn-templates) declare build targets
|
|
|
|
|
whose default `visibility` allows all other targets in the WebRTC tree (and no
|
|
|
|
|
targets outside the tree) to depend on them.
|
2018-01-10 15:54:53 +00:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Prefer to restrict the `visibility` if possible:
|
2018-01-10 15:54:53 +00:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
* If a target is used by only one or a tiny number of other targets, prefer to
|
|
|
|
|
list them explicitly: `visibility = [ ":foo", ":bar" ]`
|
2018-01-10 15:54:53 +00:00
|
|
|
* If a target is used only by targets in the same `BUILD.gn` file:
|
|
|
|
|
`visibility = [ ":*" ]`.
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Setting `visibility = [ "*" ]` means that targets outside the WebRTC tree can
|
|
|
|
|
depend on this target; use this only for build targets whose headers are part of
|
|
|
|
|
the [native WebRTC API](native-api.md).
|
2018-01-10 15:54:53 +00:00
|
|
|
|
2017-09-04 14:46:47 +02:00
|
|
|
### Conditional compilation with the C preprocessor
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
Avoid using the C preprocessor to conditionally enable or disable pieces of
|
|
|
|
|
code. But if you can't avoid it, introduce a GN variable, and then set a
|
|
|
|
|
preprocessor constant to either 0 or 1 in the build targets that need it:
|
2017-09-04 14:46:47 +02:00
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
```gn
|
2017-09-04 14:46:47 +02:00
|
|
|
if (apm_debug_dump) {
|
|
|
|
|
defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
|
|
|
|
|
} else {
|
|
|
|
|
defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In the C, C++, or Objective-C files, use `#if` when testing the flag,
|
|
|
|
|
not `#ifdef` or `#if defined()`:
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
```c
|
2017-09-04 14:46:47 +02:00
|
|
|
#if WEBRTC_APM_DEBUG_DUMP
|
|
|
|
|
// One way.
|
|
|
|
|
#else
|
|
|
|
|
// Or another.
|
|
|
|
|
#endif
|
|
|
|
|
```
|
|
|
|
|
|
2021-04-23 14:59:29 +00:00
|
|
|
When combined with the `-Wundef` compiler option, this produces compile time
|
|
|
|
|
warnings if preprocessor symbols are misspelled, or used without corresponding
|
|
|
|
|
build rules to set them.
|