CodeSampleX

Beispiel

cc 1.11.3: cc::Build

Verifiziertes Beispiel für cargo cc 1.11.3: cc::Build. Der Vertrag lief auf rust 1 · linux alpine/x64 · docker und bestand: Build::new with host, target…

sha256:ca991ddaa54a7c49f6e78626f7eecf86a89d993576a2163e76bf15b6b9e2bba8

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux · alpine · musl x64 rust cargo

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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-11-23

Fall

HOW
Ziel
verify cc::Build in pkg:cargo/cc@1.11.3
Pakete
Symbole
  • cc::Build
Erstellt
2026-11-23T16:48:54Z

Contract

  1. Build::new with host, target, opt_level, and debug resolves a GNU or Clang C compiler via try_get_compiler and emits -O2 and -g
  2. Build::include, Build::define, and Build::flag add header search paths, valued and flag-only preprocessor macros, and custom flags to compiler arguments
  3. Build::try_compile compiles C source with an included header into an ar-format static library archive in out_dir
  4. Build::try_compile returns Err when compiling syntactically invalid C source

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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

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

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

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

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

[dependencies]
cc = "=1.11.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.11.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/cc@1.11.3
Demonstrate these symbols/APIs:
  - cc::Build
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:1d927cdfda57d6796cf4b092127f43cb07dbe70816a07a9c2f1b05021a842eb3","contract":["Build::new with host, target, opt_level, and debug resolves a GNU or Clang C compiler via try_get_compiler and emits -O2 and -g","Build::include, Build::define, and Build::flag add header search paths, valued and flag-only preprocessor macros, and custom flags to compiler arguments","Build::try_compile compiles C source with an included header into an ar-format static library archive in out_dir","Build::try_compile returns Err when compiling syntactically invalid C source"],"goal":"verify cc::Build in pkg:cargo/cc@1.11.3","kind":"HOW","packages":["pkg:cargo/cc@1.11.3"],"schemaVersion":1,"symbols":["cc::Build"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"cargo","language":"rust","libc":"musl","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/cc@1.11.3"],"schemaVersion":1,"subject":"pkg:cargo/cc@1.11.3","symbols":["cc::Build"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify cc::Build in pkg:cargo/cc@1.11.3",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/cc@1.11.3"
  ],
  "symbols": [
    "cc::Build"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/arith.c
#include "arith.h"

int scale_and_add(int base, int offset) {
    return (base * SCALE_FACTOR) + offset;
}
src/arith.h
#ifndef ARITH_H
#define ARITH_H

#ifndef SCALE_FACTOR
#define SCALE_FACTOR 2
#endif

int scale_and_add(int base, int offset);

#endif
src/lib.rs
//! Minimal clean-room wrapper demonstrating `cc::Build`.

pub use cc::Build;

/// Returns the current Linux target triple for configuring `cc::Build`
/// outside a Cargo build script.
pub fn current_target_triple() -> &'static str {
    if cfg!(all(target_arch = "x86_64", target_env = "musl")) {
        "x86_64-unknown-linux-musl"
    } else if cfg!(all(target_arch = "aarch64", target_env = "musl")) {
        "aarch64-unknown-linux-musl"
    } else if cfg!(target_arch = "aarch64") {
        "aarch64-unknown-linux-gnu"
    } else {
        "x86_64-unknown-linux-gnu"
    }
}

/// Creates a `cc::Build` preconfigured for standalone execution without Cargo
/// build-script metadata emission.
pub fn configured_build() -> Build {
    let triple = current_target_triple();
    let mut build = Build::new();
    build
        .host(triple)
        .target(triple)
        .opt_level(0)
        .cargo_metadata(false)
        .cargo_output(false);
    build
}
test/contract.rs
use cc_build_sample::{configured_build, current_target_triple, Build};
use std::env;
use std::fs;
use std::path::PathBuf;

fn make_temp_dir(suffix: &str) -> PathBuf {
    let dir = env::temp_dir().join(format!(
        "cc_build_sample_{}_{}",
        std::process::id(),
        suffix
    ));
    let _ = fs::remove_dir_all(&dir);
    fs::create_dir_all(&dir).expect("create temporary output directory");
    dir
}

