| 1 | --- |
| 2 | title: Developers’ guide |
| 3 | --- |
| 4 | |
| 5 | <div class="alert alert-info"> |
| 6 | <p> |
| 7 | Please familiarise yourself with the <a href="https://github.com/pyocd/pyOCD/blob/main/CONTRIBUTING.md"> |
| 8 | contributing guide</a> before beginning any development on pyOCD or related projects. |
| 9 | </p> |
| 10 | </div> |
| 11 | |
| 12 | ## Setup |
| 13 | |
| 14 | PyOCD developers are strongly recommended to setup a working environment using either |
| 15 | [virtualenv](https://virtualenv.pypa.io/en/latest/) or the built-in `venv` module (only use of `venv` is shown |
| 16 | below, but the two are equivalent). After cloning the code, you can setup a virtualenv and install the pyOCD |
| 17 | dependencies for the current platform by following the detailed steps below. |
| 18 | |
| 19 | Install the necessary tools listed below. Skip any step where a compatible tool already exists. |
| 20 | |
| 21 | * [Install Python](https://www.python.org/downloads/) version 3.9.0 or above. Add to PATH. |
| 22 | * Note that on Windows, the 32-bit Python 2.7 must be installed for the Python-enabled `arm-none-eabi-gdb-py` to |
| 23 | work properly and for the `test/gdb_test.py` functional test to pass. |
| 24 | * [Install Git](https://git-scm.com/downloads). Add to PATH. |
| 25 | * [Install GNU Arm Embedded toolchain](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm). |
| 26 | This provides `arm-none-eabi-gdb` used for testing the gdbserver. Add to PATH. |
| 27 | |
| 28 | ## Steps |
| 29 | |
| 30 | **Step 1.** Get the sources and create a virtual environment |
| 31 | |
| 32 | ``` |
| 33 | $ git clone https://github.com/pyocd/pyOCD |
| 34 | $ cd pyOCD |
| 35 | $ python3 -m venv venv |
| 36 | ``` |
| 37 | |
| 38 | **Step 2.** Activate virtual environment |
| 39 | |
| 40 | Activate your virtualenv and install the pyOCD dependencies for the current platform by doing |
| 41 | the following. |
| 42 | |
| 43 | Linux or Mac: |
| 44 | ``` |
| 45 | $ source venv/bin/activate |
| 46 | ``` |
| 47 | |
| 48 | Windows: |
| 49 | ``` |
| 50 | $ venv\Scripts\activate |
| 51 | ``` |
| 52 | |
| 53 | **Step 3.** Install editable pyOCD |
| 54 | |
| 55 | ``` |
| 56 | $ pip install -e .[test] |
| 57 | ``` |
| 58 | |
| 59 | If you switch branches, you may need to reinstall. |
| 60 | |
| 61 | Because the `develop` branch doesn't have version tags except older tags from the `develop` branch point, |
| 62 | the version number of pyOCD might be significantly out of date. If this is an issue, you can override the |
| 63 | version by setting the `SETUPTOOLS_SCM_PRETEND_VERSION` environment variable to the desired version number |
| 64 | (without a "v" prefix). |
| 65 | |
| 66 | **Step 4.** Develop |
| 67 | |
| 68 | See the [porting guide]({% link _docs/adding_new_targets.md %}) for how to add new devices. Of course, we welcome |
| 69 | all improvements and changes. See the [contributor statement](https://github.com/pyocd/pyOCD/blob/main/CONTRIBUTING.md) for some guidelines. |
| 70 | |
| 71 | See the [branch policy](#branch-configuration-policy) below for details about branches and which branch you should |
| 72 | work from. |
| 73 | |
| 74 | If you'd like suggestions for something to work on, from small to large, the |
| 75 | [Slack](https://join.slack.com/t/pyocd/shared_invite/zt-zqjv6zr5-ZfGAXl_mFCGGmFlB_8riHA) workspace is a great |
| 76 | way to engage with the community and maintainers. |
| 77 | |
| 78 | **Step 5.** Test |
| 79 | |
| 80 | See the [automated test documentation]({% link _docs/automated_tests.md %}) for an introduction to available tests and related scripts. |
| 81 | |
| 82 | To run the unit tests, you can execute the following. |
| 83 | |
| 84 | ``` |
| 85 | $ pytest |
| 86 | ``` |
| 87 | |
| 88 | The automated test suite also needs to be run: |
| 89 | |
| 90 | ``` |
| 91 | $ cd test |
| 92 | $ python ./automated_test.py |
| 93 | ``` |
| 94 | |
| 95 | **Step 6.** Pull request |
| 96 | |
| 97 | Once you are satisfied with your changes and all automated tests pass, please create a |
| 98 | [new pull request](https://github.com/pyocd/pyOCD/pull/new) on GitHub to share your work. Please see below for |
| 99 | which branch to target. |
| 100 | |
| 101 | Pull requests should be made after a changeset is |
| 102 | [rebased](https://www.atlassian.com/git/tutorials/merging-vs-rebasing/workflow-walkthrough). |
| 103 | |
| 104 | |
| 105 | ## Branch configuration policy |
| 106 | |
| 107 | There are two primary branches: |
| 108 | |
| 109 | - `main`: Stable branch reflecting the most recent release. May contain bug fixes not yet released, but no new |
| 110 | feature commits are allowed. |
| 111 | - `develop`: Active development branch for the next minor version. Merged into `main` at release time. |
| 112 | |
| 113 | There may be other development branches present to host long term development of major new features and backwards incompatible changes, such as API changes. |
| 114 | |
| 115 | The branch that your changes should be made against depends on the type and complexity of the changes: |
| 116 | |
| 117 | - Only a bug fix: please target `main`. |
| 118 | - Any other changes, or a mix of changes: target the `develop` branch. This is also a good choice if you aren't sure. |
| 119 | |
| 120 | Maintainers will cherry-pick commits between `main` and `develop` as necessary to keep fixes in sync. |
| 121 | |
| 122 | If you have any questions about how best to submit changes or the branch policy, please ask in the |
| 123 | [Slack](https://join.slack.com/t/pyocd/shared_invite/zt-zqjv6zr5-ZfGAXl_mFCGGmFlB_8riHA) workspace or |
| 124 | [GitHub Discussions](https://github.com/pyocd/pyOCD/discussions). We'll be happy to help. |
| 125 | |
| 126 | |
| 127 | ## Debugging pyOCD |
| 128 | |
| 129 | ### VS Code |
| 130 | |
| 131 | Debugging pyOCD launched by VS Code is straightforward, and is much like debugging any package. |
| 132 | |
| 133 | 1. First, set up the pyOCD development and virtualenv as described above. |
| 134 | |
| 135 | 2. Set up the launch config as follows. |
| 136 | |
| 137 | - Set `pythonPath` to the python executable in the virtualenv. |
| 138 | |
| 139 | - Set `module` to `pyocd.__main__`, to debug pyOCD entry point as a module. This works better than trying to debug the script `.../pyocd/__main__.py` as it ensures the relative module references inside pyOCD work correctly.. |
| 140 | |
| 141 | For instance, here is a VS Code launch config for pyOCD's gdbserver: |
| 142 | |
| 143 | { |
| 144 | "name": "pyocd gdbserver", |
| 145 | "type": "python", |
| 146 | "request": "launch", |
| 147 | "module": "pyocd.__main__", |
| 148 | "console": "integratedTerminal", |
| 149 | "stopOnEntry": true, |
| 150 | "pythonPath": "/Users/username/projects/pyOCD/venv/bin/python3", |
| 151 | "showReturnValue": true, |
| 152 | "args": ["gdb", "-v"] |
| 153 | } |
| 154 | |
| 155 | |
| 156 | It's also possible to debug pyOCD remotely. This is useful either for cases where it is launched by another tool and you cannot directly control the command line , or is on another machine. |
| 157 | |
| 158 | For remote debug, use a launch config like this: |
| 159 | |
| 160 | { |
| 161 | "name": "Python: Attach using port", |
| 162 | "type": "python", |
| 163 | "request": "attach", |
| 164 | "port": 5678, |
| 165 | } |
| 166 | |
| 167 | If the pyOCD process is on another machine, add a `host` key to the launch config with the host name. (This defaults to localhost.) |
| 168 | |
| 169 | Install [debugpy](https://github.com/microsoft/debugpy/) with pip into your virtual environment. |
| 170 | |
| 171 | To remotely debug pyOCD from the command line, run it with the `debugpy` module. For instance, `python -m debugpy --listen 5678 -m pyocd gdb -v`. Additionally, `--wait-for-client` can be added after the port number to wait for the debug client to connect before running pyOCD. |
| 172 | |
| 173 | If you don't control pyOCD's launch, then `debugpy` can be called directly from pyOCD's code. Add the following code to pyocd. Inside `pyocd.__main__.PyOCDTool.run()` is a good location, though it can be done at module level in `__main__.py`. |
| 174 | |
| 175 | ```py |
| 176 | import debugpy |
| 177 | debugpy.listen(5678) |
| 178 | ``` |
| 179 | |
| 180 | To pause pyOCD's execution and wait for the debug client to connect, add this line as well: |
| 181 | ```py |
| 182 | debugpy.wait_for_client() |
| 183 | ``` |
| 184 | |
| 185 | |
| 186 | |
| 187 |