Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!README.md
Empty file added .build/README.md
Empty file.
46 changes: 44 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ jobs:
# muparser — Algorithmic API (gdal_global_reg_names fix)
# pkgconf — pkg-config shim used by cmake find-modules
# ntldd — walk PE import tree to find transitive DLL deps
#
# hdf5, netcdf — HDF5/NetCDF raster drivers (their DLL chains,
# incl. libopenblas/libgfortran, load cleanly in R;
# verified empirically 2026-07)
#
# NOTE: mingw-w64-ucrt-x86_64-gdal transitively installs podofo and
# poppler into /ucrt64 regardless of this list, so the PDF driver is
# disabled at cmake level (GDAL_ENABLE_DRIVER_PDF=OFF in build_gdal.sh):
# the MSYS2 libpodofo.dll fails DllMain (ERROR_DLL_INIT_FAILED 1114) in
# any process, which would make libgdal itself unloadable.
- name: Setup MSYS2 UCRT64
id: msys2
uses: msys2/setup-msys2@v2
Expand All @@ -219,12 +229,11 @@ jobs:
mingw-w64-ucrt-x86_64-curl
mingw-w64-ucrt-x86_64-zlib
mingw-w64-ucrt-x86_64-muparser
mingw-w64-ucrt-x86_64-arrow
mingw-w64-ucrt-x86_64-hdf5
mingw-w64-ucrt-x86_64-netcdf
mingw-w64-ucrt-x86_64-arrow
mingw-w64-ucrt-x86_64-geos
mingw-w64-ucrt-x86_64-libspatialite
mingw-w64-ucrt-x86_64-poppler
mingw-w64-ucrt-x86_64-openjpeg2
mingw-w64-ucrt-x86_64-libwebp
mingw-w64-ucrt-x86_64-libjxl
Expand Down Expand Up @@ -323,6 +332,37 @@ jobs:
Select-Object Name, @{N='MB';E={[math]::Round($_.Length/1MB,2)}} |
Sort-Object Name | Format-Table -AutoSize

# ── 7b. Smoke-test DLL loadability ────────────────────────────────────
# LoadLibrary every bundled DLL in a plain Windows process. This catches
# DllMain initialization failures (ERROR_DLL_INIT_FAILED, 1114) before a
# broken bundle can be published — the exact failure mode that shipped
# in July 2026 when MSYS2's libpodofo.dll started failing DllMain and
# made libgdal-*.dll unloadable in every consumer process, including R.
# File-presence checks alone cannot detect this class of breakage.
- name: Smoke-test bundle DLL loadability
shell: pwsh
run: |
$bin = Join-Path $PWD "gdal-bundle\bin"
$env:PATH = "$bin;$env:PATH"
$sig = @'
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadLibraryW(string path);
'@
$k32 = Add-Type -MemberDefinition $sig -Name Loader -Namespace Smoke -PassThru
$failures = @()
foreach ($dll in (Get-ChildItem "$bin\*.dll" | Sort-Object Name)) {
$h = $k32::LoadLibraryW($dll.FullName)
if ($h -eq [IntPtr]::Zero) {
$code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$failures += "$($dll.Name) (Win32 error $code)"
}
}
if ($failures.Count -gt 0) {
Write-Error ("FATAL: bundled DLLs failed to load:`n " + ($failures -join "`n "))
exit 1
}
Write-Host "✓ All $((Get-ChildItem "$bin\*.dll").Count) bundled DLLs load cleanly (no DllMain failures)"

# ── 8. Upload bundle as workflow artifact (always) ────────────────────
# compression-level: 0 — DLLs are already-compressed PE binaries.
# Re-compressing wastes 30-60s and gains <5%.
Expand Down Expand Up @@ -375,6 +415,8 @@ jobs:
| `GDAL_HIDE_INTERNAL_SYMBOLS` | `ON` | Cleaner export table |
| `--kill-at` | (linker) | Correct stdcall symbol decoration for MinGW DLLs |
| Static GCC/stdc++/winpthread | (linker) | No Rtools or MSYS2 required at runtime |
| `GDAL_ENABLE_DRIVER_PDF` | `OFF` | MSYS2 libpodofo.dll fails DllMain (ERROR_DLL_INIT_FAILED 1114) in any process, making libgdal unloadable; poppler/podofo/pdfium backends all disabled |
| Loadability gate | (CI) | Every bundled DLL is LoadLibrary-tested in CI before publishing |

### Runtime bundle asset
Download:
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
.Renviron
inst/doc
.positai
.build/
inst/extdata/gdal-ucrt64-fallback.zip
docs
/docs/
**/.quarto/
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# gdalraster.windows (development version)

