diff --git a/cmake/GtsamPythonWrap.cmake b/cmake/GtsamPythonWrap.cmake index fbad77a82..581b068ad 100644 --- a/cmake/GtsamPythonWrap.cmake +++ b/cmake/GtsamPythonWrap.cmake @@ -36,24 +36,46 @@ function(wrap_python TARGET_NAME PYTHON_MODULE_DIRECTORY) ENDIF() ENDIF(APPLE) + if(MSVC) + add_library(${moduleName}_python MODULE ${ARGN}) + set_target_properties(${moduleName}_python PROPERTIES + OUTPUT_NAME ${moduleName}_python + CLEAN_DIRECT_OUTPUT 1 + VERSION 1 + SOVERSION 0 + SUFFIX ".pyd") + target_link_libraries(${moduleName}_python ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARY} ${gtsamLib}) #temp - # Create a static library version - add_library(${TARGET_NAME} SHARED ${ARGN}) + set(PYLIB_OUTPUT_FILE $) + message(${PYLIB_OUTPUT_FILE}) + get_filename_component(PYLIB_OUTPUT_NAME ${PYLIB_OUTPUT_FILE} NAME_WE) + set(PYLIB_SO_NAME ${PYLIB_OUTPUT_NAME}.pyd) - target_link_libraries(${TARGET_NAME} ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARY} gtsam-shared) - set_target_properties(${TARGET_NAME} PROPERTIES - OUTPUT_NAME ${TARGET_NAME} - CLEAN_DIRECT_OUTPUT 1 - VERSION 1 - SOVERSION 0) + ELSE() + # Create a shared library + add_library(${moduleName}_python SHARED ${generated_cpp_file}) + set_target_properties(${moduleName}_python PROPERTIES + OUTPUT_NAME ${moduleName}_python + CLEAN_DIRECT_OUTPUT 1) + target_link_libraries(${moduleName}_python ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARY} ${gtsamLib}) #temp + # On OSX and Linux, the python library must end in the extension .so. Build this + # filename here. + get_property(PYLIB_OUTPUT_FILE TARGET ${moduleName}_python PROPERTY LOCATION) + set(PYLIB_OUTPUT_FILE $) + message(${PYLIB_OUTPUT_FILE}) + get_filename_component(PYLIB_OUTPUT_NAME ${PYLIB_OUTPUT_FILE} NAME_WE) + set(PYLIB_SO_NAME lib${moduleName}_python.so) + ENDIF(MSVC) - # On OSX and Linux, the python library must end in the extension .so. Build this - # filename here. - #get_property(PYLIB_OUTPUT_FILE TARGET ${TARGET_NAME} PROPERTY LOCATION) - set(PYLIB_OUTPUT_FILE $) - get_filename_component(PYLIB_OUTPUT_NAME ${PYLIB_OUTPUT_FILE} NAME_WE) - set(PYLIB_SO_NAME ${PYLIB_OUTPUT_NAME}.so) + # Installs the library in the gtsam folder, which is used by setup.py to create the gtsam package + set(PYTHON_MODULE_DIRECTORY ${CMAKE_SOURCE_DIR}/python/gtsam) + # Cause the library to be output in the correct directory. + add_custom_command(TARGET ${moduleName}_python + POST_BUILD + COMMAND cp -v ${PYLIB_OUTPUT_FILE} ${PYTHON_MODULE_DIRECTORY}/${PYLIB_SO_NAME} + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMENT "Copying library files to python directory" ) # Cause the library to be output in the correct directory. add_custom_command(TARGET ${TARGET_NAME} @@ -65,4 +87,4 @@ function(wrap_python TARGET_NAME PYTHON_MODULE_DIRECTORY) get_directory_property(AMCF ADDITIONAL_MAKE_CLEAN_FILES) list(APPEND AMCF ${PYTHON_MODULE_DIRECTORY}/${PYLIB_SO_NAME}) set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${AMCF}") -endfunction(wrap_python) \ No newline at end of file +endfunction(wrap_python) diff --git a/python/README.txt b/python/README.txt new file mode 100644 index 000000000..5235300c2 --- /dev/null +++ b/python/README.txt @@ -0,0 +1,12 @@ +This directory contains the basic setup script and directory structure for the gtsam python module. +During the build of gtsam, when GTSAM_BUILD_PYTHON is enabled, the following instructions will run. +* Wrap parses gtsam.h and constructs a cpp file called ${moduleName}_python.cpp +* This file is then compiled and linked with BoostPython, generating a shared library which can then be imported by python +* The shared library is then copied to python/gtsam +* The user can use the setup.py script to build and install a python package, allowing easy importing into a python project. Examples: + * python setup.py sdist ---- Builds a tarball of the python package which can then be distributed + * python setup.py install ---- Installs the package into the python dist-packages folder. Can then be imported from any python file. +* To run the unit tests, you must first install the package on your path (TODO: Make this easier) + + +TODO: There are many issues with this build system, but these are the basics. diff --git a/python/gtsam/__init__.py b/python/gtsam/__init__.py index 2e9b00af1..f1b07905b 100644 --- a/python/gtsam/__init__.py +++ b/python/gtsam/__init__.py @@ -1 +1 @@ -from libgtsam import * \ No newline at end of file +from libgtsam_python import * diff --git a/python/gtsam/libgtsam_python.so b/python/gtsam/libgtsam_python.so new file mode 100755 index 000000000..bd87730d7 Binary files /dev/null and b/python/gtsam/libgtsam_python.so differ diff --git a/python/gtsam_tests/testPoint2.py b/python/gtsam_tests/testPoint2.py new file mode 100644 index 000000000..80a6f6cbf --- /dev/null +++ b/python/gtsam_tests/testPoint2.py @@ -0,0 +1,13 @@ +import unittest +from gtsam import * + +#https://docs.python.org/2/library/unittest.html +class TestPoint2(unittest.TestCase): + def setUp(self): + self.point = Point2() + + def test_constructor(self): + pass + +if __name__ == '__main__': + unittest.main() diff --git a/python/CMakeLists.txt b/python/handwritten/CMakeLists.txt similarity index 100% rename from python/CMakeLists.txt rename to python/handwritten/CMakeLists.txt diff --git a/python/handwritten/examples/OdometeryExample.py b/python/handwritten/examples/OdometeryExample.py new file mode 100644 index 000000000..45f729a89 --- /dev/null +++ b/python/handwritten/examples/OdometeryExample.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python +from gtsam import * +import numpy_eigen as npe + +noisemodel = DiagonalNoiseModel.Sigmas([0.1, 0.1, 0.1]) +graph = NonlinearFactorGraph() +initialEstimate = Values() \ No newline at end of file diff --git a/python/exportgtsam.cpp b/python/handwritten/exportgtsam.cpp similarity index 100% rename from python/exportgtsam.cpp rename to python/handwritten/exportgtsam.cpp diff --git a/python/geometry/Point2.cpp b/python/handwritten/geometry/Point2.cpp similarity index 100% rename from python/geometry/Point2.cpp rename to python/handwritten/geometry/Point2.cpp diff --git a/python/geometry/Pose2.cpp b/python/handwritten/geometry/Pose2.cpp similarity index 100% rename from python/geometry/Pose2.cpp rename to python/handwritten/geometry/Pose2.cpp diff --git a/python/geometry/Rot2.cpp b/python/handwritten/geometry/Rot2.cpp similarity index 100% rename from python/geometry/Rot2.cpp rename to python/handwritten/geometry/Rot2.cpp diff --git a/python/linear/NoiseModel.cpp b/python/handwritten/linear/NoiseModel.cpp similarity index 100% rename from python/linear/NoiseModel.cpp rename to python/handwritten/linear/NoiseModel.cpp diff --git a/python/nonlinear/LevenbergMarquardtOptimizer.cpp b/python/handwritten/nonlinear/LevenbergMarquardtOptimizer.cpp similarity index 100% rename from python/nonlinear/LevenbergMarquardtOptimizer.cpp rename to python/handwritten/nonlinear/LevenbergMarquardtOptimizer.cpp diff --git a/python/nonlinear/NonlinearFactorGraph.cpp b/python/handwritten/nonlinear/NonlinearFactorGraph.cpp similarity index 100% rename from python/nonlinear/NonlinearFactorGraph.cpp rename to python/handwritten/nonlinear/NonlinearFactorGraph.cpp diff --git a/python/nonlinear/Values.cpp b/python/handwritten/nonlinear/Values.cpp similarity index 100% rename from python/nonlinear/Values.cpp rename to python/handwritten/nonlinear/Values.cpp diff --git a/python/slam/BearingFactor.cpp b/python/handwritten/slam/BearingFactor.cpp similarity index 100% rename from python/slam/BearingFactor.cpp rename to python/handwritten/slam/BearingFactor.cpp diff --git a/python/slam/BetweenFactor.cpp b/python/handwritten/slam/BetweenFactor.cpp similarity index 100% rename from python/slam/BetweenFactor.cpp rename to python/handwritten/slam/BetweenFactor.cpp diff --git a/python/slam/PriorFactor.cpp b/python/handwritten/slam/PriorFactor.cpp similarity index 100% rename from python/slam/PriorFactor.cpp rename to python/handwritten/slam/PriorFactor.cpp diff --git a/python/setup.py b/python/setup.py old mode 100644 new mode 100755 index abbafda4b..a6013da81 --- a/python/setup.py +++ b/python/setup.py @@ -11,5 +11,5 @@ setup(name='GTSAM', author_email='Andrew.Melim@gatech.edu', url='http://www.python.org/sigs/distutils-sig/', packages=['gtsam'], - package_data={'gtsam' : ['libgtsam.so']}, + package_data={'gtsam' : ['libgtsam_python.so']}, )