# Protected_Core build image (task 14.5; Requirements 5.3, 5.7).
#
# Builds the oathera-core cdylib (the N-API .node addon) from the Rust crate in a
# pure Rust toolchain. napi-build does NOT need Node headers: on Linux the N-API
# symbols are left undefined in the cdylib and resolved by Node at process.dlopen,
# so a Rust-only image suffices (the crate has no C deps beyond libc).
#
# The build architecture (x64 vs arm64) is selected by the caller's --platform
# (linux/amd64 or linux/arm64); the glibc build is always the napi-rs `-gnu`
# target for that arch. build-core.sh renames the extracted addon to the exact
# per-target file name `oathera-core.<target>.node` (the loader's
# coreArtifactFileName), so the artifact stage names it generically.
#
# Build context MUST be the crate directory (native/crate) so the whole
# crate (Cargo.toml, Cargo.lock, build.rs, .cargo/, src/) is present at WORKDIR.
# Symbol visibility needs no extra linker inputs: on Linux rustc's default
# cdylib version script already exports only napi_register_module_v1; on macOS
# build.rs emits the -exported_symbol cdylib link arg.

FROM rust:1-bookworm AS build
WORKDIR /build

# Copy the crate. The context is the crate dir, so this brings Cargo.toml,
# Cargo.lock, build.rs, .cargo/, and src/.
COPY . .

# Release profile (strip, fat LTO, panic=abort, no debug info) comes from
# Cargo.toml; symbol visibility (only napi_register_module_v1) comes from
# rustc's default cdylib version script on Linux (build.rs adds nothing here).
# --locked keeps the build reproducible against Cargo.lock.
RUN cargo build --release --locked

# The cdylib on Linux is target/release/liboathera_core.so. Normalize the name to
# the generic addon file name; build-core.sh renames it per target on extract.
RUN cp target/release/liboathera_core.so /oathera-core.node

# Artifact-only stage: `docker buildx build --output type=local` copies just this.
FROM scratch AS artifact
COPY --from=build /oathera-core.node /oathera-core.node
