zccache 1.12.16

Local-first compiler cache for C/C++/Rust/Emscripten
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Protobuf structs and conversions for Rust plans and bundle manifests.

use crate::core::NormalizedPath;

use super::manifest::{
    RustArtifactBundleLayerKind, RustArtifactBundleManifest, RustBundledArtifact,
};
use super::schema::{
    ensure_supported_cache_schema_version, RustArtifactClass, RustArtifactPlanV1,
    RustPlanArtifactOwner, RustPlanArtifactOwnerKind, RustPlanError, RustPlanInputs, RustPlanMode,
    RustPlanOwnershipMode, RustPlanPackages, RustToolchainIdentity,
    RUST_ARTIFACT_PLAN_SCHEMA_VERSION,
};

pub(super) mod rust_plan_proto {
    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustArtifactPlanV1 {
        #[prost(uint32, tag = "1")]
        pub schema_version: u32,
        #[prost(uint32, tag = "2")]
        pub mode: u32,
        #[prost(string, tag = "3")]
        pub workspace_root: String,
        #[prost(string, tag = "4")]
        pub target_dir: String,
        #[prost(message, optional, tag = "5")]
        pub toolchain: Option<RustToolchainIdentity>,
        #[prost(string, tag = "6")]
        pub target_triple: String,
        #[prost(string, tag = "7")]
        pub profile: String,
        #[prost(message, optional, tag = "8")]
        pub inputs: Option<RustPlanInputs>,
        #[prost(message, optional, tag = "9")]
        pub packages: Option<RustPlanPackages>,
        #[prost(uint32, repeated, tag = "10")]
        pub allowed_artifact_classes: Vec<u32>,
        #[prost(uint32, tag = "11")]
        pub cache_schema_version: u32,
        #[prost(string, tag = "12")]
        pub journal_log_path: String,
        #[prost(string, tag = "13")]
        pub cache_profile: String,
        #[prost(uint32, repeated, tag = "14")]
        pub dropped_artifact_classes: Vec<u32>,
        #[prost(string, repeated, tag = "15")]
        pub cargo_artifact_paths: Vec<String>,
        #[prost(bool, tag = "16")]
        pub cargo_artifacts_complete: bool,
    }

    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustToolchainIdentity {
        #[prost(string, tag = "1")]
        pub rustc: String,
        #[prost(string, tag = "2")]
        pub cargo: String,
        #[prost(string, tag = "3")]
        pub channel: String,
        #[prost(string, tag = "4")]
        pub host: String,
    }

    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustPlanInputs {
        #[prost(string, tag = "1")]
        pub features_hash: String,
        #[prost(string, tag = "2")]
        pub rustflags_hash: String,
        #[prost(string, tag = "3")]
        pub env_hash: String,
        #[prost(string, tag = "4")]
        pub lockfile_hash: String,
        #[prost(string, tag = "5")]
        pub cargo_config_hash: String,
        #[prost(string, repeated, tag = "6")]
        pub manifest_hashes: Vec<String>,
    }

    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustPlanPackages {
        #[prost(string, repeated, tag = "1")]
        pub selected_package_ids: Vec<String>,
        #[prost(string, repeated, tag = "2")]
        pub workspace_package_ids: Vec<String>,
        #[prost(string, repeated, tag = "3")]
        pub excluded_path_package_ids: Vec<String>,
        #[prost(string, tag = "4")]
        pub ownership_policy: String,
        #[prost(uint32, tag = "5")]
        pub ownership_mode: u32,
        #[prost(message, repeated, tag = "6")]
        pub artifact_owners: Vec<RustPlanArtifactOwner>,
        #[prost(bool, tag = "7")]
        pub ownership_complete: bool,
    }

    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustPlanArtifactOwner {
        #[prost(string, tag = "1")]
        pub relative_path: String,
        #[prost(string, tag = "2")]
        pub package_id: String,
        #[prost(uint32, tag = "3")]
        pub owner: u32,
    }

    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustArtifactBundleManifest {
        #[prost(uint32, tag = "1")]
        pub manifest_schema_version: u32,
        #[prost(uint32, tag = "2")]
        pub plan_schema_version: u32,
        #[prost(uint32, tag = "3")]
        pub cache_schema_version: u32,
        #[prost(uint32, tag = "4")]
        pub mode: u32,
        #[prost(string, tag = "5")]
        pub cache_key: String,
        #[prost(uint64, tag = "6")]
        pub created_at_secs: u64,
        #[prost(string, tag = "7")]
        pub plan_identity_hash: String,
        #[prost(message, repeated, tag = "8")]
        pub artifacts: Vec<RustBundledArtifact>,
        #[prost(uint32, tag = "9")]
        pub layer_kind: u32,
        #[prost(string, tag = "10")]
        pub base_cache_key: String,
        #[prost(string, repeated, tag = "11")]
        pub deleted_paths: Vec<String>,
    }

