ccgo 3.6.0

A high-performance C++ cross-platform build CLI
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# Dependency Management

Complete guide to managing dependencies in CCGO projects.

## Overview

CCGO provides a powerful dependency management system with:

- **Multiple sources**: Git repositories, local paths, registries (future)
- **Version control**: Semantic versioning with flexible constraints
- **Lock files**: Reproducible builds with CCGO.lock
- **Optional dependencies**: Feature-based conditional dependencies
- **Platform-specific**: Dependencies for specific platforms only
- **Vendoring**: Local copies of all dependencies
- **Automatic resolution**: Dependency tree resolution and conflict detection

## Dependency Sources

### Git Repository

Most common source for C++ libraries:

```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }
fmt = { git = "https://github.com/fmtlib/fmt.git", branch = "master" }
json = { git = "https://github.com/nlohmann/json.git", rev = "9cca280a619340f3f76dc77292b7e9d1b1c2d83e" }
```

**Git reference types:**
- `tag`: Specific release tag (recommended for stability)
- `branch`: Track a branch (for latest features/fixes)
- `rev`: Exact commit hash (for maximum reproducibility)

**Rules:**
- Only ONE of `tag`, `branch`, or `rev` can be specified
- Git dependencies are cached in `~/.ccgo/git/<repo>/`
- Shallow clones used by default for faster downloads

### Local Path

For local development and workspace dependencies:

```toml
[dependencies]
mylib = { path = "../mylib" }
utils = { path = "./libs/utils" }
common = { path = "/absolute/path/to/common" }
```

**Path types:**
- Relative paths: Relative to current CCGO.toml location
- Absolute paths: Full filesystem path

**Use cases:**
- Multi-module projects
- Local development of dependencies
- Vendored third-party libraries

### Registry (Future)

Future support for package registries:

```toml
[dependencies]
fmt = "10.1.1"              # Exact version
spdlog = "^1.12.0"          # Compatible (>=1.12.0, <2.0.0)
boost = "~1.80"             # Minor version (>=1.80.0, <1.81.0)
protobuf = ">=3.20"         # Minimum version
```

## Version Requirements

### Exact Version

```toml
[dependencies]
fmt = "10.1.1"              # Only version 10.1.1
```

### Caret (Compatible)

```toml
[dependencies]
spdlog = "^1.12.0"          # >=1.12.0, <2.0.0
```

Allows minor and patch updates, no breaking changes.

### Tilde (Minor Version)

```toml
[dependencies]
boost = "~1.80.0"           # >=1.80.0, <1.81.0
```

Allows patch updates only.

### Comparison Operators

```toml
[dependencies]
protobuf = ">=3.20.0"       # Any version >= 3.20.0
openssl = ">1.1.0, <3.0.0"  # Version between 1.1.0 and 3.0.0
```

### Wildcard

```toml
[dependencies]
catch2 = "3.*"              # Any 3.x version
```

## Managing Dependencies

### Adding Dependencies

**Command line:**

```bash
# Add from Git with tag
ccgo add spdlog --git https://github.com/gabime/spdlog.git --tag v1.12.0

# Add from Git with branch
ccgo add fmt --git https://github.com/fmtlib/fmt.git --branch master

# Add from local path
ccgo add mylib --path ../mylib

# Add from Git with specific commit
ccgo add json --git https://github.com/nlohmann/json.git --rev a1b2c3d
```

**Manual CCGO.toml edit:**

```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }
```

Then install:
```bash
ccgo install
```

### Removing Dependencies

**Command line:**

```bash
ccgo remove spdlog
```

**Manual:**

Remove from CCGO.toml, then:
```bash
ccgo install
```

### Updating Dependencies

**Update all dependencies:**

```bash
ccgo update
```

**Update specific dependency:**

```bash
ccgo update spdlog
```

**Dry run (show what would be updated):**

```bash
ccgo update --dry-run
```

