CodeSampleX

サンプル

cc 1.11.3

検証済みサンプル — cargo cc 1.11.3. rust 1 · linux alpine/x64 · docker で contract を実行し、成功しました: cc::Build::compile in build.rs compiles csrc/calc.c with…

sha256:2b39d3d1f6e8225f1092d63669a9783fb1ce1b135458b77e4626a897857576f0

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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-11-22

ケース

HOW
ゴール
verify pkg:cargo/cc@1.11.3
パッケージ
作成日
2026-11-22T20:21:15Z

コントラクト

  1. cc::Build::compile in build.rs compiles csrc/calc.c with include("csrc") and define("CALC_SCALE", Some("3")) so FFI scaled_sum(4, 6) returns 30 and scaled_sum(-2, 5) returns 9
  2. Build::try_get_compiler returns a non-MSVC GNU or Clang-compatible tool whose args include -O2, -g, -DSAMPLE_NUM=42, -DSAMPLE_FLAG, -Wall, and -I csrc
  3. Build::try_expand preprocesses csrc/calc.c with define("CALC_SCALE", Some("7")) and emits expanded source containing calc_scaled_sum and * 7
  4. Build::try_compile_intermediates emits a non-empty object file and Build::try_create_archive packages it into a non-empty libcalc_manual.a archive
  5. Build::try_compile returns Err for a missing C source file and Build::try_expand returns an InvalidArgument error when given multiple files

ファイル

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • build.rs
  • csrc/calc.c
  • csrc/calc.h
  • csx.json
  • spec.json
  • src/lib.rs
  • 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.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69"
dependencies = [
 "find-msvc-tools",
 "shlex",
]

[[package]]
name = "cc-verify-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-verify-sample"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
cc = "=1.11.3"

