CodeSampleX

示例

cc 1.4.3: cc::Build

已验证示例 — cargo cc 1.4.3: cc::Build. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过: Build::new initializes a C compiler configuration with target and…

sha256:e7e069184b8cd5edc5e826c53ae5723f4db1524e13f5d0de4045fd59fcd3f484

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux · alpine · musl x64 cargo

验证运行环境

环境 契约 阶段 运行日期
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-19

案例

HOW
目标
verify cc::Build in pkg:cargo/cc@1.4.3
包
符号
  • cc::Build
创建时间
2026-09-17T22:20:31Z

契约

  1. Build::new initializes a C compiler configuration with target and optimization level
  2. Build::get_compiler inspects the toolchain binary path and compiler family flags
  3. Build::define and Build::include add macro definitions and header search paths to compiler arguments
  4. Build::opt_level and Build::debug configure optimization and debug symbol flags
  5. Build::try_compile compiles C source into a static archive output

文件

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • src/sample.c
  • test/contract.rs

下载源代码构件 (tar.gz)

源代码

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "cc"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "509591b7bcd67f4ef775afad7662703b4935daaa6ec0e5605cfb1090b32a2b6d"
dependencies = [
 "find-msvc-tools",
 "shlex",
]

[[package]]
name = "find-msvc-tools"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d"

[[package]]
name = "sample-cc"
version = "1.0.0"
dependencies = [
 "cc",
]

[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
Cargo.toml
[package]
name = "sample-cc"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
cc = "=1.4.3"

[[bin]]
name = "contract"
path = "test/contract.rs"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify cc::Build in pkg:cargo/cc@1.4.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/cc@1.4.3
Demonstrate these symbols/APIs:
  - cc::Build

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c72157607e36180637b28001918644a352a6f577facbea252aa99677c1ab24f0","contract":["Build::new initializes a C compiler configuration with target and optimization level","Build::get_compiler inspects the toolchain binary path and compiler family flags","Build::define and Build::include add macro definitions and header search paths to compiler arguments","Build::opt_level and Build::debug configure optimization and debug symbol flags","Build::try_compile compiles C source into a static archive output"],"goal":"verify cc::Build in pkg:cargo/cc@1.4.3","kind":"HOW","packages":["pkg:cargo/cc@1.4.3"],"schemaVersion":1,"symbols":["cc::Build"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"cargo","libc":"musl","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/cc@1.4.3"],"schemaVersion":1,"subject":"pkg:cargo/cc@1.4.3","symbols":["cc::Build"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify cc::Build in pkg:cargo/cc@1.4.3",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/cc@1.4.3"
  ],
  "symbols": [
    "cc::Build"
  ]
}
src/lib.rs
//! Clean-room verification sample for cc.

pub use cc::Build;
src/sample.c
/* Clean-room C source for cc::Build contract verification */

int compute_square(int val) {
    return val * val;
}
test/contract.rs
use cc::Build;
use std::env;
use std::fs;

fn main() {
    // 1. Build::new initializes a C compiler configuration with target and optimization level
    if env::var("TARGET").is_err() {
        #[cfg(target_env = "musl")]
        let default_target = "x86_64-unknown-linux-musl";
        #[cfg(not(target_env = "musl"))]
        let default_target = "x86_64-unknown-linux-gnu";

        env::set_var("TARGET", default_target);
    }
    if env::var("HOST").is_err() {
        let current_target = env::var("TARGET").unwrap();
        env::set_var("HOST", current_target);
    }
    if env::var("OPT_LEVEL").is_err() {
        env::set_var("OPT_LEVEL", "0");
    }

    let mut b1 = Build::new();
    b1.cargo_metadata(false);
    b1.opt_level(0);

    // 2. Build::get_compiler inspects the toolchain binary path and compiler family flags
    let comp1 = b1.get_compiler();
    let comp_path = comp1.path().to_string_lossy();
    assert!(!comp_path.is_empty(), "compiler path should not be empty");
    assert!(comp1.is_like_gnu() || comp1.is_like_clang(), "toolchain should be GNU or Clang compatible");
    assert!(!comp1.is_like_msvc(), "toolchain should not be MSVC on Linux");

    // 3. Build::define and Build::include add macro definitions and header search paths to compiler arguments
    let mut b2 = Build::new();
    b2.cargo_metadata(false);
    b2.opt_level(1);
    b2.include("src");
    b2.define("CSX_TEST_MACRO", Some("100"));
    b2.define("CSX_TEST_FLAG", None);

    let comp2 = b2.get_compiler();
    let args2: Vec<String> = comp2.args().iter().map(|s| s.to_string_lossy().to_string()).collect();
    assert!(args2.iter().any(|a| a == "-DCSX_TEST_MACRO=100"), "macro with value should be in args");
    assert!(args2.iter().any(|a| a == "-DCSX_TEST_FLAG"), "macro flag without value should be in args");
    assert!(
        args2.windows(2).any(|w| w[0] == "-I" && w[1] == "src") || args2.iter().any(|a| a == "-Isrc"),
        "include path should be in args"
    );

    // 4. Build::opt_level and Build::debug configure optimization and debug symbol flags
    let mut b3 = Build::new();
    b3.cargo_metadata(false);
    b3.opt_level(3);
    b3.debug(true);

    let comp3 = b3.get_compiler();
    let args3: Vec<String> = comp3.args().iter().map(|s| s.to_string_lossy().to_string()).collect();
    assert!(args3.iter().any(|a| a == "-O3"), "opt_level(3) should emit -O3");
    assert!(args3.iter().any(|a| a == "-g"), "debug(true) should emit -g");

    // 5. Build::try_compile compiles C source into a static archive output
    let temp_out = env::temp_dir().join("csx_sample_cc_out");
    let _ = fs::remove_dir_all(&temp_out);
    fs::create_dir_all(&temp_out).expect("create temp out directory");

    let mut b4 = Build::new();
    b4.cargo_metadata(false);
    b4.opt_level(2);
    b4.out_dir(&temp_out);
    b4.file("src/sample.c");

    let compile_result = b4.try_compile("sample_c");
    assert!(compile_result.is_ok(), "try_compile should succeed: {:?}", compile_result.err());

    let archive_path = temp_out.join("libsample_c.a");
    assert!(archive_path.exists(), "libsample_c.a should be created in output directory");

    let _ = fs::remove_dir_all(&temp_out);

    println!("All cc::Build contract assertions passed successfully.");
}

原始种子者

匿名