**Update process:**
1. Fetches latest versions matching constraints
2. Updates CCGO.lock with new versions
3. Downloads/updates cached copies
4. Rebuilds project if needed

## CCGO.lock File

### Purpose

`CCGO.lock` ensures reproducible builds by recording:
- Exact dependency versions resolved
- Git commit hashes for Git dependencies
- Dependency tree structure
- Checksums for verification

### Structure

```toml
# CCGO.lock - Auto-generated, do not edit manually

[[package]]
name = "spdlog"
version = "1.12.0"
source = "git+https://github.com/gabime/spdlog.git?tag=v1.12.0#a1b2c3d4"
checksum = "sha256:..."
dependencies = ["fmt"]

[[package]]
name = "fmt"
version = "10.1.1"
source = "git+https://github.com/fmtlib/fmt.git?tag=10.1.1#e1f2g3h4"
checksum = "sha256:..."
dependencies = []
```

### Working with Lock Files

**Generate lock file:**

```bash
ccgo install          # Creates/updates CCGO.lock
```

**Use locked versions:**

```bash
ccgo install --locked # Install exact versions from CCGO.lock
```

**Update lock file:**

```bash
ccgo update           # Updates CCGO.lock with new versions
```

**Version control:**
- **Commit** CCGO.lock for applications (reproducible builds)
- **Don't commit** CCGO.lock for libraries (let users resolve)

## Optional Dependencies

### Defining Optional Dependencies

```toml
[dependencies]
# Required dependencies
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }

# Optional dependencies
[dependencies.optional]
networking = { git = "https://github.com/user/networking.git", tag = "v1.0.0" }
database = { git = "https://github.com/user/database.git", tag = "v2.0.0" }
```

### Features

Enable optional dependencies with features:

```toml
[features]
default = ["basic"]           # Default features
basic = []                    # Basic feature (no deps)
network = ["networking"]      # Enables networking dependency
db = ["database"]             # Enables database dependency
full = ["basic", "network", "db"]  # All features
```

### Using Features

```bash
# Build with specific features
ccgo build --features network,db

# Build with all features
ccgo build --all-features

# Build without default features
ccgo build --no-default-features

# Build with specific features combination
ccgo build --no-default-features --features network
```

## Platform-Specific Dependencies

### Conditional Dependencies

```toml
[dependencies]
# Common dependencies for all platforms
common = { git = "https://github.com/user/common.git", tag = "v1.0.0" }

# Android-only dependencies
[target.'cfg(target_os = "android")'.dependencies]
android-utils = { path = "./android-utils" }

# iOS-only dependencies
[target.'cfg(target_os = "ios")'.dependencies]
ios-helpers = { path = "./ios-helpers" }

# Windows-only dependencies
[target.'cfg(target_os = "windows")'.dependencies]
windows-api = { git = "https://github.com/user/windows-api.git", tag = "v1.0.0" }

# Linux-only dependencies
[target.'cfg(target_os = "linux")'.dependencies]
linux-sys = { git = "https://github.com/user/linux-sys.git", tag = "v1.0.0" }
```

### Platform Targets

| Platform | Target | Example |
|----------|--------|---------|
| Android | `target_os = "android"` | Android-specific |
| iOS | `target_os = "ios"` | iOS-specific |
| macOS | `target_os = "macos"` | macOS-specific |
| Windows | `target_os = "windows"` | Windows-specific |
| Linux | `target_os = "linux"` | Linux-specific |
| OpenHarmony | `target_os = "ohos"` | OHOS-specific |

## Vendoring

### What is Vendoring?

Vendoring creates local copies of all dependencies in your project:
- Ensures availability even if upstream repositories disappear
- Enables offline builds
- Provides complete control over dependency source

### Vendor Dependencies

```bash
# Vendor all dependencies to vendor/ directory
ccgo vendor

# Keep existing vendor directory
ccgo vendor --no-delete
```

**Result:**

