README.md
1# Open Screen Library
2
3The Open Screen Library implements the Open Screen Protocol and the Chromecast
4protocols (discovery, application control, and media streaming).
5
6Information about the Open Screen Protocol and its specification can be found
7[on GitHub](https://w3c.github.io/openscreenprotocol/).
8
9# Getting the code
10
11## Installing depot_tools
12
13Library dependencies are managed using `gclient`, from the
14[depot_tools](https://www.chromium.org/developers/how-tos/depottools) repo.
15
16To get gclient, run the following command in your terminal:
17```bash
18 git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
19```
20
21Then add the `depot_tools` folder to your `PATH` environment variable.
22
23Note that openscreen does not use other features of `depot_tools` like `repo` or
24`drover`. However, some `git-cl` functions *do* work, like `git cl try`,
25`git cl format`, `git cl lint`, and `git cl upload.`
26
27## Checking out code
28
29From the parent directory of where you want the openscreen checkout (e.g.,
30`~/my_project_dir`), configure `gclient` and check out openscreen with the
31following commands:
32
33```bash
34 cd ~/my_project_dir
35 gclient config https://chromium.googlesource.com/openscreen
36 gclient sync
37```
38
39The first `gclient` command will create a default .gclient file in
40`~/my_project_dir` that describes how to pull down the `openscreen` repository.
41The second command creates an `openscreen/` subdirectory, downloads the source
42code, all third-party dependencies, and the toolchain needed to build things;
43and at their appropriate revisions.
44
45## Syncing your local checkout
46
47To update your local checkout from the openscreen reference repository, just run
48
49```bash
50 cd ~/my_project_dir/openscreen
51 git pull
52 gclient sync
53```
54
55This will rebase any local commits on the remote top-of-tree, and update any
56dependencies that have changed.
57
58# Build setup
59
60The following are the main tools are required for development/builds:
61
62 - Build file generator: `gn`
63 - Code formatter: `clang-format`
64 - Builder: `ninja` ([GitHub releases](https://github.com/ninja-build/ninja/releases))
65 - Compiler/Linker: `clang` (installed by default) or `gcc` (installed by you)
66
67All of these--except `gcc` as noted above--are automatically downloaded/updated
68for the Linux and Mac environments via `gclient sync` as described above. The
69first two are installed into `buildtools/<platform>/`.
70
71Mac only: XCode must be installed on the system, to link against its frameworks.
72
73`clang-format` is used for maintaining consistent coding style, but it is not a
74complete replacement for adhering to Chromium/Google C++ style (that's on you!).
75The presubmit script will sanity-check that it has been run on all new/changed
76code.
77
78## Linux clang
79
80On Linux, the build will automatically download the Clang compiler from the
81Google storage cache, the same way that Chromium does it.
82
83Ensure that libstdc++ 8 is installed, as clang depends on the system
84instance of it. On Debian flavors, you can run:
85
86```bash
87 sudo apt-get install libstdc++-8-dev
88```
89
90## Linux gcc
91
92Setting the `gn` argument "is_gcc=true" on Linux enables building using gcc
93instead.
94
95```bash
96 mkdir out/debug-gcc
97 gn gen out/debug-gcc --args="is_gcc=true"
98```
99
100Note that g++ version 7 or newer must be installed. On Debian flavors you can
101run:
102
103```bash
104 sudo apt-get install gcc-7
105```
106
107## Mac clang
108
109On Mac OS X, the build will use the clang provided by
110[XCode](https://apps.apple.com/us/app/xcode/id497799835?mt=12), which must be
111installed.
112
113## Debug build
114
115Setting the `gn` argument "is_debug=true" enables debug build.
116
117```bash
118 gn gen out/debug --args="is_debug=true"
119```
120
121To install debug information for libstdc++ 8 on Debian flavors, you can run:
122
123```bash
124 sudo apt-get install libstdc++6-8-dbg
125```
126
127## gn configuration
128
129Running `gn args` opens an editor that allows to create a list of arguments
130passed to every invocation of `gn gen`.
131
132```bash
133 gn args out/debug
134```
135
136# Building targets
137
138## Cast Streaming sender and receiver
139
140TODO(jophba): Fill in details
141
142## OSP demo
143
144The following commands will build the Open Screen Protocol demo and run it.
145
146``` bash
147 mkdir out/debug
148 gn gen out/debug # Creates the build directory and necessary ninja files
149 ninja -C out/debug osp_demo # Builds the executable with ninja
150 ./out/debug/osp_demo # Runs the executable
151```
152
153The `-C` argument to `ninja` works just like it does for GNU Make: it specifies
154the working directory for the build. So the same could be done as follows:
155
156``` bash
157 ./gn gen out/debug
158 cd out/debug
159 ninja osp_demo
160 ./osp_demo
161```
162
163After editing a file, only `ninja` needs to be rerun, not `gn`. If you have
164edited a `BUILD.gn` file, `ninja` will re-run `gn` for you.
165
166Unless you like to wait longer than necessary for builds to complete, run
167`autoninja` instead of `ninja`, which takes the same command-line arguments.
168This will automatically parallelize the build for your system, depending on
169number of processor cores, RAM, etc.
170
171For details on running `osp_demo`, see its [README.md](osp/demo/README.md).
172
173## Building other targets
174
175Running `ninja -C out/debug gn_all` will build all non-test targets in the
176repository.
177
178`gn ls --type=executable out/debug` will list all of the executable targets
179that can be built.
180
181If you want to customize the build further, you can run `gn args out/debug` to
182pull up an editor for build flags. `gn args --list out/debug` prints all of
183the build flags available.
184
185## Building and running unit tests
186
187```bash
188 ninja -C out/debug openscreen_unittests
189 ./out/debug/openscreen_unittests
190```
191
192# Contributing changes
193
194Open Screen library code should follow the [Open Screen Library Style
195Guide](docs/style_guide.md).
196
197This library uses [Chromium Gerrit](https://chromium-review.googlesource.com/) for
198patch management and code review (for better or worse). You will need to register
199for an account at `chromium-review.googlesource.com` to upload patches for review.
200
201The following sections contain some tips about dealing with Gerrit for code
202reviews, specifically when pushing patches for review, getting patches reviewed,
203and committing patches.
204
205# Uploading a patch for review
206
207The `git cl` tool handles details of interacting with Gerrit (the Chromium code
208review tool) and is recommended for pushing patches for review. Once you have
209committed changes locally, simply run:
210
211```bash
212 git cl format
213 git cl upload
214```
215
216The first command will will auto-format the code changes. Then, the second
217command runs the `PRESUBMIT.py` script to check style and, if it passes, a
218newcode review will be posted on `chromium-review.googlesource.com`.
219
220If you make additional commits to your local branch, then running `git cl
221upload` again in the same branch will merge those commits into the ongoing
222review as a new patchset.
223
224It's simplest to create a local git branch for each patch you want reviewed
225separately. `git cl` keeps track of review status separately for each local
226branch.
227
228## Addressing merge conflicts
229
230If conflicting commits have been landed in the repository for a patch in review,
231Gerrit will flag the patch as having a merge conflict. In that case, use the
232instructions above to rebase your commits on top-of-tree and upload a new
233patchset with the merge conflicts resolved.
234
235## Tryjobs
236
237Clicking the `CQ DRY RUN` button (also, confusingly, labeled `COMMIT QUEUE +1`)
238will run the current patchset through all LUCI builders and report the results.
239It is always a good idea get a green tryjob on a patch before sending it for
240review to avoid extra back-and-forth.
241
242You can also run `git cl try` from the commandline to submit a tryjob.
243
244## Code reviews
245
246Send your patch to one or more committers in the
247[COMMITTERS](https://chromium.googlesource.com/openscreen/+/refs/heads/master/COMMITTERS)
248file for code review. All patches must receive at least one LGTM by a committer
249before it can be submitted.
250
251## Submitting patches
252
253After your patch has received one or more LGTM commit it by clicking the
254`SUBMIT` button (or, confusingly, `COMMIT QUEUE +2`) in Gerrit. This will run
255your patch through the builders again before committing to the main openscreen
256repository.
257
258# Additional resources
259
260* [Continuous builders](docs/continuous_build.md)
261* [Building and running fuzz tests](docs/fuzzing.md)
262* [Running on a Raspberry PI](docs/raspberry_pi.md)
263* [Unit test code coverage](docs/code_coverage.md)
264