cargo-mate 1.8.0

Rust development companion that enhances cargo with intelligent workflows, state management, performance optimization, and comprehensive project monitoring.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# DDR (Docker Dock Rust) - Parallel Docker-based Rust Build Orchestration

**Source**: `cargo-mate/captain/src/cmd/ddr.rs` and `cargo-mate/captain/src/cmd/smune.rs:DockDockRustCommands`

## Overview

DDR (Docker Dock Rust) is a sophisticated build orchestration system that leverages Docker containers to provide parallel, cross-platform Rust compilation with maximum efficiency and reproducibility. It supports multiple architectures, build targets, and optimization strategies.

**Main Handler**: `ddr.rs:handle_ddr()` (verified in `ddr.rs:503-521`)

## Quick Start

```bash
# Basic build
captain ddr build

# Cross-platform build with specific targets
captain ddr build --target x86_64-unknown-linux-gnu --target aarch64-unknown-linux-gnu

# Build with custom Docker image
captain ddr build --image rust:1.70

# Maximum parallel jobs
captain ddr build --jobs 32
```

## Architecture

### Core Components

1. **CLI Interface** (`src/cmd/ddr.rs`)
   - Command-line argument parsing
   - Build configuration management
   - Progress reporting and monitoring

2. **Build Orchestrator** (`src/ddr/ddr-orchestrator.sh`)
   - Docker container management
   - Parallel build coordination
   - Artifact collection and optimization

3. **Docker Images** (`src/ddr/docker/`)
   - Cross-compilation environments
   - Optimized build contexts
   - Multi-stage build configurations

## Commands

**Implementation**: All commands are handled by `ddr.rs:handle_ddr()` which matches on `DdrAction` enum.

### Build Command

**Source**: `ddr.rs:handle_build()` (verified in `ddr.rs:523-560`)

**Implementation**:
- If `ddr.toml` exists and `use_config` is true, loads config directly
- Otherwise prompts user if config exists
- Generates default config if none exists
- Creates `BuildOrchestrator` instance and runs build
- Prints build report

```bash
cm ddr build [OPTIONS]
```

#### Options

| Option | Short | Description | Default |
|--------|-------|-------------|---------|
| `--image` | `-i` | Docker image to use | `rust:latest` |
| `--target` | `-t` | Build targets (repeatable) | Current platform |
| `--jobs` | `-j` | Maximum parallel jobs | `16` |
| `--config` | `-c` | Configuration file | `ddr.toml` |
| `--use-config` | | Use existing config without prompting | `false` |
| `--verbose` | `-v` | Verbose output | `false` |
| `--dry-run` | | Show what would be built | `false` |
| `--force` | `-f` | Force rebuild | `false` |
| `--no-cache` | | Disable Docker layer caching | `false` |
| `--output-dir` | | Custom output directory | `target/ddr` |
| `--clean` | | Clean build artifacts | `false` |
| `--watch` | | Watch mode (rebuild on changes) | `false` |
| `--profile` | | Build profile | `release` |

### Configuration File (ddr.toml)

```toml
# DDR Configuration File
[build]
# Docker image
image = "rust:latest"

# Build targets
targets = [
    "x86_64-unknown-linux-gnu",
    "aarch64-unknown-linux-gnu",
    "armv7-unknown-linux-gnueabihf"
]

# Parallel jobs
max_jobs = 16

# Build profiles
profiles = ["debug", "release"]

# Cache settings
cache_dir = "~/.cache/cargo-mate-ddr"

# Output configuration
output_dir = "target/ddr"
artifacts = ["binary", "debug", "docs"]

# Docker settings
docker = { pull = true, buildkit = true }

# Cross-compilation
[cross]
enabled = true
toolchains = ["musl", "gnu", "mingw"]
```

## Supported Platforms

### Native Builds
- **Linux GNU**: Debian-based with full GNU toolchain
- **Linux MUSL**: Alpine Linux with static linking
- **Windows**: Cross-compilation for x64 and x86 targets
- **macOS**: Cross-compilation for Intel and Apple Silicon

### Cross-Compilation
- **ARM**: ARMv7 and AArch64 support
- **Android**: Android NDK integration
- **FreeBSD**: FreeBSD cross-compilation
- **WebAssembly**: WASM and WASI targets

### Advanced Builds
- **BuildKit**: Modern Docker build system with parallel stages
- **Multi-stage**: Optimized layered builds with caching
- **Parallel**: Concurrent target building

## Docker Images

### Available Images

| Image | Purpose | Size | Cross-compilation |
|-------|---------|------|-------------------|
| `alpine-musl.dockerfile` | Minimal static builds | ~50MB | Linux MUSL |
| `debian-gnu.dockerfile` | Full GNU compatibility | ~150MB | Linux GNU |
| `windows-cross.dockerfile` | Windows executables | ~200MB | Win64/Win32 |
| `arm-cross.dockerfile` | ARM architectures | ~180MB | ARMv7/AArch64 |
| `wasm.dockerfile` | WebAssembly builds | ~120MB | WASM/WASI |
| `macos-cross.dockerfile` | macOS applications | ~250MB | macOS x64/ARM64 |
| `android.dockerfile` | Android applications | ~300MB | Android ABIs |
| `freebsd.dockerfile` | FreeBSD compatibility | ~160MB | FreeBSD |
| `buildkit-parallel.dockerfile` | Parallel builds | ~100MB | Multi-platform |
| `multi-stage-cached.dockerfile` | Cached builds | ~80MB | Optimized |

### Custom Images

Create custom Docker images by extending the base configurations:

```dockerfile
FROM rust:latest AS builder

# Add your custom dependencies
RUN apt-get update && apt-get install -y \
    custom-package \
    && rm -rf /var/lib/apt/lists/*

# Your build logic here
```

