# Quick cross-platform checks: the results that depend on the architecture, the compiler or the
# standard library (bitshuffle/LZ4 SIMD paths, HDF5 read-back, CBF parsing, CPU spot finding, FFTW
# indexing, gemmi MTZ/CIF). Built in every configuration, the portable (viewer/rugnux-only) ones
# included, and linked only against what those build - so it runs on the macOS and Windows jobs.
# Only the cases tagged [portable] are meant to run there (a few seconds in all); the rest of these
# files is compiled in because the tagged cases live next to them.
# EXCLUDE_FROM_ALL: a product build does not pay for it, CI builds the target by name.
ADD_EXECUTABLE(jfjoch_portable_test EXCLUDE_FROM_ALL
        PortableTest.cpp
        TestData.h
        ZSTDCompressorTest.cpp
        JFJochReaderTest.cpp
        CrystFELGeometryTest.cpp
        XFELReaderTest.cpp
        SaturationCeilingTest.cpp
        DetectorModulesTest.cpp
        PanelLayoutTest.cpp
        CellPopulationTest.cpp
        MultiReaderTest.cpp
        ModuleRefinementTest.cpp
        SweepLayoutTest.cpp
        BrukerReaderTest.cpp
        CBORTest.cpp
        ImageSpotFinderCPUTest.cpp
        AdaptiveSpotFinderCPUTest.cpp
        AzimuthalIntegrationTest.cpp
        CalcBraggPredictionTest.cpp
        BraggStencilTest.cpp
        SpotFootprintTest.cpp
        BraggIntegrationEngineCompressedImageTest.cpp
        BraggIntegrationEngineCPUTest.cpp
        BraggIntegrationEngineMetalTest.cpp
        MolecularReplacementMetalTest.cpp
        ModelDensityMetalTest.cpp
        WriteReflectionsTest.cpp
        LoadReferenceMtzFreeFlagsTest.cpp
        LoadReferenceSfCifTest.cpp
        RotationIndexerTest.cpp
        FFBIDXIndexerCPUTest.cpp
)
target_link_libraries(jfjoch_portable_test Catch2WithMain JFJochRugnux JFJochReader JFJochImageAnalysis
                      JFJochWriter JFJochAPIConvert JFJochCommon)
target_include_directories(jfjoch_portable_test PRIVATE .)
# 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(jfjoch_portable_test PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
ENDIF()

# The CPU vs GPU test of the fast-feedback indexer reads the library's own test spot lists
IF (JFJOCH_CUDA_AVAILABLE)
    SET(FFBIDX_SIMPLE_DATA_DIR "${CMAKE_SOURCE_DIR}/image_analysis/indexing/fast-feedback-indexer/data")
    TARGET_COMPILE_DEFINITIONS(jfjoch_portable_test PRIVATE FFBIDX_SIMPLE_DATA_DIR="${FFBIDX_SIMPLE_DATA_DIR}")
ENDIF()

IF (JFJOCH_PORTABLE_ONLY)
    RETURN()
ENDIF()