```
project/
├── CCGO.toml
├── vendor/
│   ├── spdlog/          # Full copy of spdlog
│   ├── fmt/             # Full copy of fmt
│   └── json/            # Full copy of json
└── src/
```

### Using Vendored Dependencies

CCGO automatically uses vendored dependencies if `vendor/` exists:

```bash
# Uses vendored copies
ccgo build android

# Force offline build (only vendored deps)
ccgo install --offline
```

### Version Control with Vendoring

**Small projects:**
- Commit vendor/ directory
- Self-contained repository

**Large projects:**
- Add `vendor/` to `.gitignore`
- Document vendoring process in README
- Consider Git LFS for large vendored files

## Dependency Resolution

### Resolution Algorithm

CCGO uses the following algorithm:

1. **Parse dependency tree**: Read CCGO.toml and build full dependency graph
2. **Version resolution**: Find versions that satisfy all constraints
3. **Conflict detection**: Detect version conflicts
4. **Download**: Fetch missing dependencies
5. **Build order**: Topological sort for build order

### Conflict Resolution

**Example conflict:**

```toml
# Your project
[dependencies]
libA = { git = "...", tag = "v1.0.0" }  # Depends on libC ^1.0.0
libB = { git = "...", tag = "v2.0.0" }  # Depends on libC ^2.0.0
```

CCGO will report:
```
Error: Conflicting dependencies for libC:
  - libA requires libC ^1.0.0
  - libB requires libC ^2.0.0
```

**Solutions:**
1. Update libA or libB to compatible versions
2. Use fork with compatible version
3. Modify dependency constraints

### Build Order

CCGO builds dependencies in correct order:

```
Project
  ├── libA (depends on libC)
  ├── libB (depends on libC)
  └── libC (no dependencies)
```

**Build order:** libC → libA, libB → Project

## Best Practices

### 1. Pin Dependency Versions

**Good:**
```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }
```

**Bad:**
```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", branch = "master" }
```

**Reason:** Tagged versions ensure reproducible builds.

### 2. Use CCGO.lock

```bash
# Generate lock file
ccgo install

# Commit to version control (for applications)
git add CCGO.lock
git commit -m "Add dependency lock file"
```

### 3. Minimal Dependencies

Only add dependencies you actually need:
- Reduces build time
- Simplifies dependency management
- Improves security (fewer attack vectors)

### 4. Document Dependencies

**README.md:**
```markdown
## Dependencies

- spdlog (v1.12.0): Logging library
- fmt (v10.1.1): Formatting library
- nlohmann/json (v3.11.2): JSON parsing
```

### 5. Regular Updates

```bash
# Check for updates monthly
ccgo update --dry-run

# Update after testing
ccgo update
ccgo build android --test
```

### 6. Security Considerations

- Review dependency source code before adding
- Use official repositories
- Check for security advisories
- Keep dependencies updated
- Vendor critical dependencies

## Advanced Usage

### Dependency Patches

Patches allow you to override dependency sources, similar to Cargo's `[patch]` feature. This is useful for:
- Testing bug fixes before they're merged upstream
- Using a fork with custom changes
- Applying local patches to third-party dependencies
- Working around version conflicts

#### Basic Patch Syntax

Patches are defined in the `[patch]` section of CCGO.toml:

```toml
[dependencies]
mylib = { git = "https://github.com/user/mylib.git", tag = "v1.0.0" }

# Patch a dependency with a fork
[patch."https://github.com/user/mylib.git"]
fmt = { git = "https://github.com/me/fmt-fork.git", branch = "bugfix" }
```

#### Registry Patches (Future)

For dependencies from a package registry:

```toml
[dependencies]
mylib = "1.0.0"

# Patch registry dependency
[patch.crates-io]
fmt = { git = "https://github.com/fmtlib/fmt.git", tag = "10.2.1" }
```

#### Source-Specific Patches

Override dependencies from specific sources:

