dgobbi / vtk-dicom

A set of classes for using DICOM in VTK.
BSD 3-Clause "New" or "Revised" License
258 stars 94 forks source link

Building vtk DICOM as VTK module with USE_ITK_GDCM clashes with zlib #226

Open stanLec opened 10 months ago

stanLec commented 10 months ago

Good morning, I tried to build VtkDicom as a VTK 9.0.1 remote module using the option USE_ITK_GDCM activated to avoid rebuilding GDCM since I use it also for ITK 5.3.

This is how i build ITK as an external project of my project

      ExternalProject_Add( ${proj}
    URL "https://github.com/InsightSoftwareConsortium/ITK/archive/refs/tags/v5.3.0.zip"
    URL_MD5 06d4b2365f799c28c9361a063e0f2e5f

    CMAKE_ARGS
        -DBUILD_TESTING:BOOL=OFF
        -DBUILD_EXAMPLES:BOOL=OFF
        -DBUILD_DOCUMENTATION:BOOL=OFF
        # This is needed otherwise ITK selects Release configuration by default
        -DCMAKE_BUILD_TYPE:STRING=$<CONFIG>

        -DModule_ITKOpenJPEG:BOOL=ON
        -DModule_ITKReview:BOOL=ON
        -DITK_SKIP_PATH_LENGTH_CHECKS:BOOL=ON
        -DITK_LEGACY_SILENT:BOOL=ON
)

and VTK

        ExternalProject_Add( ${proj} 
        URL "https://gitlab.kitware.com/vtk/vtk/-/archive/v9.0.1/vtk-v9.0.1.zip"
        URL_MD5 1dc648623c71420a195e77afe3ccd833

        DEPENDS ITK

        CMAKE_ARGS
            -DBUILD_EXAMPLES:BOOL=OFF
            -DBUILD_TESTING:BOOL=OFF
            -DCMAKE_DEBUG_POSTFIX:STRING=d
            -DVTK_WRAP_TCL:BOOL=OFF
        -DVTK_WRAP_PYTHON:BOOL=OFF
            -DVTK_WRAP_JAVA:BOOL=OFF

            -DVTK_Group_Qt:BOOL=OFF
            -DVTK_Group_Imaging:BOOL=ON
            -DVTK_LEGACY_SILENT:BOOL=ON
            -DVTK_REPORT_OPENGL_ERRORS_IN_RELEASE_BUILDS:BOOL=ON
            -DVTK_BUILD_QT_DESIGNER_PLUGIN:BOOL=OFF
            -DVTK_ENABLE_KITS:BOOL=ON
            -DVTK_RENDERING_BACKEND=OpenGL2
            -DDirectX_FOUND:BOOL=OFF

            # VtkDICOM build
            -DVTK_MODULE_ENABLE_VTK_vtkDICOM=YES
        -DVTK_MODULE_ENABLE_VTK_DICOM=YES
        -DUSE_ITK_GDCM:BOOL=ON
            -DBUILD_DICOM_PROGRAMS:BOOL=OFF

    )

