
# Offline analysis CLI tools. These link only the portable libraries (reader,
# image_analysis, writer, common), so they build in viewer-only / Windows packages too
# and install into the "viewer" component.
# jfjoch_extract_hkl and jfjoch_recompress are developer utilities: still built, deliberately not
# installed into any package. They used to ride inside the viewer tarball, which is part of what
# made that artifact confusing.
ADD_EXECUTABLE(jfjoch_extract_hkl jfjoch_extract_hkl.cpp
        XdsIntegrateParser.cpp
        XdsIntegrateParser.h)
TARGET_LINK_LIBRARIES(jfjoch_extract_hkl JFJochReader)

# rugnux, the single offline analysis CLI, lives in rugnux/ next to its library.

# Developer utility, not installed: refines a model against a finished run's merged MTZ (Refinement.h), for
# measuring the refinement on battery outputs without processing anything again.
ADD_EXECUTABLE(rugnux_refine_test rugnux_refine_test.cpp)
TARGET_LINK_LIBRARIES(rugnux_refine_test JFJochStructureRefinement JFJochCommon gemmi)
# Same device-link step as rugnux (see rugnux/CMakeLists.txt): a portable CUDA build links cuFFT statically.
IF (JFJOCH_PORTABLE_ONLY AND JFJOCH_CUDA_AVAILABLE AND TARGET CUDA::cufft_static)
    SET_TARGET_PROPERTIES(rugnux_refine_test PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
ENDIF()

# In-place re-compress /entry/data/data of a _data file from bitshuffle/LZ4 to bitshuffle/zstd.
ADD_EXECUTABLE(jfjoch_recompress jfjoch_recompress.cpp)
TARGET_LINK_LIBRARIES(jfjoch_recompress JFJochHDF5Wrappers)

# On Windows these CLIs get getopt/getopt_long from the vendored wingetopt shim (libc has none).
IF (WIN32)
    TARGET_LINK_LIBRARIES(jfjoch_extract_hkl wingetopt)
ENDIF()

# Online / hardware tools (broker, FPGA, receiver, detector). They link the non-portable
# libraries and are Linux-only, so they are skipped in a viewer-only build.
IF(NOT JFJOCH_PORTABLE_ONLY)
    ADD_EXECUTABLE(jfjoch_udp_simulator jfjoch_udp_simulator.cpp UDPSimulator.cpp UDPSimulator.h)
    TARGET_LINK_LIBRARIES(jfjoch_udp_simulator JFJochCommon)
    INSTALL(TARGETS jfjoch_udp_simulator RUNTIME COMPONENT jfjoch)

    ADD_EXECUTABLE(jfjoch_hdf5_test jfjoch_hdf5_test.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_hdf5_test JFJochWriter JFJochReceiver JFJochCommon)
    INSTALL(TARGETS jfjoch_hdf5_test RUNTIME COMPONENT jfjoch)

    # Not installed: replays a stored dataset as a DECTRIS detector's image stream, to run the broker
    # without a detector (demonstrations and tests).
    ADD_EXECUTABLE(jfjoch_stream2_replay jfjoch_stream2_replay.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_stream2_replay JFJochReader JFJochZMQ CBORStream2FrameSerialize JFJochCommon)

    # Not installed: replays a stored serial dataset through the GPU analysis of converted JUNGFRAU frames.
    ADD_EXECUTABLE(jfjoch_stills_gpu_replay jfjoch_stills_gpu_replay.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_stills_gpu_replay JFJochReceiver JFJochWriter JFJochReader)

    ADD_EXECUTABLE(jfjoch_pcie_status jfjoch_pcie_status.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_pcie_status JFJochDevice JFJochCommon)
    INSTALL(TARGETS jfjoch_pcie_status RUNTIME COMPONENT jfjoch)

    ADD_EXECUTABLE(jfjoch_pcie_net_cfg jfjoch_pcie_net_cfg.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_pcie_net_cfg JFJochDevice JFJochCommon)
    INSTALL(TARGETS jfjoch_pcie_net_cfg RUNTIME COMPONENT jfjoch)

    ADD_EXECUTABLE(jfjoch_pcie_clear_net_counters jfjoch_pcie_clear_net_counters.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_pcie_clear_net_counters JFJochDevice JFJochCommon)
    INSTALL(TARGETS jfjoch_pcie_clear_net_counters RUNTIME COMPONENT jfjoch)

    ADD_EXECUTABLE(jfjoch_fpga_test jfjoch_fpga_test.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_fpga_test JFJochReceiver)
    INSTALL(TARGETS jfjoch_fpga_test RUNTIME COMPONENT jfjoch)

    ADD_EXECUTABLE(jfjoch_simplon_test jfjoch_simplon_test.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_simplon_test JFJochDetector JFJochPreview)
    INSTALL(TARGETS jfjoch_simplon_test RUNTIME COMPONENT jfjoch)

    ADD_EXECUTABLE(jfjoch_lite_perf_test jfjoch_lite_perf_test.cpp)
    TARGET_LINK_LIBRARIES(jfjoch_lite_perf_test JFJochReceiver JFJochWriter)
    INSTALL(TARGETS jfjoch_lite_perf_test RUNTIME COMPONENT jfjoch)

    ADD_SUBDIRECTORY(xbflash.qspi)
ENDIF()
