# SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
# SPDX-License-Identifier: GPL-3.0-only

ADD_LIBRARY(JFJochRugnux STATIC
        Rugnux.cpp
        RugnuxDefaults.cpp
        RugnuxDefaults.h
        Rugnux.h
        RugnuxCommandLine.cpp
        RugnuxCommandLine.h
        ModelFFT.cpp
        ModelFFT.h
        ModelScaling.cpp
        ModelScaling.h
        ModelValidation.cpp
        ModelValidation.h
        RigidBodyRefine.cpp
        RigidBodyRefine.h
        SigmaA.cpp
        SigmaA.h
        ResultReport.cpp
        ResultReport.h
        ReportDocument.h
        LeftoverLattices.cpp
        LeftoverLattices.h
        SpotWidth.cpp
        SpotWidth.h
        HotPixels.cpp
        HotPixels.h
        $<$<BOOL:${JFJOCH_CUDA_AVAILABLE}>:HotPixelsGPU.cu>
        HotPixelsGPU.h
        DiagnosticOutput.cpp
        DiagnosticOutput.h
        SpindleCuspLoss.cpp
        SpindleCuspLoss.h
        WriteModel.cpp
        WriteModel.h
)

TARGET_LINK_LIBRARIES(JFJochRugnux JFJochReader JFJochImageAnalysis JFJochWriter JFJochPreview gemmi fftw3f)

# rugnux is the single offline analysis CLI; --mode picks what it does: mx (the full pipeline, the
# default), azint, scale (re-scale/merge stored reflections) or calibration (powder-ring geometry).
ADD_EXECUTABLE(rugnux rugnux_cli.cpp)
TARGET_LINK_LIBRARIES(rugnux JFJochRugnux JFJochReader JFJochImageAnalysis JFJochWriter)
INSTALL(TARGETS rugnux RUNTIME COMPONENT rugnux)

# libcufft_static.a carries a relocatable-device-code object (separate_callback.o), so an executable
# linking it needs a CUDA device-link step -- without it the host link fails on an undefined
# __cudaRegisterLinkedBinary_* symbol. CUDA 13 no longer ships libcufft_static_nocallback.a, which
# used to be the way around it. CUDA_RESOLVE_DEVICE_SYMBOLS makes CMake emit that step while leaving
# the host link driver alone, so the -march/-flto flags CI passes still apply.
IF (JFJOCH_PORTABLE_ONLY AND JFJOCH_CUDA_AVAILABLE AND TARGET CUDA::cufft_static)
    SET_TARGET_PROPERTIES(rugnux PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
ENDIF()


# On Windows this CLI gets getopt/getopt_long from the vendored wingetopt shim (libc has none).
IF (WIN32)
    TARGET_LINK_LIBRARIES(rugnux wingetopt)
ENDIF()
