build-program
Build Solana program (Anchor or native)
mkdir -p ~/.claude/commands && curl -fsSL https://raw.githubusercontent.com/solanabr/solana-ai-kit/HEAD/.claude/commands/build-program.md -o ~/.claude/commands/build-program.mdbuild-program.md
You are building a Solana program. Follow these steps:
## Related Skills
- [programs/anchor.md](../skills/ext/solana-dev/skill/references/programs/anchor.md) - Anchor build patterns
- [programs/pinocchio.md](../skills/ext/solana-dev/skill/references/programs/pinocchio.md) - Pinocchio build optimization
## Step 1: Identify Program Type
Check which framework is being used:
```bash
# Check for Anchor
if [ -f "Anchor.toml" ]; then
echo "Anchor program detected"
fi
# Check for Cargo
if [ -f "Cargo.toml" ]; then
echo "Native Rust program detected"
fi
```
## Step 2: Build Program
### For Anchor Programs
```bash
# Clean build
anchor clean
anchor build
# Build specific program
anchor build -p program-name
# Build with verifiable build
anchor build --verifiable
# Check build output
ls -lh target/deploy/*.so
```
### For Native Rust Programs
```bash
# Build BPF program
cargo build-sbf
# Build with specific BPF SDK
cargo build-sbf --bpf-sdk /path/to/bpf-sdk
# Check output
ls -lh target/deploy/*.so
```
## Step 3: Verify Build
```bash
# Check program size (should be < 400KB ideally)
ls -lh target/deploy/*.so | awk '{print $5, $9}'
# Verify program ID
solana address -k target/deploy/program-keypair.json
# If Anchor, verify IDL generated
ls -lh target/idl/*.json
```
## Step 4: Run Format and Lint
After successful build:
```bash
# Format code
cargo fmt
# Run clippy
cargo clippy -- -W clippy::all
# Check for warnings
cargo clippy --all-targets --all-features -- -D warnings
```
## Common Build Issues
### Compilation Errors
- Check Rust version: `rustc --version` (need 1.79+)
- Update dependencies: `cargo update`
- Clean and rebuild: `anchor clean && anchor build`
### Binary Size Too Large
```bash
# Check current size
ls -lh target/deploy/*.so
# Optimize in Cargo.toml:
# [profile.release]
# opt-level = "z"
# lto = "fat"
# codegen-units = 1
```
### Missing Dependencies
```bash
# For Anchor
npm install
# For Rust
cargo fetch
```
### BPF SDK Issues
```bash
# Update Solana CLI
agave-install update
# Reinstall BPF SDK
cargo build-sbf --force-tools-install
```
## Build Optimization
### For Production
```toml
# Add to Cargo.toml
[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1
opt-level = 3
[profile.release.build-override]
opt-level = 3
```
### Measure Build Time
```bash
# Time the build
time anchor build
# Or with cargo
time cargo build-sbf
```
## Verifiable Build (Anchor)
**CRITICAL for production and security audits:**
Verifiable builds ensure your program binary can be reproduced identically by anyone, proving no hidden code was injected during compilation. This is essential for:
- Security audits (auditors can verify deployed bytecode matches source)
- User trust (anyone can verify what's deployed)
- Mainnet deployments (industry best practice)
```bash
# Create verifiable build (ALWAYS use for mainnet!)
anchor build --verifiable
# This produces identical builds across machines
# Uses Docker to ensure reproducible compilation environment
# Verify a deployed program matches source
anchor verify <program-id> --provider.cluster mainnet
```
**When to use verifiable builds:**
- ✅ Always for mainnet deployments
- ✅ For security audits
- ✅ Before professional code reviews
- ⚠️ Optional for devnet testing (but good practice)
- ❌ Not needed for local development iteration
**Note:** First verifiable build may take longer as it downloads Docker image, but subsequent builds are faster.
## After Successful Build
- [ ] Program compiled without errors
- [ ] Binary size acceptable (< 400KB preferred)
- [ ] Clippy shows no warnings
- [ ] Code is formatted
- [ ] IDL generated (if Anchor)
- [ ] Verifiable build successful (for mainnet)
- [ ] Ready for testingAnchor framework specialist for rapid Solana program development. Use for building programs with Anchor macros, IDL generation, account validation, and standardized patterns. Prioritizes developer experience while maintaining security.\\n\\nUse when: Building new programs quickly, team projects needing standardization, projects requiring IDL for client generation, or when developer experience is prioritized over maximum CU optimization.
DeFi integration specialist for composing with Solana protocols including Jupiter, Drift, Kamino, Raydium, Orca, Meteora, Marginfi, and Sanctum. Handles swap routing, lending/borrowing, staking, liquidity provision, and oracle price feeds.\n\nUse when: Integrating DeFi protocols, building swap interfaces, implementing lending/borrowing, setting up yield strategies, working with Pyth/Switchboard oracles, or composing multi-protocol transactions.
CI/CD, infrastructure, and deployment specialist for Solana projects. Handles GitHub Actions, Docker, monitoring, RPC management, and Cloudflare Workers edge deployment.\n\nUse when: Setting up CI/CD pipelines, containerizing Solana validators or programs, configuring monitoring and alerting, managing RPC infrastructure, deploying edge workers, or automating build and deploy workflows.
Senior Solana game architect for game system design, Unity/C# architecture, on-chain game state, player progression, NFT integration, and PlaySolana ecosystem. Use for high-level game design decisions, architecture reviews, and planning complex game systems.\n\nUse when: Designing new Solana games from scratch, planning game state on-chain, Unity project architecture, integrating with PlaySolana/PSG1, or deciding between implementation approaches.
React Native and Expo specialist for building Solana mobile dApps. Handles mobile wallet adapter integration, transaction signing UX, deep linking, and mobile-specific performance optimization.\n\nUse when: Building React Native or Expo mobile apps with Solana integration, implementing mobile wallet adapter flows, setting up deep links for transaction signing, or optimizing mobile dApp performance.
CU optimization specialist using Pinocchio framework. Use for performance-critical programs requiring 80-95% CU reduction vs Anchor. Specializes in zero-copy access, manual validation, and minimal binary size.\\n\\nUse when: CU limits are being hit, transaction costs are significant at scale, binary size must be minimized, or maximum throughput is required.
Rust backend specialist for building async services that interact with Solana blockchain. Builds APIs, indexing services, and off-chain processing using Axum, Tokio, and modern async patterns.\n\nUse when: Building REST/WebSocket APIs for Solana dApps, implementing transaction indexers, creating webhook services, or any Rust backend that interacts with Solana.
Senior Solana program architect for system design, account structures, PDA schemes, token economics, and cross-program composability. Use for high-level design decisions, architecture reviews, and planning complex multi-program systems.\n\nUse when: Designing new programs from scratch, planning account structures, optimizing PDA schemes, reviewing architecture for security, or deciding between implementation approaches.