```toml
[dependencies]
libA = { git = "https://github.com/user/libA.git", tag = "v1.0.0" }
libB = { git = "https://github.com/user/libB.git", tag = "v2.0.0" }

# Patch libA's dependency on common-utils
[patch."https://github.com/user/libA.git"]
common-utils = { git = "https://github.com/me/common-utils-fork.git", tag = "v1.5-custom" }

# Patch libB's dependency on common-utils (different patch)
[patch."https://github.com/user/libB.git"]
common-utils = { git = "https://github.com/me/common-utils.git", rev = "abc123" }
```

**Priority:** Source-specific patches take precedence over registry patches.

#### Local Path Patches

Use local versions for development:

```toml
[dependencies]
myapp = { git = "https://github.com/user/myapp.git", tag = "v1.0.0" }

# Use local version of fmt for testing
[patch."https://github.com/user/myapp.git"]
fmt = { path = "../fmt-local" }
```

#### Git Patch Options

All git options are supported in patches:

```toml
[patch.crates-io]
# Specific branch
spdlog = { git = "https://github.com/gabime/spdlog.git", branch = "v1.x" }

# Specific tag
fmt = { git = "https://github.com/fmtlib/fmt.git", tag = "10.2.1" }

# Exact revision
json = { git = "https://github.com/nlohmann/json.git", rev = "9cca280a" }
```

#### Patch Resolution Rules

1. **Source matching**: Patches are matched against the original dependency source
2. **Priority order**: Source-specific patches > Registry patches
3. **Lockfile records**: Patches are tracked in CCGO.lock with original and replacement sources
4. **Locked mode**: In `ccgo install --locked`, locked revisions are respected

#### Example: Testing Upstream Fix

```toml
[dependencies]
mylib = { git = "https://github.com/upstream/mylib.git", tag = "v1.0.0" }

# Test a pending PR before it's merged
[patch."https://github.com/upstream/mylib.git"]
problematic-dep = {
    git = "https://github.com/contributor/problematic-dep.git",
    branch = "fix-issue-123"
}
```

Then install:
```bash
ccgo install
```

Output shows patch is applied:
```
📦 Installing mylib...
   Source: https://github.com/upstream/mylib.git

📦 Installing problematic-dep...
   🔧 Applying patch for problematic-dep...
   Original: git+https://github.com/original/problematic-dep.git
   Patched:  git+https://github.com/contributor/problematic-dep.git
```

#### Example: Local Development Workflow

```toml
[dependencies]
production-lib = { git = "https://github.com/company/lib.git", tag = "v2.0.0" }

# Override with local development version
[patch."https://github.com/company/lib.git"]
utils = { path = "../utils-dev" }
logger = { path = "../logger-dev" }
```

This allows you to develop multiple dependencies simultaneously without modifying the main dependency definitions.

#### Lockfile Format

When patches are applied, CCGO.lock records both sources:

```toml
[[package]]
name = "fmt"
version = "10.2.1"
source = "git+https://github.com/me/fmt-fork.git#abc123"
git.revision = "abc123"
git.branch = "bugfix"
patch.patched_source = "git+https://github.com/fmtlib/fmt.git"
patch.replacement_source = "git+https://github.com/me/fmt-fork.git"
patch.is_path_patch = false
```

#### Removing Patches

To stop using a patch, simply remove it from CCGO.toml and re-run:

```bash
ccgo install --force
```

This will reinstall dependencies using their original sources.

### Private Git Repositories

**SSH authentication:**
```toml
[dependencies]
private-lib = { git = "git@github.com:company/private-lib.git", tag = "v1.0.0" }
```

**HTTPS with credentials:**
```bash
# Configure Git credentials
git config --global credential.helper store

# Or use SSH key
ssh-add ~/.ssh/id_rsa
```

### Workspace Dependencies

For mono-repo setups:

```
workspace/
├── CCGO.toml (workspace root)
├── lib1/
│   └── CCGO.toml
├── lib2/
│   └── CCGO.toml (depends on lib1)
└── app/
    └── CCGO.toml (depends on lib1, lib2)
```

