Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ gdalraster::gdal_global_reg_names()
- a top-level GDAL runtime DLL (`libgdal-*.dll`)
- all required non-Windows dependent DLLs
- required runtime data directories (`share/gdal`, `share/proj`)
- pure-python GDAL utilities (`python/osgeo_utils`) for embedded-python
algorithms (e.g. `gdal driver gpkg validate`)
4. Build `gdalraster` from source against that bundled GDAL runtime.
5. Ensure runtime loading is configured so `gdalraster` resolves bundled DLL
dependencies at runtime (without relying on a matching user-installed
Rtools environment).
6. Verify success with `gdalraster::gdal_global_reg_names()` returning non-empty output.
Rtools environment), and so GDAL's embedded python can import bundled
`osgeo_utils` (session-scoped `PYTHONPATH`).
6. Verify success with `gdalraster::gdal_global_reg_names()` returning non-empty output and the `driver gpkg validate` algorithm running end-to-end.
7. Keep installs non-destructive by default:
- runtime installs under package-managed user data paths
- source builds target an isolated library path unless explicitly overridden
Expand Down
19 changes: 18 additions & 1 deletion R/gdal_runtime.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,31 @@ configure_gdal_home <- function(path, mode = c("option", "env")) {
#'
#' The selected zip must contain a GDAL root with `bin/libgdal-*.dll`.
#'
#' @section Offline / air-gapped installation:
#' On machines without network access, download the release asset manually
#' from <https://github.com/jimbrig/gdalraster.windows/releases>, transfer it
#' to the target machine, and install directly:
#'
#' ```r
#' gdalraster.windows::install_gdal_runtime(
#' local_zip = "C:/Downloads/gdal-ucrt64-v3.13.0-windows-x64.zip"
#' )
#' ```
#'
#' Note that no fallback zip is shipped with the package (a full runtime
#' bundle is too large to vendor), so the fallback path only applies when you
#' provide a `fallback_zip` yourself.
#'
#' @param repo GitHub repo slug, e.g. `"jimbrig/gdalraster.windows"`.
#' @param tag Release tag or `"latest"`.
#' @param asset_pattern Regex used to select the release asset.
#' @param gdal_home Destination GDAL home directory.
#' @param overwrite Whether to replace existing `gdal_home`.
#' @param local_zip Optional local GDAL runtime zip to install directly.
#' @param fallback_zip Optional fallback zip path used when release download
#' fails. Defaults to package file `inst/extdata/gdal-ucrt64-fallback.zip`.
#' fails. When `NULL` (default), a vendored zip at
#' `inst/extdata/gdal-ucrt64-fallback.zip` is used if present; none ships
#' with the package, so by default no fallback is attempted.
#'
#' @return Invisibly returns installed GDAL home path.
#' @export
Expand Down
58 changes: 56 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ library(gdalraster.windows)
gdalraster::gdal_global_reg_names()
```

## Offline / Air-Gapped Installation

`install_gdal_runtime()` downloads the runtime bundle from GitHub Releases by
default. On machines without network access (or to pin an exact asset), pass a
local zip directly:

```r
# 1) download the release asset on a connected machine:
# https://github.com/jimbrig/gdalraster.windows/releases
# 2) transfer to the target machine
# 3) install from the local zip
gdalraster.windows::install_gdal_runtime(
local_zip = "C:/Downloads/gdal-ucrt64-v3.13.0-windows-x64.zip"
)
```

A `fallback_zip` argument is also supported: when a release download fails and
a fallback zip exists, it is installed instead.

## Common Flows

If runtime and custom `gdalraster` install are already present:
Expand Down Expand Up @@ -170,6 +189,7 @@ needs to resolve `libgdal-*.dll` and its dependency tree in the active process.

- prepending bundle `bin/` to `PATH`
- setting GDAL/PROJ data env vars
- prepending bundle `python/` to `PYTHONPATH` (see section 9)
- optionally preloading `libgdal-*.dll` with `dyn.load(..., local = FALSE, now = TRUE)`

This is why explicit runtime activation exists in addition to source builds.
Expand All @@ -188,7 +208,41 @@ succeeds.

from the installed bundle when available.

### 9) Compile-time paths vs runtime paths
### 9) Embedded Python utilities (`osgeo_utils`)

Some GDAL CLI algorithms are implemented in Python rather than C++. For
example, `gdal driver gpkg validate` is a thin C++ entry point in `libgdal`
that embeds a CPython interpreter at runtime: GDAL locates a `python.exe` on
`PATH`, dynamically loads the matching `libpython` DLL, calls
`Py_Initialize()`, and imports `osgeo_utils.samples.validate_gpkg`.

`osgeo_utils` is the pure-Python package shipped by GDAL's `gdal-utils`
distribution (`swig/python/gdal-utils/` in the GDAL source tree). Because it
contains no compiled extension modules, it has no CPython ABI coupling — any
embedded interpreter version can import it.

The runtime bundle ships this package under `python/osgeo_utils`,
version-locked to the built GDAL tag. `activate_gdal_runtime()` prepends
`<gdal_home>/python` to `PYTHONPATH` (session-scoped; never persisted to user
or machine environment) so the embedded interpreter can resolve it.
`PYTHONPATH` is read at `Py_Initialize()`, which `libgdal` triggers lazily on
first use of an embedded-python algorithm, so activation-time configuration is
early enough.

Without this, such algorithms fail with:

```text
GDAL FAILURE 1: ... ModuleNotFoundError: No module named 'osgeo_utils'
```

Note: the compiled `osgeo` SWIG bindings (`from osgeo import gdal`) are
intentionally **not** built or bundled (`BUILD_PYTHON_BINDINGS` stays off —
no Python/SWIG in the build environment). They would pin the bundle to a
single CPython version/ABI. The Python-implemented validators degrade
gracefully without them (e.g. `validate_gpkg` skips only tiled gridded
coverage checks).

### 10) Compile-time paths vs runtime paths

These are separate concerns:

Expand All @@ -198,7 +252,7 @@ These are separate concerns:
This package scopes compile-time settings to install calls (`withr`), then
manages runtime activation separately for session reliability.

### 10) Optional `.Rprofile` startup hook
### 11) Optional `.Rprofile` startup hook

For users who want persistence, `add_gdal_rprofile_hook()` writes a managed
startup block that can load runtime context early in each session.
Expand Down
4 changes: 2 additions & 2 deletions dev/docs/01-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ This project closes that gap with a custom GDAL build and distribution pipeline,

## high-level flow

1. CI compiles GDAL and assembles `gdal-bundle/` (`bin`, `include`, `lib`, `share`).
2. CI verifies `gdalraster` source builds against that bundle and validates Algorithm API availability.
1. CI compiles GDAL and assembles `gdal-bundle/` (`bin`, `include`, `lib`, `share`, `python`).
2. CI verifies `gdalraster` source builds against that bundle, validates Algorithm API availability, and runs the embedded-python `driver gpkg validate` algorithm end-to-end.
3. End users install runtime locally, source-build `gdalraster`, then activate/load through helper APIs.

## upstream status snapshot
Expand Down
17 changes: 13 additions & 4 deletions dev/docs/03-ci-and-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@ Triggers:

- configures MSYS2 `UCRT64`
- restores/saves GDAL build cache keyed to version and script hash
- runs [`tools/build_gdal.sh`](../../tools/build_gdal.sh)
- runs [`tools/collect_dlls.sh`](../../tools/collect_dlls.sh)
- runs [`tools/build_gdal.sh`](../../tools/build_gdal.sh) (also stages
pure-python `osgeo_utils` from the GDAL source tree into the install prefix)
- runs [`tools/collect_dlls.sh`](../../tools/collect_dlls.sh) (carries
`python/` into the bundle alongside `bin`, `include`, `lib`, `share`)
- verifies runtime bundle integrity
- uploads intermediate artifact for downstream job
- on tag builds, publishes runtime bundle zip to GitHub release

note: release publication happens inside job 1, so a job 2 failure can leave a
published release asset alongside a red workflow run.

### job 2: `verify-gdalraster-build`

- installs R and Rtools45
- downloads runtime bundle artifact from job 1
- writes `Makevars.win` to point source compilation at bundled GDAL headers/libs
- builds `gdalraster` Windows binary from source
- runs smoke test for GDAL load and algorithm registry presence
- runs embedded-python smoke test: creates a GeoPackage, sets `PYTHONPATH` to
the bundle `python/` dir, runs `driver gpkg validate`, asserts success
- uploads verification artifact

## release artifacts to expect
Expand All @@ -44,8 +51,10 @@ Triggers:

## local rehearsal tools

- [`tools/audit_gdal_bundle.ps1`](../../tools/audit_gdal_bundle.ps1): inspect and repair local bundle dependency state
- outputs `.audit` reports under selected bundle path
- `ntldd -R <gdal_home>/bin/libgdal-*.dll` (from an MSYS2/Rtools shell):
inspect the transitive DLL dependency tree of a local bundle
- a dedicated bundle audit script (`tools/audit_gdal_bundle.ps1`) is planned
but not yet present in the repository

## common CI maintenance tasks

Expand Down
4 changes: 4 additions & 0 deletions dev/docs/04-r-runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ Configures runtime home for current session via:
- ensures runtime paths/files exist
- prepends runtime `bin` directory to `PATH`
- sets `GDAL_DATA`, `PROJ_LIB`, and `PROJ_DATA` when available
- prepends runtime `python` directory to `PYTHONPATH` when available
(exposes bundled pure-python `osgeo_utils` to GDAL's embedded python)
- optionally preloads `libgdal-*.dll` discovered in runtime `bin/`
- invisibly returns a list of configured paths
(`gdal_home`, `gdal_bin`, `gdal_dll`, `gdal_data`, `proj_data`, `gdal_python`)

### `load_gdal_dll(gdal_home = default_gdal_home(), quiet = FALSE)`

Expand Down
25 changes: 23 additions & 2 deletions dev/docs/05-troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
- `gdalraster.windows::activate_gdal_runtime()`
3. verify behavior:
- `gdalraster.windows::verify_gdalraster_runtime()`
4. if still failing, audit bundle:
- [`tools/audit_gdal_bundle.ps1`](../../tools/audit_gdal_bundle.ps1)
4. if still failing, audit the bundle dependency tree:
- `ntldd -R <gdal_home>/bin/libgdal-*.dll` from an MSYS2/Rtools shell

Quick checks in R:

Expand Down Expand Up @@ -52,6 +52,27 @@ Example:
Rscript -e "gdalraster.windows::activate_gdal_runtime(); library(gdalraster); print(length(gdalraster::gdal_global_reg_names()))"
```

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

Applies to GDAL algorithms implemented in Python, e.g. `driver gpkg validate`.

Most likely:

- runtime bundle predates `python/osgeo_utils` support
- `PYTHONPATH` was not set in this session (activation did not run)

First actions:

- check `dir.exists(file.path(gdalraster.windows::gdal_home(), "python", "osgeo_utils"))`;
if missing, reinstall: `gdalraster.windows::install_gdal_runtime(overwrite = TRUE)`
- run `gdalraster.windows::activate_gdal_runtime()` and confirm
`Sys.getenv("PYTHONPATH")` contains `<gdal_home>/python`
- confirm a `python.exe` is discoverable on `PATH` (GDAL needs one to embed an
interpreter; the GDAL debug stream shows which python/libpython it loads)

Note: `validate_gpkg` treats the compiled `osgeo` bindings as optional; without
them it still runs all checks except tiled gridded coverage content checks.

### install/test-load fails but manual session works

Most likely:
Expand Down
32 changes: 31 additions & 1 deletion dev/docs/06-toolchain-and-abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,42 @@ transitive non-Windows dependencies are available.

`tools/collect_dlls.sh` does this by:

1. collecting primary GDAL install outputs (`bin`, `include`, `lib`, `share`)
1. collecting primary GDAL install outputs (`bin`, `include`, `lib`, `share`, `python`)
2. recursively inspecting dependencies with `ntldd -R`
3. copying required `/ucrt64` DLLs into bundle `bin/`
4. failing if unresolved non-Windows dependencies remain

This closure check is one of the main safety guarantees in this repository.

## embedded python layer (osgeo_utils)

Some GDAL CLI algorithms (e.g. `gdal driver gpkg validate`) are thin C++
entry points around Python implementations. The mechanism in `libgdal`:

1. locate a `python.exe` on `PATH` and the corresponding `libpython` DLL
2. load it dynamically (`LoadLibrary`, not an import-table dependency —
`libgdal` has no static link against CPython)
3. call `Py_Initialize()` lazily on first use
4. import the needed module, e.g. `osgeo_utils.samples.validate_gpkg`

Implications for this repository:

- `osgeo_utils` (GDAL's `gdal-utils` distribution, pure python) must be on the
embedded interpreter's `sys.path`. `build_gdal.sh` stages it from the GDAL
source checkout (version-locked) into `<prefix>/python`, and
`activate_gdal_runtime()` prepends `<gdal_home>/python` to `PYTHONPATH`.
- because the package is pure python (no `.pyd` extension modules), it has no
CPython version/ABI coupling — any embedded interpreter can import it.
- the compiled SWIG bindings (`osgeo` with `_gdal.pyd` etc.) are deliberately
not built (`BUILD_PYTHON_BINDINGS` is effectively off: no python/swig in the
CI build environment). Shipping them would pin the bundle to one CPython
ABI and require the bundled `osgeo` to resolve this repo's `libgdal-*.dll`
at import time. Python-implemented validators degrade gracefully without
them (`has_gdal = False` path in `validate_gpkg`).
- `PYTHONPATH` handling stays session-scoped: it leaks into no other python
environments (pixi/conda/system), and within an R session it only adds a
directory containing `osgeo_utils`.

## compile-time vs runtime paths

### compile-time
Expand All @@ -122,6 +151,7 @@ dependencies. `activate_gdal_runtime()` handles this by:

- prepending `<gdal_home>/bin` to `PATH`
- setting `GDAL_DATA`, `PROJ_LIB`, `PROJ_DATA`
- prepending `<gdal_home>/python` to `PYTHONPATH` (embedded-python algorithms)
- optionally preloading the GDAL DLL via `dyn.load()`

Compile-time success does not guarantee runtime success without this step.
Expand Down
20 changes: 19 additions & 1 deletion man/install_gdal_runtime.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vignettes/runtime-guide.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,44 @@ gdalraster.windows::load_gdalraster()
gdalraster::gdal_global_reg_names()
```

## Offline / air-gapped install

Download the release asset from
<https://github.com/jimbrig/gdalraster.windows/releases> on a connected
machine, transfer it, then:

```{r eval = FALSE}
gdalraster.windows::install_gdal_runtime(
local_zip = "C:/Downloads/gdal-ucrt64-v3.13.0-windows-x64.zip"
)
```

## Embedded-python algorithms

Some GDAL algorithms (e.g. `gdal driver gpkg validate`) are implemented in
Python: `libgdal` embeds a CPython interpreter at runtime and imports the
pure-python `osgeo_utils` package. The runtime bundle ships that package under
`<gdal_home>/python`, and `activate_gdal_runtime()` exposes it to the embedded
interpreter by prepending it to `PYTHONPATH` (session-scoped).

```{r eval = FALSE}
library(gdalraster.windows)
library(gdalraster)

alg <- gdalraster::gdal_alg(cmd = "driver gpkg validate")
alg$setArg("dataset", "path/to/file.gpkg")
alg$setArg("full-check", TRUE)
alg$run()
alg$output()
#> [1] "Checking gpkg_spatial_ref_sys\n...\nValidation succeeded"
```

A `python.exe` must be discoverable on `PATH` for GDAL to embed an
interpreter (Rtools' UCRT64 python works, as does any system python).
If such an algorithm fails with `ModuleNotFoundError: No module named
'osgeo_utils'`, the runtime bundle predates python support — reinstall with
`install_gdal_runtime(overwrite = TRUE)` and re-activate.

## Default install behavior

By default:
Expand Down
Loading