Garret Kelly | 9eebde0 | 2019-10-22 15:36:49 -0400 | [diff] [blame] | 1 | --- |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 2 | title: "util/vendor.py: Vendor-in Components" |
Garret Kelly | 9eebde0 | 2019-10-22 15:36:49 -0400 | [diff] [blame] | 3 | --- |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 4 | |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 5 | Not all code contained in this repository is actually developed within this repository. |
| 6 | Code which we include from external sources is placed in `vendor` sub-directories (e.g. `hw/vendor`) and copied over from upstream sources. |
| 7 | The process of copying the upstream sources is called vendoring, and it is automated by the `util/vendor` tool. |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 8 | |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 9 | The `util/vendor` tool can go beyond simply copying in source files: it can patch them, it can export patches from commits in a Git repository, and it can commit the resulting changes with a meaningful commit message. |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 10 | |
| 11 | ## Tool usage overview |
| 12 | |
| 13 | ```text |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 14 | usage: vendor [-h] [--refresh-patches] [--commit] [--verbose] file |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 15 | |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 16 | vendor, copy source code from upstream into this repository |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 17 | |
| 18 | positional arguments: |
| 19 | file vendoring description file (*.vendor.hjson) |
| 20 | |
| 21 | optional arguments: |
| 22 | -h, --help show this help message and exit |
Sam Elliott | a24497b | 2020-04-23 15:06:46 +0100 | [diff] [blame] | 23 | --update, -U Update locked version of repository with upstream changes |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 24 | --refresh-patches Refresh the patches from the patch repository |
| 25 | --commit, -c Commit the changes |
| 26 | --verbose, -v Verbose |
| 27 | ``` |
| 28 | |
| 29 | ## The vendor description file |
| 30 | |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 31 | For each vendored-in component a description file must be created, which serves as input to the `util/vendor` tool. |
| 32 | The vendor description file is stored in `vendor/<vendor>_<name>.vendor.hjson`. |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 33 | By convention all imported code is named `<vendor>_<name>`, with `<vendor>` typically being the GitHub user or organization name, and `<name>` the project name. |
| 34 | It is recommended to use only lower-case characters. |
| 35 | |
| 36 | A full commented example of a vendor description file is given below. |
| 37 | All relative paths are relative to the description file. |
| 38 | Optional parts can be removed if they are not used. |
| 39 | |
| 40 | ``` |
| 41 | // Copyright lowRISC contributors. |
| 42 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 43 | // SPDX-License-Identifier: Apache-2.0 |
| 44 | { |
| 45 | // Name of the vendored-in project |
| 46 | name: "pulp_riscv_dbg", |
| 47 | |
| 48 | // Target directory: typically equal to the name |
| 49 | // All imported code is copied into this directory |
| 50 | target_dir: "pulp_riscv_dbg", |
| 51 | |
| 52 | // Git upstream source code repository |
| 53 | upstream: { |
| 54 | // Upstream Git repository URL. HTTPS URLs are preferred. |
| 55 | url: "https://github.com/pulp-platform/riscv-dbg", |
| 56 | // Upstream revision or branch. Can be a commit hash or a branch name. |
| 57 | rev: "pulpissimo_integration", |
| 58 | }, |
| 59 | |
Rupert Swarbrick | 249b4c3 | 2020-05-19 17:11:31 +0100 | [diff] [blame] | 60 | // Optional: Pick specific files or subdirectories from upstream and |
| 61 | // specify where to put them. |
| 62 | mapping: [ |
| 63 | {from: 'src', to: 'the-source'}, |
| 64 | {from: 'doc', to: 'some/documentation', patch_dir: 'doc_patches'} |
| 65 | ] |
| 66 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 67 | // Optional: Apply patches from the following directory to the upstream |
| 68 | // sources |
| 69 | patch_dir: "patches/pulp_riscv_dbg", |
| 70 | |
| 71 | // Optional: Update patches in |patch_dir| from a Git repository |
Miguel Osorio | ba17f11 | 2019-11-15 12:30:14 -0800 | [diff] [blame] | 72 | // If util/vendor is run with --refresh-patches, all commits in the repository |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 73 | // at |url| between |rev_base| and |rev_patched| are exported into the |
| 74 | // |patch_dir|, replacing all existing patches. |
| 75 | patch_repo: { |
| 76 | url: "git@github.com:lowRISC/riscv-dbg.git", |
| 77 | rev_base: "pulpissimo_integration", |
| 78 | rev_patched: "ot", |
| 79 | }, |
| 80 | |
| 81 | // Optional: Exclude files or directories from the upstream sources |
Rupert Swarbrick | 249b4c3 | 2020-05-19 17:11:31 +0100 | [diff] [blame] | 82 | // The standard glob wildcards (*, ?, etc.) are supported. |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 83 | exclude_from_upstream: [ |
| 84 | "src/dm_top.sv", |
| 85 | "src_files.yml", |
| 86 | ] |
| 87 | } |
| 88 | ``` |
| 89 | |
Rupert Swarbrick | 249b4c3 | 2020-05-19 17:11:31 +0100 | [diff] [blame] | 90 | If only the contents of a single subdirectory (including its children) of an upstream repository are to be copied in, the optional `only_subdir` key of can be used in the `upstream` section to specify the subdirectory to be copied. |
Tim Shepard | 24b7203 | 2019-10-23 16:40:04 -0400 | [diff] [blame] | 91 | The contents of that subdirectory will populate the `target_dir` directly (without any intervening directory levels). |
| 92 | |
Rupert Swarbrick | 249b4c3 | 2020-05-19 17:11:31 +0100 | [diff] [blame] | 93 | For a more complicated set of copying rules ("get directories `A/B` and `A/C` but not anything else in `A`"), use a `mapping` list. |
| 94 | Each element of the list should be a dictionary with keys `from` and `to`. |
| 95 | The value of `from` should be a path relative to the source directory (either the top of the cloned directory, or the `only_subdir` subdirectory, if set). |
| 96 | The value of `to` should be a path relative to `target_dir`. |
| 97 | |
| 98 | If `patch_dir` is supplied, it names a directory containing patches to be applied to the vendored code. |
| 99 | If there is no `mapping` list, this directory's patches are applied in lexicographical order relative to `target_dir`. |
| 100 | If there is a mapping list, each element of the list may contain a `patch_dir` key. |
| 101 | The value at that key is a directory, relative to the global `patch_dir` and patches in that directory are applied in lexicographical order relative to the target directory of the mapping, `to`. |
| 102 | |
Tim Shepard | 24b7203 | 2019-10-23 16:40:04 -0400 | [diff] [blame] | 103 | In the example vendor description file below, the mpsse directory is populated from the chromiumos platform2 repository, extracting just the few files in the trunks/ftdi subdirectory. |
| 104 | |
| 105 | ``` |
| 106 | // Copyright lowRISC contributors. |
| 107 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 108 | // SPDX-License-Identifier: Apache-2.0 |
| 109 | { |
| 110 | name: "mpsse", |
| 111 | target_dir: "mpsse", |
| 112 | |
| 113 | upstream: { |
| 114 | url: "https://chromium.googlesource.com/chromiumos/platform2/", |
| 115 | rev: "master", |
| 116 | only_subdir: "trunks/ftdi", |
| 117 | }, |
| 118 | } |
| 119 | ``` |
| 120 | |
Sam Elliott | a24497b | 2020-04-23 15:06:46 +0100 | [diff] [blame] | 121 | ## Updating and The Vendor Lock File |
| 122 | |
| 123 | In order to document which version of a repositoy has been cloned and committed to the repository with the vendor tool, a vendor lock file is stored in `vendor/<vendor>_<name>.lock.hjson`. |
| 124 | This contains only the upstream information, including the URL and the exact git revision that was cloned. |
| 125 | |
| 126 | Beyond just documentation, this enables users to re-clone the previously-cloned upstream repository -- including re-applying patches, choosing subdirectories, and excluding additional files -- without having to integrate any upstream changes. |
| 127 | Indeed the default behaviour of the vendor tool is to use the upstream information from `<vendor>_<name>.lock.hjson` if this file exists. |
| 128 | |
| 129 | Once the lock file exists, the vendor tool will only use the upstream information in `<vendor>_<name>.vendor.json` if the `--update` command-line option is used. |
| 130 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 131 | ## Examples |
| 132 | |
Sam Elliott | a24497b | 2020-04-23 15:06:46 +0100 | [diff] [blame] | 133 | ### Re-clone code and apply new file exclusions or patches |
| 134 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 135 | ```command |
| 136 | $ cd $REPO_TOP |
Sam Elliott | a24497b | 2020-04-23 15:06:46 +0100 | [diff] [blame] | 137 | $ ./util/vendor.py hw/vendor/google_riscv-dv.vendor.hjson -v |
| 138 | ``` |
| 139 | |
| 140 | ### Update code and commit the new code |
| 141 | |
| 142 | This will generate a commit message based off the git shortlog between the |
| 143 | previously cloned revision and the newly cloned revision of the repository. |
| 144 | |
| 145 | ```command |
| 146 | $ cd $REPO_TOP |
| 147 | $ ./util/vendor.py hw/vendor/google_riscv-dv.vendor.hjson -v --update --commit |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 148 | ``` |