From d06dd49dd01571155941261fdb660d0c33b1e33e Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 13 Jun 2017 13:34:17 +0100 Subject: [PATCH] python fixes for win32 --- CMakeLists.txt | 95 ++++++++++++++++++-------- CXX/yapt.hh | 4 +- Packages.cmake | 21 ------ cmake/python.cmake | 39 ++++------- packages/gecode/dev/extractor/Makefile | 3 +- packages/python/CMakeLists.txt | 10 +-- packages/python/swig/CMakeLists.txt | 32 ++++++--- packages/python/swig/__init__.py | 21 ++++-- packages/python/swig/setup.py.in | 12 +--- packages/swig/yap.i | 1 + 10 files changed, 130 insertions(+), 108 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6cb63d40c..fd4a894ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ # value of 3.4.0 or lower. +cmake_minimum_required(VERSION 3.4) if(COMMAND cmake_policy) cmake_policy(SET CMP0003 NEW) @@ -61,36 +62,57 @@ include(Sources NO_POLICY_SCOPE) include(Model NO_POLICY_SCOPE) + include_directories ( utf8proc packages/myddas packages/myddas/sqlite3/src) if (ANDROID) include_directories ( - packages/myddas/sqlite3/src/Android/jni/sqlite - packages/myddas/sqlite3/src/Android/jni/sqlite/nativehelper - ) - endif (ANDROID) + packages/myddas/sqlite3/src/Android/jni/sqlite + packages/myddas/sqlite3/src/Android/jni/sqlite/nativehelper + ) +endif (ANDROID) - set_property( SOURCE ${LIBYAP_SOURCES} APPEND PROPERTY COMPILE_DEFINITIONS YAP_KERNEL=1) +set_property( SOURCE ${LIBYAP_SOURCES} APPEND PROPERTY COMPILE_DEFINITIONS YAP_KERNEL=1) add_definitions(-DUSE_MYDDAS=1 -DMYDDAS_SQLITE3=1) if (MYSQL_FOUND) - add_definitions(= -DMYDDAS_MYSQL=1) - endif() + add_definitions(= -DMYDDAS_MYSQL=1) +endif() if (ODBC_FOUND) - add_definitions(= -DMYDDAS_ODBC=1) - endif() + add_definitions(= -DMYDDAS_ODBC=1) +endif() if (MYSQL_POSTGRES) - add_definitions(= -DMYDDAS_POSTGRES=1) - endif() - if (MYSQL_POSTGRES) - set(SWIG_SOURCES packages/swig/python/yap_PYTHONwrapper.cxx ) - endif() + add_definitions(= -DMYDDAS_POSTGRES=1) +endif() - if (ANDROID) +OPTION (WITH_SWIG " Enable SWIG interfaces to foreign languages" ON) + +IF (WITH_SWIG) + find_host_package (SWIG) + macro_log_feature (SWIG_FOUND "Swig" + "Use SWIG Interface Generator " + "http://www.swig.org" ON) +ENDIF (WITH_SWIG) + +option (WITH_PYTHON + "Allow Python->YAP and YAP->Python" ON) + +IF (WITH_PYTHON) + include(python NO_POLICY_SCOPE) +ENDIF (WITH_PYTHON) + + +IF (SWIG_FOUND) + add_subDIRECTORY (packages/swig NO_POLICY_SCOPE) +ENDIF(SWIG_FOUND) + + + +if (ANDROID) ADD_SUBDIRECTORY(os) ADD_SUBDIRECTORY(OPTYap) @@ -101,18 +123,21 @@ if (MYSQL_POSTGRES) else() - List (APPEND YLIBS $ ) - List (APPEND YLIBS $ ) - List (APPEND YLIBS $ ) - List (APPEND YLIBS $ ) - List (APPEND YLIBS $ ) - List (APPEND YLIBS $ ) -if (WIN32) - List (APPEND YLIBS $ ) - if (PYTHON_INCLUDE_DIRS AND PYTHON_LIBRARIES ) - List (APPEND YLIBS $ ) - endif() - endif() + List (APPEND YLIBS $ ) + List (APPEND YLIBS $ ) + List (APPEND YLIBS $ ) + List (APPEND YLIBS $ ) + List (APPEND YLIBS $ ) + List (APPEND YLIBS $ ) + if (WIN32) + List (APPEND YLIBS $ ) + if (WITH_PYTHON ) + List (APPEND YLIBS $ ) + endif() + endif() + +endif() + add_library( # Sets the name of the library. @@ -137,8 +162,6 @@ endif (WIN32) include(libYap NO_POLICY_SCOPE) -endif() - if (USE_READLINE) target_link_libraries(libYap ${READLINE_LIBRARIES}) endif (USE_READLINE) @@ -155,4 +178,18 @@ set_target_properties(libYap ) MY_include(Packages NO_POLICY_SCOPE) + + +if (PYTHONLIBS_FOUND AND SWIG_FOUND) + add_subdirectory(packages/python/swig) + + include(FindPythonModule) + + find_python_module( jupyter ) + + if (PY_JUPYTER) + add_subdirectory(packages/python/yap_kernel) + ENDIF() +endif() + include(Config NO_POLICY_SCOPE) diff --git a/CXX/yapt.hh b/CXX/yapt.hh index 634b97af6..670b2f0c7 100644 --- a/CXX/yapt.hh +++ b/CXX/yapt.hh @@ -20,13 +20,15 @@ * */ + #include #ifndef YAPT_HH #define YAPT_HH 1 extern "C" { - Term YAP_ReadBuffer(const char *s, Term *tp); + + X_API Term YAP_ReadBuffer(const char *s, Term *tp); } class YAPError; diff --git a/Packages.cmake b/Packages.cmake index 947530d00..8e6e73153 100644 --- a/Packages.cmake +++ b/Packages.cmake @@ -18,27 +18,6 @@ if (WITH_JIT) add_subDIRECTORY(JIT) endif (WITH_JIT) -OPTION (WITH_SWIG " Enable SWIG interfaces to foreign languages" ON) - -IF (WITH_SWIG) - find_host_package (SWIG) - macro_log_feature (SWIG_FOUND "Swig" - "Use SWIG Interface Generator " - "http://www.swig.org" ON) -ENDIF (WITH_SWIG) - -option (WITH_PYTHON - "Allow Python->YAP and YAP->Python" ON) - -IF (WITH_PYTHON) - include(python) -ENDIF (WITH_PYTHON) - - -IF (SWIG_FOUND) - add_subDIRECTORY (packages/swig) -ENDIF(SWIG_FOUND) - add_subDIRECTORY (packages/raptor) add_subDIRECTORY (packages/xml) diff --git a/cmake/python.cmake b/cmake/python.cmake index e92e209e5..e3cb40b61 100644 --- a/cmake/python.cmake +++ b/cmake/python.cmake @@ -1,17 +1,4 @@ set (Python_ADDITIONAL_VERSIONS 3.7 3.6 3.5 3.6 3.4 ) -set (PythonInterp_FIND_VERSION 3) -find_package(PythonInterp) - -get_filename_component( d ${PYTHON_EXECUTABLE} DIRECTORY ) -get_filename_component( s ${PYTHON_EXECUTABLE} EXT ) -get_filename_component( n ${PYTHON_EXECUTABLE} NAME_WE ) - -set( o ${d}/${n}3${s} ) -if (EXISTS o) - set (PYTHON_EXECUTABLE ${o}) - endif() - -find_package(PythonLibs) # PYTHONLIBS_FOUND - have the Python libs been found @@ -22,8 +9,22 @@ find_package(PythonLibs) # PYTHONLIBS_VERSION_STRING - version of the Python libs found (since CMake 2.8.8) ## # +if (WIN32) + set (PYTHONLIBS_FOUND YES CACHE BOOL "MINGW/MSYS2" FORCE ) + set (PYTHON_LIBRARY C:/msys64/mingw64/lib/libpython3.5m.dll.a CACHE FILEPATH "MINGW/MSYS2" FORCE ) + set (PYTHON_LIBRARIES C:/msys64/mingw64/lib/libpython3.5m.dll.a CACHE FILEPATH "MINGW/MSYS2" FORCE ) + set (PYTHON_INCLUDE_PATH C:/msys64/mingw64/include/python3.5m CACHE PATH "MINGW/MSYS2" FORCE ) + set (PYTHON_INCLUDE_DIRS C:/msys64/mingw64/include/python3.5m CACHE PATH "MINGW/MSYS2" FORCE ) + set (PYTHON_EXECUTABLE C:/msys64/mingw64/bin/python3.exe CACHE FILEPATH "MINGW/MSYS2" FORCE ) + set (PYTHONLIBS_VERSION_STRING 3.5 CACHE STRING "MINGW/MSYS2" FORCE ) + else() + find_package(PythonInterp) + + find_package(PythonLibs) + +endif() include_directories( BEFORE ${PYTHON_INCLUDE_DIRS} ) @@ -36,15 +37,3 @@ check_include_file(Python.h HAVE_PYTHON_H) IF (PYTHONLIBS_FOUND) add_subDIRECTORY (packages/python) ENDIF() - -if (PYTHONLIBS_FOUND AND SWIG_FOUND) - add_subdirectory(packages/python/swig) - - include(FindPythonModule) - - find_python_module( jupyter ) - - if (PY_JUPYTER) - add_subdirectory(packages/python/yap_kernel) - ENDIF() -endif() diff --git a/packages/gecode/dev/extractor/Makefile b/packages/gecode/dev/extractor/Makefile index 7f97fcd6c..1af42d21f 100644 --- a/packages/gecode/dev/extractor/Makefile +++ b/packages/gecode/dev/extractor/Makefile @@ -25,8 +25,7 @@ $(PROTOTYPES): % : xml/namespaceGecode.xml extract-prototypes.xsl | sed 's/GECODE_BOOL_EXPORT //g' \ | grep -v '*' | sort >> $@ || { rm -f $@; exit 1; } -$(ENUMS): % : xml/namespaceGecode.xml extract-enums.xsl - cat notice.py > $@ || { rm -f $@; exit 1; } +b cat notice.py > $@ || { rm -f $@; exit 1; } xsltproc extract-enums.xsl $< >> $@ || { rm -f $@; exit 1; } xml/namespaceGecode.xml: Doxyfile diff --git a/packages/python/CMakeLists.txt b/packages/python/CMakeLists.txt index 75669ce76..655e5f0dc 100644 --- a/packages/python/CMakeLists.txt +++ b/packages/python/CMakeLists.txt @@ -3,20 +3,22 @@ set (PYTHON_SOURCES python.c pl2py.c pybips.c py2pl.c pl2pl.c pypreds.c) -set (PYTHON_HEADERS python.h) +set (PYTHON_HEADERS py4yap.h) set (CMAKE_POSITION_INDEPENDENT_CODE TRUE) -add_library (YAPPython0 OBJECT ${PYTHON_SOURCES}) +include_directories( ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include ) + +add_library (Obj4Py OBJECT ${PYTHON_SOURCES} ${PYTHON_HEADERS}) if (WIN32) -add_library (YAPPython SHARED pyload.c ) +add_library (YAPPython SHARED pyload.c ${PYTHON_HEADERS} ) endif() # arithmetic hassle. set_property(TARGET YAPPython PROPERTY CXX_STANDARD 11) set_property(TARGET YAPPython PROPERTY CXX_STANDARD_REQUIRED ON) -target_link_libraries(YAPPython libYap ${PYTHON_LIBRARY}) +target_link_libraries(YAPPython libYap ${PYTHON_LIBRARIES}) set_property( SOURCE ${PYTHON_SOURCES} APPEND PROPERTY COMPILE_DEFINITIONS YAP_KERNEL=1 _GNU-SOURCE=1) diff --git a/packages/python/swig/CMakeLists.txt b/packages/python/swig/CMakeLists.txt index 5f5889ab2..5b325f180 100644 --- a/packages/python/swig/CMakeLists.txt +++ b/packages/python/swig/CMakeLists.txt @@ -23,6 +23,12 @@ SET_SOURCE_FILES_PROPERTIES(../../swig/yap.i PROPERTIES SWIG_FLAGS "-O;-py3") SET_SOURCE_FILES_PROPERTIES(../../swiyap.i PROPERTIES SWIG_MODULE_NAME yap) #SET_SOURCE_FILES_PROPERTIES(../../swi/yap.i PROPERTIES OUTPUT_NAME yap) +set(SYS_DLLS /mingw64/bin/libgcc_s_seh-1.dll + /mingw64/bin/libgmp-10.dll + /mingw64/bin/libstdc++-6.dll + /mingw64/bin/libwinpthread-1.dll + + ) # inform we are compiling YAP # s used in MSYS @@ -35,37 +41,41 @@ SET_SOURCE_FILES_PROPERTIES(../../swiyap.i PROPERTIES SWIG_MODULE_NAME yap) # )x - set (dlls $ + set (python_dlls $ $ $ $ $ $ - $) + $ + ) if (TARGET real) - list( APPEND dlls $ + list( APPEND python_dlls $ ) endif() set (PL ${pl_library} ${PROLOG_SOURCES} ) add_custom_target( YAP4PY_SETUP + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/packages/swig/yap.i ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/yap4py + COMMAND ${CMAKE_COMMAND} -E copy ${python_dlls} ${SYS_DLLS} ${CMAKE_BINARY_DIR}/libYap${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${PYTHON_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/yap4py + COMMAND ${CMAKE_COMMAND} -E copy ${PL} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog + COMMAND ${CMAKE_COMMAND} -E copy ${pl_boot_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/pl + COMMAND ${CMAKE_COMMAND} -E copy ${pl_os_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os + DEPENDS YAP4PY_SETUP_DIRS STARTUP ${python_dlls} ${PYTHON_SOURCES} ${PROLOG_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/setup.py ${SWIG_MODULE_Py2YAP_REAL_NAME} ) + +add_custom_target( YAP4PY_SETUP_DIRS COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/yap.py - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/packages/swig/yap.i ${CMAKE_CURRENT_BINARY_DIR}/yap.i COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py - COMMAND ${CMAKE_COMMAND} -E copy ${dlls} ${CMAKE_BINARY_DIR}/libYap${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${PYTHON_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/yap4py COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog - COMMAND ${CMAKE_COMMAND} -E copy ${PL} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/pl - COMMAND ${CMAKE_COMMAND} -E copy ${pl_boot_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/pl COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os - COMMAND ${CMAKE_COMMAND} -E copy ${pl_os_library} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os -DEPENDS STARTUP ${dlls} ${PYTHON_SOURCES} ${PROLOG_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/setup.py ${SWIG_MODULE_Py2YAP_REAL_NAME} ) + ) add_custom_target( YAP4PY ALL COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} DEPENDS YAP4PY_SETUP) diff --git a/packages/python/swig/__init__.py b/packages/python/swig/__init__.py index aa2bffb4b..ed87bc822 100644 --- a/packages/python/swig/__init__.py +++ b/packages/python/swig/__init__.py @@ -4,14 +4,23 @@ import ctypes import glob import os.path import sys +import platform global yap_lib_path yap_lib_path = os.path.dirname(__file__) - -def load( dll ): - dll = glob.glob(os.path.join(yap_lib_path,dll))[0] - dll = os.path.abspath(dll) - ctypes.CDLL(dll, mode=ctypes.RTLD_GLOBAL) +if platform.system() == 'Windows': + def load( dll ): + dll = glob.glob(os.path.join(yap_lib_path,dll))[0] + dll = os.path.abspath(dll) + ctypes.WinDLL(dll) +else: + def load( dll ): + dll = glob.glob(os.path.join(yap_lib_path,dll))[0] + dll = os.path.abspath(dll) + ctypes.CDLL(dll, mode=ctypes.RTLD_GLOBAL) -load('libYap*') +load('libgcc*') +load('libstdc*') +load('libsinpt*') +load('libgmp*') diff --git a/packages/python/swig/setup.py.in b/packages/python/swig/setup.py.in index edd748f0f..a31b3a011 100644 --- a/packages/python/swig/setup.py.in +++ b/packages/python/swig/setup.py.in @@ -60,14 +60,7 @@ elif platform.system() == 'Windows': my_extra_link_args = ['-Wl,-export-all-symbols','-Wl,-enable-auto-import','-Wl,-enable-runtime-pseudo-relocs'] cplus = [''] -bpy2yap = ['${CMAKE_SOURCE_DIR}/packages/python/python.c', - '${CMAKE_SOURCE_DIR}/packages/python/pl2py.c', - '${CMAKE_SOURCE_DIR}/packages/python/pybips.c', - '${CMAKE_SOURCE_DIR}/packages/python/py2pl.c', - '${CMAKE_SOURCE_DIR}/packages/python/pl2pl.c', - '${CMAKE_SOURCE_DIR}/packages/python/pypreds.c' - ] - +bpy2yap = \ native_sources = ['yap.i'] here = path.abspath(path.dirname(__file__)) @@ -80,7 +73,8 @@ extensions = [Extension('_yap', native_sources, ('_GNU_SOURCE', '1'), ('YAP_PYTHON', '1')], runtime_library_dirs=['yap4py', '${libdir}', '${bindir}'], - swig_opts=['-modern', '-c++', '-py3', '-I${CMAKE_SOURCE_DIR}/CXX', '-I${CMAKE_SOURCE_DIR}/include', + swig_opts=['-modern', '-c++', '-py3', + '-DX_API', '-I${CMAKE_SOURCE_DIR}/CXX', '-I${CMAKE_SOURCE_DIR}/include', '-I${CMAKE_SOURCE_DIR}/H', '-I${CMAKE_SOURCE_DIR}/H/generated', '-I${CMAKE_SOURCE_DIR}/os', '-I${CMAKE_SOURCE_DIR}/OPTYap', '-I../../..'], library_dirs=['../../..', '../../../CXX', '../../packages/python', "${dlls}", "${bindir}", '.'], diff --git a/packages/swig/yap.i b/packages/swig/yap.i index 9956bfe79..e7d08ac5d 100644 --- a/packages/swig/yap.i +++ b/packages/swig/yap.i @@ -1,6 +1,7 @@ %{ + #include #include extern "C"{