## GDAL runtime bundle

- Fixed the bundle-wide `LoadLibrary failure: A dynamic link library (DLL) initialization routine failed` (Windows error 1114) that made published bundles unloadable on machines without Rtools' UCRT64 tree. Root cause: the MSYS2 `libpodofo.dll` (PDF driver backend) fails its `DllMain`-time OpenSSL initialization against the MSYS2 `libcrypto-3-x64.dll`. The PDF driver is now disabled outright (`GDAL_ENABLE_DRIVER_PDF=OFF` plus the poppler/podofo/pdfium backends).
- HDF5 and NetCDF drivers are enabled again; the earlier theory that their OpenBLAS/Fortran dependency chain conflicts with R's own DLLs was disproven empirically (every DLL in that chain loads cleanly inside R).
- Bundle verification now has three layers: dependency closure (`ntldd` walk plus a banned-DLL guard for PDF/NSS chains in `collect_dlls.sh`), loadability (every bundled DLL is `LoadLibrary`-tested in CI before an asset can publish), and functionality (the e2e workflow runs on every published bundle release).

## Fixes

- `install_gdal_runtime(tag = "latest")` no longer trusts GitHub's single "latest release" pointer, which broke the default install path whenever an R package release (`v*`, no bundle asset) was marked latest. "latest" now scans the release list and selects the newest non-draft, non-prerelease release that publishes a runtime bundle asset matching `asset_pattern`, decoupling the package release track from the GDAL bundle release track.
Expand Down
2 changes: 2 additions & 0 deletions dev/archive/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
85 changes: 85 additions & 0 deletions dev/docs/00-the-full-picture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# the full picture

> [!NOTE]
> Start here. This is the narrative that connects everything: what problem
> this project solves, why every major design decision was forced rather
> than chosen, and what guarantees the system provides today. It assumes a
> senior engineer who does not need to know Windows C++ internals up front —
> concepts are introduced when the story needs them, and
> [`08-windows-dll-abi-deep-dive.md`](08-windows-dll-abi-deep-dive.md) has
> the full mechanics when you want them.

## the one-paragraph version

`gdalraster` needs a modern GDAL with the Algorithm API (muparser), which the R toolchain's own GDAL doesn't provide. Because `gdalraster` binds GDAL's **C++** API, and C++ has no cross-compiler binary standard, the GDAL it links must come from the same compiler family as R's Windows toolchain (MinGW/UCRT) — ruling out every convenient prebuilt GDAL (conda, OSGeo4W, vcpkg: all MSVC). So this project builds GDAL from source with MinGW in CI, ships it with its complete DLL closure as a versioned bundle, rebuilds `gdalraster` from source against that bundle on the user's machine, and manages the Windows loader at session startup so the right DLLs resolve. Each of those clauses exists because something broke without it.

## act 1: the original problem (May 2026)

`gdalraster::gdal_global_reg_names()` returned `character(0)` on Windows — the GDAL Algorithm API registry was empty. Two upstream causes:

1. GDAL's algorithm registration uses C++ static constructors, which some static-linking toolchains dead-code-eliminated ([firelab/gdalraster#826](https://github.com/firelab/gdalraster/issues/826), fixed upstream in GDAL 3.12.2 via [OSGeo/gdal#13592](https://github.com/OSGeo/gdal/pull/13592)).
2. Rtools' bundled GDAL lacked muparser, which parts of the Algorithm API require.

