xref: /aosp_15_r20/external/pigweed/pw_ide/guide/cli.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_ide-guide-cli:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker==========
4*61c4878aSAndroid Build Coastguard Workerpw_ide CLI
5*61c4878aSAndroid Build Coastguard Worker==========
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module-subpage::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_ide
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerThe ``pw_ide`` command-line interface (CLI) provides an editor-independent
10*61c4878aSAndroid Build Coastguard Workermechanism for managing code intelligence for Pigweed projects.
11*61c4878aSAndroid Build Coastguard Worker
12*61c4878aSAndroid Build Coastguard Worker.. note::
13*61c4878aSAndroid Build Coastguard Worker
14*61c4878aSAndroid Build Coastguard Worker   Currently, the CLI only supports :ref:`bootstrap projects<module-pw_ide-design-projects-bootstrap>`,
15*61c4878aSAndroid Build Coastguard Worker   For :ref:`Bazel projects<module-pw_ide-design-projects-bazel>`, we recommend
16*61c4878aSAndroid Build Coastguard Worker   using the :ref:`Visual Studio Code integration<module-pw_ide-guide-vscode>`.
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker--------------------------------
19*61c4878aSAndroid Build Coastguard WorkerSetting up C++ code intelligence
20*61c4878aSAndroid Build Coastguard Worker--------------------------------
21*61c4878aSAndroid Build Coastguard Worker`clangd <https://clangd.llvm.org/>`_ is a language server that provides C/C++
22*61c4878aSAndroid Build Coastguard Workercode intelligence features to any editor that supports the language server
23*61c4878aSAndroid Build Coastguard Workerprotocol (LSP). It uses a `compilation database <https://clang.llvm.org/docs/JSONCompilationDatabase.html>`_,
24*61c4878aSAndroid Build Coastguard Workera JSON file containing the compile commands for the project. Projects that have
25*61c4878aSAndroid Build Coastguard Workermultiple targets and/or use multiple toolchains need separate compilation
26*61c4878aSAndroid Build Coastguard Workerdatabases for each target toolchain. ``pw_ide`` provides tools for managing
27*61c4878aSAndroid Build Coastguard Workerthose databases.
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard WorkerAssuming you have one or more compilation databases that have been generated by
30*61c4878aSAndroid Build Coastguard Workeryour build system, start with:
31*61c4878aSAndroid Build Coastguard Worker
32*61c4878aSAndroid Build Coastguard Worker.. code-block:: bash
33*61c4878aSAndroid Build Coastguard Worker
34*61c4878aSAndroid Build Coastguard Worker   pw ide sync
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard WorkerThis command will:
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker- Find every compilation database in your build directory
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker- Analyze each database
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard Worker  - If a database is internally consistent (i.e., it only contains valid
43*61c4878aSAndroid Build Coastguard Worker    compile commands for a single target), it will use that database as-is for
44*61c4878aSAndroid Build Coastguard Worker    the target toolchain that database pertains to. This is the typical case for
45*61c4878aSAndroid Build Coastguard Worker    CMake builds.
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker  - Otherwise, if a database contains commands for multiple target toolchains
48*61c4878aSAndroid Build Coastguard Worker    and/or contains invalid compile commands, the database will be processed,
49*61c4878aSAndroid Build Coastguard Worker    yielding one new compilation database for each target toolchain. Those
50*61c4878aSAndroid Build Coastguard Worker    databases will be used instead of the original.
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker- Link each target to its respective compilation database
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard WorkerNow, you can list the available target toolchains with:
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker.. code-block:: bash
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker   pw ide cpp --list
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard WorkerThen set the target toolchain that ``clangd`` should use with:
61*61c4878aSAndroid Build Coastguard Worker
62*61c4878aSAndroid Build Coastguard Worker.. code-block:: bash
63*61c4878aSAndroid Build Coastguard Worker
64*61c4878aSAndroid Build Coastguard Worker   pw ide cpp --set <selected target name>
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker``clangd`` will now work as designed since it is configured to use a compilation
67*61c4878aSAndroid Build Coastguard Workerdatabase that is consistent to just a single target toolchain.
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Worker``clangd`` must be run with arguments that provide the Pigweed environment paths
70*61c4878aSAndroid Build Coastguard Workerto the correct toolchains and sysroots. One way to do this is to launch your
71*61c4878aSAndroid Build Coastguard Workereditor from the terminal in an activated environment (e.g. running ``vim`` from
72*61c4878aSAndroid Build Coastguard Workerthe terminal), in which case nothing special needs to be done as long as your
73*61c4878aSAndroid Build Coastguard Workertoolchains are in the Pigweed environment or ``$PATH``. But if you launch your
74*61c4878aSAndroid Build Coastguard Workereditor outside of the activated environment (e.g. launching Visual Studio Code
75*61c4878aSAndroid Build Coastguard Workerfrom your GUI shell's launcher), you can generate the command that invokes
76*61c4878aSAndroid Build Coastguard Worker``clangd`` with the right arguments with:
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard Worker.. code-block:: bash
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker   pw ide cpp --clangd-command
81*61c4878aSAndroid Build Coastguard Worker
82*61c4878aSAndroid Build Coastguard Worker-----------------------------------
83*61c4878aSAndroid Build Coastguard WorkerSetting up Python code intelligence
84*61c4878aSAndroid Build Coastguard Worker-----------------------------------
85*61c4878aSAndroid Build Coastguard WorkerAny Python language server should work well with Pigweed projects as long as
86*61c4878aSAndroid Build Coastguard Workerit's configured to use the Pigweed virtual environment. You can output the path
87*61c4878aSAndroid Build Coastguard Workerto the virtual environment on your system with:
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker.. code-block:: bash
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker   pw ide python --venv
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard Worker---------------------------------
94*61c4878aSAndroid Build Coastguard WorkerSetting up docs code intelligence
95*61c4878aSAndroid Build Coastguard Worker---------------------------------
96*61c4878aSAndroid Build Coastguard WorkerThe `esbonio <https://github.com/swyddfa/esbonio>`_ language server will provide
97*61c4878aSAndroid Build Coastguard Workercode intelligence for reStructuredText and Sphinx. It works well with Pigweed
98*61c4878aSAndroid Build Coastguard Workerprojects as long as it is pointed to Pigweed's Python virtual environment. For
99*61c4878aSAndroid Build Coastguard WorkerVisual Studio Code, simply install the esbonio extension, which will be
100*61c4878aSAndroid Build Coastguard Workerrecommended to you after setting up ``pw_ide``. Once it's installed, a prompt
101*61c4878aSAndroid Build Coastguard Workerwill ask if you want to automatically install esbonio in your Pigweed Python
102*61c4878aSAndroid Build Coastguard Workerenvironment. After that, give esbonio some time to index, then you're done!
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard Worker--------------------------------
105*61c4878aSAndroid Build Coastguard WorkerCommand-line interface reference
106*61c4878aSAndroid Build Coastguard Worker--------------------------------
107*61c4878aSAndroid Build Coastguard Worker.. argparse::
108*61c4878aSAndroid Build Coastguard Worker   :module: pw_ide.cli
109*61c4878aSAndroid Build Coastguard Worker   :func: _build_argument_parser
110*61c4878aSAndroid Build Coastguard Worker   :prog: pw ide
111*61c4878aSAndroid Build Coastguard Worker
112*61c4878aSAndroid Build Coastguard Worker-------------
113*61c4878aSAndroid Build Coastguard WorkerConfiguration
114*61c4878aSAndroid Build Coastguard Worker-------------
115*61c4878aSAndroid Build Coastguard Worker``pw_ide`` has a built-in default configuration, so you don't need to create
116*61c4878aSAndroid Build Coastguard Workera configuration file to get started. You can create a configuration file if you
117*61c4878aSAndroid Build Coastguard Workerneed to override those defaults.
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard WorkerA project configuration can be defined in ``.pw_ide.yaml`` in the project root.
120*61c4878aSAndroid Build Coastguard WorkerThis configuration will be checked into source control and apply to all
121*61c4878aSAndroid Build Coastguard Workerdevelopers of the project. Each user can also create a ``.pw_ide.user.yaml``
122*61c4878aSAndroid Build Coastguard Workerfile that overrides both the default and project settings, is not checked into
123*61c4878aSAndroid Build Coastguard Workersource control, and applies only to that checkout of the project. All of these
124*61c4878aSAndroid Build Coastguard Workerfiles have the same schema, in which these options can be configured:
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.working_dir
127*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.compdb_gen_cmd
128*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.compdb_search_paths
129*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.target_inference
130*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.targets_include
131*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.targets_exclude
132*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.default_target
133*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.cascade_targets
134*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.sync
135*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.clangd_additional_query_drivers
136*61c4878aSAndroid Build Coastguard Worker.. autoproperty:: pw_ide.settings.PigweedIdeSettings.workspace_root
137*61c4878aSAndroid Build Coastguard Worker
138*61c4878aSAndroid Build Coastguard WorkerWhen to provide additional configuration to support your use cases
139*61c4878aSAndroid Build Coastguard Worker==================================================================
140*61c4878aSAndroid Build Coastguard WorkerThe default configuration for ``clangd`` in ``pw_ide`` should work without
141*61c4878aSAndroid Build Coastguard Workeradditional configuration as long as you're using only toolchains provided by
142*61c4878aSAndroid Build Coastguard WorkerPigweed and your native host toolchain. If you're using other toolchains, keep
143*61c4878aSAndroid Build Coastguard Workerreading.
144*61c4878aSAndroid Build Coastguard Worker
145*61c4878aSAndroid Build Coastguard Worker``clangd`` needs two pieces of information to use a toolchain:
146*61c4878aSAndroid Build Coastguard Worker
147*61c4878aSAndroid Build Coastguard Worker#. A path to the compiler, which will be taken from the compile command.
148*61c4878aSAndroid Build Coastguard Worker
149*61c4878aSAndroid Build Coastguard Worker#. The same compiler to be reflected in the
150*61c4878aSAndroid Build Coastguard Worker   `query driver <https://releases.llvm.org/10.0.0/tools/clang/tools/extra/docs/clangd/Configuration.html>`_
151*61c4878aSAndroid Build Coastguard Worker   argument provided when running ``clangd``.
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard WorkerWhen using ``pw_ide`` with external toolchains, you only need to add a path to
154*61c4878aSAndroid Build Coastguard Workerthe compiler to ``clangd_additional_query_drivers`` in your project's
155*61c4878aSAndroid Build Coastguard Worker``pw_ide.yaml`` file. When processing a compilation database, ``pw_ide`` will
156*61c4878aSAndroid Build Coastguard Workeruse the query driver globs to find your compiler and configure ``clangd`` to
157*61c4878aSAndroid Build Coastguard Workeruse it.
158*61c4878aSAndroid Build Coastguard Worker
159*61c4878aSAndroid Build Coastguard WorkerUsing compiler wrappers
160*61c4878aSAndroid Build Coastguard Worker=======================
161*61c4878aSAndroid Build Coastguard WorkerIf you're using ``ccache`` or any other wrapper command that is configured
162*61c4878aSAndroid Build Coastguard Workerusing ``ccache``'s' ``KEY=VALUE`` pattern, it will work out of the box.
163