# 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
        RugnuxPipeline.h
        RugnuxStills.cpp
        ModuleDiagnostics.cpp
        ModuleDiagnostics.h
        RugnuxCalibration.cpp
        RugnuxPreScan.cpp
        RugnuxPasses.cpp
        RugnuxImagePass.cpp
        RugnuxFarEnd.cpp
        RugnuxFirstPass.h
        RugnuxFirstPass.cpp
        RugnuxFirstPassRescues.cpp
        RugnuxFirstPassAccept.cpp
        RugnuxScaleMerge.cpp
        RugnuxJointSweeps.cpp
        RugnuxJointSweeps.h
        RugnuxDefaults.cpp
        RugnuxDefaults.h
        RugnuxLive.cpp
        RugnuxLive.h
        StillsLiveMerge.cpp
        StillsLiveMerge.h
        RugnuxRefine.cpp
        RugnuxRefine.h
        Rugnux.h
        RugnuxCommandLine.cpp
        RugnuxCommandLine.h
        ResultReport.cpp
        ResultReport.h
        ReportDocument.h
        DiagnosticOutput.cpp
        DiagnosticOutput.h
        BrokerFeed.cpp
        BrokerFeed.h
)

TARGET_LINK_LIBRARIES(JFJochRugnux JFJochReader JFJochImageAnalysis JFJochPreview gemmi fftw3f
                      httplib::httplib)

# The files of a run, written by the programs that run one (rugnux, the viewer) from what JFJochRugnux
# returns: the library itself writes none.
ADD_LIBRARY(JFJochRugnuxOutput STATIC RugnuxOutputFiles.cpp RugnuxOutputFiles.h RugnuxInputKind.cpp RugnuxInputKind.h
        RugnuxSweeps.cpp RugnuxSweeps.h)
TARGET_LINK_LIBRARIES(JFJochRugnuxOutput JFJochRugnux JFJochWriter JFJochModelWriter ZLIB::ZLIB)

# 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 RugnuxConsole.cpp RugnuxConsole.h)
TARGET_LINK_LIBRARIES(rugnux JFJochRugnuxOutput JFJochRugnux JFJochReader JFJochImageAnalysis JFJochWriter JFJochAPIConvert)
INSTALL(TARGETS rugnux RUNTIME COMPONENT rugnux)
# macOS: sign the installed binary with the Developer ID (hardened runtime, secure timestamp) so the
# .tgz can be notarized - see JFJOCH_MACOS_SIGNING_IDENTITY in the top-level CMakeLists.txt. The
# binary links only system libraries, so no entitlements are needed.
IF(APPLE AND JFJOCH_MACOS_SIGNING_IDENTITY)
    INSTALL(CODE "execute_process(COMMAND codesign --force --options runtime --timestamp
                                          --sign \"${JFJOCH_MACOS_SIGNING_IDENTITY}\"
                                          \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/bin/rugnux\"
                                  COMMAND_ERROR_IS_FATAL ANY)"
            COMPONENT rugnux)
ENDIF()

# 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()
