# This is a CMake file that sets up the add_python_module() macro.  This macro
# lets you easily make python modules that use dlib.  
#
# The macro takes the module name as its first argument and then a list of
# source files to compile into the module.  See ../tools/python/CMakeLists.txt
# for an example.
#
# It also sets up a macro called install_${module_name}_to() where
# ${module_name} is whatever you named your module.  This install_*_to() macro
# takes a folder name and creates an install target that will copy the compiled
# python module to that folder when you run "make install".  Note that the path
# given to install_*_to() is relative to your CMakeLists.txt file.



# A list of various paths you need to search on windows since people install
# boost in a bunch of different places.
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} 
        "C:/local/boost_1_*"
        "C:/Program Files (x86)/boost/boost_1_*"  
        "C:/Program Files/boost/boost_1_*")
set(BOOST_LIBRARYDIR "C:/local/boost_1_*/lib32-msvc-*")



#SET(Boost_USE_STATIC_LIBS OFF)
#SET(Boost_USE_MULTITHREADED ON)
#SET(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_NO_BOOST_CMAKE ON)

FIND_PACKAGE(Boost 1.41.0 COMPONENTS python REQUIRED)
FIND_PACKAGE(PythonLibs 2.6 REQUIRED)

if (WIN32 AND NOT Boost_LIBRARIES)
    message(FATAL_ERROR "We couldn't find the right version of boost python.  If you installed boost and you are still "
            "getting this error then you might have installed a version of boost that was compiled with a different "
            "version of visual studio than the one you are using.  So you have to make sure that the version of "
            "visual studio is the same version that was used to compile the copy of boost you are using.")
endif()


INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}")
if (PYTHON_INCLUDE_PATH)
   INCLUDE_DIRECTORIES("${PYTHON_INCLUDE_PATH}" )
else()
   INCLUDE_DIRECTORIES("${PYTHON_INCLUDE_DIRS}" )
endif()

if (CMAKE_COMPILER_IS_GNUCXX)
   add_definitions("-fPIC")
endif()

# include dlib so we can link against it
string(REPLACE "add_python_module" "" dlib_path ${CMAKE_CURRENT_LIST_FILE})
include(${dlib_path}/cmake)

# We put the extra _ on the end of the name just so it's possible to
# have a module name of dlib and not get a conflict with the target named
# dlib in ../dlib/cmake.  We use the target OUPUT_NAME property to ensure the
# output name is set to what the user asked for (i.e. no _).
macro(add_python_module module_name module_sources )
   ADD_LIBRARY(${module_name}_ SHARED ${module_sources} ${ARGN} )
   TARGET_LINK_LIBRARIES(${module_name}_ ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}  dlib)
   if(WIN32)
      SET_TARGET_PROPERTIES( ${module_name}_
         PROPERTIES
         PREFIX ""
         SUFFIX ".pyd"
         OUTPUT_NAME ${module_name}
         )
   else()
      SET_TARGET_PROPERTIES( ${module_name}_
         PROPERTIES
         PREFIX ""
         SUFFIX ".so"
         OUTPUT_NAME ${module_name}
         )
   endif()

   macro(install_${module_name}_to path)
       # Determine the path to our CMakeLists.txt file.
       string(REPLACE "CMakeLists.txt" "" base_path ${CMAKE_CURRENT_LIST_FILE})
       INSTALL(TARGETS ${module_name}_ 
            DESTINATION "${base_path}/${path}"
           )

       # On windows we will usually need to have the boost-python .dll files in the same folder or
       # you will get an error about how they can't be found.  So copy the boost .dll files along with
       # your module to the install folder to avoid this.
       if (WIN32)
           list(GET Boost_LIBRARIES 1 boostlibs1)
           list(GET Boost_LIBRARIES 3 boostlibs2)
           string(REPLACE ".lib" ".dll" boostdlls1 ${boostlibs1})
           string(REPLACE ".lib" ".dll" boostdlls2 ${boostlibs2})
           INSTALL(FILES ${boostdlls1} ${boostdlls2} 
                DESTINATION "${base_path}/${path}"
                )
       endif()
   endmacro()

endmacro()



