blob: 1a46d8623d0f242f273ab54a388a0a3189904f7b [file] [log] [blame] [view]
Armando Montanezf25bc9f2020-03-18 11:42:59 -07001# Getting Started
Armando Montanez1d001202020-03-04 11:51:32 -08002
Armando Montanezf25bc9f2020-03-18 11:42:59 -07003This guide will walk you through setup and general use of Pigweed.
Armando Montanez4db74e12020-03-17 15:55:59 -07004We hope to make the setup process as smooth as possible. If any of this doesn't
Armando Montanez3d92e812020-03-19 12:13:36 -07005work, please [let us know](mailto:pigweed@googlegroups.com).
Armando Montanez4db74e12020-03-17 15:55:59 -07006
Keir Mierle086ef1c2020-03-19 02:03:51 -07007## Express setup
8
Armando Montanez3d92e812020-03-19 12:13:36 -07009If you'd like to skip the detailed explanations, below is the shorter version of
10getting setup for Pigweed. If you run into trouble, look at the more in-depth
11guide below, starting at [Prerequisites](getting_started.md#prerequisites). The
12express setup configures Pigweed's watcher for three targets to give a taste of
13Pigweed:
Keir Mierle086ef1c2020-03-19 02:03:51 -070014
151. **Host** - Mac, Linux, or Windows. Builds and runs tests
162. **Device/STM32F429** - Build only; see later in the guide to run tests on
17 device
183. **Docs** - Builds the Pigweed docs
19
20To get setup:
21
22(1) Make sure you have Git and Python installed and on your path.
23
24(2) Clone Pigweed and bootstrap the environment (compiler setup & more). **Be
25 patient, this step downloads ~1GB of LLVM, GCC, and other tooling**.
26
27```bash
28$ cd ~
29$ git clone https://pigweed.googlesource.com/pigweed/pigweed
30...
31$ cd pigweed
Rob Mohr63304bf2020-03-27 10:31:40 -070032$ source ./bootstrap.sh
Keir Mierle086ef1c2020-03-19 02:03:51 -070033...
34```
35
Armando Montaneza761e322020-06-15 16:30:40 -070036(3) Configure the GN build.
Keir Mierle086ef1c2020-03-19 02:03:51 -070037
38```bash
Armando Montaneza761e322020-06-15 16:30:40 -070039$ gn gen out
40Done. Made 1047 targets from 91 files in 114ms
Keir Mierle086ef1c2020-03-19 02:03:51 -070041```
42
43(4) Start the watcher. The watcher will invoke Ninja to build all the targets
44
45```bash
Armando Montanez7366d5a2020-06-17 15:04:43 -070046$ pw watch out default stm32f429i
Keir Mierle086ef1c2020-03-19 02:03:51 -070047
48 ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
49 ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌
50 ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌
51 ▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌
52 ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
53
5420200319 01:41:37 INF Starting Pigweed build watcher
5520200319 01:41:37 INF Searching for GN build dirs...
5620200319 01:41:37 INF Will build [1/3]: out/host
5720200319 01:41:37 INF Will build [2/3]: out/disco
5820200319 01:41:37 INF Will build [3/3]: out/docs
5920200319 01:41:39 INF Directory to watch: $HOME/wrk/pigweed
6020200319 01:41:39 INF Watching for file changes. Ctrl-C exits.
6120200319 01:41:39 INF Triggering initial build...
62...
63```
64
65(5) **Congratulations, you're ready to go!** Now take Pigweed for a spin with
66 the below steps.
67
68(6) With the watcher running in a separate window, edit
69 `pw_status/status_test.cc` to make an expectation fail; for example, add
70 `EXPECT_EQ(0, 1);` in a test.
71
72(7) Save the file. Observe the watcher rebuild & retest, and fail. Restore the
73 test if you feel like it.
74
75(8) Open the generated docs in `out/docs/gen/docs/html/index.html` in your
76 browser.
77
78(9) Edit `docs/getting_started.md` (this file!) and make any change. Save. See
79 the watcher rebuild the docs. Reload your browser, and see the changes.
80
81See below for equivalent Windows commands, and for more details on what each
82part does.
83
84**Note:** After running bootstrap once, use `. ./activate.sh` (or
85`activate.bat` on Windows) to re-activate the environment without
86re-bootstrapping.
87
Armando Montanez7b7a32f2020-03-18 11:02:41 -070088## Prerequisites
Armando Montanez4db74e12020-03-17 15:55:59 -070089
90**Linux**<br/>
Keir Mierle086ef1c2020-03-19 02:03:51 -070091Most Linux installations should work out of box, and not require any manual
92installation of prerequisites beyond basics like `git` and `build-essential`.
Armando Montanez3d92e812020-03-19 12:13:36 -070093Make sure gcc is set to gcc-8.
Armando Montanez4db74e12020-03-17 15:55:59 -070094
95**macOS**<br/>
Rob Mohr5808f042020-03-23 08:25:42 -070096On macOS you may get SSL certificate errors with the system Python
97installation. Run `sudo pip install certifi` to fix this. If you get SSL
98errors with the Python from [Homebrew](https://brew.sh) try running the
99following commands to ensure Python knows how to use OpenSSL.
100
101```bash
102brew install openssl
103brew uninstall python
104brew install python
105```
Armando Montanez4db74e12020-03-17 15:55:59 -0700106
Armando Montanez4db74e12020-03-17 15:55:59 -0700107To flash firmware to a STM32 Discovery development board (and run `pw test`)
Rob Mohr5808f042020-03-23 08:25:42 -0700108from macOS, you will need to install OpenOCD. Install
109[Homebrew](https://brew.sh), then install OpenOCD with `brew install openocd`.
Armando Montanez4db74e12020-03-17 15:55:59 -0700110
111**Windows**<br/>
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700112To start using Pigweed on Windows, you'll need to install
113[Git](https://git-scm.com/download/win) and
114[Python](https://www.python.org/downloads/windows/) (2.7 or above). We recommend
115you install Git to run from the command line and third party software.
Armando Montanez4db74e12020-03-17 15:55:59 -0700116
Armando Montanez7b7a32f2020-03-18 11:02:41 -0700117If you plan to flash devices with firmware, you'll need to install OpenOCD and
118ensure it's on your system path.
Armando Montanez4db74e12020-03-17 15:55:59 -0700119
Armando Montanez7b7a32f2020-03-18 11:02:41 -0700120## Bootstrap
121
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700122Once you satisfied the prerequisites, you will be able to clone Pigweed and
Armando Montanez7b7a32f2020-03-18 11:02:41 -0700123run the bootstrap that initializes the Pigweed virtual environment. The
124bootstrap may take several minutes to complete, so please be patient.
Armando Montanez4db74e12020-03-17 15:55:59 -0700125
126**Linux/macOS**
127```bash
Wyatt Hepler41f4d482020-03-18 16:03:19 -0700128$ git clone https://pigweed.googlesource.com/pigweed/pigweed ~/pigweed
Armando Montanez4db74e12020-03-17 15:55:59 -0700129$ cd ~/pigweed
Rob Mohr63304bf2020-03-27 10:31:40 -0700130$ source ./bootstrap.sh
Armando Montanez4db74e12020-03-17 15:55:59 -0700131```
132
133**Windows**
134```batch
135:: Run git commands from the shell you set up to use with Git during install.
Wyatt Hepler41f4d482020-03-18 16:03:19 -0700136> git clone https://pigweed.googlesource.com/pigweed/pigweed %HOMEPATH%\pigweed
Armando Montanez4db74e12020-03-17 15:55:59 -0700137> cd %HOMEPATH%\pigweed
Armando Montanez4db74e12020-03-17 15:55:59 -0700138> bootstrap.bat
139```
140
141Below is a real-time demo with roughly what you should expect to see as output:
142
Armando Montanez7b7a32f2020-03-18 11:02:41 -0700143![build example using pw watch](images/pw_env_setup_demo.gif)
Armando Montanez4db74e12020-03-17 15:55:59 -0700144
Armando Montanez7b7a32f2020-03-18 11:02:41 -0700145Congratulations, you are now set up to start using Pigweed!
Armando Montanez1d001202020-03-04 11:51:32 -0800146
147## Pigweed Environment
148
149After going through the initial setup process, your current terminal will be in
150the Pigweed development environment that provides all the tools you should need
Armando Montanez7b7a32f2020-03-18 11:02:41 -0700151to develop on Pigweed. If you leave that session, you can activate the
Armando Montanez1d001202020-03-04 11:51:32 -0800152environment in a new session with the following command:
153
154**Linux/macOS**
155```bash
Keir Mierle8e754222020-03-17 13:11:33 -0700156$ . ./activate.sh
Armando Montanez1d001202020-03-04 11:51:32 -0800157```
158
159**Windows**
160```batch
Wyatt Hepler10543692020-03-18 17:30:43 -0700161> activate.bat
Armando Montanez1d001202020-03-04 11:51:32 -0800162```
163
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700164Some major changes may require triggering the bootstrap again, so if you run
165into host tooling changes after a pull it may be worth re-running bootstrap.
Armando Montanez1d001202020-03-04 11:51:32 -0800166
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700167## Build Pigweed for Host
Armando Montanez1d001202020-03-04 11:51:32 -0800168
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700169Pigweed's primary build system is GN/Ninja based. There are CMake and Bazel
170builds in-development, but they are incomplete and don't have feature parity
171with the GN build. We strongly recommend you stick to the GN build system.
Armando Montanez1d001202020-03-04 11:51:32 -0800172
173GN (Generate Ninja) just does what it says on the tin; GN generates
174[Ninja](https://ninja-build.org/) build files.
175
Armando Montaneza761e322020-06-15 16:30:40 -0700176The default GN configuration generates build files that allow you to build host
177binaries, device binaries, and upstream documentation all in one Ninja
178invocation.
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700179
180Run GN as seen below:
181
Armando Montanez1d001202020-03-04 11:51:32 -0800182```bash
Armando Montaneza761e322020-06-15 16:30:40 -0700183$ gn gen out
Armando Montanez1d001202020-03-04 11:51:32 -0800184```
185
Armando Montaneza761e322020-06-15 16:30:40 -0700186Note that `out` is simply the directory the build files are saved to. Unless
187this directory is deleted or you desire to do a clean build, there's no need to
188run GN again; just rebuild using Ninja directly.
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700189
190Now that we have build files, it's time to build Pigweed!
191
Armando Montaneza761e322020-06-15 16:30:40 -0700192Now you *could* manually invoke the host build using `ninja -C out` every
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700193time you make a change, but that's tedious. Instead, let's use `pw_watch`.
194
195Go ahead and start `pw_watch`:
Armando Montanez1d001202020-03-04 11:51:32 -0800196
197```bash
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700198$ pw watch
Armando Montanez1d001202020-03-04 11:51:32 -0800199```
200
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700201When `pw_watch` starts up, it will automatically build the directory we
Armando Montaneza761e322020-06-15 16:30:40 -0700202generated in `out`. Additionally, `pw_watch` watches source code files for
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700203changes, and triggers a Ninja build whenever it notices a file has been saved.
204You might be surprised how much time it can save you!
Armando Montanez1d001202020-03-04 11:51:32 -0800205
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700206With `pw watch` running, try modifying `pw_status/public/pw_status/status.h` and
207watch the build re-trigger when you save the file.
Armando Montanez1d001202020-03-04 11:51:32 -0800208
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700209See below for a demo of this in action:
Armando Montanez1d001202020-03-04 11:51:32 -0800210
211![build example using pw watch](images/pw_watch_build_demo.gif)
212
Armando Montanez1d001202020-03-04 11:51:32 -0800213## Running Unit Tests
214
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700215Fun fact, you've been running the unit tests already! Ninja builds targeting the
216host automatically build and run the unit tests. Unit tests err on the side of
217being quiet in the success case, and only output test results when there's a
218failure.
219
220To see the a test failure, you can modify `pw_status/status_test.cc` to fail
221by changing one of the strings in the "KnownString" test.
Armando Montanez1d001202020-03-04 11:51:32 -0800222
223![example test failure using pw watch](images/pw_watch_test_demo.gif)
224
225Running tests as part of the build isn't particularly expensive because GN
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700226caches passing tests. Each time you build, only the tests that are affected
227(whether directly or transitively) by the code changes since the last build will
228be re-built and re-run.
Armando Montanez1d001202020-03-04 11:51:32 -0800229
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700230Try running the `pw_status` test manually:
231```bash
Armando Montaneza761e322020-06-15 16:30:40 -0700232$ ./host_[compiler]/obj/pw_status/status_test
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700233```
Armando Montanez1d001202020-03-04 11:51:32 -0800234
Armando Montaneza761e322020-06-15 16:30:40 -0700235Depending on your host OS, `[compiler]` will default to either Clang or GCC.
236
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700237## Building for a Device
Armando Montanez1d001202020-03-04 11:51:32 -0800238
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700239As mentioned previously, Pigweed builds for host by default. In the context of
Armando Montaneza761e322020-06-15 16:30:40 -0700240Pigweed, a Pigweed "target" is a build configuration that includes a toolchain,
241default library configurations, and more to result in binaries that run
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700242natively on the target.
Armando Montanez1d001202020-03-04 11:51:32 -0800243
Armando Montaneza761e322020-06-15 16:30:40 -0700244Switch to the window running `pw_watch`, and quit using `ctrl+c`. To get
245`pw_watch` to build the new STM32F429I-DISC1 target, re-launch by specifying
246which Ninja targets to build:
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700247
248```bash
Armando Montanez7366d5a2020-06-17 15:04:43 -0700249$ pw watch out default stm32f429i
Armando Montaneza761e322020-06-15 16:30:40 -0700250```
251
252This is equivalent to the following Ninja invocation:
253
254```bash
255$ ninja -C out default stm32f429i
256```
257
258Or since the "default" target builds host and docs,
259
260```bash
261$ ninja -C out host docs stm32f429i
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700262```
263
264Now `pw_watch` is building for host and a device!
Armando Montanez1d001202020-03-04 11:51:32 -0800265
266## Running Tests on a Device
267
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700268While tests run automatically on the host, it takes a few more steps to get
269tests to run automatically on a device, too. Even though we've verified tests
270pass on the host, it's crucial to verify the same with on-device testing. We've
271encountered some unexpected bugs that can only be found by running the unit
272tests directly on the device.
Armando Montanez1d001202020-03-04 11:51:32 -0800273
274### 1. Connect Device(s)
275Connect any number of STM32F429I-DISC1 boards to your computer using the mini
276USB port on the board (**not** the micro USB). Pigweed will automatically detect
277the boards and distribute the tests across the devices. More boards = faster
278tests!
279
Armando Montanezbcc194b2020-03-10 10:23:18 -0700280![development boards connected via USB](images/stm32f429i-disc1_connected.jpg)
Armando Montanez1d001202020-03-04 11:51:32 -0800281
282### 2. Launch Test Server
283To allow Ninja to run tests on an arbitrary number of devices, Ninja will send
284test requests to a server running in the background. Launch the server in
285another window using the command below (remember, you'll need to activate the
Armando Montanez3d92e812020-03-19 12:13:36 -0700286Pigweed environment first).
Armando Montanez1d001202020-03-04 11:51:32 -0800287
288```shell
289 $ stm32f429i_disc1_test_server
290```
291
292**Note:** If you attach or detach any more boards to your workstation you'll
293need to relaunch this server.
294
295### 3. Configure GN
296
297We can tell GN to use the testing server by enabling a build arg specific to
Armando Montaneza761e322020-06-15 16:30:40 -0700298the stm32f429i-disc1 target.
Armando Montanez1d001202020-03-04 11:51:32 -0800299
300```shell
301$ gn args out/disco
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700302# Append this line to the file that opens in your editor to tell GN to run
303# on-device unit tests.
Armando Montanez1d001202020-03-04 11:51:32 -0800304pw_use_test_server = true
305```
306
307### Done!
308
309Whenever you make code changes and trigger a build, all the affected unit tests
310will be run across the attached boards!
311
Armando Montanez4db74e12020-03-17 15:55:59 -0700312See the demo below for an example of what this all looks like put together:
313
314![pw watch running on-device tests](images/pw_watch_on_device_demo.gif)
315
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700316## Building the Documentation
317
Armando Montaneza761e322020-06-15 16:30:40 -0700318In addition to the markdown documentation, Pigweed has a collection of
319information-rich RST files that are built by the default invocation of GN. You
320will find the documents at `out/docs/gen/docs/html`.
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700321
Armando Montaneza761e322020-06-15 16:30:40 -0700322You can build the documentation manually by with the command below.
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700323
Armando Montaneza761e322020-06-15 16:30:40 -0700324```shell
325$ ninja -C out docs
Armando Montanezf25bc9f2020-03-18 11:42:59 -0700326```
327
Armando Montanez1d001202020-03-04 11:51:32 -0800328## Next steps
329
330This concludes the introduction to developing with Pigweed. If you'd like to see
331more of what Pigweed has to offer, feel free to dive into the per-module
Armando Montanez3d92e812020-03-19 12:13:36 -0700332documentation. If you run into snags along the way, please [let us
333know](mailto:pigweed@googlegroups.com)!