IF(VIGRANUMPY_DEPENDENCIES_FOUND)
    ADD_CUSTOM_TARGET(vigranumpy)
    ADD_DEPENDENCIES(vigranumpy vigraimpex)

    SET(vigranumpy_tmp_dir "${CMAKE_BINARY_DIR}/vigranumpy/vigra")
    FILE(MAKE_DIRECTORY ${vigranumpy_tmp_dir})

    INCLUDE_DIRECTORIES(${SUPPRESS_WARNINGS} ${VIGRANUMPY_INCLUDE_DIRS})

    IF(HDF5_FOUND)
        ADD_DEFINITIONS(-DHasHDF5 ${HDF5_CPPFLAGS})
        INCLUDE_DIRECTORIES(${SUPPRESS_WARNINGS} ${HDF5_INCLUDE_DIR})
        SET(VIGRANUMPY_IMPEX_LIBRARIES vigraimpex ${HDF5_LIBRARIES})
    ELSE()
        SET(VIGRANUMPY_IMPEX_LIBRARIES vigraimpex)
    ENDIF(HDF5_FOUND)

    IF(MSVC OR MINGW)
        ADD_DEFINITIONS(-DVIGRA_DLL)
    ENDIF ()

    include(VIGRA_ADD_NUMPY_MODULE)

    ADD_SUBDIRECTORY(src)
    ADD_SUBDIRECTORY(lib)
    ADD_SUBDIRECTORY(test)
    ADD_SUBDIRECTORY(docsrc)

    IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/private/CMakeLists.txt)
        ADD_SUBDIRECTORY(private EXCLUDE_FROM_ALL)
    ENDIF()

    IF(PYTHON_PLATFORM MATCHES "^windows$")
        FILE(MAKE_DIRECTORY ${vigranumpy_tmp_dir}/dlls)
        file(RELATIVE_PATH VIGRAIMPEX_DOCDIR ${vigranumpy_tmp_dir} ${DOCDIR})

        if(CMAKE_MAJOR_VERSION LESS 3)
            GET_TARGET_PROPERTY(VIGRAIMPEX_LOCATION vigraimpex LOCATION)
            CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.cmake2.in
                           ${CMAKE_CURRENT_BINARY_DIR}/setup.py
                           @ONLY)
        else()
            CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
                           ${CMAKE_CURRENT_BINARY_DIR}/setup.py.in
                           @ONLY)

            # two-stage file configuration is necessary because certain target
            # properties are only known at generation time (policy CMP0026)
            file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/setup.py
                          INPUT  ${CMAKE_CURRENT_BINARY_DIR}/setup.py.in
                          CONDITION $<CONFIG:Release>)
        endif()

        add_custom_target(PACKAGE_VIGRANUMPY
                           COMMAND ${CMAKE_COMMAND} -E copy_directory ${DOCDIR} ${vigranumpy_tmp_dir}/doc
                           COMMAND python setup.py bdist_wininst
                           DEPENDS check
                           WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                           COMMENT "Creating vigranumpy setup in ${CMAKE_CURRENT_BINARY_DIR}/dist")
    ENDIF()

    set(VIGRANUMPY_CONFIG_INSTALL_PATH lib${LIB_SUFFIX}/vigranumpy)

    set(VIGRANUMPY_INCLUDE_RELATIVE "")
    foreach(PATH ${CMAKE_INSTALL_PREFIX}/include ${VIGRANUMPY_INCLUDE_DIRS})
        file(RELATIVE_PATH RELPATH ${CMAKE_INSTALL_PREFIX}/${VIGRANUMPY_CONFIG_INSTALL_PATH} ${PATH})
        set(VIGRANUMPY_INCLUDE_RELATIVE ${VIGRANUMPY_INCLUDE_RELATIVE} ${RELPATH})
    endforeach(PATH)

    set(VIGRANUMPY_LIBRARIES_RELATIVE "")
    foreach(PATH ${VIGRANUMPY_LIBRARIES})
        if(EXISTS ${PATH})
            file(RELATIVE_PATH RELPATH ${CMAKE_INSTALL_PREFIX}/${VIGRANUMPY_CONFIG_INSTALL_PATH} ${PATH})
            set(VIGRANUMPY_LIBRARIES_RELATIVE ${VIGRANUMPY_LIBRARIES_RELATIVE} ${RELPATH})
        else()
            # If ${PATH} is not referring to a path, it is probably a keyword like
            # "optimized" or "debug" (e.g. on the Mac). We then pass it on unchanged.
            set(VIGRANUMPY_LIBRARIES_RELATIVE ${VIGRANUMPY_LIBRARIES_RELATIVE} ${PATH})
        endif()
    endforeach(PATH)

    CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/config/VigranumpyConfig.cmake.in
            ${PROJECT_BINARY_DIR}/lib/vigranumpy/CMake/VigranumpyConfig.cmake
            @ONLY IMMEDIATE)
    INSTALL(FILES ${PROJECT_BINARY_DIR}/lib/vigranumpy/CMake/VigranumpyConfig.cmake
            DESTINATION ${VIGRANUMPY_CONFIG_INSTALL_PATH})
ENDIF()