    #[derive(Clone, PartialEq, ::prost::Message)]
    pub struct RustBundledArtifact {
        #[prost(string, tag = "1")]
        pub relative_path: String,
        #[prost(uint32, tag = "2")]
        pub class: u32,
        #[prost(uint64, tag = "3")]
        pub size: u64,
        #[prost(string, tag = "4")]
        pub content_hash: String,
        #[prost(uint64, tag = "5")]
        pub mtime_unix_nanos: u64,
        #[prost(uint32, tag = "6")]
        pub owner: u32,
    }
}

fn plan_mode_to_proto(mode: RustPlanMode) -> u32 {
    match mode {
        RustPlanMode::Thin => 1,
        RustPlanMode::Full => 2,
    }
}

fn plan_mode_from_proto(value: u32) -> Result<RustPlanMode, RustPlanError> {
    match value {
        1 => Ok(RustPlanMode::Thin),
        2 => Ok(RustPlanMode::Full),
        other => Err(RustPlanError::InvalidManifest(format!(
            "unknown plan mode code {other}"
        ))),
    }
}

fn artifact_class_to_proto(class: RustArtifactClass) -> u32 {
    match class {
        RustArtifactClass::Rlib => 1,
        RustArtifactClass::Rmeta => 2,
        RustArtifactClass::DepInfo => 3,
        RustArtifactClass::ProcMacro => 4,
        RustArtifactClass::SharedLib => 5,
        RustArtifactClass::CargoFingerprint => 6,
        RustArtifactClass::CargoFingerprintMeta => 7,
        RustArtifactClass::CargoFingerprintOutputs => 8,
        RustArtifactClass::BuildScriptMetadata => 9,
        RustArtifactClass::BuildScriptOutput => 10,
        RustArtifactClass::BuildScriptBuild => 11,
        RustArtifactClass::Incremental => 12,
        RustArtifactClass::Dwo => 13,
        RustArtifactClass::Pdb => 14,
        RustArtifactClass::Dsym => 15,
        RustArtifactClass::FullTarget => 16,
    }
}

fn artifact_class_from_proto(value: u32) -> Result<RustArtifactClass, RustPlanError> {
    match value {
        1 => Ok(RustArtifactClass::Rlib),
        2 => Ok(RustArtifactClass::Rmeta),
        3 => Ok(RustArtifactClass::DepInfo),
        4 => Ok(RustArtifactClass::ProcMacro),
        5 => Ok(RustArtifactClass::SharedLib),
        6 => Ok(RustArtifactClass::CargoFingerprint),
        7 => Ok(RustArtifactClass::CargoFingerprintMeta),
        8 => Ok(RustArtifactClass::CargoFingerprintOutputs),
        9 => Ok(RustArtifactClass::BuildScriptMetadata),
        10 => Ok(RustArtifactClass::BuildScriptOutput),
        11 => Ok(RustArtifactClass::BuildScriptBuild),
        12 => Ok(RustArtifactClass::Incremental),
        13 => Ok(RustArtifactClass::Dwo),
        14 => Ok(RustArtifactClass::Pdb),
        15 => Ok(RustArtifactClass::Dsym),
        16 => Ok(RustArtifactClass::FullTarget),
        other => Err(RustPlanError::InvalidManifest(format!(
            "unknown artifact class code {other}"
        ))),
    }
}