[build-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 pkg:cargo/cc@1.11.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/cc@1.11.3
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.
build.rs
use std::env;

fn main() {
    println!("cargo:rerun-if-changed=csrc/calc.c");
    println!("cargo:rerun-if-changed=csrc/calc.h");

    let target = env::var("TARGET").expect("TARGET env var set by Cargo");
    let host = env::var("HOST").expect("HOST env var set by Cargo");
    println!("cargo:rustc-env=SAMPLE_BUILD_TARGET={target}");
    println!("cargo:rustc-env=SAMPLE_BUILD_HOST={host}");

    cc::Build::new()
        .file("csrc/calc.c")
        .include("csrc")
        .define("CALC_SCALE", Some("3"))
        .flag("-Wall")
        .compile("calc_native");
}
csrc/calc.c
#include "calc.h"

int calc_scaled_sum(int a, int b) {
    return (a + b) * CALC_SCALE;
}
csrc/calc.h
#ifndef CALC_H
#define CALC_H

#ifndef CALC_SCALE
#define CALC_SCALE 1
#endif

int calc_scaled_sum(int a, int b);

#endif
csx.json
{"case":{"caseId":"case:sha256:b71b33de8ee2e63ad6ae2d4a919102d667d41515080a829f2d6bbf278d0a1573","contract":["cc::Build::compile in build.rs compiles csrc/calc.c with include(\"csrc\") and define(\"CALC_SCALE\", Some(\"3\")) so FFI scaled_sum(4, 6) returns 30 and scaled_sum(-2, 5) returns 9","Build::try_get_compiler returns a non-MSVC GNU or Clang-compatible tool whose args include -O2, -g, -DSAMPLE_NUM=42, -DSAMPLE_FLAG, -Wall, and -I csrc","Build::try_expand preprocesses csrc/calc.c with define(\"CALC_SCALE\", Some(\"7\")) and emits expanded source containing calc_scaled_sum and * 7","Build::try_compile_intermediates emits a non-empty object file and Build::try_create_archive packages it into a non-empty libcalc_manual.a archive","Build::try_compile returns Err for a missing C source file and Build::try_expand returns an InvalidArgument error when given multiple files"],"goal":"verify pkg:cargo/cc@1.11.3","kind":"HOW","packages":["pkg:cargo/cc@1.11.3"],"schemaVersion":1},"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.11.3"],"schemaVersion":1,"subject":"pkg:cargo/cc@1.11.3","verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/cc@1.11.3",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/cc@1.11.3"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/lib.rs
//! Clean-room verification sample for `cc` 1.11.3.

extern "C" {
    fn calc_scaled_sum(a: i32, b: i32) -> i32;
}

/// Calls the C function compiled and linked via `cc::Build` in `build.rs`.
pub fn scaled_sum(a: i32, b: i32) -> i32 {
    unsafe { calc_scaled_sum(a, b) }
}
test/contract.rs
use cc::Build;
use std::env;
use std::fs;
use std::process;

fn main() {
    // 1. End-to-end FFI call into C code compiled and linked by cc::Build in build.rs
    assert_eq!(
        cc_verify_sample::scaled_sum(4, 6),
        30,
        "build.rs compiled C function should apply CALC_SCALE=3"
    );
    assert_eq!(
        cc_verify_sample::scaled_sum(-2, 5),
        9,
        "build.rs compiled C function should handle signed inputs"
    );

    let target = env!("SAMPLE_BUILD_TARGET");
    let host = env!("SAMPLE_BUILD_HOST");

    // 2. Build::try_get_compiler reflects target, optimization, debug, include, define, and flag settings
    let mut cfg = Build::new();
    cfg.target(target)
        .host(host)
        .opt_level(2)
        .debug(true)
        .include("csrc")
        .define("SAMPLE_NUM", Some("42"))
        .define("SAMPLE_FLAG", None)
        .flag("-Wall")
        .cargo_metadata(false)
        .cargo_warnings(false);

    let tool = cfg
        .try_get_compiler()
        .expect("try_get_compiler should locate a C compiler");
    assert!(
        !tool.path().as_os_str().is_empty(),
        "compiler path should not be empty"
    );
    assert!(
        tool.is_like_gnu() || tool.is_like_clang(),
        "Linux host compiler should be GNU or Clang compatible"
    );
    assert!(!tool.is_like_msvc(), "Linux host compiler should not be MSVC");

    let args: Vec<String> = tool
        .args()
        .iter()
        .map(|a| a.to_string_lossy().into_owned())
        .collect();
    assert!(args.iter().any(|a| a == "-O2"), "opt_level(2) should emit -O2");
    assert!(args.iter().any(|a| a == "-g"), "debug(true) should emit -g");
    assert!(
        args.iter().any(|a| a == "-DSAMPLE_NUM=42"),
        "define with value should emit -DSAMPLE_NUM=42"
    );
    assert!(
        args.iter().any(|a| a == "-DSAMPLE_FLAG"),
        "define without value should emit -DSAMPLE_FLAG"
    );
    assert!(
        args.iter().any(|a| a == "-Wall"),
        "flag(\"-Wall\") should appear in compiler args"
    );
    assert!(
        args.windows(2).any(|w| w[0] == "-I" && w[1] == "csrc")
            || args.iter().any(|a| a == "-Icsrc"),
        "include(\"csrc\") should appear in compiler args"
    );

    // 3. Build::try_expand preprocesses a C source file with custom macro definitions
    let mut expand_build = Build::new();
    expand_build
        .target(target)
        .host(host)
        .opt_level(0)
        .file("csrc/calc.c")
        .include("csrc")
        .define("CALC_SCALE", Some("7"))
        .cargo_metadata(false)
        .cargo_warnings(false);

    let expanded_bytes = expand_build
        .try_expand()
        .expect("try_expand should preprocess csrc/calc.c");
    let expanded = String::from_utf8_lossy(&expanded_bytes);
    assert!(
        expanded.contains("calc_scaled_sum") && expanded.contains("* 7"),
        "expanded C source should substitute CALC_SCALE with 7"
    );

    // 4. Build::try_compile_intermediates and Build::try_create_archive produce object and static archive files
    let out_dir = env::temp_dir().join(format!("cc_verify_sample_{}", process::id()));
    let _ = fs::remove_dir_all(&out_dir);
    fs::create_dir_all(&out_dir).expect("create temp out_dir");

    let mut archive_build = Build::new();
    archive_build
        .target(target)
        .host(host)
        .opt_level(1)
        .out_dir(&out_dir)
        .file("csrc/calc.c")
        .include("csrc")
        .define("CALC_SCALE", Some("2"))
        .cargo_metadata(false)
        .cargo_warnings(false);

    let objects = archive_build
        .try_compile_intermediates()
        .expect("try_compile_intermediates should compile csrc/calc.c");
    assert_eq!(objects.len(), 1, "one C file should produce one object file");
    assert!(
        objects[0].exists() && fs::metadata(&objects[0]).unwrap().len() > 0,
        "compiled object file should exist and be non-empty"
    );

    let archive_path = archive_build
        .try_create_archive("calc_manual", &objects)
        .expect("try_create_archive should package objects into a static library");
    assert!(
        archive_path.exists() && fs::metadata(&archive_path).unwrap().len() > 0,
        "static archive should exist and be non-empty"
    );
    assert_eq!(
        archive_path.file_name().and_then(|n| n.to_str()),
        Some("libcalc_manual.a"),
        "archive filename should be libcalc_manual.a"
    );

    // 5. Error paths: try_compile rejects missing source files and try_expand rejects multiple files
    let mut missing_build = Build::new();
    missing_build
        .target(target)
        .host(host)
        .opt_level(0)
        .out_dir(&out_dir)
        .file("csrc/missing_source.c")
        .cargo_metadata(false)
        .cargo_warnings(false);
    let missing_err = missing_build
        .try_compile("missing_lib")
        .expect_err("try_compile must fail for a nonexistent C source file");
    assert!(
        !missing_err.to_string().is_empty(),
        "try_compile error message should be non-empty"
    );

    let mut multi_expand = Build::new();
    multi_expand
        .target(target)
        .host(host)
        .opt_level(0)
        .file("csrc/calc.c")
        .file("csrc/calc.h")
        .cargo_metadata(false)
        .cargo_warnings(false);
    let multi_err = multi_expand
        .try_expand()
        .expect_err("try_expand must reject more than one input file");
    assert!(
        multi_err.to_string().contains("InvalidArgument"),
        "try_expand multi-file error should report InvalidArgument, got: {multi_err}"
    );

    let _ = fs::remove_dir_all(&out_dir);
    println!("cc@1.11.3 contract verification passed");
}

オリジンシーダー

匿名