blob: ad6db897b3533708fc06585a05ca2cddcf549c72 [file] [log] [blame] [view]
Harry Cutts44615192022-04-21 22:39:141# ChromiumOS Development Basics
Daniel Erat0576ce72017-03-27 05:43:062
Harry Cutts44615192022-04-21 22:39:143This document covers guidelines to keep in mind when working on the ChromiumOS
Daniel Erat0576ce72017-03-27 05:43:064project.
5
6[TOC]
7
8## Documentation
9
10### General guidelines
11
Harry Cutts44615192022-04-21 22:39:1412ChromiumOS follows the Chromium project's [documentation guidelines] and
Daniel Erat0576ce72017-03-27 05:43:0613[documentation best practices].
14
15### Design documents
16
17Design documents typically describe the problem that you're trying to solve,
18different approaches that you considered, and the path that you're planning to
19take. They're useful for soliciting opinions from others before you start on the
20implementation, but they also help your code reviewers get their bearings and
21can answer other developers' "why was this done like that?" questions after the
22work is complete (even after you've forgotten the reasons why!). On top of all
23that, a design doc often helps you solidify a design in your own mind and
24convince yourself that you're not missing anything.
25
26Most non-trivial additions of new functionality, and particularly ones that
27require adding new daemons or new interactions between different processes,
28should have their own design docs. For smaller changes, documenting what you're
29doing and why in the issue tracker is often enough.
30
31Share your design docs with your team mailing list, if not with the full
Harry Cutts44615192022-04-21 22:39:1432[chromium-os-dev] mailing list. See the existing [ChromiumOS design docs] and
Daniel Erat0576ce72017-03-27 05:43:0633[Chromium design docs] for inspiration.
34
35## Programming languages and style
36
Zach Reiznercdfcbd32021-04-21 06:21:5437### Rust
38
Harry Cutts44615192022-04-21 22:39:1439The newest userspace code in ChromiumOS is usually written in Rust to take
Zach Reiznercdfcbd32021-04-21 06:21:5440advantage of its improved security and ergonomics. Being a memory safe language
41with a runtime overhead similar to C/C++ makes it uniquely suited for new code
42with reduced incidence of security and stability bugs.
43
Harry Cutts44615192022-04-21 22:39:1444The Rust style guide is currently in development by the ChromiumOS Authors, but
Zach Reiznercdfcbd32021-04-21 06:21:5445it basically follows that of Rust's default rustfmt configuration. Additionally
46clippy is used a linter, usually with certain lints suppressed. For an example
47of what lints might be disabled, look at the [set from crosvm]. The [Rust on
Harry Cutts44615192022-04-21 22:39:1448ChromeOS] document has more information.
Zach Reiznercdfcbd32021-04-21 06:21:5449
50In certain cases C++ is the better choice for userspace code. In particular,
51existing C++ projects should not be converted to Rust, except in exceptional
52circumstances. However, new modules to existing C++ projects have been
53successfully written in Rust. Other cases where C++ should be considered are
54projects reliant on Mojo, which has no production ready Rust bindings.
55
Daniel Erat0576ce72017-03-27 05:43:0656### C++
57
Harry Cutts44615192022-04-21 22:39:1458Most userspace code in the ChromiumOS project is written in C++, whether it's
Zach Reiznercdfcbd32021-04-21 06:21:5459part of the Chrome browser itself or a system daemon exposing new functionality.
Daniel Erat0576ce72017-03-27 05:43:0660
Harry Cutts44615192022-04-21 22:39:1461The Chromium project, and by extension the ChromiumOS project, follow the
Daniel Erat0576ce72017-03-27 05:43:0662[Google C++ style guide], with the [Chromium C++ style guide] layered on top to
Grace Chamf22a5912022-06-07 08:04:2863provide additional guidelines and clarify ambiguities.
64
65The [Modern C++ use in Chromium] document lists which of the many new features
66introduced in the C++11, 14, and 17 standard are allowed. ChromiumOS follows the
Ian Barkley-Yeung9b9a2332023-09-28 21:00:0267list except:
68
691. std::optional. It is banned on Chromium due to its undefined
70 behavior when a std::nullopt is dereferenced. However, ChromiumOS C++ is
71 hardened and will crash in such cases. Use std::optional instead of
72 absl::optional.
732. C++20. Chromium does not allow C++20 features at all. ChromiumOS
74 supports C++20 and developers may use C++20 features.
Daniel Erat0576ce72017-03-27 05:43:0675
76New C++ programs should go into new directories in the [platform2 repository],
77which is checked out to `src/platform2`.
78
79### C
80
Harry Cutts44615192022-04-21 22:39:1481C should only be used for code that is part of the Linux kernel or ChromeOS
Daniel Erat0576ce72017-03-27 05:43:0682firmware.
83
84Both kernel and firmware code should follow the [Linux kernel coding style]. The
Harry Cutts44615192022-04-21 22:39:1485[Kernel Development] guide has more details about ChromeOS kernel development, and the
Daniel Erat0576ce72017-03-27 05:43:0686[EC Development page] discusses firmware.
87
Clayton Whitelaw21a73a62021-12-01 18:23:5888Usage of C in new first-party userspace projects is strongly discouraged. Unless
Harry Cutts44615192022-04-21 22:39:1489there is a critical, external to ChromeOS reason to write a new userspace
Clayton Whitelaw21a73a62021-12-01 18:23:5890project in C, usage of C will be disallowed.
91
92`platform` and `platform2` OWNERS are encouraged to reject new C code in
Harry Cutts44615192022-04-21 22:39:1493first-party userspace ChromeOS components.
Clayton Whitelaw21a73a62021-12-01 18:23:5894
Daniel Erat0576ce72017-03-27 05:43:0695### Shell
96
97Sometimes shell scripting can be the best way to perform lightweight,
Harry Cutts44615192022-04-21 22:39:1498non-persistent tasks that need to run periodically on ChromeOS systems, but
Daniel Erat0576ce72017-03-27 05:43:0699there are also big downsides: it's much more difficult to write tests for shell
100scripts than for C++ code, and scripts have a tendency to grow to the point
101where they become difficult to maintain. If you're planning to add a shell
102script that is likely to grow in complexity, consider instead using C++ from the
103start to save yourself the trouble of rewriting it down the road.
104
Harry Cutts44615192022-04-21 22:39:14105Shell scripts are mainly used for a few categories of tasks in ChromeOS:
Daniel Erat0576ce72017-03-27 05:43:06106
107* Upstart initialization scripts, as in [src/platform2/init]. See the
108 [init STYLE file] and [init README file] for guidelines.
Daniel Erate3b67cd2017-03-29 00:22:42109* Portage `.ebuild` files, as in the [chromiumos-overlay repository]. We
110 follow the upstream guidelines; see the [Ebuild Writing] page and
111 specifically the [Ebuild File Format].
Daniel Erat0576ce72017-03-27 05:43:06112* Miscellaneous development-related tasks
113
Mike Frysinger8a96b0f2023-11-20 06:27:48114Read the [ChromiumOS shell style guidelines] before writing scripts, with the
115caveat that the Upstart or Portage guidelines take precedence when writing
116those types of scripts.
Daniel Erat0576ce72017-03-27 05:43:06117
Mattias Nissler32b6aab2017-04-06 12:28:11118For shell scripts that ship with the OS image, be extra careful. The shell
119provides powerful features, but the flip side is that security pitfalls are
120tricky to avoid. Think twice whether your shell statements can have unintended
121side effects, in particular if your script runs with full privileges (as is the
122case with init scripts). As a guideline, keep things simple and move more
123complex processing to a properly sandboxed environment in an C++ daemon.
124
Daniel Erat0576ce72017-03-27 05:43:06125### Python
126
Harry Cutts44615192022-04-21 22:39:14127The Python interpreter is not included in production ChromeOS system images,
Daniel Erat0576ce72017-03-27 05:43:06128but Python is used heavily for development and testing.
129
130We largely follow the [Google Python style guide], but see the
Harry Cutts44615192022-04-21 22:39:14131[ChromiumOS Python style guidelines] for important differences, particularly
Daniel Erate3b67cd2017-03-29 00:22:42132around indenting and naming. For tests, see the [autotest coding style].
Daniel Erat0576ce72017-03-27 05:43:06133
134## Testing
135
Harry Cutts44615192022-04-21 22:39:14136The [ChromiumOS testing site] is the main repository of information about
Daniel Erat03138d42017-03-29 15:08:01137testing.
138
Daniel Erat0576ce72017-03-27 05:43:06139### Unit tests
140
141Unit tests should be added alongside new code. It's important to design your
142code with testability in mind, as adding tests after-the-fact often requires
143heavy refactoring.
144
145Good unit tests are fast, lightweight, reliable, and easy to run within the
146chroot as part of your development workflow. We use [Google Test] (which is
Daniel Erate3b67cd2017-03-29 00:22:42147comprised of the GoogleTest unit testing framework and the GoogleMock mocking
148framework) to test C++ code. [Why Google C++ Testing Framework?] and the
149[Google Test FAQ] are good introductions, and the [unit testing document] has
150more details about how unit tests get run.
Daniel Erat0576ce72017-03-27 05:43:06151
Harry Cutts44615192022-04-21 22:39:14152See the [Best practices for writing ChromeOS unit tests] document for more
Daniel Erate5228332017-08-30 00:17:07153guidance on writing good unit tests.
Daniel Erat0576ce72017-03-27 05:43:06154
155### Autotest
156
Harry Cutts44615192022-04-21 22:39:14157[Autotest] is used to run tests on live ChromeOS systems. Autotests are useful
Daniel Erat0576ce72017-03-27 05:43:06158for performing integration testing (e.g. verifying that two processes are able
159to communicate with each other over IPC), but they have heavy costs:
160
161* Autotests are harder to run than unit tests: you need either a dedicated
162 test device or a virtual machine.
163* Autotests are much slower than unit tests: even a no-op test can take 30
164 seconds or longer per run.
165* Since autotests involve at least a controlling system and a test device,
166 they're susceptible to networking issues and hardware flakiness.
167* Since autotests run on full, live systems, failures can be caused by issues
168 in components unrelated to the one that you're trying to test.
169
170Whenever you can get equivalent coverage from either unit tests or autotests,
171use unit tests. Design your system with unit testing in mind.
172
173## Code reviews
174
Harry Cutts44615192022-04-21 22:39:14175The ChromiumOS project follows the [Chromium code review policy]: all code and
Daniel Erat0576ce72017-03-27 05:43:06176data changes must be reviewed by another project member before being committed.
Harry Cutts44615192022-04-21 22:39:14177Note that ChromiumOS's review process has some differences; see the
Daniel Erat0576ce72017-03-27 05:43:06178[Developer Guide's code review instructions].
179
Harry Cutts44615192022-04-21 22:39:14180OWNERS files are not (yet) enforced for non-browser parts of the ChromiumOS
Daniel Erat0576ce72017-03-27 05:43:06181codebase, but please act as if they were. If there's an OWNERS file in the
182directory that you're modifying or a parent directory, add at least one
183developer that's listed in it to your code review and wait for their approval
184before committing your change.
185
186Owners may want to consider setting up notifications for changes to their code.
187To receive notifications of changes to `src/platform2/debugd`, open your
188[Gerrit notification settings] and add a new entry for project
189`chromiumos/platform2` and expression `file:"^debugd/.*"`.
190
191## Issue trackers
192
Harry Cutts44615192022-04-21 22:39:14193Public ChromiumOS bugs and feature requests are tracked using the
Daniel Erat0576ce72017-03-27 05:43:06194[chromium issue tracker]. Note that this tracker is shared with the Chromium
195project; most OS-specific issues are classified under an `OS>`-prefixed
196component and have an `OS=Chrome` label. The `crbug.com` redirector makes it
197easy to jump directly to an issue with a given ID; `https://crbug.com/123` will
198redirect to issue #123, for instance.
199
200Keep discussion in the issue tracker instead of in email or over IM. Issues
201remain permanently available and are viewable by people not present for the
202original discussion; email and IM exchanges are harder to find after the fact
203and may even be deleted automatically. `BUG=chromium:123` lines in commit
204descriptions and `https://crbug.com/123` mentions in code comments also make it
205easy to link back to the issue that describes the original motivation for a
206change.
207
208Avoid discussing multiple problems in a single issue. It's not possible to split
209an existing issue into multiple new issues, and it can take a long time to read
210through an issue's full history to figure out what is currently being discussed.
211
212Similarly, do not reopen an old, closed issue in response to the reoccurrence of
213a bug: the old issue probably contains now-irrelevant milestone and merge labels
214and outdated information. Instead, create a new issue and refer to the prior
215issue in its initial description. Text of the form `issue 123` will
216automatically be turned into a link.
217
Daniel Erat03138d42017-03-29 15:08:01218There is much more information about filing bugs and using labels in the
219[Chromium bug reporting guidelines].
220
Daniel Erat0576ce72017-03-27 05:43:06221## Mailing lists
222
Mike Frysinger98748412018-10-09 07:56:19223See the [contact] document for more details.
Daniel Erat0576ce72017-03-27 05:43:06224
225## Developing in the open
226
Harry Cutts44615192022-04-21 22:39:14227ChromiumOS is an open-source project. Whenever possible (i.e. when not
Daniel Erat0576ce72017-03-27 05:43:06228discussing private, partner-related information), use the public issue tracker
229and mailing lists rather than the internal versions.
230
Mike Frysinger98748412018-10-09 07:56:19231[contact]: contact.md
Mike Frysinger9fc0fc02020-09-05 05:18:57232[documentation guidelines]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/documentation_guidelines.md
233[documentation best practices]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/documentation_best_practices.md
Harry Cutts44615192022-04-21 22:39:14234[ChromiumOS design docs]: https://www.chromium.org/chromium-os/chromiumos-design-docs
Daniel Erat0576ce72017-03-27 05:43:06235[Chromium design docs]: https://www.chromium.org/developers/design-documents
Zach Reiznercdfcbd32021-04-21 06:21:54236[set from crosvm]: https://chromium.googlesource.com/chromiumos/platform/crosvm/+/HEAD/bin/clippy
Harry Cutts44615192022-04-21 22:39:14237[Rust on ChromeOS]: https://chromium.googlesource.com/chromiumos/docs/+/HEAD/rust_on_cros.md
Daniel Erat0576ce72017-03-27 05:43:06238[Google C++ style guide]: https://google.github.io/styleguide/cppguide.html
Mike Frysinger9fc0fc02020-09-05 05:18:57239[Chromium C++ style guide]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/c++/c++.md
Grace Chamf22a5912022-06-07 08:04:28240[Modern C++ use in Chromium]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/c++/c++-features.md
Evan Benn4811c3a2019-10-31 00:18:42241[platform2 repository]: platform2_primer.md
Mike Frysinger9fc0fc02020-09-05 05:18:57242[Linux kernel coding style]: https://github.com/torvalds/linux/blob/HEAD/Documentation/process/coding-style.rst
Jesse Barnes704211c2020-07-22 21:37:32243[Kernel Development]: kernel_development.md
Mike Frysinger9fc0fc02020-09-05 05:18:57244[EC Development page]: https://chromium.googlesource.com/chromiumos/platform/ec/+/HEAD/README.md
245[src/platform2/init]: https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/init/
246[init STYLE file]: https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/init/STYLE
247[init README file]: https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/init/README
Mike Frysinger2db7b852020-09-10 08:37:44248[chromiumos-overlay repository]: https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay
Daniel Erat0576ce72017-03-27 05:43:06249[Ebuild Writing]: https://devmanual.gentoo.org/ebuild-writing/index.html
250[Ebuild File Format]: https://devmanual.gentoo.org/ebuild-writing/file-format/index.html
Harry Cutts44615192022-04-21 22:39:14251[ChromiumOS shell style guidelines]: styleguide/shell.md
Daniel Erat0576ce72017-03-27 05:43:06252[Google Python style guide]: https://google.github.io/styleguide/pyguide.html
Harry Cutts44615192022-04-21 22:39:14253[ChromiumOS Python style guidelines]: styleguide/python.md
Mike Frysinger9fc0fc02020-09-05 05:18:57254[autotest coding style]: https://chromium.googlesource.com/chromiumos/third_party/autotest/+/HEAD/docs/coding-style.md
Harry Cutts44615192022-04-21 22:39:14255[ChromiumOS testing site]: https://www.chromium.org/chromium-os/testing
Daniel Erat0576ce72017-03-27 05:43:06256[Google Test]: https://github.com/google/googletest
Mike Frysinger9fc0fc02020-09-05 05:18:57257[Why Google C++ Testing Framework?]: https://github.com/google/googletest/blob/HEAD/googletest/docs/primer.md
258[Google Test FAQ]: https://github.com/google/googletest/blob/HEAD/googletest/docs/faq.md
Daniel Erat0576ce72017-03-27 05:43:06259[unit testing document]: https://www.chromium.org/chromium-os/testing/adding-unit-tests-to-the-build
Harry Cutts44615192022-04-21 22:39:14260[Best practices for writing ChromeOS unit tests]: testing/unit_tests.md
Mike Frysinger9fc0fc02020-09-05 05:18:57261[Autotest]: https://chromium.googlesource.com/chromiumos/third_party/autotest/+/HEAD/docs/user-doc.md
262[Chromium code review policy]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/code_reviews.md
Evan Benn4811c3a2019-10-31 00:18:42263[Developer Guide's code review instructions]: developer_guide.md#Upload-your-changes-and-get-a-code-review
Daniel Erat0576ce72017-03-27 05:43:06264[Gerrit notification settings]: https://chromium-review.googlesource.com/settings/#Notifications
Mike Frysinger9fc0fc02020-09-05 05:18:57265[chromium issue tracker]: https://crbug.com/
Daniel Erat03138d42017-03-29 15:08:01266[Chromium bug reporting guidelines]: https://www.chromium.org/for-testers/bug-reporting-guidelines
Daniel Erat0576ce72017-03-27 05:43:06267[chromium-os-dev]: https://groups.google.com/a/chromium.org/forum/#!forum/chromium-os-dev