fn layer_kind_to_proto(kind: RustArtifactBundleLayerKind) -> u32 {
    match kind {
        RustArtifactBundleLayerKind::Complete => 0,
        RustArtifactBundleLayerKind::Base => 1,
        RustArtifactBundleLayerKind::Delta => 2,
    }
}

fn layer_kind_from_proto(value: u32) -> Result<RustArtifactBundleLayerKind, RustPlanError> {
    match value {
        0 => Ok(RustArtifactBundleLayerKind::Complete),
        1 => Ok(RustArtifactBundleLayerKind::Base),
        2 => Ok(RustArtifactBundleLayerKind::Delta),
        other => Err(RustPlanError::InvalidManifest(format!(
            "unknown layer kind code {other}"
        ))),
    }
}

fn normalized_path_to_proto(path: &NormalizedPath) -> String {
    path.as_path().to_string_lossy().into_owned()
}

fn optional_normalized_path_to_proto(path: &Option<NormalizedPath>) -> String {
    path.as_ref()
        .map(normalized_path_to_proto)
        .unwrap_or_default()
}

fn optional_normalized_path_from_proto(raw: String) -> Option<NormalizedPath> {
    if raw.is_empty() {
        None
    } else {
        Some(NormalizedPath::new(raw))
    }
}

fn optional_string_from_proto(raw: String) -> Option<String> {
    if raw.is_empty() {
        None
    } else {
        Some(raw)
    }
}

fn ownership_mode_to_proto(mode: Option<RustPlanOwnershipMode>) -> u32 {
    match mode {
        None => 0,
        Some(RustPlanOwnershipMode::CookPartitionedV1) => 1,
        Some(RustPlanOwnershipMode::ZccacheAllV1) => 2,
    }
}

fn ownership_mode_from_proto(value: u32) -> Result<Option<RustPlanOwnershipMode>, RustPlanError> {
    match value {
        0 => Ok(None),
        1 => Ok(Some(RustPlanOwnershipMode::CookPartitionedV1)),
        2 => Ok(Some(RustPlanOwnershipMode::ZccacheAllV1)),
        other => Err(RustPlanError::InvalidPlan(format!(
            "unknown ownership mode code {other}"
        ))),
    }
}

fn artifact_owner_to_proto(owner: RustPlanArtifactOwnerKind) -> u32 {
    match owner {
        RustPlanArtifactOwnerKind::Cook => 1,
        RustPlanArtifactOwnerKind::Zccache => 2,
        RustPlanArtifactOwnerKind::ThinV3 => 3,
        RustPlanArtifactOwnerKind::None => 4,
    }
}

fn artifact_owner_from_proto(value: u32) -> Result<RustPlanArtifactOwnerKind, RustPlanError> {
    match value {
        1 => Ok(RustPlanArtifactOwnerKind::Cook),
        2 => Ok(RustPlanArtifactOwnerKind::Zccache),
        3 => Ok(RustPlanArtifactOwnerKind::ThinV3),
        4 => Ok(RustPlanArtifactOwnerKind::None),
        other => Err(RustPlanError::InvalidPlan(format!(
            "unknown artifact owner code {other}"
        ))),
    }
}