ADD_EXECUTABLE(jfjoch_test
        PortableTest.cpp
        DiffractionExperimentTest.cpp
        RawToConvertedGeometryTest.cpp
        ../common/RawToConvertedGeometry.h
        ../common/DiffractionExperiment.h
        CheckImageOutput.h
        FPGAIntegrationTest.cpp
        StrongPixelSetTest.cpp
        AdaptiveThresholdTest.cpp
        AdaptiveSpotFinderCPUTest.cpp
        ZSTDCompressorTest.cpp
        FrameTransformationTest.cpp
        HDF5WritingTest.cpp PedestalCalcTest.cpp
        ZMQImagePusherTest.cpp StreamWriterTest.cpp
        CoordTest.cpp JFJochStateMachineTest.cpp
        JFJochReceiverIntegrationTest.cpp
        AcquisitionCountersTest.cpp
        IndexingUnitTest.cpp
        FFBIDXIndexerCPUTest.cpp
        ThreadSafeFIFOSetTest.cpp
        ReceiveRingBufferTest.cpp
        FPGANetworkTest.cpp
        JFPedestalTest.cpp
        JFCalibrationTest.cpp
        AzimuthalIntegrationTest.cpp
        StatusVectorTest.cpp
        CBORTest.cpp
        DetectorGeometryTest.cpp DetectorSetupTest.cpp DiffractionGeometryTest.cpp
        FPGASpotFindingUnitTest.cpp
        PreviewCounterTest.cpp
        FPGAFrameGeneratorTest.cpp
        FPGAHostWriterTest.cpp
        DiffractionSpotTest.cpp
        TIFFTest.cpp
        JFJochReceiverProcessingTest.cpp
        JPEGTest.cpp
        HistogramTest.cpp
        ROIMapTest.cpp
        ROIIntegrationCPUTest.cpp
        ROIIntegrationGPUTest.cpp
        BraggIntegrationEngineGPUTest.cpp
        BraggIntegrationEngineCompressedImageTest.cpp
        BraggIntegrationEngineCPUTest.cpp
        BackgroundGainTest.cpp
        BackgroundApertureTest.cpp
        BraggStencilTest.cpp
        SpotFootprintTest.cpp
        SpotTilesTest.cpp
        LossyFilterTest.cpp
        ImageBufferTest.cpp
        JFJochImageBufferReaderTest.cpp
        PixelMaskTest.cpp
        RegressionTest.cpp
        FPGAHLSModulesTest.cpp
        ZMQPreviewSocketTest.cpp
        DetectorSettingsTest.cpp
        AutoIncrVectorTest.cpp
        ModuleSummationTest.cpp
        ZMQMetadataSocketTest.cpp
        JFJochReaderTest.cpp
        MultiReaderTest.cpp
        CrystFELGeometryTest.cpp
        XFELReaderTest.cpp
        SaturationCeilingTest.cpp
        DetectorModulesTest.cpp
        PanelLayoutTest.cpp
        CellPopulationTest.cpp
        ModuleRefinementTest.cpp
        LiveCollectionTest.cpp
        RugnuxLiveStatusTest.cpp
        StillsGPUAnalysisTest.cpp
        RugnuxStructureTest.cpp
        SweepLayoutTest.cpp
        BrukerReaderTest.cpp
        ShadowFinderTest.cpp
        HotPixelFinderTest.cpp
        BandwidthEstimateTest.cpp
        ParallelForTest.cpp
        HostMemoryTest.cpp
        RugnuxTest.cpp
        ResultReportTest.cpp
        DiagnosticOutputTest.cpp
        PostRefineTest.cpp
        CellEsdTest.cpp
        RotationScaleWalkTest.cpp
        RotationScaleMergeSegmentsTest.cpp
        AndersonMixingTest.cpp
        RugnuxLargeTest.cpp
        TestData.h
        MovingAverageTest.cpp
        ImageMetadataTest.cpp
        JFJochReceiverLiteTest.cpp
        GridScanSettingsTest.cpp
        JFJochReceiverPlotsTest.cpp
        GoniometerAxisTest.cpp
        DetGeomCalibTest.cpp
        RingsFromProfileTest.cpp
        CalibrationTest.cpp
        XtalOptimizerTest.cpp
        XtalRefineTest.cpp
        XtalRefineCeres.h
        CrystalLatticeTest.cpp
        FPGAPTPTest.cpp
        ResolutionShellsTest.cpp
        ImageSpotFinderCPUTest.cpp
        ImageSpotFinderGPUTest.cpp
        BSLZ4DecoderGPUTest.cpp
        BSLZ4DecoderGPUFuzzTest.cpp
        ImagePreprocessorGPUFusedTest.cpp
        ImagePreprocessorPixelSumTest.cpp
        StoredImageDecodeGPUTest.cpp
        DirectHostReadGPUTest.cpp
        AdaptiveSpotFinderGPUTest.cpp
        SpotExtractorGPUParityTest.cpp
        CUDAMemHelpersTest.cpp
        CudaHandledErrorTest.cpp
        CalcBraggPredictionTest.cpp
        SpotUtilsTest.cpp
        LatticeSearchTest.cpp
        LePageLatticeTest.cpp
        TimeTest.cpp
        RotationIndexerTest.cpp
        SpindleBlindFractionTest.cpp
        SpindleCuspLossTest.cpp
        TopPixelsTest.cpp
        HKLKeyTest.cpp
        TCPImagePusherTest.cpp
        PreflightTest.cpp
        ModelValidationTest.cpp
        MolecularReplacementTest.cpp
        ModelMaskGPUTest.cpp
        ModelDensityGPUTest.cpp
        RefinementTest.cpp
        RefineOnlyTest.cpp
        SolventSitesTest.cpp
        SolventBuildTest.cpp
        SearchSpaceGroupTest.cpp
        SearchSpaceGroupTwinTest.cpp
        SyntheticMergedReflections.h
        XDSPluginTest.cpp
        MergeScaleTest.cpp
        AnisotropyAnalysisTest.cpp
        ModelScalingTest.cpp
        ModelScaleGPUTest.cpp
        CorrectionSurfaceGPUTest.cpp
        RotationIngestGPUTest.cpp
        TwinningAnalysisTest.cpp
        TranslationalNCSTest.cpp
        RfreeFlagsTest.cpp
        FrenchWilsonTest.cpp
        SphericalHarmonicSurfaceTest.cpp
        WilsonOutliersTest.cpp
        IceRingVerdictTest.cpp
        PairSortGPUTest.cpp
        ReindexAmbiguityTest.cpp
        CrystalSettingTest.cpp
        LoadReferenceMtzFreeFlagsTest.cpp
        LoadReferenceSfCifTest.cpp
        WriteReflectionsTest.cpp
        RugnuxResultFilesTest.cpp
        RugnuxInputKindTest.cpp
        RugnuxSweepsTest.cpp
        RugnuxJointSweepsTest.cpp
        UnitCellTest.cpp
        CCTest.cpp
        MultiLatticeSearchTest.cpp
        LatticeReductionTest.cpp
        BeamCenterFromBackgroundTest.cpp
        BeamCenterFFTTest.cpp
        BeamCenterFFTGPUTest.cpp
        BearerTokensTest.cpp
        BrokerHttpAuthTest.cpp
        DectrisSimplonClientTest.cpp
        ../broker/JFJochBrokerHttp.cpp
)

target_link_libraries(jfjoch_test Catch2WithMain JFJochBroker JFJochReceiver JFJochReader JFJochStreamWriter
                      JFJochRugnuxOutput JFJochRugnux JFJochModelWriter JFJochImageAnalysis JFJochCommon JFJochHLSSimulation JFJochPreview
                      jfjoch_xds_plugin ZLIB::ZLIB httplib::httplib)
target_include_directories(jfjoch_test PRIVATE .)
IF (JFJOCH_CUDA_AVAILABLE)
    TARGET_COMPILE_DEFINITIONS(jfjoch_test PRIVATE FFBIDX_SIMPLE_DATA_DIR="${FFBIDX_SIMPLE_DATA_DIR}")
ENDIF()

find_package(Threads REQUIRED)

add_library(enospc_shim MODULE enospc_shim.c)
target_link_libraries(enospc_shim PRIVATE Threads::Threads dl)
set_target_properties(enospc_shim PROPERTIES PREFIX "" POSITION_INDEPENDENT_CODE ON) # remove "lib"

ADD_EXECUTABLE(jfjoch_hdf5_enospc_test jfjoch_hdf5_enospc_test.cpp)
target_link_libraries(jfjoch_hdf5_enospc_test Catch2WithMain JFJochWriter)