**lib2/CCGO.toml:**
```toml
[dependencies]
lib1 = { path = "../lib1" }
```

**app/CCGO.toml:**
```toml
[dependencies]
lib1 = { path = "../lib1" }
lib2 = { path = "../lib2" }
```

## Troubleshooting

### Dependency Not Found

```
Error: Could not find dependency 'spdlog'
```

**Solutions:**
1. Check repository URL is correct
2. Verify Git tag/branch exists
3. Check network connectivity
4. Try manual clone: `git clone <url>`

### Version Conflict

```
Error: Conflicting versions for dependency 'fmt'
```

**Solutions:**
1. Run `ccgo update` to resolve conflicts
2. Manually adjust version constraints
3. Use `ccgo vendor` to lock specific versions

### Build Fails After Update

```
Error: Build failed after dependency update
```

**Solutions:**
1. Revert to previous CCGO.lock: `git checkout CCGO.lock`
2. Install locked versions: `ccgo install --locked`
3. Test dependencies individually
4. Check compatibility matrix

### Slow Dependency Resolution

```
Taking too long to resolve dependencies...
```

**Solutions:**
1. Use `--locked` to skip resolution
2. Vendor dependencies: `ccgo vendor`
3. Check network speed
4. Clear cache: `rm -rf ~/.ccgo/cache/`

### Git Authentication Failed

```
Error: Authentication failed for Git repository
```

**Solutions:**
1. Set up SSH keys: `ssh-keygen`
2. Add key to Git provider
3. Or use personal access token
4. Configure Git credentials manager

## Dependency Ecosystem

### Popular C++ Dependencies

**Logging:**
- spdlog: https://github.com/gabime/spdlog
- glog: https://github.com/google/glog

**Formatting:**
- fmt: https://github.com/fmtlib/fmt

**JSON:**
- nlohmann/json: https://github.com/nlohmann/json
- RapidJSON: https://github.com/Tencent/rapidjson

**Testing:**
- Catch2: https://github.com/catchorg/Catch2
- Google Test: https://github.com/google/googletest

**Networking:**
- Boost.Asio: https://github.com/boostorg/asio
- cpp-httplib: https://github.com/yhirose/cpp-httplib

**Serialization:**
- protobuf: https://github.com/protocolbuffers/protobuf
- flatbuffers: https://github.com/google/flatbuffers

### Finding Dependencies

- **GitHub**: Search for C++ libraries
- **Awesome C++**: https://github.com/fffaraz/awesome-cpp
- **Conan Center**: https://conan.io/center/
- **vcpkg**: https://github.com/microsoft/vcpkg

## Migration Guides

### From Conan

**conanfile.txt:**
```ini
[requires]
spdlog/1.12.0
fmt/10.1.1

[options]
spdlog:shared=False
```

**CCGO.toml:**
```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }
fmt = { git = "https://github.com/fmtlib/fmt.git", tag = "10.1.1" }

[library]
type = "static"
```

### From vcpkg

**vcpkg.json:**
```json
{
  "dependencies": [
    "spdlog",
    "fmt",
    "nlohmann-json"
  ]
}
```

**CCGO.toml:**
```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }
fmt = { git = "https://github.com/fmtlib/fmt.git", tag = "10.1.1" }
json = { git = "https://github.com/nlohmann/json.git", tag = "v3.11.2" }
```

### From Git Submodules

**Replace:**
```bash
git submodule add https://github.com/gabime/spdlog.git third_party/spdlog
```

**With:**
```toml
[dependencies]
spdlog = { git = "https://github.com/gabime/spdlog.git", tag = "v1.12.0" }
```

Then:
```bash
ccgo install
```

## See Also

- [CCGO.toml Reference]../reference/ccgo-toml.md
- [Build System]build-system.md
- [Publishing]publishing.md
- [Project Structure]../getting-started/project-structure.md