# On windows 64 bits platform, enable big obj (.o) handling, in order to correctly compile this (heavy) project
if (WIN32)
    if(CMAKE_CL_64)
        if (MSVC)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
        elseif(MINGW)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -finline-functions")
        endif()
    endif()
endif()

# Create a specific pool, so that parallel build is optimize to reduce memory management
include(ProcessorCount)
ProcessorCount(NUMBER_OF_PROC)
# remove some to avoid processor overload
if (MSVC)
    # trying to avoid "fatal error c1060, the compiler is out of heap space"
    set(NUMBER_OF_PROC_OPTIMAL 2)
else()
    math(EXPR NUMBER_OF_PROC_OPTIMAL "${NUMBER_OF_PROC} / 2")
endif()
if(NUMBER_OF_PROC_OPTIMAL GREATER 0)
    message(STATUS "Detected ${NUMBER_OF_PROC} processors but using only ${NUMBER_OF_PROC_OPTIMAL} for building itkfilters")
    set_property(GLOBAL PROPERTY JOB_POOLS itk_filters_pool=${NUMBER_OF_PROC_OPTIMAL})
endif()

camitk_extension(ACTION_EXTENSION
                 NEEDS_ITK
                 NEEDS_CEP_LIBRARIES camitkitktemplates
                 CXX_FLAGS "-w"
                 CEP_NAME CEP_IMAGING
                 DESCRIPTION "This action features basic ITK image processing filters"
                 DEFINES COMPILE_ITKFILTER_API
                 ENABLE_AUTO_TEST
                 TEST_FILES bassin.msh BigEndian.hdr BigEndian.img BigEndianCompressed1.hdr BigEndianCompressed1.img biorad.pic brain.mha cthead1.lsm LittleEndian1.hdr LittleEndian1.img LittleEndianCompressed1.hdr LittleEndianCompressed1.img LittleEndianZ1.hdr mini-complex-slow1.nrrd mini-ten-nomask-slow1.nrrd mini-vector-slow1.nrrd ramp1.gipl
                 ENABLE_INTEGRATION_TEST
)

if(NUMBER_OF_PROC_OPTIMAL GREATER 0)
    set_property(TARGET action-itkfilters PROPERTY JOB_POOL_COMPILE itk_filters_pool)
endif()

# --------------------------------
# --- Specific test management ---
# --------------------------------
camitk_disable_tests(TESTS action-itkfilters-level1-6
                           action-itkfilters-level1-8
                           action-itkfilters-level1-14
                     REASON "Test failure due to ITK exception.
  When applying action Gradient Magnitude With Smoothing to test files
  biorad.pic, cthead1.lsm, mini-complex-slow1.nrrd, the following exception
  is raised:
  \"camitk-testcomponents aborted by std exception: 
  /usr/include/ITK-5.2/itkRecursiveSeparableImageFilter.hxx:224:
  ITK ERROR: RecursiveGaussianImageFilter(0x55d1275a5760): The number of
  pixels along direction 2 is less than 4. This filter requires a minimum
  of four pixels along the dimension to be processed\""
)

camitk_tests_requirement(TESTS action-itkfilters-integration-test
                         REQUIRES "NOT WIN32"
                         REASON "WIN32 OpenGL failure on a VM
  This test will always fail when run inside a VM due to OpenGL crash.
  This test will pass when run directly from a physical windows machine."
)

camitk_tests_requirement(TESTS action-itkfilters-integration-test
                         REQUIRES "${VTK_VERSION} VERSION_EQUAL 9.1.0"
                         REASON "VTK version is not equals than 9.1.0
  The default test files are written using the currently supported 
  version of VTK (VTK 9.1).
  This test might therefore fail when comparing the input to the 
  output if another version of VTK is used.
  Although everything should work fine, the output diff will make
  this test unconditionally fail."
)