I get a CMake error when building vtk Dicom when the find_package(ITK) is done, Zlib seems to be clashing between ITK and VTKDicom. We also build ZLib as an external library in the project. If I use the argument ITK_USE_SYSTEM_ZLIB for ITK I don't have the issue but the ZLib path in the CMake exported target of ITK is absolute and not relative to ITK project.

  -- vtkDICOM: Building vtkDICOM as a Remote VTK Module

  CMake Error at XXX/lib/cmake/ITK-5.3/ITKTargets.cmake:42 (message):

    Some (but not all) targets in this export set were already defined.

    Targets Defined: zlib

    Targets not yet defined: itkdouble-conversion, itksys, itkvcl,

    itkv3p_netlib, itkvnl, itkvnl_algo, itktestlib, ITKVNLInstantiation,

    ITKCommon, itkNetlibSlatec, ITKStatistics, ITKTransform, ITKMesh,

    ITKMetaIO, ITKSpatialObjects, ITKPath, ITKImageIntensity, ITKLabelMap,

    ITKMathematicalMorphology, ITKQuadEdgeMesh, ITKFastMarching,

    ITKIOImageBase, ITKFFT, ITKConvolution, ITKSmoothing, ITKImageFeature,

    ITKOptimizers, ITKPolynomials, ITKBiasCorrection, ITKColormap,

    ITKDICOMParser, ITKDeformableMesh, ITKDenoising, ITKDiffusionTensorImage,

    ITKEXPAT, gdcmjpeg8, gdcmjpeg12, gdcmjpeg16, gdcmopenjp2, gdcmcharls,

    gdcmsocketxx, gdcmCommon, gdcmDICT, gdcmDSED, gdcmIOD, gdcmMSFF, gdcmMEXD,

    ITKznz, ITKniftiio, ITKgiftiio, ITKPDEDeformableRegistration, hdf5-shared,

    hdf5_hl-shared, hdf5_cpp-shared, hdf5_hl_cpp-shared, ITKIOBMP, ITKIOBioRad,

    ITKIOBruker, ITKIOCSV, ITKIOGDCM, ITKIOIPL, ITKIOGE, ITKIOGIPL, ITKIOHDF5,

    itkjpeg, ITKIOJPEG, itkopenjpeg, ITKIOJPEG2000, itktiff, ITKIOTIFF,

    ITKIOLSM, itkminc2, ITKIOMINC, ITKIOMRC, ITKIOMeshBase, ITKIOMeshBYU,

    ITKIOMeshFreeSurfer, ITKIOMeshGifti, ITKIOMeshOBJ, ITKIOMeshOFF,

    ITKIOMeshVTK, ITKIOMeta, ITKIONIFTI, ITKNrrdIO, ITKIONRRD, itkpng,

    ITKIOPNG, ITKIOSiemens, ITKIOXML, ITKIOSpatialObjects, ITKIOStimulate,

    ITKTransformFactory, ITKIOTransformBase, ITKIOTransformHDF5,

    ITKIOTransformInsightLegacy, ITKIOTransformMatlab, ITKIOVTK,

    ITKKLMRegionGrowing, itklbfgs, ITKMarkovRandomFieldsClassifiers,

    ITKOptimizersv4, ITKQuadEdgeMeshFiltering, ITKRegionGrowing,

    ITKRegistrationMethodsv4, ITKVTK, ITKWatersheds, ITKReview, itkTestDriver,

    ITKTestKernel, ITKVideoCore, ITKVideoIO

Do you have any leads about this error ? Thank you

dgobbi commented 10 months ago

I ran some tests today, and I can confirm that using USE_ITK_GDCM=ON with vtkDICOM as a VTK remote module causes this error in find_package(ITK). Simply adding find_package(ITK) to any of VTK's cmake files is enough to trigger the error, since this causes ITK to see VTK's zlib target directly instead of as "VTK::zlib". There might be some kind of namespace trick that will allow ITK to be used from VTK.

Things work if VTK is built without enabling the remote module, and then vtkDICOM is externally (since find_package(VTK) sets up namespaces so that VTK's zlib and ITK's zlib don't collide).

dgobbi commented 10 months ago

As a follow-up, vtkDICOM builds as a remote module if I edit ITKTargets.cmake and replace every occurrence of zlib with ITK::zlib. So that's a possible fix, but not a pretty one.

stanLec commented 10 months ago

Thank you for your really quick answer. Could this behavior be documented, if it is not possible to fix ? I spent some time trying to figure out the issue. Meanwhile I will try to fix the issue by patching the ITKTargets.cmake as you suggested. I suppose that I can patch ITK repository to define ITK::zlib target instead of zlib target. EDIT: I did a dirty patch by renaming the zlib target in Modules/ThirdParty/ZLIB/CMakeLists.txt and in Modules/ThirdParty/ZLIB/src/itkzlib-ng/CMakeLists.txt in the ITK source code. It seems to resolve the conflict with VTK::zlib