PROJECT(opencv.torch)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)

SET(BUILD_TESTS OFF)

SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

FIND_PACKAGE(OpenCV REQUIRED)
FIND_PACKAGE(Torch REQUIRED)
INCLUDE_DIRECTORIES(${Torch_INSTALL_INCLUDE} ${OpenCV_INCLUDE_DIRS})

SET(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include")

ADD_LIBRARY(Common SHARED "${SOURCE_DIR}/Common.cpp")
TARGET_LINK_LIBRARIES(Common TH ${OpenCV_LIBS})

SET(SHARED_LIBS imgcodecs highgui features2d flann imgproc ml optflow photo superres video videoio Classes)

FOREACH(LIB ${SHARED_LIBS})
    ADD_LIBRARY(${LIB} SHARED "${SOURCE_DIR}/${LIB}.cpp")
    TARGET_LINK_LIBRARIES(${LIB} Common)
ENDFOREACH()

if (BUILD_TESTS)
    file(GLOB Tests_SRC "${SOURCE_DIR}/tests/*.cpp")
    ADD_LIBRARY(Tests SHARED ${Tests_SRC})
    TARGET_LINK_LIBRARIES(Tests Common)
endif()

PROJECT(Torch_OpenCV)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)

SET(BUILD_TESTS OFF)

SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

SET(OPENCV_MODULES_TO_WRAP imgcodecs highgui features2d flann imgproc ml optflow photo superres video videoio)

# Unfortunately, OpenCVConfig.cmake doesn't make use of FIND_PACKAGE's "OPTIONAL_COMPONENTS"
# So let's invoke FIND_PACKAGE twice:

# Once (error-throwing) to check if OpenCV is present
FIND_PACKAGE(OpenCV REQUIRED)
# Once more (silently) to look for the installed modules
FIND_PACKAGE(OpenCV OPTIONAL_COMPONENTS ${OPENCV_MODULES_TO_WRAP})

FIND_PACKAGE(Torch REQUIRED)
INCLUDE_DIRECTORIES(${Torch_INSTALL_INCLUDE} ${OpenCV_INCLUDE_DIRS})

SET(SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include")

ADD_LIBRARY(Common SHARED "${SOURCE_DIR}/Common.cpp")
TARGET_LINK_LIBRARIES(Common TH ${OpenCV_LIBS})

ADD_LIBRARY(Classes SHARED "${SOURCE_DIR}/Classes.cpp")
TARGET_LINK_LIBRARIES(Classes Common)

FOREACH(MODULE ${OPENCV_MODULES_TO_WRAP})
    STRING(TOUPPER ${MODULE} MODULE_UPPERCASE)
    IF (OPENCV_${MODULE_UPPERCASE}_FOUND)
        ADD_LIBRARY(${MODULE} SHARED "${SOURCE_DIR}/${MODULE}.cpp")
        TARGET_LINK_LIBRARIES(${MODULE} Common)
    ENDIF()
ENDFOREACH()

IF (BUILD_TESTS)
    FILE(GLOB Tests_SRC "${SOURCE_DIR}/tests/*.cpp")
    ADD_LIBRARY(Tests SHARED ${Tests_SRC})
    TARGET_LINK_LIBRARIES(Tests Common)
endif()

Enable dropping support of nonexistent OpenCV modules
Enable dropping support of nonexistent OpenCV modules
