|
| 1 | +# Cargo Architecture |
| 2 | + |
| 3 | +This document gives a high level overview of Cargo internals. You may |
| 4 | +find it useful if you want to contribute to Cargo or if you are |
| 5 | +interested in the inner workings of Cargo. |
| 6 | + |
| 7 | + |
| 8 | +## Subcommands |
| 9 | + |
| 10 | +Cargo is organized as a set of `clap` subcommands. All subcommands live in |
| 11 | +`src/bin/commands` directory. `src/bin/cargo.rs` is the entry point. |
| 12 | + |
| 13 | +A typical subcommand, such as `src/bin/commands/build.rs`, parses command line |
| 14 | +options, reads the configuration files, discovers the Cargo project in |
| 15 | +the current directory and delegates the actual implementation to one |
| 16 | +of the functions in `src/cargo/ops/mod.rs`. This short file is a good |
| 17 | +place to find out about most of the things that Cargo can do. |
| 18 | + |
| 19 | + |
| 20 | +## Important Data Structures |
| 21 | + |
| 22 | +There are some important data structures which are used throughout |
| 23 | +Cargo. |
| 24 | + |
| 25 | +`Config` is available almost everywhere and holds "global" |
| 26 | +information, such as `CARGO_HOME` or configuration from |
| 27 | +`.cargo/config` files. The `shell` method of `Config` is the entry |
| 28 | +point for printing status messages and other info to the console. |
| 29 | + |
| 30 | +`Workspace` is the description of the workspace for the current |
| 31 | +working directory. Each workspace contains at least one |
| 32 | +`Package`. Each package corresponds to a single `Cargo.toml`, and may |
| 33 | +define several `Target`s, such as the library, binaries, integration |
| 34 | +test or examples. Targets are crates (each target defines a crate |
| 35 | +root, like `src/lib.rs` or `examples/foo.rs`) and are what is actually |
| 36 | +compiled by `rustc`. |
| 37 | + |
| 38 | +A typical package defines the single library target and several |
| 39 | +auxiliary ones. Packages are a unit of dependency in Cargo, and when |
| 40 | +package `foo` depends on package `bar`, that means that each target |
| 41 | +from `foo` needs the library target from `bar`. |
| 42 | + |
| 43 | +`PackageId` is the unique identifier of a (possibly remote) |
| 44 | +package. It consist of three components: name, version and source |
| 45 | +id. Source is the place where the source code for package comes |
| 46 | +from. Typical sources are crates.io, a git repository or a folder on |
| 47 | +the local hard drive. |
| 48 | + |
| 49 | +`Resolve` is the representation of a directed acyclic graph of package |
| 50 | +dependencies, which uses `PackageId`s for nodes. This is the data |
| 51 | +structure that is saved to the lock file. If there is no lockfile, |
| 52 | +Cargo constructs a resolve by finding a graph of packages which |
| 53 | +matches declared dependency specification according to semver. |
| 54 | + |
| 55 | + |
| 56 | +## Persistence |
| 57 | + |
| 58 | +Cargo is a non-daemon command line application, which means that all |
| 59 | +the information used by Cargo must be persisted on the hard drive. The |
| 60 | +main sources of information are `Cargo.toml` and `Cargo.lock` files, |
| 61 | +`.cargo/config` configuration files and the globally shared registry |
| 62 | +of packages downloaded from crates.io, usually located at |
| 63 | +`~/.cargo/registry`. See `src/sources/registry` for the specifics of |
| 64 | +the registry storage format. |
| 65 | + |
| 66 | + |
| 67 | +## Concurrency |
| 68 | + |
| 69 | +Cargo is mostly single threaded. The only concurrency inside a single |
| 70 | +instance of Cargo happens during compilation, when several instances |
| 71 | +of `rustc` are invoked in parallel to build independent |
| 72 | +targets. However there can be several different instances of Cargo |
| 73 | +process running concurrently on the system. Cargo guarantees that this |
| 74 | +is always safe by using file locks when accessing potentially shared |
| 75 | +data like the registry or the target directory. |
| 76 | + |
| 77 | + |
| 78 | +## Tests |
| 79 | + |
| 80 | +Cargo has an impressive test suite located in the `tests` folder. Most |
| 81 | +of the test are integration: a project structure with `Cargo.toml` and |
| 82 | +rust source code is created in a temporary directory, `cargo` binary |
| 83 | +is invoked via `std::process::Command` and then stdout and stderr are |
| 84 | +verified against the expected output. To simplify testing, several |
| 85 | +macros of the form `[MACRO]` are used in the expected output. For |
| 86 | +example, `[..]` matches any string and `[/]` matches `/` on Unixes and |
| 87 | +`\` on windows. |
| 88 | + |
| 89 | +To see stdout and stderr streams of the subordinate process, add `.stream()` |
| 90 | +call to `execs()`: |
| 91 | + |
| 92 | +```rust |
| 93 | +// Before |
| 94 | +assert_that( |
| 95 | + p.cargo("run"), |
| 96 | + execs().with_status(0) |
| 97 | +); |
| 98 | + |
| 99 | +// After |
| 100 | +assert_that( |
| 101 | + p.cargo("run"), |
| 102 | + execs().stream().with_status(0) |
| 103 | +); |
| 104 | +``` |
| 105 | + |
| 106 | +Alternatively to build and run a custom version of cargo simply run `cargo build` |
| 107 | +and execute `target/debug/cargo`. Note that `+nightly`/`+stable` (and variants), |
| 108 | +being [rustup](https://rustup.rs/) features, won't work when executing the locally |
| 109 | +built cargo binary directly, you have to instead build with `cargo +nightly build` |
| 110 | +and run with `rustup run` (e.g `rustup run nightly |
| 111 | +<path-to-cargo>/target/debug/cargo <args>..`) (or set the `RUSTC` env var to point |
| 112 | +to nightly rustc). |
| 113 | + |
| 114 | +Because the test suite has `#![deny(warnings)]` at times you might find it |
| 115 | +convenient to override this with `RUSTFLAGS`, for example |
| 116 | +`RUSTFLAGS="--cap-lints warn" cargo build`. |
| 117 | + |
| 118 | +## Logging |
| 119 | + |
| 120 | +Cargo uses [`env_logger`](https://docs.rs/env_logger/*/env_logger/), so you can set |
| 121 | +`RUST_LOG` environment variable to get the logs. This is useful both for diagnosing |
| 122 | +bugs in stable Cargo and for local development. Cargo also has internal hierarchical |
| 123 | +profiling infrastructure, which is activated via `CARGO_PROFILE` variable |
| 124 | + |
| 125 | +``` |
| 126 | +# Outputs all logs with levels debug and higher |
| 127 | +$ RUST_LOG=debug cargo generate-lockfile |
| 128 | +
|
| 129 | +# Don't forget that you can filter by module as well |
| 130 | +$ RUST_LOG=cargo::core::resolver=trace cargo generate-lockfile |
| 131 | +
|
| 132 | +# Output first three levels of profiling info |
| 133 | +$ CARGO_PROFILE=3 cargo generate-lockfile |
| 134 | +``` |
0 commit comments