cmake_minimum_required(VERSION 2.4.6)

if(COMMAND cmake_policy)
    cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

MATH(EXPR BITS ${CMAKE_SIZEOF_VOID_P}*8)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Search for PCL for example compilation
set(DISABLE_PCL 0 CACHE BOOL "Disables OpenCL example")
if(NOT DISABLE_PCL)
    message(STATUS "looking for PCL")
    find_package(PCL 1.3 COMPONENTS common io filters)

    if(PCL_FOUND)
        include_directories(${PCL_INCLUDE_DIRS})
        link_directories(${PCL_LIBRARY_DIRS})
        add_definitions(${PCL_DEFINITIONS})
    else()
        message(WARNING "Not building PCL examples!")
    endif()
endif()

# Search for OpenCV for example compilation
set(DISABLE_OPENCV 0 CACHE BOOL "Disables OpenCV example")
if(NOT DISABLE_OPENCV)
    message(STATUS "looking for OpenCV")
    find_package(OpenCV)
    if(OpenCV_FOUND)
        include_directories(${OpenCV_INCLUDE_DIRS})
    else()
        message(WARNING "Not building OpenCV examples!")
    endif()
endif()

# Search for python3
set(DISABLE_PYTHON 0 CACHE BOOL "Disables python library")
if(NOT DISABLE_PYTHON)
    message(STATUS "looking for python3")

    if(WIN32 AND NOT MINGW)
        # Make sure we use the Windows python, not a matching one from msys!
        set(USERPROFILE $ENV{USERPROFILE})
        if(${BITS} EQUAL 32)
            file(GLOB Python3_EXECUTABLE
                "${USERPROFILE}/AppData/Local/Programs/Python/Python3?-32/python.exe"
                "${USERPROFILE}/AppData/Local/Programs/Python/Python3??-32/python.exe")
        else()
            file(GLOB Python3_EXECUTABLE
                "${USERPROFILE}/AppData/Local/Programs/Python/Python3?/python.exe"
                "${USERPROFILE}/AppData/Local/Programs/Python/Python3??/python.exe")
        endif()

        message(WARNING "Windows build - assuming Python 3 is \"${Python3_EXECUTABLE}\".")
    else()
        set(Python3_EXECUTABLE "python3")
        # This is for CMake 3.12 and up; making sure we get python3
        find_package (Python3 COMPONENTS Interpreter HINTS "/mingw64")
        if(NOT Python3_FOUND)
            # We don't give up just yet
            message(WARNING "Failed finding python3 with FindPython3. Assuming python3 is \"${Python3_EXECUTABLE}\"")
        endif()
    endif()

    # Search for cython
    message(STATUS "looking for Cython")
    execute_process(COMMAND "${Python3_EXECUTABLE}" "-c"
"\
from distutils.core import setup\n\
from distutils.extension import Extension\n\
from Cython.Build import cythonize\n\
print('OK')\
"
        OUTPUT_VARIABLE cython_output
        OUTPUT_STRIP_TRAILING_WHITESPACE)
    if("${cython_output}" STREQUAL "OK")
        message(STATUS "Cython test successful, will build python library")
        set(BUILD_CYTHON 1)
    else()
        message(WARNING "${cython_output}")
        message(WARNING "Cython not found! Not building python library!")
    endif()
endif()

if(NOT CMAKE_SUBMODULE)
    #set the default path for built libraries to the "lib" directory
    if(NOT WIN32 OR MINGW)
        set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    else()
        set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib${BITS})
    endif()
endif()

add_subdirectory(visiontransfer)
add_subdirectory(examples)

if(BUILD_CYTHON)
    add_subdirectory(python)
endif()

