xref: /aosp_15_r20/external/pigweed/seed/0101.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _seed-0101:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker==================
4*61c4878aSAndroid Build Coastguard Worker0101: pigweed.json
5*61c4878aSAndroid Build Coastguard Worker==================
6*61c4878aSAndroid Build Coastguard Worker.. seed::
7*61c4878aSAndroid Build Coastguard Worker   :number: 0101
8*61c4878aSAndroid Build Coastguard Worker   :name: pigweed.json
9*61c4878aSAndroid Build Coastguard Worker   :status: Accepted
10*61c4878aSAndroid Build Coastguard Worker   :proposal_date: 2023-02-06
11*61c4878aSAndroid Build Coastguard Worker   :cl: 128010
12*61c4878aSAndroid Build Coastguard Worker   :authors: Rob Mohr
13*61c4878aSAndroid Build Coastguard Worker   :facilitator: Ted Pudlik
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker-------
16*61c4878aSAndroid Build Coastguard WorkerSummary
17*61c4878aSAndroid Build Coastguard Worker-------
18*61c4878aSAndroid Build Coastguard WorkerCombine several of the configuration options downstream projects use to
19*61c4878aSAndroid Build Coastguard Workerconfigure parts of Pigweed in one place, and use this place for further
20*61c4878aSAndroid Build Coastguard Workerconfiguration options.
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker----------
23*61c4878aSAndroid Build Coastguard WorkerMotivation
24*61c4878aSAndroid Build Coastguard Worker----------
25*61c4878aSAndroid Build Coastguard WorkerPigweed-based projects configure Pigweed and themselves in a variety of ways.
26*61c4878aSAndroid Build Coastguard WorkerThe environment setup is controlled by a JSON file that's referenced in
27*61c4878aSAndroid Build Coastguard Worker``bootstrap.sh`` files and in internal infrastructure repos that looks
28*61c4878aSAndroid Build Coastguard Workersomething like this:
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker.. code-block::
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker   {
33*61c4878aSAndroid Build Coastguard Worker     "root_variable": "<PROJNAME>_ROOT",
34*61c4878aSAndroid Build Coastguard Worker     "cipd_package_files": ["tools/default.json"],
35*61c4878aSAndroid Build Coastguard Worker     "virtualenv": {
36*61c4878aSAndroid Build Coastguard Worker       "gn_args": ["dir_pw_third_party_stm32cube=\"\""],
37*61c4878aSAndroid Build Coastguard Worker       "gn_root": ".",
38*61c4878aSAndroid Build Coastguard Worker       "gn_targets": [":python.install"]
39*61c4878aSAndroid Build Coastguard Worker     },
40*61c4878aSAndroid Build Coastguard Worker     "optional_submodules": ["vendor/shhh-secret"],
41*61c4878aSAndroid Build Coastguard Worker     "gni_file": "build_overrides/pigweed_environment.gni"
42*61c4878aSAndroid Build Coastguard Worker   }
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard WorkerThe plugins to the ``pw`` command-line utility are configured in ``PW_PLUGINS``,
45*61c4878aSAndroid Build Coastguard Workerwhich looks like this:
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker.. code-block::
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker   # <name> <Python module> <function>
50*61c4878aSAndroid Build Coastguard Worker   console pw_console.__main__ main
51*61c4878aSAndroid Build Coastguard Worker   format pw_presubmit.format_code _pigweed_upstream_main
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard WorkerIn addition, changes have been proposed to configure some of the behavior of
54*61c4878aSAndroid Build Coastguard Worker``pw format`` and the formatting steps of ``pw presubmit`` from config files,
55*61c4878aSAndroid Build Coastguard Workerbut there's no standard place to put these configuration options.
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker---------------
58*61c4878aSAndroid Build Coastguard WorkerGuide reference
59*61c4878aSAndroid Build Coastguard Worker---------------
60*61c4878aSAndroid Build Coastguard WorkerThis proposal affects two sets of people: developers looking to use Pigweed,
61*61c4878aSAndroid Build Coastguard Workerand developers looking to add configurable features to Pigweed.
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard WorkerDevelopers looking to use Pigweed will have one config file that contains all
64*61c4878aSAndroid Build Coastguard Workerthe options they need to set. Documentation for individual Pigweed modules will
65*61c4878aSAndroid Build Coastguard Workershow only the configuration options relevant for that module, and multiple of
66*61c4878aSAndroid Build Coastguard Workerthese examples can simply be concatenated to form a valid config file.
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard WorkerDevelopers looking to add configurable features to Pigweed no longer need to
69*61c4878aSAndroid Build Coastguard Workerdefine a new file format, figure out where to find it in the tree (or how to
70*61c4878aSAndroid Build Coastguard Workerhave Pigweed-projects specify a location), or parse this format.
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker---------------------
73*61c4878aSAndroid Build Coastguard WorkerProblem investigation
74*61c4878aSAndroid Build Coastguard Worker---------------------
75*61c4878aSAndroid Build Coastguard WorkerThere are multiple issues with the current system that need to be addressed.
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Worker* ``PW_PLUGINS`` works, but is a narrow custom format with exactly one purpose.
78*61c4878aSAndroid Build Coastguard Worker* The environment config file is somewhat extensible, but is still specific to
79*61c4878aSAndroid Build Coastguard Worker  environment setup.
80*61c4878aSAndroid Build Coastguard Worker* There's no accepted place for other modules to retrieve configuration options.
81*61c4878aSAndroid Build Coastguard Worker
82*61c4878aSAndroid Build Coastguard WorkerThese should be combined into a single file. There are several formats that
83*61c4878aSAndroid Build Coastguard Workercould be selected, and many more arguments for and against each. Only a subset
84*61c4878aSAndroid Build Coastguard Workerof these arguments are reproduced here.
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker* JSON does not support comments
87*61c4878aSAndroid Build Coastguard Worker* JSON5 is not supported in the Python standard library
88*61c4878aSAndroid Build Coastguard Worker* XML is too verbose
89*61c4878aSAndroid Build Coastguard Worker* YAML is acceptable, but implicit type conversion could be a problem, and it's
90*61c4878aSAndroid Build Coastguard Worker  not supported in the Python standard library
91*61c4878aSAndroid Build Coastguard Worker* TOML is acceptable, and `was selected for a similar purpose by Python
92*61c4878aSAndroid Build Coastguard Worker  <https://snarky.ca/what-the-heck-is-pyproject-toml/>`_, but it's
93*61c4878aSAndroid Build Coastguard Worker  not supported in the Python standard library before Python v3.11
94*61c4878aSAndroid Build Coastguard Worker* Protobuf Text Format is acceptable and widely used within Google, but is not
95*61c4878aSAndroid Build Coastguard Worker  supported in the Python standard library
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard WorkerThe location of the file is also an issue. Environment config files can be found
98*61c4878aSAndroid Build Coastguard Workerin a variety of locations depending on the project—all of the following paths
99*61c4878aSAndroid Build Coastguard Workerare used by at least one internal Pigweed-based project.
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Worker* ``build/environment.json``
102*61c4878aSAndroid Build Coastguard Worker* ``build/pigweed/env_setup.json``
103*61c4878aSAndroid Build Coastguard Worker* ``environment.json``
104*61c4878aSAndroid Build Coastguard Worker* ``env_setup.json``
105*61c4878aSAndroid Build Coastguard Worker* ``pw_env_setup.json``
106*61c4878aSAndroid Build Coastguard Worker* ``scripts/environment.json``
107*61c4878aSAndroid Build Coastguard Worker* ``tools/environment.json``
108*61c4878aSAndroid Build Coastguard Worker* ``tools/env_setup.json``
109*61c4878aSAndroid Build Coastguard Worker
110*61c4878aSAndroid Build Coastguard Worker``PW_PLUGINS`` files can in theory be in any directory and ``pw`` will search up
111*61c4878aSAndroid Build Coastguard Workerfor them from the current directory, but in practice they only exist at the root
112*61c4878aSAndroid Build Coastguard Workerof checkouts. Having this file in a fixed location with a fixed name makes it
113*61c4878aSAndroid Build Coastguard Workersignificantly easier to find as a user, and the fixed name (if not path) makes
114*61c4878aSAndroid Build Coastguard Workerit easy to find programmatically too.
115*61c4878aSAndroid Build Coastguard Worker
116*61c4878aSAndroid Build Coastguard Worker---------------
117*61c4878aSAndroid Build Coastguard WorkerDetailed design
118*61c4878aSAndroid Build Coastguard Worker---------------
119*61c4878aSAndroid Build Coastguard WorkerThe ``pw_env_setup`` Python module will provide an API to retrieve a parsed
120*61c4878aSAndroid Build Coastguard Worker``pigweed.json`` file from the root of the checkout. ``pw_env_setup`` is the
121*61c4878aSAndroid Build Coastguard Workercorrect location because it can't depend on anything else, but other modules can
122*61c4878aSAndroid Build Coastguard Workerdepend on it. Code in other languages does not yet depend on configuration
123*61c4878aSAndroid Build Coastguard Workerfiles.
124*61c4878aSAndroid Build Coastguard Worker
125*61c4878aSAndroid Build Coastguard WorkerA ``pigweed.json`` file might look like the following. Individual option names
126*61c4878aSAndroid Build Coastguard Workerand structures are not final but will evolve as those options are
127*61c4878aSAndroid Build Coastguard Workerimplemented—this is merely an example of what an actual file could look like.
128*61c4878aSAndroid Build Coastguard WorkerThe ``pw`` namespace is reserved for Pigweed, but other projects can use other
129*61c4878aSAndroid Build Coastguard Workernamespaces for their own needs. Within the ``pw`` namespace all options are
130*61c4878aSAndroid Build Coastguard Workerfirst grouped by their module name, which simplifies searching for the code and
131*61c4878aSAndroid Build Coastguard Workerdocumentation related to the option in question.
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker.. code-block::
134*61c4878aSAndroid Build Coastguard Worker
135*61c4878aSAndroid Build Coastguard Worker   {
136*61c4878aSAndroid Build Coastguard Worker     "pw": {
137*61c4878aSAndroid Build Coastguard Worker       "pw_cli": {
138*61c4878aSAndroid Build Coastguard Worker         "plugins": {
139*61c4878aSAndroid Build Coastguard Worker           "console": {
140*61c4878aSAndroid Build Coastguard Worker             "module": "pw_console.__main__",
141*61c4878aSAndroid Build Coastguard Worker             "function": "main"
142*61c4878aSAndroid Build Coastguard Worker           },
143*61c4878aSAndroid Build Coastguard Worker           "format": {
144*61c4878aSAndroid Build Coastguard Worker             "module": "pw_presubmit.format_code",
145*61c4878aSAndroid Build Coastguard Worker             "function": "_pigweed_upstream_main"
146*61c4878aSAndroid Build Coastguard Worker           }
147*61c4878aSAndroid Build Coastguard Worker         }
148*61c4878aSAndroid Build Coastguard Worker       },
149*61c4878aSAndroid Build Coastguard Worker       "pw_env_setup": {
150*61c4878aSAndroid Build Coastguard Worker         "root_variable": "<PROJNAME>_ROOT",
151*61c4878aSAndroid Build Coastguard Worker         "rosetta": "allow",
152*61c4878aSAndroid Build Coastguard Worker         "gni_file": "build_overrides/pigweed_environment.gni",
153*61c4878aSAndroid Build Coastguard Worker         "cipd": {
154*61c4878aSAndroid Build Coastguard Worker           "package_files": [
155*61c4878aSAndroid Build Coastguard Worker             "tools/default.json"
156*61c4878aSAndroid Build Coastguard Worker           ]
157*61c4878aSAndroid Build Coastguard Worker         },
158*61c4878aSAndroid Build Coastguard Worker         "virtualenv": {
159*61c4878aSAndroid Build Coastguard Worker           "gn_args": [
160*61c4878aSAndroid Build Coastguard Worker             "dir_pw_third_party_stm32cube=\"\""
161*61c4878aSAndroid Build Coastguard Worker           ],
162*61c4878aSAndroid Build Coastguard Worker           "gn_targets": [
163*61c4878aSAndroid Build Coastguard Worker             "python.install"
164*61c4878aSAndroid Build Coastguard Worker           ],
165*61c4878aSAndroid Build Coastguard Worker           "gn_root": "."
166*61c4878aSAndroid Build Coastguard Worker         },
167*61c4878aSAndroid Build Coastguard Worker         "submodules": {
168*61c4878aSAndroid Build Coastguard Worker           "optional": [
169*61c4878aSAndroid Build Coastguard Worker             "vendor/shhh-secret"
170*61c4878aSAndroid Build Coastguard Worker           ]
171*61c4878aSAndroid Build Coastguard Worker         }
172*61c4878aSAndroid Build Coastguard Worker       },
173*61c4878aSAndroid Build Coastguard Worker       "pw_presubmit": {
174*61c4878aSAndroid Build Coastguard Worker         "format": {
175*61c4878aSAndroid Build Coastguard Worker           "python": {
176*61c4878aSAndroid Build Coastguard Worker             "formatter": "black",
177*61c4878aSAndroid Build Coastguard Worker             "black_path": "pyink"
178*61c4878aSAndroid Build Coastguard Worker           }
179*61c4878aSAndroid Build Coastguard Worker         }
180*61c4878aSAndroid Build Coastguard Worker       }
181*61c4878aSAndroid Build Coastguard Worker     }
182*61c4878aSAndroid Build Coastguard Worker   }
183*61c4878aSAndroid Build Coastguard Worker
184*61c4878aSAndroid Build Coastguard WorkerSome teams will resist a new file at the root of their checkout, but this seed
185*61c4878aSAndroid Build Coastguard Workerwon't be adding any files, it'll be combining at least one top-level file, maybe
186*61c4878aSAndroid Build Coastguard Workertwo, into a new top-level file, so there won't be any additional files in the
187*61c4878aSAndroid Build Coastguard Workercheckout root.
188*61c4878aSAndroid Build Coastguard Worker
189*61c4878aSAndroid Build Coastguard Worker------------
190*61c4878aSAndroid Build Coastguard WorkerAlternatives
191*61c4878aSAndroid Build Coastguard Worker------------
192*61c4878aSAndroid Build Coastguard Worker``pw format`` and the formatting steps of ``pw presubmit`` could read from yet
193*61c4878aSAndroid Build Coastguard Workeranother config file, further fracturing Pigweed's configuration.
194*61c4878aSAndroid Build Coastguard Worker
195*61c4878aSAndroid Build Coastguard WorkerA different file format could be chosen over JSON. Since JSON is parsed into
196*61c4878aSAndroid Build Coastguard Workeronly Python lists, dicts, and primitives, switching to another format that can
197*61c4878aSAndroid Build Coastguard Workerbe parsed into the same internal structure should be trivial.
198*61c4878aSAndroid Build Coastguard Worker
199*61c4878aSAndroid Build Coastguard Worker--------------
200*61c4878aSAndroid Build Coastguard WorkerOpen questions
201*61c4878aSAndroid Build Coastguard Worker--------------
202*61c4878aSAndroid Build Coastguard WorkerNone?
203