# Building

Complete guide for building NCN Network components from source.

***

## Quick Build

```bash
# Build all components (debug)
cargo build

# Build all components (release)
cargo build --release
```

***

## Build Individual Components

### Gateway Node

```bash
cargo build -p gateway_node
cargo build --release -p gateway_node

# Binary location
./target/debug/gateway_node
./target/release/gateway_node
```

### Compute Node

```bash
cargo build -p compute_node
cargo build --release -p compute_node

# Binary location
./target/debug/compute_node
./target/release/compute_node
```

### P2P Registry Node

```bash
cargo build -p p2p_registry_node
cargo build --release -p p2p_registry_node

# Binary location
./target/debug/p2p_registry_node
./target/release/p2p_registry_node
```

### Test Client

```bash
cargo build -p test_client
cargo build --release -p test_client

# Binary location
./target/debug/test_client
./target/release/test_client
```

### Common Types Library

```bash
cargo build -p common_types
```

***

## Build Features

### Gateway Features

```bash
# With all features
cargo build -p gateway_node --all-features

# Specific features
cargo build -p gateway_node --features "metrics"
```

### Compute Node Features

```bash
# Without sandbox (for development)
cargo build -p compute_node --no-default-features

# With full sandbox
cargo build -p compute_node --features "sandbox"
```

***

## Build Profiles

### Debug Build

```bash
cargo build
```

* Includes debug symbols
* No optimizations
* Faster compilation
* Larger binaries
* Better for debugging

### Release Build

```bash
cargo build --release
```

* Optimized for performance
* Smaller binaries
* Slower compilation
* Better for production

### Custom Profile

```toml
# Cargo.toml - already configured
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
```

***

## Cross-Compilation

### Linux Target from macOS

```bash
# Add target
rustup target add x86_64-unknown-linux-gnu

# Build
cargo build --target x86_64-unknown-linux-gnu --release
```

### ARM64 Target

```bash
# Add target
rustup target add aarch64-unknown-linux-gnu

# Build
cargo build --target aarch64-unknown-linux-gnu --release
```

***

## Build Smart Contracts

```bash
cd contracts

# Install dependencies
forge install

# Build contracts
forge build

# Output location
ls -la out/
```

### Contract Artifacts

```
out/
├── InferencePayment.sol/
│   └── InferencePayment.json
├── NCNToken.sol/
│   └── NCNToken.json
└── NCNValidatorRegistry.sol/
    └── NCNValidatorRegistry.json
```

***

## Build Docker Images

### All Images

```bash
docker-compose build
```

### Individual Images

```bash
# Gateway
docker build -t ncn-gateway -f gateway_node/Dockerfile .

# Compute
docker build -t ncn-compute -f compute_node/Dockerfile .

# Registry
docker build -t ncn-registry -f p2p_registry_node/Dockerfile .

# Test Client
docker build -t ncn-test-client -f test_client/Dockerfile .
```

### Multi-stage Build

Docker images use multi-stage builds for smaller images:

```dockerfile
# Build stage
FROM rust:1.75 as builder
WORKDIR /app
COPY . .
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim
COPY --from=builder /app/target/release/gateway_node /usr/local/bin/
CMD ["gateway_node"]
```

***

## Protocol Buffer Generation

Protobuf code is auto-generated during build via `build.rs`:

```bash
# Regenerate if proto changes
cargo clean -p common_types
cargo build -p common_types

# Generated code location
target/debug/build/common_types-*/out/
```

### Manual Generation

```bash
# If needed manually
protoc --rust_out=src/generated \
  --tonic_out=src/generated \
  proto/common_types.proto
```

***

## Build Verification

### Check Build

```bash
# Verify binaries exist
ls -la target/release/gateway_node
ls -la target/release/compute_node
ls -la target/release/p2p_registry_node
ls -la target/release/test_client

# Check binary version (if implemented)
./target/release/gateway_node --version
```

### Run Tests

```bash
# All tests
cargo test --all

# Specific package tests
cargo test -p gateway_node
cargo test -p compute_node

# With logging
RUST_LOG=debug cargo test --all -- --nocapture
```

### Lint Check

```bash
# Clippy
cargo clippy --all -- -D warnings

# Format
cargo fmt --all -- --check
```

***

## Build Troubleshooting

### Out of Memory

```bash
# Reduce parallelism
cargo build -j 2

# Or build one package at a time
cargo build -p common_types
cargo build -p gateway_node
```

### Slow Builds

```bash
# Use sccache
cargo install sccache
export RUSTC_WRAPPER=sccache

# Or use mold linker (Linux)
sudo apt install mold
export RUSTFLAGS="-C link-arg=-fuse-ld=mold"
```

### Dependency Conflicts

```bash
# Clean and rebuild
cargo clean
cargo build

# Update dependencies
cargo update
cargo build
```

### Protobuf Errors

```bash
# Ensure protoc is installed
protoc --version

# Regenerate
cargo clean -p common_types
cargo build
```

***

## Build Artifacts

### Binary Locations

| Component   | Debug                            | Release                            |
| ----------- | -------------------------------- | ---------------------------------- |
| Gateway     | `target/debug/gateway_node`      | `target/release/gateway_node`      |
| Compute     | `target/debug/compute_node`      | `target/release/compute_node`      |
| Registry    | `target/debug/p2p_registry_node` | `target/release/p2p_registry_node` |
| Test Client | `target/debug/test_client`       | `target/release/test_client`       |

### Binary Sizes (Release)

| Binary              | Approximate Size |
| ------------------- | ---------------- |
| gateway\_node       | \~25 MB          |
| compute\_node       | \~20 MB          |
| p2p\_registry\_node | \~18 MB          |
| test\_client        | \~15 MB          |

***

## Next Steps

* [Running Locally](https://docs.neurochain.ai/nc/neurochainai-guides/development/running-locally) - Start the services
* [Testing](https://docs.neurochain.ai/nc/neurochainai-guides/development/testing) - Run the test suite
* [Docker Deployment](https://docs.neurochain.ai/nc/neurochainai-guides/deployment/docker) - Deploy with containers
