diff --git a/docs/project/contribution_tools.md b/docs/project/contribution_tools.md index 7e7fc7aeb8b7..127a5a2e7067 100644 --- a/docs/project/contribution_tools.md +++ b/docs/project/contribution_tools.md @@ -24,6 +24,7 @@ contributions. - [Optional tools](#optional-tools) - [Jujutsu (`jj`)](#jujutsu-jj) - [AI assistants](#ai-assistants) + - [Updating tools installed with `cargo`](#updating-tools-installed-with-cargo) - [Running tests with AddressSanitizer (ASan)](#running-tests-with-addresssanitizer-asan) - [Manually building Clang and LLVM (not recommended)](#manually-building-clang-and-llvm-not-recommended) - [Troubleshooting build issues](#troubleshooting-build-issues) @@ -251,7 +252,7 @@ considering if they fit your workflow. purposes, such as editing files, without interfering with `bazel`. - We also provide recommended setups for debugging in VS Code with either [LLDB](/toolchain/docs/debugging.md#debugging-with-lldb) or - [GDB]((/toolchain/docs/debugging.md#debugging-with-gdb) + [GDB](/toolchain/docs/debugging.md#debugging-with-gdb) - [clangd](https://clangd.llvm.org/installation): An LSP server implementation for C/C++. @@ -259,12 +260,10 @@ considering if they fit your workflow. generated file called `compile_commands.json`. This can be generated by invoking the command below: - ``` + ```sh ./scripts/create_compdb.py ``` - - **NOTE**: This assumes you have `python` 3 installed on your system. - #### Jujutsu (`jj`) [Jujutsu](https://github.com/jj-vcs/jj) is a Git-compatible version control @@ -345,6 +344,16 @@ git show git status ``` +### Updating tools installed with `cargo` + +We recommend and use a number of tools that are installed by way of +`cargo install --locked`. To update these tools you can run the following +command: + +```sh +./scripts/cargo_update.py +``` + ### Running tests with AddressSanitizer (ASan) By default, the Bazel build mode for the toolchain does not enable diff --git a/scripts/cargo_update.py b/scripts/cargo_update.py new file mode 100755 index 000000000000..6b0ae5ef0637 --- /dev/null +++ b/scripts/cargo_update.py @@ -0,0 +1,60 @@ +#!/usr/bin/env -S uv run --script + +# /// script +# requires-python = ">=3.12" +# /// + + +"""Update any binaries installed by cargo. + +This script collects a list of binaries that were installed by cargo, via `cargo +install --locked`, and it installs a newer version if there is one available. +""" + +__copyright__ = """ +Part of the Carbon Language project, under the Apache License v2.0 with LLVM +Exceptions. See /LICENSE for license information. +SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +""" + +import subprocess +import sys + + +def Run( + cmd: list[str], capture_output: bool = True +) -> subprocess.CompletedProcess[bytes]: + return subprocess.run(cmd, capture_output=capture_output) + + +def main() -> int: + out = Run(["cargo", "install", "--list"]) + if out.returncode != 0: + return out.returncode + lines = out.stdout.decode("utf-8").splitlines() + + i = 0 + while i < len(lines): + package_line = lines[i] + i = i + 1 + + bins = [] + while i < len(lines) and lines[i][0] == " ": + # bin line + bins.append(lines[i].strip()) + i = i + 1 + + package = package_line.split()[0] + print(f"Updating {package}") + args = ["cargo", "install", "--locked", package] + for b in bins: + args.extend(["--bin", b]) + ran = Run(args, capture_output=False) + if ran.returncode != 0: + return out.returncode + + return 0 + + +if __name__ == "__main__": + sys.exit(main())