What exactly does directory `target/release/build` do?

After compiling a project, target/release/deps has the metadata files for all the dependent crates. I saw there are several directories generated in target/release/build. I'm confused with what exactly this build directory does?

For example, for the prorgram exa, the build directory contains the following directories: 2 exa-some_hash, 3 libc-some_hash, 2 libgit2-sys-some_hash, libz-sys-some_hash, and log-some_hash. exa has about 40 dependent crates. I'm confused that why only these crates related files are generated?

Here is the what cargo tree gives for exa:

exa v0.10.1 (/Users/jie/projects/srust/progs/exa)
├── ansi_term v0.12.1
├── datetime v0.5.2
│   ├── libc v0.2.117
│   ├── locale v0.2.2
│   │   └── libc v0.2.117
│   └── pad v0.1.6
│       └── unicode-width v0.1.9
├── git2 v0.13.25
│   ├── bitflags v1.3.2
│   ├── libc v0.2.117
│   ├── libgit2-sys v0.12.26+1.3.0
│   │   ├── libc v0.2.117
│   │   └── libz-sys v1.1.3
│   │       └── libc v0.2.117
│   │       [build-dependencies]
│   │       ├── cc v1.0.72
│   │       │   └── jobserver v0.1.24
│   │       │       └── libc v0.2.117
│   │       └── pkg-config v0.3.24
│   │   [build-dependencies]
│   │   ├── cc v1.0.72 (*)
│   │   └── pkg-config v0.3.24
│   ├── log v0.4.14
│   │   └── cfg-if v1.0.0
│   └── url v2.2.2
│       ├── form_urlencoded v1.0.1
│       │   ├── matches v0.1.9
│       │   └── percent-encoding v2.1.0
│       ├── idna v0.2.3
│       │   ├── matches v0.1.9
│       │   ├── unicode-bidi v0.3.7
│       │   └── unicode-normalization v0.1.19
│       │       └── tinyvec v1.5.1
│       │           └── tinyvec_macros v0.1.0
│       ├── matches v0.1.9
│       └── percent-encoding v2.1.0
├── glob v0.3.0
├── lazy_static v1.4.0
├── libc v0.2.117
├── locale v0.2.2 (*)
├── log v0.4.14 (*)
├── natord v1.0.9
├── num_cpus v1.13.1
│   └── libc v0.2.117
├── number_prefix v0.4.0
├── scoped_threadpool v0.1.9
├── term_grid v0.2.0
│   └── unicode-width v0.1.9
├── terminal_size v0.1.17
│   └── libc v0.2.117
├── unicode-width v0.1.9
├── users v0.11.0
│   ├── libc v0.2.117
│   └── log v0.4.14 (*)
└── zoneinfo_compiled v0.5.1
    ├── byteorder v1.4.3
    └── datetime v0.5.2 (*)
[build-dependencies]
└── datetime v0.5.2 (*)

I believe that's the output directory for build.rs files.

1 Like

Some crates under build-dependencies are indeed in the build directory. However, some are not. For example, in the exa example above, the log crate appears in build but it seems not to be under the build-dependencies of any crate.

I think it's just crates with a direct build script, not all crates that need to be compiled for build dependencies

So if my crate A has a build script that needs crate B to build, A would have a folder but B would not.

1 Like

Yes this makes sense, and it explains why some crates appearing in cargo tree's build-dependencies do not appear in the target/release/build directory. My confusion is the other way around: Why log appears in the build directory, while it is not a dependency for any other crates in build-dependencies?

Because exa depends on log and log has a build script

2 Likes