macro(ecvl_update_3rdparty SUBMODULE)
    if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
    # Update submodule as needed
        message(STATUS "${SUBMODULE} update")
        execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive "3rdparty/${SUBMODULE}"
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                        RESULT_VARIABLE GIT_SUBMOD_RESULT)
        if(NOT GIT_SUBMOD_RESULT EQUAL "0")
            message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout ${SUBMODULE} submodule")
        endif()
    endif()
endmacro()


if(${ECVL_WITH_DICOM})
    ecvl_update_3rdparty(dcmtk)

    SET(BUILD_APPS OFF CACHE INTERNAL "" FORCE)
    SET(BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE)
    SET(BUILD_SINGLE_SHARED_LIBRARY OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_BUILTIN_DICTIONARY ON CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_CHARSET_CONVERSION <disabled> CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_CXX11 INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_EXTERNAL_DICTIONARY ON CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_LFS lfs CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_PRIVATE_TAGS OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_ALGORITHM INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_LIMITS INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_LIST INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_MAP INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_MEMORY INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_STACK INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_STRING INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_SYSTEM_ERROR INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_TUPLE INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_TYPE_TRAITS INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_STL_VECTOR INFERRED CACHE INTERNAL "" FORCE)
    SET(DCMTK_GENERATE_DOXYGEN_TAGFILE OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_MODULES ofstd;oflog;dcmdata;dcmimgle;dcmimage;dcmjpeg CACHE INTERNAL "" FORCE)
    SET(DCMTK_OVERWRITE_WIN32_COMPILER_FLAGS ON CACHE INTERNAL "" FORCE)
    SET(DCMTK_WIDE_CHAR_FILE_IO_FUNCTIONS OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WIDE_CHAR_MAIN_FUNCTION OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_DOXYGEN OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_ICONV OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_ICU OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_OPENJPEG OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_OPENSSL OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_PNG OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_SNDFILE OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_STDLIBC_ICONV OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_THREADS ON CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_TIFF OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_XML OFF CACHE INTERNAL "" FORCE)
    SET(DCMTK_WITH_ZLIB OFF CACHE INTERNAL "" FORCE)
    SET(DOXYGEN_DOT_EXECUTABLE DOXYGEN_DOT_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(DOXYGEN_EXECUTABLE DOXYGEN_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_DATA_LIBRARY_DEBUG ICU_DATA_LIBRARY_DEBUG-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_DATA_LIBRARY_RELEASE ICU_DATA_LIBRARY_RELEASE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_DERB_EXECUTABLE ICU_DERB_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENBRK_EXECUTABLE ICU_GENBRK_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENCCODE_EXECUTABLE ICU_GENCCODE_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENCFU_EXECUTABLE ICU_GENCFU_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENCMN_EXECUTABLE ICU_GENCMN_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENCNVAL_EXECUTABLE ICU_GENCNVAL_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENDICT_EXECUTABLE ICU_GENDICT_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENNORM2_EXECUTABLE ICU_GENNORM2_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENRB_EXECUTABLE ICU_GENRB_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_GENSPREP_EXECUTABLE ICU_GENSPREP_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_ICU-CONFIG_EXECUTABLE ICU_ICU-CONFIG_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_ICUINFO_EXECUTABLE ICU_ICUINFO_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_ICUPKG_EXECUTABLE ICU_ICUPKG_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_INCLUDE_DIR ICU_INCLUDE_DIR-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_MAKECONV_EXECUTABLE ICU_MAKECONV_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_MAKEFILE_INC ICU_MAKEFILE_INC-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_PKGDATA_EXECUTABLE ICU_PKGDATA_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_PKGDATA_INC ICU_PKGDATA_INC-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_UCONV_EXECUTABLE ICU_UCONV_EXECUTABLE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_UC_LIBRARY_DEBUG ICU_UC_LIBRARY_DEBUG-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(ICU_UC_LIBRARY_RELEASE ICU_UC_LIBRARY_RELEASE-NOTFOUND CACHE INTERNAL "" FORCE)
    SET(WITH_LIBICONVINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_LIBPNGINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_LIBTIFFINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_LIBXMLINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_OPENJPEGINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_OPENSSLINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_SNDFILEINC "" CACHE INTERNAL "" FORCE)
    SET(WITH_ZLIBINC "" CACHE INTERNAL "" FORCE)
    SET(DCMTK_ENABLE_LFS lfs CACHE INTERNAL "" FORCE)
    mark_as_advanced(DCMTK_ENABLE_LFS)
    mark_as_advanced(DCMTK_COMPILE_WIN32_MULTITHREADED_DLL)

    set(CMAKE_FOLDER 3rdparty/dcmtk)
    add_subdirectory(3rdparty/dcmtk)
    set(CMAKE_FOLDER 3rdparty)

    set(DCMTK_LIBS ofstd;oflog;dcmdata;dcmimgle;dcmimage;dcmjpeg)

    message(STATUS "DCMTK libs: ${DCMTK_LIBS}")
    target_link_libraries(ECVL_CORE PUBLIC ${DCMTK_LIBS})

    set(DCMTK_INCLUDE_DIRS "")
    foreach(DCMTK_LIB config ${DCMTK_LIBS})
        list(APPEND DCMTK_INCLUDE_DIRS "dcmtk/${DCMTK_LIB}/include")
    endforeach()

    foreach(DCMTK_LIB ${DCMTK_LIBS})
        set_target_properties(${DCMTK_LIB} PROPERTIES
        OUTPUT_NAME_DEBUG "${DCMTK_LIB}d"
        OUTPUT_NAME_RELEASE "${DCMTK_LIB}")
    endforeach()

    message(STATUS "DCMTK incldue dure: ${DCMTK_INCLUDE_DIRS}")

    foreach(dcmtk_include_dir ${DCMTK_INCLUDE_DIRS})
        target_include_directories(ECVL_CORE PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/${dcmtk_include_dir}> $<INSTALL_INTERFACE:include> )
    endforeach()
    target_include_directories(ECVL_CORE PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/3rdparty/dcmtk/config/include> $<INSTALL_INTERFACE:include> ) 

    # for osconfig.h
    #
    #target_include_directories(ECVL_CORE PUBLIC
    #    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    #    $<INSTALL_INTERFACE:include>
    #)

    target_sources(ECVL_CORE PRIVATE "../modules/core/include/ecvl/core/support_dcmtk.h" "../modules/core/src/support_dcmtk.cpp")
endif(${ECVL_WITH_DICOM})