pub(super) fn plan_to_proto(plan: &RustArtifactPlanV1) -> rust_plan_proto::RustArtifactPlanV1 {
    rust_plan_proto::RustArtifactPlanV1 {
        schema_version: plan.schema_version,
        mode: plan_mode_to_proto(plan.mode),
        workspace_root: normalized_path_to_proto(&plan.workspace_root),
        target_dir: normalized_path_to_proto(&plan.target_dir),
        toolchain: Some(rust_plan_proto::RustToolchainIdentity {
            rustc: plan.toolchain.rustc.clone(),
            cargo: plan.toolchain.cargo.clone(),
            channel: plan.toolchain.channel.clone(),
            host: plan.toolchain.host.clone(),
        }),
        target_triple: plan.target_triple.clone(),
        profile: plan.profile.clone(),
        inputs: Some(rust_plan_proto::RustPlanInputs {
            features_hash: plan.inputs.features_hash.clone(),
            rustflags_hash: plan.inputs.rustflags_hash.clone(),
            env_hash: plan.inputs.env_hash.clone(),
            lockfile_hash: plan.inputs.lockfile_hash.clone(),
            cargo_config_hash: plan.inputs.cargo_config_hash.clone(),
            manifest_hashes: plan.inputs.manifest_hashes.clone(),
        }),
        packages: Some(rust_plan_proto::RustPlanPackages {
            selected_package_ids: plan.packages.selected_package_ids.clone(),
            workspace_package_ids: plan.packages.workspace_package_ids.clone(),
            excluded_path_package_ids: plan.packages.excluded_path_package_ids.clone(),
            ownership_policy: plan.packages.ownership_policy.clone().unwrap_or_default(),
            ownership_mode: ownership_mode_to_proto(plan.packages.ownership_mode),
            artifact_owners: plan
                .packages
                .artifact_owners
                .iter()
                .map(|record| rust_plan_proto::RustPlanArtifactOwner {
                    relative_path: record.relative_path.clone(),
                    package_id: record.package_id.clone(),
                    owner: artifact_owner_to_proto(record.owner),
                })
                .collect(),
            ownership_complete: plan.packages.ownership_complete,
        }),
        allowed_artifact_classes: plan
            .allowed_artifact_classes
            .iter()
            .copied()
            .map(artifact_class_to_proto)
            .collect(),
        cache_schema_version: plan.cache_schema_version,
        journal_log_path: optional_normalized_path_to_proto(&plan.journal_log_path),
        cache_profile: plan.cache_profile.clone().unwrap_or_default(),
        dropped_artifact_classes: plan
            .dropped_artifact_classes
            .iter()
            .copied()
            .map(artifact_class_to_proto)
            .collect(),
        cargo_artifact_paths: plan.cargo_artifact_paths.clone(),
        cargo_artifacts_complete: plan.cargo_artifacts_complete,
    }
}

pub(super) fn plan_from_proto(
    proto: rust_plan_proto::RustArtifactPlanV1,
) -> Result<RustArtifactPlanV1, RustPlanError> {
    if proto.schema_version != RUST_ARTIFACT_PLAN_SCHEMA_VERSION {
        return Err(RustPlanError::UnsupportedSchemaVersion {
            found: proto.schema_version,
            supported: RUST_ARTIFACT_PLAN_SCHEMA_VERSION,
        });
    }
    ensure_supported_cache_schema_version(proto.cache_schema_version)?;

    let toolchain = proto
        .toolchain
        .ok_or_else(|| RustPlanError::InvalidPlan("toolchain is required".to_string()))?;
    let inputs = proto
        .inputs
        .ok_or_else(|| RustPlanError::InvalidPlan("inputs are required".to_string()))?;
    let packages = proto.packages.unwrap_or_default();
    let plan = RustArtifactPlanV1 {
        schema_version: proto.schema_version,
        mode: plan_mode_from_proto(proto.mode)?,
        workspace_root: NormalizedPath::new(proto.workspace_root),
        target_dir: NormalizedPath::new(proto.target_dir),
        toolchain: RustToolchainIdentity {
            rustc: toolchain.rustc,
            cargo: toolchain.cargo,
            channel: toolchain.channel,
            host: toolchain.host,
        },
        target_triple: proto.target_triple,
        profile: proto.profile,
        inputs: RustPlanInputs {
            features_hash: inputs.features_hash,
            rustflags_hash: inputs.rustflags_hash,
            env_hash: inputs.env_hash,
            lockfile_hash: inputs.lockfile_hash,
            cargo_config_hash: inputs.cargo_config_hash,
            manifest_hashes: inputs.manifest_hashes,
        },
        packages: RustPlanPackages {
            selected_package_ids: packages.selected_package_ids,
            workspace_package_ids: packages.workspace_package_ids,
            excluded_path_package_ids: packages.excluded_path_package_ids,
            ownership_policy: optional_string_from_proto(packages.ownership_policy),
            ownership_mode: ownership_mode_from_proto(packages.ownership_mode)?,
            artifact_owners: packages
                .artifact_owners
                .into_iter()
                .map(|record| {
                    Ok(RustPlanArtifactOwner {
                        relative_path: record.relative_path,
                        package_id: record.package_id,
                        owner: artifact_owner_from_proto(record.owner)?,
                    })
                })
                .collect::<Result<Vec<_>, RustPlanError>>()?,
            ownership_complete: packages.ownership_complete,
        },
        allowed_artifact_classes: proto
            .allowed_artifact_classes
            .into_iter()
            .map(artifact_class_from_proto)
            .collect::<Result<Vec<_>, RustPlanError>>()?,
        cache_schema_version: proto.cache_schema_version,
        journal_log_path: optional_normalized_path_from_proto(proto.journal_log_path),
        cache_profile: optional_string_from_proto(proto.cache_profile),
        dropped_artifact_classes: proto
            .dropped_artifact_classes
            .into_iter()
            .map(artifact_class_from_proto)
            .collect::<Result<Vec<_>, RustPlanError>>()?,
        cargo_artifact_paths: proto.cargo_artifact_paths,
        cargo_artifacts_complete: proto.cargo_artifacts_complete,
    };
    plan.validate()?;
    Ok(plan)
}