## Build Optimization

### Caching Strategies

1. **Dependency Caching**: Cache Rust dependencies separately
2. **Layer Optimization**: Minimize layer changes for better caching
3. **Artifact Reuse**: Reuse build artifacts across builds

### Performance Tuning

```bash
# Maximum parallelism
captain ddr build --jobs 32

# Use BuildKit for parallel builds
export DOCKER_BUILDKIT=1

# Enable SCCache
export RUSTC_WRAPPER=sccache

# Custom cache directory
export SCCACHE_DIR=~/.cache/sccache
```

## Examples

### Basic Linux Build

```bash
captain ddr build
```

### Cross-Platform Release

```bash
captain ddr build \
    --target x86_64-unknown-linux-gnu \
    --target x86_64-pc-windows-gnu \
    --target x86_64-apple-darwin \
    --profile release
```

### Android Application

```bash
captain ddr build \
    --target aarch64-linux-android \
    --target armv7-linux-androideabi \
    --image android-ndk:latest
```

### WebAssembly

```bash
captain ddr build \
    --target wasm32-unknown-unknown \
    --target wasm32-wasi
```

### Multi-Architecture

```bash
captain ddr build \
    --target x86_64-unknown-linux-gnu \
    --target aarch64-unknown-linux-gnu \
    --target armv7-unknown-linux-gnueabihf \
    --jobs 8
```

## Monitoring and Debugging

### Progress Reporting

DDR provides real-time progress reporting:

```
[DDR] Building 12 targets with 8 parallel jobs
[DDR] [1/12] x86_64-unknown-linux-gnu: Building...
[DDR] [1/12] x86_64-unknown-linux-gnu: ✅ Completed (2.3s)
[DDR] [2/12] aarch64-unknown-linux-gnu: Building...
```

### Verbose Mode

```bash
captain ddr build --verbose
```

Shows detailed Docker commands, build logs, and timing information.

### Debug Information

```bash
captain ddr build --dry-run
```

Shows what would be executed without actually building.

## Troubleshooting

### Common Issues

#### Docker Not Found
```bash
# Install Docker
curl -fsSL https://get.docker.com | sh

# Start Docker service
sudo systemctl start docker
```

#### Permission Issues
```bash
# Add user to docker group
sudo usermod -aG docker $USER

# Restart session or run:
newgrp docker
```

#### Build Failures
```bash
# Check Docker image
docker run --rm rust:latest rustc --version

# Clear caches
rm -rf ~/.cache/cargo-mate-ddr
docker system prune -f
```

#### Memory Issues
```bash
# Reduce parallel jobs
captain ddr build --jobs 4

# Increase Docker memory limit
docker system info | grep "Total Memory"
```

### Logs and Debugging

```bash
# Enable verbose logging
export RUST_LOG=debug
captain ddr build --verbose

# View Docker logs
docker logs <container_id>

# Inspect build artifacts
ls -la target/ddr/
```

## Advanced Configuration

### Custom Docker Registry

```bash
export DDR_DOCKER_REGISTRY=myregistry.com
captain ddr build --image myregistry.com/rust:latest
```

### CI/CD Integration

```yaml
# GitHub Actions
- name: Build with DDR
  run: |
    captain ddr build \
      --target x86_64-unknown-linux-gnu \
      --target x86_64-pc-windows-gnu \
      --jobs 4
```

### Custom Build Scripts

Create `ddr-orchestrator.sh` hooks:

```bash
# Pre-build hook
function pre_build_hook() {
    echo "Setting up build environment..."
    # Your custom setup
}

# Post-build hook
function post_build_hook() {
    echo "Build completed, running tests..."
    # Your custom post-processing
}
```

## Security Considerations

### Sandboxed Builds
- All builds run in isolated Docker containers
- No access to host filesystem (except explicitly mounted volumes)
- Network access can be restricted

### Dependency Scanning
DDR integrates with security scanning tools:
- Cargo audit for Rust dependencies
- Container image vulnerability scanning
- Binary analysis for compiled artifacts

## Performance Benchmarks

### Build Times (example project)

| Configuration | Targets | Time | Improvement |
|---------------|---------|------|-------------|
| Native build | 1 | 45s | Baseline |
| DDR parallel | 3 | 52s | +15% overhead |
| DDR cached | 3 | 18s | 60% faster |
| DDR BuildKit | 5 | 28s | 38% faster |

### Resource Usage

| Configuration | CPU | Memory | Disk |
|---------------|-----|--------|------|
| Single target | 100% | 512MB | 2GB |
| 8 parallel jobs | 800% | 4GB | 16GB |
| 16 parallel jobs | 1600% | 8GB | 32GB |

## Contributing

### Development Setup

```bash
# Clone repository
git clone https://github.com/your-org/cargo-mate.git
cd cargo-mate

# Build development version
cargo build

# Run tests
cargo test --package cargo-mate
```

### Adding New Targets

1. Create Docker image in `src/ddr/docker/`
2. Add target to `DdrAction::Build` enum
3. Update documentation
4. Test cross-compilation

### Code Structure

```
src/
├── cmd/
│   └── ddr.rs              # CLI interface
├── ddr/
│   ├── docker/            # Docker images
│   │   └── *.dockerfile
│   └── ddr-orchestrator.sh # Build script
└── main.rs                # Command routing
```

## License

DDR is part of the cargo-mate project and follows the same licensing terms.

## See Also

- [Cargo Book]https://doc.rust-lang.org/cargo/
- [Docker Documentation]https://docs.docker.com/
- [Cross-compilation Guide]https://rust-lang.github.io/rustup/cross-compilation.html
- [BuildKit]https://docs.docker.com/develop/dev-best-practices/