The fix that worked ([firelab/gdalraster#982](https://github.com/firelab/gdalraster/issues/982), reproduced in the original runbook kept locally under `dev/archive/`): build GDAL 3.13 from source in the Rtools45 UCRT64 shell with `GDAL_USE_MUPARSER=ON`, point `gdalraster` compilation at it via Makevars, and make the DLLs resolvable at runtime via PATH + preload in `.Rprofile`.

That manual setup (`C:/gdal-ucrt64` + `C:/rtools45/ucrt64/bin` on PATH) is the ancestor of everything in this repo. Understanding *why it worked so reliably* took until July: all of its dependency DLLs came from one internally coherent, curated package set (Rtools45's ucrt64 tree). The productized bundle kept breaking precisely where it departed from that property.

## act 2: why the design looks the way it does

Each design element is a response to a constraint. In order:

**Why build GDAL with MinGW at all, instead of using an existing GDAL?** R packages on Windows are compiled by Rtools, which is MinGW-GCC targeting UCRT. `gdalraster` binds GDAL's C++ classes (not a C shim), and C++ has no standard ABI: MSVC and GCC differ in name mangling, exception handling, class layout, and STL types. An MSVC GDAL (conda-forge/pixi, OSGeo4W, vcpkg) is not linkable from Rtools-compiled code — the symbols don't even have the same names. So the GDAL must be MinGW-built. (Deep dive §4.)

**Why build in CI with MSYS2 rather than require users to build locally?** The manual runbook takes an hour and a toolchain install. CI builds once, publishes a versioned artifact, and every machine consumes the same bytes. The cost: MSYS2 is a rolling release, so the build environment's package set changes under us between builds — the root of two of the three production incidents (below).

**Why ship a "bundle" (DLL closure) instead of just `libgdal-*.dll`?** The import table of `libgdal-39.dll` names ~78 direct dependency DLLs, with hundreds transitive. On the build machine they live in `/ucrt64/bin`; user machines don't have that. `tools/collect_dlls.sh` walks the import tree (`ntldd -R`) and copies everything non-Windows into `bundle/bin`, then fails the build if anything remains unresolved. (Deep dive §2, §8.)

**Why rebuild `gdalraster` from source on the user's machine?** Same C++ ABI logic one level up: `gdalraster.dll` must be compiled against the bundle's GDAL headers and linked to its import library. The CRAN binary is linked (statically) against Rtools' own GDAL — different GDAL, no Algorithm API. `install_gdalraster()` does the source build with scoped Makevars (`withr::with_makevars`), so nothing persists in user config.

**Why does the package manage `PATH`/preload at session start?** Compile-time linking and run-time loading are separate systems. At `library(gdalraster)`, the Windows loader must resolve `libgdal-39.dll` and its entire closure *at that moment*, by base name, through the DLL search order. `activate_gdal_runtime()` prepends the bundle's `bin/` to `PATH`, sets GDAL/PROJ data variables, and preloads `libgdal` into the process so every later request reuses it (first-loaded-wins rule). (Deep dive §2.)

**Why is everything non-destructive/isolated by default?** The runtime installs under `tools::R_user_dir()` paths and `gdalraster` goes to an isolated library, because the target machines often *also* have the manual `C:/gdal-ucrt64` stack, CRAN sf/terra (which embed their own static GDAL), and unrelated GDALs (pixi/conda). Base-name collisions between GDAL stacks in one process are resolved by load order, so the package never wants to win by overwriting — only by activating first in sessions that opt in.

## act 3: the three production incidents

Every incident was a *distribution* failure, not a build failure — the DLL compiled and linked fine each time. This is the pattern to internalize: on Windows, the hard part is not producing a binary, it is controlling the environment the binary meets.

| # | Incident | Mechanism | Countermeasure now in place |
|---|---|---|---|
| 1 | `msodbcsql17.dll` (June, #13) | GDAL's cmake auto-detected the SQL Server ODBC SDK on the GitHub runner and linked it; bundle verification blanket-trusted System32, where that DLL exists on runners but not user machines | `GDAL_USE_MSSQL_ODBC/NCLI=OFF`; verifier denies known vendor DLLs in System32 |
| 2 | Missing-module failures during early adoption | Transitive DLLs absent from `PATH` at load time in sessions without activation | loud preload failure in `activate_gdal_runtime()`; troubleshooting flow |
| 3 | `ERROR_DLL_INIT_FAILED` 1114 (July, e2e) | MSYS2's `libpodofo.dll` runs OpenSSL setup in its static initializers (DllMain, under loader lock) and fails against MSYS2's `libcrypto` — in every process; `libgdal` imported podofo because MSYS2's gdal package pulls it in and cmake auto-detected it | PDF driver disabled outright (`GDAL_ENABLE_DRIVER_PDF=OFF` + all backends); banned-DLL guard in `collect_dlls.sh`; LoadLibrary smoke gate in `build.yml` |

Incident 3 deserves its own moral because it defeated the existing verification: the bundle was *complete* (every import resolved — `ntldd` proved it) but not *loadable* (one DLL failed initialization). Static inspection of any kind cannot catch that class; only actually loading the DLLs can. Hence the three-layer gate that now guards every release:

1. **closure** — `ntldd` walk + banned-DLL regex (`collect_dlls.sh`)
2. **loadability** — `LoadLibrary` over every bundled DLL in a plain process (`build.yml` step 7b), before anything is published
3. **functionality** — the e2e workflow runs the full documented user flow on a clean Windows runner whenever a bundle release is published

A secondary moral: incident 3 was initially misdiagnosed (blamed on an HDF5→OpenBLAS→Fortran conflict with R's own DLLs) and the wrong fix was merged before empirical bisection found podofo. Ten minutes of `LoadLibrary` sweeps beat hours of dependency-graph theory. When a DLL question arises, load things and observe.

## act 4: what "working" means today

The system is working when all of these hold:

- `build.yml` produces a bundle that passes closure + loadability gates and publishes it as a `gdal-v*` release asset
- `install_gdal_runtime()` + `install_gdalraster()` + `verify_gdalraster_runtime()` succeed on a clean machine (proven weekly and per-release by `e2e.yml`)
- `gdalraster::gdal_global_reg_names()` returns a non-empty registry, and the embedded-python path (`gdal driver gpkg validate`) can import `osgeo_utils`

The moving parts most likely to break it again, in likelihood order:

1. **MSYS2 package churn** — a dependency's new build fails DllMain or changes behavior (incident 3's class). Caught by the loadability gate.
2. **GDAL cmake auto-detection drift** — a new optional dependency appears in the build env and gets linked (incident 1's class). Caught partially by the banned-DLL guard; watch the "disabled features" cmake summary on version bumps.
3. **GDAL soname bump** (`libgdal-40.dll` for 3.14) — everything discovers the DLL by glob, but rebuilt `gdalraster` is required per bundle.
4. **Rtools GCC major-version jumps** — GCC's C++ ABI is backward-compatible in practice, and the bundle statically embeds its own libstdc++, but a jump is the right time to rerun e2e before trusting local results.

## reading map

| You want | Read |
|---|---|
| Use the package | `vignettes/getting-started.qmd` → `runtime-guide.qmd` (canonical, on pkgdown) |
| Fix a broken session | `vignettes/troubleshooting.qmd`, then `05-troubleshooting.md` |
| Understand the design | this file, then `vignettes/architecture.qmd` |
| Understand Windows DLL/ABI mechanics | [`08-windows-dll-abi-deep-dive.md`](08-windows-dll-abi-deep-dive.md) |
| Change the build/CI | `03-ci-and-release.md`, comments in `tools/build_gdal.sh` and `.github/workflows/build.yml` (the comments are the authoritative rationale) |
| Historical archaeology | `07-*`, `dev/archive/` (session-specific; never treat as current) |
19 changes: 18 additions & 1 deletion dev/docs/05-troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ First actions:

### `LoadLibrary` failure on package load

Most likely:
Two distinct classes — read the error text carefully:

**"The specified module could not be found" (error 126)**

- missing transitive DLLs in runtime `bin`
- profile/path setup points to mixed toolchain DLLs
Expand All @@ -52,6 +54,21 @@ Example:
Rscript -e "gdalraster.windows::activate_gdal_runtime(); library(gdalraster); print(length(gdalraster::gdal_global_reg_names()))"
```

**"A dynamic link library (DLL) initialization routine failed" (error 1114)**

- a dependency DLL is present and resolvable but its `DllMain`/static
initializers fail; closure checks (`ntldd`) cannot catch this
- behavior can depend on which copy of a sub-dependency got loaded first
(e.g. libpodofo's OpenSSL init fails against MSYS2 libcrypto but succeeds
against Rtools45's — July 2026 incident)

First actions:

- sweep-load every bundle DLL individually (`dyn.load` loop or plain
`LoadLibrary`) to find the one that fails to initialize
- see `08-windows-dll-abi-deep-dive.md` sections 3 and 7 for mechanics and
the bisect method

### embedded-python algorithm fails with `ModuleNotFoundError: No module named 'osgeo_utils'`

Applies to GDAL algorithms implemented in Python, e.g. `driver gpkg validate`.
Expand Down
10 changes: 10 additions & 0 deletions dev/docs/06-toolchain-and-abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ Current key flags and their intent:
- `-DGDAL_HIDE_INTERNAL_SYMBOLS=ON`
reduces exported symbol surface and avoids export-table pressure on Windows.

- `-DGDAL_ENABLE_DRIVER_PDF=OFF` (+ `GDAL_USE_POPPLER/PODOFO/PDFIUM=OFF`)
excludes the PDF driver and all its backends. MSYS2's `libpodofo.dll`
fails `DllMain` (error 1114) against the MSYS2 `libcrypto`, which made
the whole `libgdal-*.dll` unloadable in bundles published before July
2026. See `08-windows-dll-abi-deep-dive.md` §3 for the full mechanics.

- `-DGDAL_USE_MSSQL_ODBC=OFF`, `-DGDAL_USE_MSSQL_NCLI=OFF`
prevents linking the proprietary SQL Server ODBC driver present on CI
runner images but absent on end-user machines (issue #13).

- `-Wl,--kill-at`
adjusts MinGW stdcall export decoration behavior for cleaner export names.

Expand Down
Loading