cyang-kth / fmm

Fast map matching, an open source framework in C++
https://fmm-wiki.github.io/
Apache License 2.0
892 stars 215 forks source link

Python 3 support #159

Closed PaulKemppi closed 3 years ago

PaulKemppi commented 3 years ago

Thanks for sharing this work. I would suggest to change the python version used in the Swig interface to Python 3. As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported.

In the CMakeLists.txt in python dir, the python version is set to 2.7:

find_package(PythonInterp 2.7 REQUIRED)
find_package(PythonLibs 2.7 REQUIRED)

To change the default version to Python3, one could replace the CMakeLists.txt current content with the following code. I was able to run the example/python/fmm_test.py with Python 3.8 in Ubuntu 20.04.

# Make sure the swig package is loaded.
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
if (SWIG_FOUND)
  message(STATUS "Swig version is ${SWIG_VERSION}")
else()
  message(FATAL_ERROR "Swig not found!\n")
endif()

find_package(Python3 COMPONENTS Interpreter Development)
if (Python3_FOUND)
  message(STATUS "Python header found at ${Python3_INCLUDE_DIRS}")
  message(STATUS "Python library found at ${Python3_LIBRARIES}")
  message(STATUS "Python site package directory found at ${Python3_SITEARCH}")
  include_directories(${Python3_INCLUDE_DIRS})
else()
  message(FATAL_ERROR "Python library not found!\n")
endif()

set_source_files_properties(
${PROJECT_SOURCE_DIR}/python/fmm.i PROPERTIES CPLUSPLUS ON)

if (${CMAKE_VERSION} VERSION_LESS 3.13.0)
  message(STATUS "Using swig add module")
  SWIG_ADD_MODULE(fmm python ${PROJECT_SOURCE_DIR}/python/fmm.i)
  swig_link_libraries(fmm FMMLIB ${Python3_LIBRARIES})
  install(TARGETS ${SWIG_MODULE_fmm_REAL_NAME} DESTINATION ${Python3_SITEARCH})
else()
  message(STATUS "Using swig add library")
  SWIG_ADD_LIBRARY(pyfmm
    LANGUAGE python
    SOURCES ${PROJECT_SOURCE_DIR}/python/fmm.i)
  set_property(TARGET pyfmm PROPERTY OUTPUT_NAME fmm)
  swig_link_libraries(pyfmm FMMLIB ${Python3_LIBRARIES})
  install(TARGETS pyfmm DESTINATION ${Python3_SITEARCH})
endif()
install(FILES ${CMAKE_BINARY_DIR}/python/fmm.py DESTINATION ${Python3_SITEARCH})
cyang-kth commented 3 years ago

Thank you for this suggestion. I think there are still a lot of people working with Python 2.7. So it is better to make it flexible to work for both Python 2 and 3.