pub(super) fn manifest_to_proto(
    manifest: &RustArtifactBundleManifest,
) -> rust_plan_proto::RustArtifactBundleManifest {
    rust_plan_proto::RustArtifactBundleManifest {
        manifest_schema_version: manifest.manifest_schema_version,
        plan_schema_version: manifest.plan_schema_version,
        cache_schema_version: manifest.cache_schema_version,
        mode: plan_mode_to_proto(manifest.mode),
        cache_key: manifest.cache_key.clone(),
        created_at_secs: manifest.created_at_secs,
        plan_identity_hash: manifest.plan_identity_hash.clone(),
        artifacts: manifest
            .artifacts
            .iter()
            .map(|artifact| rust_plan_proto::RustBundledArtifact {
                relative_path: artifact.relative_path.clone(),
                class: artifact_class_to_proto(artifact.class),
                size: artifact.size,
                content_hash: artifact.content_hash.clone(),
                mtime_unix_nanos: artifact.mtime_unix_nanos,
                owner: artifact
                    .owner
                    .map(artifact_owner_to_proto)
                    .unwrap_or_default(),
            })
            .collect(),
        layer_kind: layer_kind_to_proto(manifest.layer_kind),
        base_cache_key: manifest.base_cache_key.clone().unwrap_or_default(),
        deleted_paths: manifest.deleted_paths.clone(),
    }
}

pub(super) fn manifest_from_proto(
    proto: rust_plan_proto::RustArtifactBundleManifest,
) -> Result<RustArtifactBundleManifest, RustPlanError> {
    Ok(RustArtifactBundleManifest {
        manifest_schema_version: proto.manifest_schema_version,
        plan_schema_version: proto.plan_schema_version,
        cache_schema_version: proto.cache_schema_version,
        mode: plan_mode_from_proto(proto.mode)?,
        cache_key: proto.cache_key,
        created_at_secs: proto.created_at_secs,
        plan_identity_hash: proto.plan_identity_hash,
        artifacts: proto
            .artifacts
            .into_iter()
            .map(|artifact| {
                Ok(RustBundledArtifact {
                    relative_path: artifact.relative_path,
                    class: artifact_class_from_proto(artifact.class)?,
                    size: artifact.size,
                    content_hash: artifact.content_hash,
                    mtime_unix_nanos: artifact.mtime_unix_nanos,
                    owner: match artifact.owner {
                        0 => None,
                        value => Some(artifact_owner_from_proto(value)?),
                    },
                })
            })
            .collect::<Result<Vec<_>, RustPlanError>>()?,
        layer_kind: layer_kind_from_proto(proto.layer_kind)?,
        base_cache_key: if proto.base_cache_key.is_empty() {
            None
        } else {
            Some(proto.base_cache_key)
        },
        deleted_paths: proto.deleted_paths,
    })
}