fn main() {
    let triple = current_target_triple();

    // 1. Build::new configures explicit host, target, optimization, and debug flags
    let mut inspect_build = Build::new();
    inspect_build
        .host(triple)
        .target(triple)
        .opt_level(2)
        .debug(true)
        .cargo_metadata(false)
        .cargo_output(false);

    let compiler = inspect_build
        .try_get_compiler()
        .expect("try_get_compiler should locate a C compiler on Linux");
    assert!(
        !compiler.path().as_os_str().is_empty(),
        "resolved C compiler path must not be empty"
    );
    assert!(
        compiler.is_like_gnu() || compiler.is_like_clang(),
        "Linux host compiler should report GNU or Clang family"
    );
    assert!(
        !compiler.is_like_msvc(),
        "Linux host compiler should not report MSVC family"
    );

    let base_args: Vec<String> = compiler
        .args()
        .iter()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect();
    assert!(
        base_args.iter().any(|a| a == "-O2"),
        "opt_level(2) should add -O2, got: {:?}",
        base_args
    );
    assert!(
        base_args.iter().any(|a| a == "-g"),
        "debug(true) should add -g, got: {:?}",
        base_args
    );

    // 2. Build::include, Build::define, and Build::flag propagate preprocessor and compiler flags
    let mut flag_build = configured_build();
    flag_build
        .include("src")
        .define("SCALE_FACTOR", "3")
        .define("ENABLE_TRACE", None)
        .flag("-fno-strict-aliasing");

    let flag_compiler = flag_build.get_compiler();
    let flag_args: Vec<String> = flag_compiler
        .args()
        .iter()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect();
    assert!(
        flag_args.windows(2).any(|w| w[0] == "-I" && w[1] == "src")
            || flag_args.iter().any(|a| a == "-Isrc"),
        "include(\"src\") should appear in compiler arguments: {:?}",
        flag_args
    );
    assert!(
        flag_args.iter().any(|a| a == "-DSCALE_FACTOR=3"),
        "valued define should emit -DSCALE_FACTOR=3: {:?}",
        flag_args
    );
    assert!(
        flag_args.iter().any(|a| a == "-DENABLE_TRACE"),
        "valueless define should emit -DENABLE_TRACE: {:?}",
        flag_args
    );
    assert!(
        flag_args.iter().any(|a| a == "-fno-strict-aliasing"),
        "flag() should forward custom compiler flags: {:?}",
        flag_args
    );

    // 3. Build::try_compile compiles C source with header lookup into a valid static archive
    let ok_out = make_temp_dir("ok");
    let mut compile_build = configured_build();
    compile_build
        .include("src")
        .define("SCALE_FACTOR", "4")
        .file("src/arith.c")
        .out_dir(&ok_out);

    compile_build
        .try_compile("arith")
        .expect("try_compile should build libarith.a from src/arith.c");

    let archive_path = ok_out.join("libarith.a");
    let archive_bytes = fs::read(&archive_path).expect("static archive libarith.a should exist");
    assert!(
        archive_bytes.starts_with(b"!<arch>\n"),
        "output archive should begin with ar magic header"
    );
    let _ = fs::remove_dir_all(&ok_out);

    // 4. Build::try_compile returns Err on invalid C source instead of panicking
    let err_out = make_temp_dir("err");
    let bad_src = err_out.join("broken.c");
    fs::write(&bad_src, "int broken( { return ; }\n").expect("write invalid C source");

    let mut fail_build = configured_build();
    fail_build
        .cargo_warnings(false)
        .file(&bad_src)
        .out_dir(&err_out);

    let fail_res = fail_build.try_compile("broken");
    assert!(
        fail_res.is_err(),
        "try_compile must return Err when C compilation fails"
    );
    let _ = fs::remove_dir_all(&err_out);

    println!("cc::Build contract verified.");
}

Ursprungs-Seeder

anonym