From f92720795cb6b2a31b2cba69e3b492991fb5c40b Mon Sep 17 00:00:00 2001
From: Andrews Sobral <andrewssobral@gmail.com>
Date: Thu, 13 Jul 2017 14:45:59 +0200
Subject: [PATCH] Fix for Python support on Windows

---
 CMakeLists.txt                       | 38 +++++++++++++++++++++++-----
 wrapper_python/bgslibrary_module.cpp |  1 +
 wrapper_python/np_opencv_converter.h |  1 +
 wrapper_python/utils/conversion.h    |  1 -
 4 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3c317a2..1509d8c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,8 +23,13 @@ set(CMAKE_BUILD_TYPE Release)
 #set(CMAKE_BUILD_TYPE Debug)
 
 if(WIN32)
-	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
-	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
+	if(BGS_PYTHON_SUPPORT)
+		set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
+		set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
+	else()
+		set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
+		set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
+  endif()
 endif(WIN32)
 
 set(bgs_out_dir ".")
@@ -68,11 +73,13 @@ if(${OpenCV_VERSION} VERSION_LESS 2.3.1)
 endif()
 
 if(BGS_PYTHON_SUPPORT)
-	set(Boost_USE_STATIC_LIBS OFF)
+	#set(Boost_USE_STATIC_LIBS OFF)
+	set(Boost_USE_STATIC_LIBS ON)
 	set(Boost_USE_MULTITHREADED ON)
 	set(Boost_USE_STATIC_RUNTIME OFF)
 	
 	find_package(Boost REQUIRED COMPONENTS python)
+	find_package(PythonInterp REQUIRED)
 	find_package(PythonLibs REQUIRED)
 
 	message(STATUS "Boost library status:")
@@ -81,9 +88,20 @@ if(BGS_PYTHON_SUPPORT)
 	message(STATUS "    include path: ${Boost_INCLUDE_DIRS}")
 
 	message(STATUS "Python library status:")
-	message(STATUS "    version: ${PYTHON_VERSION}")
-	message(STATUS "    libraries: ${PYTHON_LIBRARIES}")
+	message(STATUS "    executable: ${PYTHON_EXECUTABLE}")
+	message(STATUS "    version: ${PYTHON_VERSION_STRING}")
+	#message(STATUS "    libraries: ${PYTHON_LIBRARIES}")
+	message(STATUS "    library: ${PYTHON_LIBRARY}")
 	message(STATUS "    include path: ${PYTHON_INCLUDE_DIRS}")
+	
+	if(NOT NUMPY_INCLUDE_DIR)
+		# message(FATAL_ERROR "You must define NUMPY_INCLUDE_DIR by 'cmake -D NUMPY_INCLUDE_DIR=/python/lib/site-packages/numpy/core/include ..'")
+		exec_program ("${PYTHON_EXECUTABLE}"
+			ARGS "-c \"import numpy; print numpy.get_include()\""
+			OUTPUT_VARIABLE NUMPY_INCLUDE_DIR
+			RETURN_VALUE NUMPY_NOT_FOUND)
+	endif()
+	message(STATUS "NUMPY_INCLUDE_DIR: ${NUMPY_INCLUDE_DIR}")
 endif()
 
 #file(GLOB sources FrameProcessor.cpp PreProcessor.cpp VideoAnalysis.cpp VideoCapture.cpp)
@@ -97,7 +115,7 @@ file(GLOB_RECURSE analysis_src package_analysis/*.cpp)
 if(BGS_PYTHON_SUPPORT)
 	file(GLOB_RECURSE bgs_src package_bgs/*.cpp package_bgs/*.c wrapper_python/*.cpp)
 	file(GLOB_RECURSE bgs_include package_bgs/*.h wrapper_python/*.h)
-	include_directories(${CMAKE_SOURCE_DIR} ${OpenCV_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
+	include_directories(${CMAKE_SOURCE_DIR} ${OpenCV_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR})
 else()
 	file(GLOB_RECURSE bgs_src package_bgs/*.cpp package_bgs/*.c)
 	file(GLOB_RECURSE bgs_include package_bgs/*.h)
@@ -113,7 +131,7 @@ endif()
 if(BGS_PYTHON_SUPPORT)
 	#add_library(libbgs SHARED ${sources} ${bgs_src} ${analysis_src})
 	add_library(libbgs SHARED ${bgs_src} ${analysis_src})
-	target_link_libraries(libbgs ${OpenCV_LIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
+	target_link_libraries(libbgs ${OpenCV_LIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARY})
 	target_compile_definitions(libbgs PRIVATE BGS_PYTHON_SUPPORT=1)
 else()
 	#add_library(libbgs STATIC ${sources} ${bgs_src} ${analysis_src})
@@ -121,12 +139,17 @@ else()
 	target_link_libraries(libbgs ${OpenCV_LIBS})
 endif()
 set_property(TARGET libbgs PROPERTY PUBLIC_HEADER ${bgs_include})
+
 if(WIN32)
 	# set_property(TARGET libbgs PROPERTY SUFFIX ".lib")
+	if(BGS_PYTHON_SUPPORT)
+		set_property(TARGET libbgs PROPERTY SUFFIX ".pyd")
+	endif()
 else()
 	set_property(TARGET libbgs PROPERTY OUTPUT_NAME "bgs")
 endif()
 
+if(NOT BGS_PYTHON_SUPPORT)
 add_executable(bgslibrary ${main})
 target_link_libraries(bgslibrary ${OpenCV_LIBS} libbgs)
 # set_target_properties(bgslibrary PROPERTIES OUTPUT_NAME bgs)
@@ -145,3 +168,4 @@ install(TARGETS libbgs
 	PUBLIC_HEADER DESTINATION include/package_bgs COMPONENT dev
 	FRAMEWORK DESTINATION "/Library/Frameworks"
 )
+endif()
\ No newline at end of file
diff --git a/wrapper_python/bgslibrary_module.cpp b/wrapper_python/bgslibrary_module.cpp
index a8aa624..35fc57f 100644
--- a/wrapper_python/bgslibrary_module.cpp
+++ b/wrapper_python/bgslibrary_module.cpp
@@ -14,6 +14,7 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 */
+#define BOOST_PYTHON_STATIC_LIB
 #include <boost/python.hpp>
 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
 #include <exception>
diff --git a/wrapper_python/np_opencv_converter.h b/wrapper_python/np_opencv_converter.h
index 68386f0..6d8a6aa 100644
--- a/wrapper_python/np_opencv_converter.h
+++ b/wrapper_python/np_opencv_converter.h
@@ -16,6 +16,7 @@ along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 */
 #pragma once
 
+#define BOOST_PYTHON_STATIC_LIB
 #include <boost/python.hpp>
 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
 
diff --git a/wrapper_python/utils/conversion.h b/wrapper_python/utils/conversion.h
index f9e5780..17288f9 100644
--- a/wrapper_python/utils/conversion.h
+++ b/wrapper_python/utils/conversion.h
@@ -21,7 +21,6 @@ along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <Python.h>
 #include <opencv2/opencv.hpp>
-#include <opencv2/core.hpp>
 #include <numpy/ndarrayobject.h>
 
 static PyObject* opencv_error = 0;
-- 
GitLab