diff --git a/.gitattributes b/.gitattributes index 717b707e..b81e799d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,5 @@ *.mcs filter=lfs diff=lfs merge=lfs -text *.mcs.gz filter=lfs diff=lfs merge=lfs -text + +# Large reference datasets for the [large] Catch tests (git-LFS; may not be pulled in CI). +tests/data/*.h5 filter=lfs diff=lfs merge=lfs -text diff --git a/.gitea/workflows/build_and_test.yml b/.gitea/workflows/build_and_test.yml index ac19d87e..fa13a3ce 100644 --- a/.gitea/workflows/build_and_test.yml +++ b/.gitea/workflows/build_and_test.yml @@ -31,6 +31,16 @@ jobs: CTEST_OUTPUT_ON_FAILURE: '1' steps: - uses: actions/checkout@v4 + - name: Configure auth and fetch LFS + shell: bash + env: + GITEA_TOKEN: ${{ secrets.PIP_REPOSITORY_API_TOKEN }} + run: | + git lfs install --local + AUTH=$(git config --local http.${{ github.server_url }}/.extraheader) + git config --local --unset http.${{ github.server_url }}/.extraheader + git config --local http.${{ github.server_url }}/${{ github.repository }}.git/info/lfs/objects/batch.extraheader "$AUTH" + git lfs pull - name: Build tests shell: bash run: | @@ -140,9 +150,13 @@ jobs: echo "No package files found for pattern: ${{ matrix.pkg_glob }}" exit 1 fi + url="${{ matrix.upload_url }}" + if [[ "$url" == *"/rpm/"* ]]; then + url="${url}?sign=true" + fi for file in "${files[@]}"; do - echo "Uploading $file -> ${{ matrix.upload_url }}" - curl --fail --user __token__:"$TOKEN" --upload-file "$file" "${{ matrix.upload_url }}" + echo "Uploading $file -> $url" + curl --fail --user __token__:"$TOKEN" --upload-file "$file" "$url" done cd .. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..f57bc197 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,192 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## What this is + +Jungfraujoch is the data-acquisition and analysis system for the PSI JUNGFRAU and EIGER +X-ray detectors. It receives detector data, runs it through an FPGA-accelerated pipeline +(spot finding, azimuthal/ROI integration, compression), streams images out over ZeroMQ for +writing to HDF5, and runs crystallographic analysis (indexing, integration, scaling/merging). +Most authoritative documentation lives in `docs/` and on Read The Docs +(https://jungfraujoch.readthedocs.io). When changing CLI behaviour, the program's own +usage message is the source of truth, not the docs. + +## Build + +Out-of-source CMake build, C++20, heavy use of `FetchContent` (spdlog, zstd, HDF5, +slsDetectorPackage, Catch2, cpp-httplib, libzmq, Ceres, fast-feedback-indexer are downloaded +and statically linked — the **first configure needs network access** and is slow). + +``` +mkdir build && cd build +cmake -DCMAKE_BUILD_TYPE=Release .. +make -j$(nproc) jfjoch_broker # the main service; build other targets by name +``` + +Key CMake options: +- `JFJOCH_USE_CUDA` (default ON) — GPU path. Needs CUDA ≥ 12.8. Provides the `ffbidx` and `fft` + GPU indexers; without it only the CPU `fftw` indexer is available (requires FFTW at configure + time, auto-detected). CUDA absence is not a build error. +- `JFJOCH_WRITER_ONLY` (default OFF) — builds only the HDF5 writer; skips broker, FPGA, + receiver, analysis, tests, frontend. +- `JFJOCH_VIEWER_BUILD` (default OFF) — builds the Qt6 `jfjoch_viewer` desktop app. +- `SLS9` (default OFF) — build against slsDetectorPackage 9.2.0 instead of 8.0.2. + +The frontend is a separate custom target: `make frontend` (runs `npm ci && npm run build` in +`frontend/`). It is not built by default unless installing. + +## Test + +Tests use **Catch2** and are collected into a single binary `tests/jfjoch_test`. + +``` +make -j$(nproc) jfjoch_test +cd tests +./jfjoch_test # all tests +./jfjoch_test "" # one test case (exact name in TEST_CASE) +./jfjoch_test "[tag]" # by tag +./jfjoch_test -r junit -o report.xml +``` + +`make jfjoch_hdf5_test` builds the HDF5 write-speed benchmark, also used by CI to produce files +that are validated against XDS (Durin/Neggia) and CrystFEL. + +Lint config is `.clang-tidy` (broad `*` check set with many exceptions; namespaces lower_case, +classes CamelCase, global constants UPPER_CASE). + +## Code style + +The overriding principle is **simple, readable code** — favour the smallest, most direct +implementation that a reader can verify at a glance. Extra abstraction, speculative guards, and +clever-but-dense constructs are treated as actively harmful, not as polish. When torn between a +tidy abstraction and a flat, obvious version, pick the obvious one. + +This matters most in `image_analysis/pixel_refinement/` (e.g. `PixelRefine.cpp`), which is an +experimental prototype where readability is how the physics gets verified — keep it especially +plain. **Do not add defensive/unrequested code** (extra validation, rejection heuristics, "just in +case" branches) without asking first; if a guard isn't clearly needed, leave it out. + +Match the surrounding code's idiom, naming, and comment density rather than importing a different +style. + +## Local end-to-end run (no detector / no FPGA) + +The FPGA HLS logic can be simulated on the CPU (`HLSSimulatedDevice`), so the full software +stack runs without hardware (slowly — fixed-point math on CPU). See +`docs/JFJOCH_BROKER.md` for the canonical walkthrough. + +``` +cd build/broker +./jfjoch_broker ../../etc/broker_local.json 5232 # config JSON + HTTP port +# then, separately: +cd tests/test_data && python jfjoch_broker_test.py # feeds a test image, starts collection +# observe at http://localhost:5232 ; HDF5 is written under build/broker +``` + +`etc/broker_local.json`, `broker_eiger.json`, `broker_crmx.json` are example broker configs +(schema = `jfjoch_settings` in `broker/jfjoch_api.yaml`). + +## Architecture + +**Data flow (online):** detector → FPGA acquisition (`fpga/`, `acquisition_device/`) → +`receiver/` builds full images from per-module FPGA output → `image_pusher/` streams CBOR-encoded +images over ZeroMQ → `jfjoch_writer` (`writer/`) consumes the stream and writes NXmx HDF5. The +broker also emits a low-rate preview stream and a metadata stream (`preview/`). + +**Writer file split:** one acquisition produces one `_master.h5` plus many `_data_NNNNNN.h5` +files. Dataset-wide metadata (geometry, detector config, ROI/azimuthal definitions — anything +fixed for the whole run) is written to the **master** file in `writer/HDF5NXmx.cpp` (the `NXmx` +class). Per-image arrays (one entry per frame) are written to the **data** files by the +`HDF5DataFilePlugin` subclasses in `writer/`. Put shared metadata in `NXmx`, not in a data-file +plugin. + +The HDF5 master/data layout is one of three `FileWriterFormat`s (`common/JFJochMessages.h`), +all NXmx: **`NXmxLegacy`** (master + `_data_NNNNNN.h5` joined by external links), +**`NXmxVDS`** (master + data joined by HDF5 virtual datasets — the default), and +**`NXmxIntegrated`** (a single self-contained file, no separate data files). Per-image plugins +must work for all three; with `NXmxIntegrated` "master" and "data" are the same file. + +**Two acquisition workflows:** the FPGA-accelerated path (JUNGFRAU at PSI; FPGA does masking, +summation, spot finding, ROI/azimuthal integration, compression) and the DECTRIS SIMPLON path +(EIGER), which has no FPGA — masking/ROI/azimuthal analysis then runs on CPU through the shared +`image_analysis/` library. Treat ROI and azimuthal features as available in **both** workflows, +not FPGA-only. + +**`jfjoch_broker`** (`broker/`) is the central online service: HTTP/REST + OpenAPI control plane, +FPGA configuration, image building, ZeroMQ output. `JFJochStateMachine` drives acquisition state; +`JFJochServices` wires the pieces; `OpenAPIConvert`/`JFJochBrokerParser` translate between the +generated API model and internal types. + +**Three analysis frontends share one analysis library** (`image_analysis/`, built as +`JFJochImageAnalysis`): +- `jfjoch_broker` — online, real-time (FPGA + GPU). +- `jfjoch_viewer` — interactive Qt desktop (`viewer/`), results not persisted. +- `jfjoch_process` (`tools/jfjoch_process.cpp`) — offline batch over a stored HDF5; writes + `_process.h5` and `.mtz`/`.cif`/`.hkl`. `jfjoch_scale` re-scales/merges already-integrated data. + +**`image_analysis/` pipeline** (subdirs): `spot_finding`, `indexing` (`ffbidx`/`fft` GPU, +`fftw` CPU), `lattice_search`, `geom_refinement`, `pixel_refinement`, `bragg_prediction`, +`bragg_integration`, `rotation_indexer`, `azint`, `roi`, `scale_merge`. Least-squares refinement +uses **Ceres** (fetched, built with miniglog, no MKL, CXX_THREADS). `ffbidx` needs a known cell +(`-C`) and suits sparse serial stills; `fft`/`fftw` index de novo and suit strong rotation data. + +**FPGA** (`fpga/`): `hls/` is the Vitis HLS source (image-analysis kernels), `hls_simulation/` +runs that same HLS on CPU for hardware-free testing, `host_library/` is the host-side driver, +`pcie_driver/` is the kernel module. The HLS algorithms are documented in +`docs/FPGA_DATA_ANALYSIS.md`. + +**Detector control** (`detector_control/`): wrappers for SLS (JUNGFRAU) and DECTRIS SIMPLON +(EIGER). **`jungfrau/`**: JUNGFRAU ADU→energy gain/pedestal calibration. + +**Other libs:** `common/` (geometry, diffraction experiment, image buffer, CUDA wrappers — the +shared core, linked nearly everywhere), `compression/` (zstd + bitshuffle + sqrt lossy), +`frame_serialize/` (CBOR stream codec), `gemmi_gph/` (vendored GEMMI for MTZ/XDS_ASCII I/O), +`xds-plugin/` (XDS HDF5 read plugin). + +## Portability (jfjoch_viewer) + +Cross-platform support is a goal **only for `jfjoch_viewer` and its dependency tree** — keep that +code, and any shared library it transitively links (`common/`, `image_analysis/`, `reader/`, +`gemmi_gph/`, etc.), **MSVC-compatible** so the viewer can build on Windows. The rest of the +project (broker, receiver, FPGA host, detector control, …) is Linux-only and does not need to be +portable; don't constrain it for portability's sake. + +- **Windows/MSVC** is the primary portability target. The end goal is a Windows viewer built + with **MSVC *and* CUDA** (`JFJOCH_USE_CUDA=ON`): GPU processing is a wanted feature, not + optional, so the intended Windows config is the full GPU path (`ffbidx`, GPU `fft`), not a + CPU-only fallback. MSVC is required regardless, because CUDA on Windows requires it. Avoid + GCC/Clang-only extensions, POSIX-only APIs, and other non-MSVC constructs in viewer-reachable + code, and keep CUDA-reachable viewer code (`ffbidx`, GPU indexers) MSVC-buildable too. +- **macOS** is a nice-to-have for the viewer. It rules out CUDA, so anything the viewer depends on + must also have a working CPU-only / non-CUDA path (the `JFJOCH_USE_CUDA=OFF`, `fftw`-indexer + configuration). This non-CUDA path must keep working, but it is the macOS fallback — *not* the + intended Windows configuration. + +A self-contained Windows build still needs a few dependencies that the Linux build picks up from +the system and that are **not** auto-provided via `FetchContent` (besides Qt, supplied +externally): **ZLIB** (pulled by hdf5/libtiff/gemmi/libzmq — a bundled zlib-ng in `ZLIB_COMPAT` +mode is the intended fix, presented as the `ZLIB::ZLIB` target so the scattered +`find_package(ZLIB)` calls resolve), **libjpeg-turbo** (used by `preview/`; upstream discourages +`add_subdirectory`, so bring it in via `ExternalProject`), and **Eigen** (header-only; needed by +Ceres, by the analysis libs directly, and by `ffbidx` under CUDA — fetch it and point Ceres' +internal `find_package(Eigen3)` at it). Wire each guarded so the Linux build, which finds these on +the system, stays unchanged. + +## OpenAPI is the single source of truth + +`broker/jfjoch_api.yaml` defines the entire REST API **and the shared data schemas**. From it, +`update_version.sh` regenerates three clients — **do not hand-edit generated code**: +- C++ server model → `broker/gen/` (cpp-pistache-server generator; compiled as `JFJochAPI`). +- Python client → `python-client/` (and `gen_python_client.sh`, published as PyPI `jfjoch-client`). +- TypeScript frontend client → `frontend/src/client/` (hey-api `openapi-ts`, `npm run openapi`). + +When you change `jfjoch_api.yaml`, regenerate the relevant client(s); for a version bump run +`update_version.sh` (also rewrites `VERSION`, `frontend/src/version.ts`, and the Redoc html). + +## Frontend + +React 19 + TypeScript + MUI + Vite (`frontend/`). Data layer is generated from the OpenAPI spec +(`@hey-api/openapi-ts` → fetch client + TanStack Query hooks + zod schemas). Scripts: +`npm start` (dev server), `npm run build` (tsc + vite), `npm run openapi` (regen client), +`npm run redocly4broker` (regen `broker/redoc-static.html`). diff --git a/CMakeLists.txt b/CMakeLists.txt index 02668e08..7a5c6fcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,18 +8,24 @@ SET(CMAKE_POLICY_DEFAULT_CMP0077 NEW) SET(CMAKE_CXX_STANDARD 20) SET(CMAKE_CXX_STANDARD_REQUIRED True) -SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wno-deprecated-enum-enum-conversion -DNDEBUG") -SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") - SET(JFJOCH_WRITER_ONLY OFF CACHE BOOL "Compile HDF5 writer only") SET(JFJOCH_INSTALL_DRIVER_SOURCE OFF CACHE BOOL "Install kernel driver source (ignored if building writer only; necessary for RPM building)") SET(JFJOCH_USE_CUDA ON CACHE BOOL "Compile Jungfraujoch with CUDA") SET(JFJOCH_VIEWER_BUILD OFF CACHE BOOL "Compile Jungfraujoch viewer") -FIND_PACKAGE(ZLIB REQUIRED) +# Only the viewer (and its portable dependency tree) is supported on Windows and macOS -- the +# broker/receiver/FPGA/writer server stack is Linux-only. Force viewer-only on those platforms so a +# plain configure builds the right subset; on Linux it remains a user-togglable option. +IF (WIN32 OR APPLE) + SET(JFJOCH_VIEWER_ONLY ON CACHE BOOL "Compile only jfjoch_viewer and its dependencies" FORCE) +ELSE() + SET(JFJOCH_VIEWER_ONLY OFF CACHE BOOL "Compile only jfjoch_viewer and its dependencies") +ENDIF() SET (ZLIB_USE_STATIC_LIBS TRUE) +FIND_PACKAGE(ZLIB REQUIRED) + OPTION(SLS9 "Build with sls_detector_package v9.2.0" OFF) SET(BUILD_SHARED_LIBS OFF) @@ -39,10 +45,26 @@ SET(BUILD_FAST_INDEXER_STATIC ON) INCLUDE(CheckLanguage) INCLUDE(CheckIncludeFile) + +# Locate nvcc ourselves when it isn't already pinned (-DCMAKE_CUDA_COMPILER / $CUDACXX). CHECK_LANGUAGE +# below only searches PATH, which is missed routinely on Windows and intermittently on Linux; the +# standard CUDA install locations (CUDA_PATH on Windows, /usr/local/cuda on Linux) cover both, so the +# compiler need not be passed by hand. Only set it when actually found, leaving CHECK_LANGUAGE to run +# its normal detection otherwise. +IF (JFJOCH_USE_CUDA AND NOT CMAKE_CUDA_COMPILER AND NOT DEFINED ENV{CUDACXX}) + FIND_PROGRAM(_jfjoch_nvcc nvcc + HINTS ENV CUDA_PATH ENV CUDA_HOME ENV CUDA_ROOT /usr/local/cuda /opt/cuda + PATH_SUFFIXES bin) + IF (_jfjoch_nvcc) + SET(CMAKE_CUDA_COMPILER "${_jfjoch_nvcc}") + ENDIF() +ENDIF() + CHECK_LANGUAGE(CUDA) SET(CMAKE_CUDA_ARCHITECTURES 75 80 86 89 90 100 120) # T4, A100, RTX A4000, L4 -SET(CMAKE_CUDA_STANDARD 17) +SET(CMAKE_CUDA_STANDARD 20) +SET(CMAKE_CUDA_STANDARD_REQUIRED True) SET(CMAKE_CUDA_FLAGS_RELEASE "-O3 -lineinfo") SET(CMAKE_CUDA_RUNTIME_LIBRARY Static) @@ -56,6 +78,11 @@ IF (CMAKE_CUDA_COMPILER) FIND_PACKAGE(CUDAToolkit REQUIRED) ADD_COMPILE_DEFINITIONS(JFJOCH_USE_CUDA) SET(JFJOCH_CUDA_AVAILABLE ON) + # Blackwell GB10 (DGX Spark) is sm_121, only known to nvcc >= 12.9; add it there so the + # binary launches natively on Spark (the list above tops out at sm_120 and embeds no PTX). + IF (CMAKE_CUDA_COMPILER_VERSION VERSION_GREATER_EQUAL "12.9") + LIST(APPEND CMAKE_CUDA_ARCHITECTURES 121) + ENDIF() ELSE() MESSAGE(WARNING "CUDA older than 12.8 not supported") ENDIF() @@ -64,20 +91,7 @@ IF (CMAKE_CUDA_COMPILER) ENDIF() ENDIF() -FIND_LIBRARY(FFTWF_LIBRARY NAMES libfftw3f.a libfftw3f.so fftw3f DOC "FFTW single-precision library" - PATHS /usr/lib /usr/lib64 /usr/lib/x86_64-linux-gnu/) -CHECK_INCLUDE_FILE(fftw3.h HAS_FFTW3_H) - -IF(HAS_FFTW3_H AND FFTWF_LIBRARY) - ADD_COMPILE_DEFINITIONS(JFJOCH_USE_FFTW) -ENDIF() - INCLUDE_DIRECTORIES(include) -INCLUDE(CheckIncludeFile) - -FIND_LIBRARY(NUMA_LIBRARY NAMES numa DOC "NUMA Library") -CHECK_INCLUDE_FILE(numaif.h HAS_NUMAIF) -CHECK_INCLUDE_FILE(numa.h HAS_NUMA_H) include(FetchContent) @@ -143,43 +157,168 @@ FetchContent_Declare( GIT_TAG v0.39.0 EXCLUDE_FROM_ALL ) +# httplib enables Brotli content-encoding whenever it finds system Brotli; we don't use it +# (gzip/zlib is enough for the broker), so disable it to avoid linking libbrotli. +SET(HTTPLIB_USE_BROTLI_IF_AVAILABLE OFF CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(zstd sls_detector_package catch2 hdf5 spdlog httplib) +# ZeroMQ (libzmq): fetch it here, at the top level, so that THIS project - not +# slsDetectorPackage - controls the version. slsDetectorPackage bundles its own libzmq +# archive, but only populates it behind `if(NOT libzmq_POPULATED)`; by making libzmq +# available before sls below, sls reuses this copy and a single libzmq-static target is +# built (no duplicate target / double-symbol clash). A future viewer-only / Windows build +# (which does not build sls) fetches the same libzmq standalone. +SET(BUILD_SHARED OFF CACHE BOOL "" FORCE) # libzmq: static only (matches sls) +SET(BUILD_TESTS OFF CACHE BOOL "" FORCE) # libzmq: no test build +SET(WITH_PERF_TOOL OFF CACHE BOOL "" FORCE) # libzmq: no perf tools +SET(WITH_DOCS OFF CACHE BOOL "" FORCE) # libzmq: no docs +SET(ENABLE_CPACK OFF CACHE BOOL "" FORCE) # libzmq: no CPack injection +FetchContent_Declare( + libzmq + GIT_REPOSITORY https://github.com/zeromq/libzmq.git + GIT_TAG v4.3.5 + EXCLUDE_FROM_ALL +) -ADD_SUBDIRECTORY(jungfrau) -ADD_SUBDIRECTORY(compression) -ADD_SUBDIRECTORY(common) -ADD_SUBDIRECTORY(writer) -ADD_SUBDIRECTORY(frame_serialize) -ADD_SUBDIRECTORY(reader) -ADD_SUBDIRECTORY(detector_control) -ADD_SUBDIRECTORY(image_puller) -ADD_SUBDIRECTORY(preview) -ADD_SUBDIRECTORY(gemmi_gph) -ADD_SUBDIRECTORY(xds-plugin) +# CMake >= 4.0 (e.g. the 4.x bundled with Visual Studio 2026) makes any +# cmake_minimum_required(VERSION < 3.5) a fatal error. Some fetched dependencies still +# declare such old floors (libzmq: 3.0.2), so raise the policy-version floor for the +# FetchContent subprojects. Ignored (harmless) on CMake < 3.30, which lacks this variable. +SET(CMAKE_POLICY_VERSION_MINIMUM 3.5) -IF (JFJOCH_WRITER_ONLY) - MESSAGE(STATUS "Compiling HDF5 writer only") +# libzmq must be made available BEFORE sls_detector_package for the override above to take effect. +FetchContent_MakeAvailable(libzmq) +IF (JFJOCH_VIEWER_ONLY) + # A viewer-only build still needs zstd/hdf5/spdlog/httplib (JFJochReader uses httplib). + # Only sls_detector_package (detector) and catch2 (tests) are not built here -- and sls + # in particular does not configure under MSVC -- so skip just those two. + FetchContent_MakeAvailable(zstd hdf5 spdlog httplib) ELSE() - ADD_SUBDIRECTORY(image_pusher) - ADD_SUBDIRECTORY(broker) - ADD_SUBDIRECTORY(fpga) - ADD_SUBDIRECTORY(acquisition_device) - ADD_SUBDIRECTORY(receiver) + FetchContent_MakeAvailable(zstd sls_detector_package catch2 hdf5 spdlog httplib) +ENDIF() + +# libtiff (used by JFJochPreview in every build mode): build it ourselves. Its C++ binding +# (tiffxx) is packaged inconsistently across distros (missing on Rocky 9) and absent on +# Windows. Library only; the SET()s below are libtiff's own codec/tool switches. +# We only need DEFLATE (zlib) + the internal LZW codec, matching what Python tifffile writes +# by default. The remaining codecs default to ON whenever their library is found on the build +# host, so each must be explicitly turned OFF to keep the dependency set minimal -- otherwise +# webp pulls in libwebp + libsharpyuv and lerc pulls in libLerc. +SET(jbig OFF) +SET(zstd OFF) +SET(lzma OFF) +SET(jpeg OFF) +SET(old-jpeg OFF) +SET(webp OFF) +SET(lerc OFF) +SET(tiff-tools OFF) +SET(tiff-tests OFF) +FetchContent_Declare(tiff + GIT_REPOSITORY https://gitlab.com/libtiff/libtiff.git + GIT_TAG v4.7.1 + EXCLUDE_FROM_ALL) +FetchContent_MakeAvailable(tiff) + +# FFTW single precision (target fftw3f): the CPU fallback indexer, enables JFJOCH_USE_FFTW. +# Built from the release tarball -- the git repo ships no pre-generated codelets (needs the +# OCaml genfft). +SET(ENABLE_FLOAT ON CACHE BOOL "" FORCE) +SET(BUILD_TESTS OFF CACHE BOOL "" FORCE) +# SIMD codelets are runtime-dispatched (cpuid), so enabling AVX2 does NOT require an AVX2 CPU +# -- FFTW falls back at runtime. The codelet sets are arch-specific, so guard by processor: +# x86 gets SSE2/AVX/AVX2, aarch64 (e.g. Grace on DGX Spark) gets NEON; other arches build scalar. +IF (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64") + SET(ENABLE_SSE2 ON CACHE BOOL "" FORCE) + SET(ENABLE_AVX ON CACHE BOOL "" FORCE) + SET(ENABLE_AVX2 ON CACHE BOOL "" FORCE) +ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|ARM64") + SET(ENABLE_NEON ON CACHE BOOL "" FORCE) +ENDIF() +FetchContent_Declare(fftw + URL https://www.fftw.org/fftw-3.3.10.tar.gz + URL_HASH SHA256=56c932549852cddcfafdab3820b0200c7742675be92179e59e6215b340e26467 + EXCLUDE_FROM_ALL) +FetchContent_MakeAvailable(fftw) +# FFTW exposes fftw3.h only via $, so consuming fftw3f straight from +# the build tree (FetchContent) leaves the header unreachable. On Linux a system-installed +# fftw3.h masks this; on Windows it does not. Add the source-tree api/ dir for build-tree use. +TARGET_INCLUDE_DIRECTORIES(fftw3f INTERFACE $) +ADD_COMPILE_DEFINITIONS(JFJOCH_USE_FFTW) + +# Eigen is an EXTERNAL dependency (like ZLIB), resolved by find_package(Eigen3) in the subdirectories +# that need it (image_analysis, and transitively Ceres and ffbidx). It is deliberately NOT vendored +# via FetchContent: declaring Eigen with OVERRIDE_FIND_PACKAGE makes the CMake bundled with Visual +# Studio (cmake 4.x "-msvc") intermittently SEGFAULT during configure. The fault is in CMake's own +# FetchContent variable-stack cleanup, reached when Ceres' find_package(Eigen3) resolves the override +# through a nested FetchContent_MakeAvailable -- ~1 in 3 fresh configures, and 100% with Ceres CUDA on. +# Stock Kitware CMake runs the identical scripts fine, so it is a bug in the VS-bundled cmake binary; +# providing Eigen externally avoids that code path entirely and is stable (verified, both Ceres CUDA +# on and off). Provide it via the system package on Linux (eigen3-devel / libeigen3-dev) or a build +# prefix on Windows (point CMAKE_PREFIX_PATH / Eigen3_DIR at it), exactly as for ZLIB. + +# getopt/getopt_long shim for Windows: the MSVC CRT has no . Vendored OpenBSD/NetBSD +# implementation (BSD-licensed). Built only on Windows and defined here, before the subdirectories, +# so the portable CLI tools in tools/ can link it. A no-op on Linux/macOS, where getopt lives in +# libc (macOS uses the system getopt even though it is also a forced viewer-only platform). +IF (WIN32) + ADD_LIBRARY(wingetopt STATIC tools/wingetopt/getopt.c tools/wingetopt/getopt.h) + TARGET_INCLUDE_DIRECTORIES(wingetopt PUBLIC tools/wingetopt) +ENDIF() + +IF (JFJOCH_VIEWER_ONLY) + # Minimal subtree: jfjoch_viewer and only the libraries it transitively links. + # (broker here provides JFJochAPI only; its service targets are gated out.) + ADD_SUBDIRECTORY(jungfrau) + ADD_SUBDIRECTORY(compression) + ADD_SUBDIRECTORY(common) + ADD_SUBDIRECTORY(gemmi_gph) + ADD_SUBDIRECTORY(frame_serialize) + ADD_SUBDIRECTORY(preview) + ADD_SUBDIRECTORY(writer) ADD_SUBDIRECTORY(image_analysis) - ADD_SUBDIRECTORY(tests) - ADD_SUBDIRECTORY(tools) -ENDIF() - -IF (JFJOCH_VIEWER_BUILD) + ADD_SUBDIRECTORY(broker) + ADD_SUBDIRECTORY(reader) + ADD_SUBDIRECTORY(process) ADD_SUBDIRECTORY(viewer) + ADD_SUBDIRECTORY(tools) # builds only the portable analysis tools (process/scale/azint/extract_hkl) +ELSE() + ADD_SUBDIRECTORY(jungfrau) + ADD_SUBDIRECTORY(compression) + ADD_SUBDIRECTORY(common) + ADD_SUBDIRECTORY(writer) + ADD_SUBDIRECTORY(frame_serialize) + ADD_SUBDIRECTORY(reader) + ADD_SUBDIRECTORY(detector_control) + ADD_SUBDIRECTORY(image_puller) + ADD_SUBDIRECTORY(preview) + ADD_SUBDIRECTORY(gemmi_gph) + ADD_SUBDIRECTORY(xds-plugin) + + IF (JFJOCH_WRITER_ONLY) + MESSAGE(STATUS "Compiling HDF5 writer only") + ELSE() + ADD_SUBDIRECTORY(image_pusher) + ADD_SUBDIRECTORY(broker) + ADD_SUBDIRECTORY(fpga) + ADD_SUBDIRECTORY(acquisition_device) + ADD_SUBDIRECTORY(receiver) + ADD_SUBDIRECTORY(image_analysis) + ADD_SUBDIRECTORY(process) + ADD_SUBDIRECTORY(tests) + ADD_SUBDIRECTORY(tools) + ENDIF() + + IF (JFJOCH_VIEWER_BUILD) + ADD_SUBDIRECTORY(viewer) + ENDIF() ENDIF() -IF (NOT JFJOCH_WRITER_ONLY) +IF (NOT JFJOCH_WRITER_ONLY AND NOT JFJOCH_VIEWER_ONLY) ADD_CUSTOM_COMMAND(OUTPUT frontend/dist/index.html COMMAND npm ci COMMAND npm run build + COMMAND npm run licenses # write dist/THIRD_PARTY_LICENSES.txt (npm deps attribution) COMMAND npm run redocly + COMMAND npm run docs # bundle Sphinx docs into dist/docs (served at /frontend/docs) WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/frontend) ADD_CUSTOM_TARGET(frontend DEPENDS frontend/dist/index.html) @@ -207,20 +346,19 @@ IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) # Set Package Name -# Initialize CPACK_COMPONENTS_ALL with common components -set(CPACK_COMPONENTS_ALL jfjoch writer) - SET(CPACK_PACKAGE_NAME "jfjoch") -# Add optional components -if (JFJOCH_INSTALL_DRIVER_SOURCE) - list(APPEND CPACK_COMPONENTS_ALL driver-dkms) +# Select the components to package based on build mode +if (JFJOCH_VIEWER_ONLY) + set(CPACK_COMPONENTS_ALL viewer) else() set(CPACK_COMPONENTS_ALL jfjoch writer) -endif() - -if (JFJOCH_VIEWER_BUILD) - list(APPEND CPACK_COMPONENTS_ALL viewer) + if (JFJOCH_INSTALL_DRIVER_SOURCE) + list(APPEND CPACK_COMPONENTS_ALL driver-dkms) + endif() + if (JFJOCH_VIEWER_BUILD) + list(APPEND CPACK_COMPONENTS_ALL viewer) + endif() endif() # Common metadata @@ -228,8 +366,47 @@ set(CPACK_PACKAGE_CONTACT "Filip Leonarski ") set(CPACK_PACKAGE_VENDOR "Paul Scherrer Institut") set(CPACK_PACKAGE_VERSION ${JFJOCH_VERSION}) -# OS-aware packaging: DEB on Debian/Ubuntu, RPM on RHEL/Rocky -if (EXISTS "/etc/debian_version") +# OS-aware packaging: DragNDrop (.dmg) on macOS, NSIS installer on Windows, DEB on Debian/Ubuntu, +# RPM on RHEL/Rocky. macOS/Windows are checked first because the /etc/* probes below are Linux-only. +if (APPLE) + # .dmg containing jfjoch_viewer.app (Qt runtime already deployed into the bundle). + set(CPACK_GENERATOR "DragNDrop") +elseif (WIN32) + # NSIS installer .exe (Qt runtime deployed next to the binary by windeployqt). + set(CPACK_GENERATOR "NSIS") + + # GPLv3 text shown as the click-through license page of the installer. + set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE") + + # Branding is split across three CPack knobs so the CUDA/CPU variant surfaces exactly where we + # want it and nowhere else: + # - Install folder + Start Menu group come from CPACK_PACKAGE_INSTALL_DIRECTORY and + # CPACK_NSIS_PACKAGE_NAME (the latter feeds $(^Name), the Start Menu group's default folder). + # Both stay plain "Jungfraujoch", so the Start Menu group carries no variant tag and the two + # builds install to the same place (CUDA is a strict superset -- they replace, not coexist). + # - CPACK_NSIS_DISPLAY_NAME is the Add/Remove Programs entry -- tagged "(CUDA)"/"(CPU)". + # - CPACK_PACKAGE_FILE_NAME is the installer .exe filename -- tagged "-cuda"/"-cpu", so + # the (much larger) CUDA download is self-identifying, with the CUDA major version baked in. + # The tag follows JFJOCH_CUDA_AVAILABLE automatically; CUDAToolkit_VERSION_MAJOR is set whenever + # it is ON (find_package(CUDAToolkit) ran in the same guard above). + set(CPACK_PACKAGE_INSTALL_DIRECTORY "Jungfraujoch") + set(CPACK_NSIS_PACKAGE_NAME "Jungfraujoch") + if (JFJOCH_CUDA_AVAILABLE) + set(CPACK_NSIS_DISPLAY_NAME "Jungfraujoch (CUDA)") + set(CPACK_PACKAGE_FILE_NAME "jfjoch-${JFJOCH_VERSION}-win64-cuda${CUDAToolkit_VERSION_MAJOR}") + else() + set(CPACK_NSIS_DISPLAY_NAME "Jungfraujoch (CPU)") + set(CPACK_PACKAGE_FILE_NAME "jfjoch-${JFJOCH_VERSION}-win64-cpu") + endif() + + # Start Menu shortcut for the viewer (";