diff options
| author | Bruce Ashfield <bruce.ashfield@gmail.com> | 2025-12-08 14:23:46 +0000 |
|---|---|---|
| committer | Bruce Ashfield <bruce.ashfield@gmail.com> | 2025-12-08 20:57:44 -0500 |
| commit | 2385a741401386d129a0cd4dc4c332d3d54af4b3 (patch) | |
| tree | ffbc0539e22a0ac230580715480d9759df4fb24e | |
| parent | 4d63f769924095a8aa85bbe21faeeed904ed36ef (diff) | |
| download | meta-virtualization-2385a741401386d129a0cd4dc4c332d3d54af4b3.tar.gz | |
docs: add QUICKSTART for go-mod-vcs
Signed-off-by: Bruce Ashfield <bruce.ashfield@gmail.com>
| -rw-r--r-- | scripts/QUICKSTART-oe-go-mod-vcs.md | 408 |
1 files changed, 408 insertions, 0 deletions
diff --git a/scripts/QUICKSTART-oe-go-mod-vcs.md b/scripts/QUICKSTART-oe-go-mod-vcs.md new file mode 100644 index 00000000..40db7885 --- /dev/null +++ b/scripts/QUICKSTART-oe-go-mod-vcs.md | |||
| @@ -0,0 +1,408 @@ | |||
| 1 | # Quickstart: Go Module VCS Build System for Yocto/BitBake | ||
| 2 | |||
| 3 | This guide covers how to create and maintain Go recipes using the `go-mod-vcs` system, which provides reproducible, offline Go builds by fetching dependencies directly from their git repositories. | ||
| 4 | |||
| 5 | ## Overview | ||
| 6 | |||
| 7 | The `go-mod-vcs` system replaces vendor directories with a build-time module cache constructed from git repositories. This provides: | ||
| 8 | |||
| 9 | - **Reproducible builds** - Every module comes from a verified git commit | ||
| 10 | - **Offline builds** - After `do_fetch`, no network access is required | ||
| 11 | - **Auditable dependencies** - Every dependency is traceable to a specific git commit | ||
| 12 | - **Smaller recipes** - No need to maintain large vendor directories in source trees | ||
| 13 | |||
| 14 | ### How It Works | ||
| 15 | |||
| 16 | ``` | ||
| 17 | ┌─────────────────────────────────────────────────────────────────────┐ | ||
| 18 | │ Your Recipe │ | ||
| 19 | │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────┐ │ | ||
| 20 | │ │ myapp_git.bb │ │ go-mod-git.inc │ │ go-mod-cache.inc │ │ | ||
| 21 | │ │ (your code) │ │ (git fetches) │ │ (module metadata) │ │ | ||
| 22 | │ └─────────────────┘ └─────────────────┘ └─────────────────────┘ │ | ||
| 23 | └───────────────────────────────┬─────────────────────────────────────┘ | ||
| 24 | │ | ||
| 25 | ┌───────────────────────┼───────────────────────┐ | ||
| 26 | │ │ │ | ||
| 27 | ▼ ▼ ▼ | ||
| 28 | ┌─────────┐ ┌─────────────┐ ┌───────────────┐ | ||
| 29 | │do_fetch │ │do_create_ │ │ do_compile │ | ||
| 30 | │ │──────────▶│module_cache │────────▶│ │ | ||
| 31 | │(git) │ │(build cache)│ │(go build) │ | ||
| 32 | └─────────┘ └─────────────┘ └───────────────┘ | ||
| 33 | ``` | ||
| 34 | |||
| 35 | 1. **do_fetch** - BitBake fetches all git repositories listed in `go-mod-git.inc` | ||
| 36 | 2. **do_create_module_cache** - The `go-mod-vcs` class builds a Go module cache from git checkouts | ||
| 37 | 3. **do_compile** - Go builds offline using the pre-populated module cache | ||
| 38 | |||
| 39 | --- | ||
| 40 | |||
| 41 | ## Converting an Existing Recipe | ||
| 42 | |||
| 43 | ### Step 1: Add Discovery Configuration | ||
| 44 | |||
| 45 | Add these lines to your recipe (before `inherit go`): | ||
| 46 | |||
| 47 | ```bitbake | ||
| 48 | # go-mod-discovery configuration | ||
| 49 | GO_MOD_DISCOVERY_BUILD_TARGET = "./cmd/..." # Your build target | ||
| 50 | GO_MOD_DISCOVERY_GIT_REPO = "https://github.com/org/repo.git" | ||
| 51 | GO_MOD_DISCOVERY_GIT_REF = "${SRCREV}" | ||
| 52 | |||
| 53 | inherit go-mod-discovery | ||
| 54 | ``` | ||
| 55 | |||
| 56 | ### Step 2: Include the Generated Files | ||
| 57 | |||
| 58 | Add includes after your `SRC_URI`: | ||
| 59 | |||
| 60 | ```bitbake | ||
| 61 | SRC_URI = "git://github.com/org/repo;branch=main;protocol=https;destsuffix=${GO_SRCURI_DESTSUFFIX}" | ||
| 62 | |||
| 63 | include go-mod-git.inc | ||
| 64 | include go-mod-cache.inc | ||
| 65 | ``` | ||
| 66 | |||
| 67 | ### Step 3: Create Placeholder Files | ||
| 68 | |||
| 69 | Create empty placeholder files (they'll be generated): | ||
| 70 | |||
| 71 | ```bash | ||
| 72 | cd recipes-containers/myapp/ | ||
| 73 | touch go-mod-git.inc go-mod-cache.inc | ||
| 74 | ``` | ||
| 75 | |||
| 76 | ### Step 4: Run Discovery | ||
| 77 | |||
| 78 | ```bash | ||
| 79 | bitbake myapp -c discover_and_generate | ||
| 80 | ``` | ||
| 81 | |||
| 82 | This will: | ||
| 83 | 1. Build your project with network access to discover dependencies | ||
| 84 | 2. Extract module metadata from the Go module cache | ||
| 85 | 3. Generate `go-mod-git.inc` and `go-mod-cache.inc` files | ||
| 86 | |||
| 87 | ### Step 5: Update do_compile() | ||
| 88 | |||
| 89 | Ensure your `do_compile()` doesn't set conflicting environment variables. The `go-mod-vcs.bbclass` automatically sets: | ||
| 90 | |||
| 91 | - `GOMODCACHE` - Points to the built module cache | ||
| 92 | - `GOPROXY=off` - Enforces offline build | ||
| 93 | - `GOSUMDB=off` - Disables checksum database | ||
| 94 | - `GOTOOLCHAIN=local` - Uses the native Go toolchain | ||
| 95 | |||
| 96 | A minimal `do_compile()`: | ||
| 97 | |||
| 98 | ```bitbake | ||
| 99 | do_compile() { | ||
| 100 | cd ${S}/src/import | ||
| 101 | |||
| 102 | export GOPATH="${S}/src/import/.gopath:${STAGING_DIR_TARGET}/${prefix}/local/go" | ||
| 103 | export CGO_ENABLED="1" | ||
| 104 | |||
| 105 | ${GO} build -trimpath ./cmd/... | ||
| 106 | } | ||
| 107 | ``` | ||
| 108 | |||
| 109 | ### Step 6: Build and Test | ||
| 110 | |||
| 111 | ```bash | ||
| 112 | bitbake myapp | ||
| 113 | ``` | ||
| 114 | |||
| 115 | --- | ||
| 116 | |||
| 117 | ## Creating a New Recipe | ||
| 118 | |||
| 119 | ### Minimal Recipe Template | ||
| 120 | |||
| 121 | ```bitbake | ||
| 122 | SUMMARY = "My Go Application" | ||
| 123 | HOMEPAGE = "https://github.com/org/myapp" | ||
| 124 | |||
| 125 | SRCREV = "abc123def456..." | ||
| 126 | SRC_URI = "git://github.com/org/myapp;branch=main;protocol=https;destsuffix=${GO_SRCURI_DESTSUFFIX}" | ||
| 127 | |||
| 128 | include go-mod-git.inc | ||
| 129 | include go-mod-cache.inc | ||
| 130 | |||
| 131 | LICENSE = "Apache-2.0" | ||
| 132 | LIC_FILES_CHKSUM = "file://src/import/LICENSE;md5=..." | ||
| 133 | |||
| 134 | GO_IMPORT = "import" | ||
| 135 | PV = "v1.0.0+git" | ||
| 136 | |||
| 137 | # go-mod-discovery configuration | ||
| 138 | GO_MOD_DISCOVERY_BUILD_TARGET = "./cmd/..." | ||
| 139 | GO_MOD_DISCOVERY_GIT_REPO = "https://github.com/org/myapp.git" | ||
| 140 | GO_MOD_DISCOVERY_GIT_REF = "${SRCREV}" | ||
| 141 | |||
| 142 | inherit go goarch | ||
| 143 | inherit go-mod-discovery | ||
| 144 | |||
| 145 | do_compile() { | ||
| 146 | cd ${S}/src/import | ||
| 147 | |||
| 148 | export GOPATH="${S}/src/import/.gopath:${STAGING_DIR_TARGET}/${prefix}/local/go" | ||
| 149 | export CGO_ENABLED="1" | ||
| 150 | |||
| 151 | ${GO} build -trimpath -o ${B}/myapp ./cmd/myapp | ||
| 152 | } | ||
| 153 | |||
| 154 | do_install() { | ||
| 155 | install -d ${D}${bindir} | ||
| 156 | install -m 755 ${B}/myapp ${D}${bindir}/ | ||
| 157 | } | ||
| 158 | ``` | ||
| 159 | |||
| 160 | ### Generate Dependencies | ||
| 161 | |||
| 162 | ```bash | ||
| 163 | # Create placeholder files | ||
| 164 | touch go-mod-git.inc go-mod-cache.inc | ||
| 165 | |||
| 166 | # Run discovery | ||
| 167 | bitbake myapp -c discover_and_generate | ||
| 168 | |||
| 169 | # Build | ||
| 170 | bitbake myapp | ||
| 171 | ``` | ||
| 172 | |||
| 173 | --- | ||
| 174 | |||
| 175 | ## Updating a Recipe to a New Version | ||
| 176 | |||
| 177 | ### Quick Update (Same Repository) | ||
| 178 | |||
| 179 | 1. **Update the SRCREV** in your recipe: | ||
| 180 | ```bitbake | ||
| 181 | SRCREV = "new_commit_hash_here" | ||
| 182 | ``` | ||
| 183 | |||
| 184 | 2. **Re-run discovery**: | ||
| 185 | ```bash | ||
| 186 | bitbake myapp -c discover_and_generate | ||
| 187 | ``` | ||
| 188 | |||
| 189 | 3. **Build**: | ||
| 190 | ```bash | ||
| 191 | bitbake myapp | ||
| 192 | ``` | ||
| 193 | |||
| 194 | ### Full Workflow Example | ||
| 195 | |||
| 196 | ```bash | ||
| 197 | # 1. Find the new commit/tag | ||
| 198 | git ls-remote https://github.com/org/myapp refs/tags/v2.0.0 | ||
| 199 | |||
| 200 | # 2. Update SRCREV in recipe | ||
| 201 | # SRCREV = "abc123..." | ||
| 202 | |||
| 203 | # 3. Clean old discovery cache (optional, recommended for major updates) | ||
| 204 | bitbake myapp -c clean_discovery | ||
| 205 | |||
| 206 | # 4. Run discovery and generation | ||
| 207 | bitbake myapp -c discover_and_generate | ||
| 208 | |||
| 209 | # 5. Review generated files | ||
| 210 | git diff recipes-containers/myapp/go-mod-*.inc | ||
| 211 | |||
| 212 | # 6. Build and test | ||
| 213 | bitbake myapp | ||
| 214 | |||
| 215 | # 7. Commit changes | ||
| 216 | git add recipes-containers/myapp/ | ||
| 217 | git commit -m "myapp: update to v2.0.0" | ||
| 218 | ``` | ||
| 219 | |||
| 220 | --- | ||
| 221 | |||
| 222 | ## Discovery Tasks Reference | ||
| 223 | |||
| 224 | | Task | Purpose | Network? | | ||
| 225 | |------|---------|----------| | ||
| 226 | | `discover_modules` | Build project and download modules from proxy | Yes | | ||
| 227 | | `extract_modules` | Extract metadata from cache to JSON | No | | ||
| 228 | | `generate_modules` | Generate .inc files from metadata | No | | ||
| 229 | | `discover_and_generate` | All three steps in sequence | Yes | | ||
| 230 | | `show_upgrade_commands` | Print command lines without running | No | | ||
| 231 | | `clean_discovery` | Remove the discovery cache | No | | ||
| 232 | |||
| 233 | ### Step-by-Step vs All-in-One | ||
| 234 | |||
| 235 | **All-in-one** (recommended for most cases): | ||
| 236 | ```bash | ||
| 237 | bitbake myapp -c discover_and_generate | ||
| 238 | ``` | ||
| 239 | |||
| 240 | **Step-by-step** (useful for debugging): | ||
| 241 | ```bash | ||
| 242 | bitbake myapp -c discover_modules # Download modules | ||
| 243 | bitbake myapp -c extract_modules # Extract to JSON | ||
| 244 | bitbake myapp -c generate_modules # Generate .inc files | ||
| 245 | ``` | ||
| 246 | |||
| 247 | --- | ||
| 248 | |||
| 249 | ## Configuration Variables | ||
| 250 | |||
| 251 | | Variable | Default | Description | | ||
| 252 | |----------|---------|-------------| | ||
| 253 | | `GO_MOD_DISCOVERY_BUILD_TARGET` | *(required)* | Go build target (e.g., `./cmd/...`) | | ||
| 254 | | `GO_MOD_DISCOVERY_GIT_REPO` | *(required for generate)* | Git repository URL | | ||
| 255 | | `GO_MOD_DISCOVERY_GIT_REF` | `${SRCREV}` | Git commit/tag | | ||
| 256 | | `GO_MOD_DISCOVERY_SRCDIR` | `${S}/src/import` | Directory containing go.mod | | ||
| 257 | | `GO_MOD_DISCOVERY_BUILD_TAGS` | `${TAGS}` | Go build tags | | ||
| 258 | | `GO_MOD_DISCOVERY_RECIPEDIR` | `${FILE_DIRNAME}` | Output directory for .inc files | | ||
| 259 | |||
| 260 | --- | ||
| 261 | |||
| 262 | ## Troubleshooting | ||
| 263 | |||
| 264 | ### "module lookup disabled by GOPROXY=off" | ||
| 265 | |||
| 266 | This error during `do_compile` means a module is missing from the cache. | ||
| 267 | |||
| 268 | **Fix:** | ||
| 269 | ```bash | ||
| 270 | # Re-run discovery to find missing modules | ||
| 271 | bitbake myapp -c discover_and_generate | ||
| 272 | bitbake myapp | ||
| 273 | ``` | ||
| 274 | |||
| 275 | ### Discovery Cache Location | ||
| 276 | |||
| 277 | The discovery cache persists in `${TOPDIR}/go-mod-discovery/${PN}/${PV}/` and survives `bitbake -c cleanall`. To fully reset: | ||
| 278 | |||
| 279 | ```bash | ||
| 280 | bitbake myapp -c clean_discovery | ||
| 281 | bitbake myapp -c discover_and_generate | ||
| 282 | ``` | ||
| 283 | |||
| 284 | ### Viewing Generated Commands | ||
| 285 | |||
| 286 | To see what commands would be run without executing them: | ||
| 287 | |||
| 288 | ```bash | ||
| 289 | bitbake myapp -c show_upgrade_commands | ||
| 290 | ``` | ||
| 291 | |||
| 292 | ### Build Fails After SRCREV Update | ||
| 293 | |||
| 294 | If changing SRCREV causes sstate errors: | ||
| 295 | |||
| 296 | ```bash | ||
| 297 | # Clean sstate for the recipe | ||
| 298 | bitbake myapp -c cleansstate | ||
| 299 | |||
| 300 | # Re-run discovery | ||
| 301 | bitbake myapp -c discover_and_generate | ||
| 302 | |||
| 303 | # Build | ||
| 304 | bitbake myapp | ||
| 305 | ``` | ||
| 306 | |||
| 307 | ### Multiple Source Repositories | ||
| 308 | |||
| 309 | For recipes with multiple git sources, use named SRCREVs: | ||
| 310 | |||
| 311 | ```bitbake | ||
| 312 | SRCREV_myapp = "abc123..." | ||
| 313 | SRCREV_plugins = "def456..." | ||
| 314 | SRCREV_FORMAT = "myapp_plugins" | ||
| 315 | |||
| 316 | SRC_URI = "\ | ||
| 317 | git://github.com/org/myapp;name=myapp;branch=main;protocol=https;destsuffix=${GO_SRCURI_DESTSUFFIX} \ | ||
| 318 | git://github.com/org/plugins;name=plugins;branch=main;protocol=https;destsuffix=${GO_SRCURI_DESTSUFFIX}/plugins \ | ||
| 319 | " | ||
| 320 | |||
| 321 | GO_MOD_DISCOVERY_GIT_REF = "${SRCREV_myapp}" | ||
| 322 | ``` | ||
| 323 | |||
| 324 | --- | ||
| 325 | |||
| 326 | ## Example Recipes | ||
| 327 | |||
| 328 | ### Simple Recipe (rootlesskit) | ||
| 329 | |||
| 330 | ```bitbake | ||
| 331 | SRCREV_rootless = "8059d35092db167ec53cae95fb6aa37fc577060c" | ||
| 332 | SRCREV_FORMAT = "rootless" | ||
| 333 | |||
| 334 | SRC_URI = "git://github.com/rootless-containers/rootlesskit;name=rootless;branch=master;protocol=https;destsuffix=${GO_SRCURI_DESTSUFFIX}" | ||
| 335 | |||
| 336 | include go-mod-git.inc | ||
| 337 | include go-mod-cache.inc | ||
| 338 | |||
| 339 | GO_MOD_DISCOVERY_BUILD_TARGET = "./cmd/..." | ||
| 340 | GO_MOD_DISCOVERY_GIT_REPO = "https://github.com/rootless-containers/rootlesskit.git" | ||
| 341 | GO_MOD_DISCOVERY_GIT_REF = "${SRCREV_rootless}" | ||
| 342 | |||
| 343 | inherit go goarch go-mod-discovery | ||
| 344 | ``` | ||
| 345 | |||
| 346 | ### Complex Recipe (k3s with build tags) | ||
| 347 | |||
| 348 | ```bitbake | ||
| 349 | TAGS = "static_build netcgo osusergo providerless" | ||
| 350 | |||
| 351 | GO_MOD_DISCOVERY_BUILD_TARGET = "./cmd/server/main.go" | ||
| 352 | GO_MOD_DISCOVERY_GIT_REPO = "https://github.com/rancher/k3s.git" | ||
| 353 | GO_MOD_DISCOVERY_GIT_REF = "${SRCREV_k3s}" | ||
| 354 | |||
| 355 | inherit go goarch go-mod-discovery | ||
| 356 | ``` | ||
| 357 | |||
| 358 | --- | ||
| 359 | |||
| 360 | ## Files Generated | ||
| 361 | |||
| 362 | ### go-mod-git.inc | ||
| 363 | |||
| 364 | Contains `SRC_URI` entries for each module's git repository: | ||
| 365 | |||
| 366 | ```bitbake | ||
| 367 | SRC_URI += "git://github.com/spf13/cobra;protocol=https;nobranch=1;rev=e94f6d0...;name=git_41456771_1;destsuffix=vcs_cache/2d91d6bc..." | ||
| 368 | ``` | ||
| 369 | |||
| 370 | ### go-mod-cache.inc | ||
| 371 | |||
| 372 | Contains module metadata and inherits the build class: | ||
| 373 | |||
| 374 | ```bitbake | ||
| 375 | inherit go-mod-vcs | ||
| 376 | |||
| 377 | GO_MODULE_CACHE_DATA = '[{"module":"github.com/spf13/cobra","version":"v1.8.1","vcs_hash":"2d91d6bc...","timestamp":"2024-06-01T10:31:11Z","subdir":""},...]' | ||
| 378 | ``` | ||
| 379 | |||
| 380 | --- | ||
| 381 | |||
| 382 | ## Advanced: Manual Script Invocation | ||
| 383 | |||
| 384 | For cases where BitBake isn't available or you need more control: | ||
| 385 | |||
| 386 | ```bash | ||
| 387 | # Direct generation from git (no BitBake needed) | ||
| 388 | python3 ./meta-virtualization/scripts/oe-go-mod-fetcher.py \ | ||
| 389 | --git-repo https://github.com/org/myapp.git \ | ||
| 390 | --git-ref abc123... \ | ||
| 391 | --recipedir ./meta-virtualization/recipes-containers/myapp/ | ||
| 392 | |||
| 393 | # Using existing discovery cache | ||
| 394 | python3 ./meta-virtualization/scripts/oe-go-mod-fetcher.py \ | ||
| 395 | --discovered-modules ${TOPDIR}/go-mod-discovery/myapp/v1.0.0/modules.json \ | ||
| 396 | --git-repo https://github.com/org/myapp.git \ | ||
| 397 | --git-ref abc123... \ | ||
| 398 | --recipedir ./meta-virtualization/recipes-containers/myapp/ | ||
| 399 | ``` | ||
| 400 | |||
| 401 | --- | ||
| 402 | |||
| 403 | ## Getting Help | ||
| 404 | |||
| 405 | - **Show available commands**: `bitbake myapp -c show_upgrade_commands` | ||
| 406 | - **Architecture details**: See `scripts/ARCHITECTURE.md` | ||
| 407 | - **Report issues**: Check the generated files and error messages from discovery | ||
| 408 | |||
