Commit 0f098e01 authored by Guillaume Pasero's avatar Guillaume Pasero

Merge branch 'pkg_python_wrappers' into 'develop'

Package Python wrappers sources

See merge request orfeotoolbox/otb!566
parents f27ae0b8 9797de13
Pipeline #2362 passed with stages
in 6 minutes and 19 seconds
......@@ -24,10 +24,10 @@
# NUMPY_FOUND - True if Numpy headers are found.
# NUMPY_INCLUDE_DIR - where to find numpy/arrayobject.h, etc.
EXEC_PROGRAM ("${PYTHON_EXECUTABLE}"
ARGS "${CMAKE_SOURCE_DIR}/CMake/otbTestNumpy.py"
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c "import sys, numpy; sys.stdout.write(numpy.get_include())"
OUTPUT_VARIABLE NUMPY_INCLUDE_DIR
RETURN_VALUE NUMPY_NOT_FOUND)
RESULT_VARIABLE NUMPY_NOT_FOUND)
if( NUMPY_INCLUDE_DIR MATCHES "Traceback" )
# Did not successfully include numpy
......
This diff is collapsed.
import numpy
print(numpy.get_include())
......@@ -22,6 +22,8 @@ cmake_minimum_required(VERSION 3.10.2)
foreach(p
CMP0072 # CMake 3.11
CMP0078 # CMake 3.13
CMP0086 # CMake 3.14
)
if(POLICY ${p})
cmake_policy(SET ${p} NEW)
......
......@@ -18,18 +18,10 @@
# limitations under the License.
#
if ( OTB_WRAP_PYTHON )
include ( UseSWIGLocal )
list(APPEND SWIG_EXTRA_DEPS
otbWrapperSWIGIncludes.h
itkBase.includes
${CMAKE_CURRENT_SOURCE_DIR}/itkBase.i
${CMAKE_CURRENT_SOURCE_DIR}/itkMacro.i
)
include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} )
endif()
include(UseSWIG)
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )
# Python SWIG configuration
if ( OTB_WRAP_PYTHON )
add_subdirectory(python)
endif()
\ No newline at end of file
endif()
......@@ -21,6 +21,14 @@
include_directories ( ${PYTHON_INCLUDE_DIR} )
include_directories ( ${CMAKE_CURRENT_SOURCE_DIR})
set_source_files_properties ( ../otbApplication.i PROPERTIES CPLUSPLUS ON )
# Get target name
set(extension_target _otbApplication)
if(POLICY CMP0078)
cmake_policy(GET CMP0078 swig_target_name_policy)
if (swig_target_name_policy STREQUAL "NEW")
set(extension_target otbApplication)
endif()
endif()
# Run swig
set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_GLOBAL_FLAGS})
if(NUMPY_FOUND)
......@@ -36,34 +44,35 @@ set(SWIG_MODULE_otbApplication_EXTRA_DEPS
otbPythonLogOutput.h
otbProgressReporterManager.h
OTBApplicationEngine)
SWIG_add_module( otbApplication python ../otbApplication.i
otbApplicationPYTHON_wrap.cxx
itkPyCommand.cxx
../python/otbPythonLogOutput.cxx
../python/otbProgressReporterManager.cxx)
SWIG_link_libraries( otbApplication ${PYTHON_LIBRARIES} OTBApplicationEngine )
set_target_properties(_otbApplication PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR})
swig_add_library( otbApplication
LANGUAGE python
SOURCES ../otbApplication.i
itkPyCommand.cxx
otbPythonLogOutput.cxx
otbProgressReporterManager.cxx)
swig_link_libraries( otbApplication ${PYTHON_LIBRARIES} OTBApplicationEngine )
set_target_properties(${extension_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR})
if(MSVC)
set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "/wd4005" )
set_source_files_properties( ${CMAKE_SWIG_OUTDIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "/wd4005" )
else()
set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "-w" )
set_source_files_properties( ${CMAKE_SWIG_OUTDIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "-w" )
endif()
# byte-compile the resulting python file
add_custom_command(
TARGET _otbApplication
TARGET ${extension_target}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Byte-compiling otbApplication.py"
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_SOURCE_DIR}/CMake/PythonCompile.py
${CMAKE_SWIG_OUTDIR}/otbApplication.py
DEPENDS _otbApplication
DEPENDS ${extension_target}
)
otb_module_target_label( _otbApplication )
otb_module_target_label( ${extension_target} )
install( TARGETS _otbApplication
install( TARGETS ${extension_target}
DESTINATION ${OTB_INSTALL_PYTHON_DIR}
COMPONENT RuntimeLibraries )
......@@ -78,3 +87,20 @@ if(PYTHON_VERSION_STRING VERSION_LESS "3.2.0")
else()
# TODO : pyc file is in a subfolder.
endif()
configure_file(StandaloneWrapper.in CMakeLists.txt @ONLY)
install( FILES ${CMAKE_SWIG_OUTDIR}/otbApplicationPYTHON_wrap.cxx
${CMAKE_SWIG_OUTDIR}/otbApplicationPYTHON_wrap.h
itkPyCommand.cxx
itkPyCommand.h
otbPythonLogOutput.cxx
otbPythonLogOutput.h
otbSwigPrintCallback.h
otbProgressReporterManager.cxx
otbProgressReporterManager.h
../itkBase.includes
../otbWrapperSWIGIncludes.h
${CMAKE_CURRENT_BINARY_DIR}/CMakeLists.txt
DESTINATION ${OTB_INSTALL_DATA_DIR}/swig
COMPONENT Resources )
#
# Copyright (C) 2005-2019 CS Systemes d'Information (CS SI)
#
# This file is part of Orfeo Toolbox
#
# https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
cmake_minimum_required(VERSION 3.10.1)
project(OTBPythonWrappers)
get_filename_component(PKG_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}" PATH)
get_filename_component(PKG_PREFIX "${PKG_PREFIX}" PATH)
get_filename_component(PKG_PREFIX "${PKG_PREFIX}" PATH)
find_package(OTB REQUIRED HINTS ${PKG_PREFIX}/@OTB_INSTALL_PACKAGE_DIR@)
list(APPEND CMAKE_MODULE_PATH ${OTB_CMAKE_DIR})
include(${OTB_USE_FILE})
set ( Python_ADDITIONAL_VERSIONS "3;2" )
set ( PythonInterp_FIND_REQUIRED ${OTB_WRAP_PYTHON} )
set ( PythonLibs_FIND_REQUIRED ${OTB_WRAP_PYTHON} )
find_package( PythonInterp )
if ( OTB_WRAP_PYTHON AND ( ${PYTHON_VERSION_MAJOR} EQUAL 2 ) )
message (WARNING "Python3 not found. There is no longer support of \
wrapping in python2 in OTB, but it can still be used.")
endif()
# Now that we have a python version the addditional versions should only
# contain the version we have to get the corresponding libs
set (Python_ADDITIONAL_VERSIONS "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
find_package ( PythonLibs )
include_directories(${PYTHON_INCLUDE_DIR})
find_package(Numpy)
if ( NUMPY_FOUND )
include_directories(${NUMPY_INCLUDE_DIRS})
else()
message( WARNING
"OTB wrappers will be done without support for NumPy (not found).")
endif()
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
file(GLOB OTBPythonWrappers_SOURCES "*.cxx")
# declare the python wrapper library
add_library(PythonWrapperLib MODULE ${OTBPythonWrappers_SOURCES})
target_link_libraries(PythonWrapperLib ${PYTHON_LIBRARIES} OTBApplicationEngine)
set_target_properties(PythonWrapperLib PROPERTIES
OUTPUT_NAME "_otbApplication"
NO_SONAME ON
PREFIX "")
if(WIN32 AND NOT CYGWIN)
set_target_properties(PythonWrapperLib PROPERTIES SUFFIX ".pyd")
endif()
if(NUMPY_FOUND)
add_definitions(-DOTB_SWIGNUMPY=1)
endif()
install(TARGETS PythonWrapperLib
DESTINATION lib/python
COMPONENT RuntimeLibraries )
......@@ -19,7 +19,7 @@
#
set(TEST_DRIVER otbTestDriver
--add-before-env PYTHONPATH $<TARGET_FILE_DIR:_otbApplication>
--add-before-env PYTHONPATH ${CMAKE_BINARY_DIR}/${OTB_INSTALL_PYTHON_DIR}
--add-before-env OTB_APPLICATION_PATH $<TARGET_FILE_DIR:otbapp_Smoothing> )
if(WIN32)
......
#
# Copyright (C) 2005-2019 CS Systemes d'Information (CS SI)
#
# This file is part of Orfeo Toolbox
#
# https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
get_filename_component(PKG_PREFIX "${CTEST_SCRIPT_DIRECTORY}" PATH)
get_filename_component(PKG_PREFIX "${PKG_PREFIX}" PATH)
get_filename_component(PKG_PREFIX "${PKG_PREFIX}" PATH)
set(CTEST_SOURCE_DIRECTORY ${CTEST_SCRIPT_DIRECTORY})
set(CTEST_BINARY_DIRECTORY ${CTEST_SCRIPT_DIRECTORY}/build)
if(WIN32)
set(CTEST_CMAKE_GENERATOR "NMake Makefiles")
else()
set(CTEST_CMAKE_GENERATOR "Unix Makefiles")
endif()
set(CTEST_BUILD_CONFIGURATION "Release")
if(IS_DIRECTORY "${CTEST_BINARY_DIRECTORY}")
file(REMOVE_RECURSE "${CTEST_BINARY_DIRECTORY}")
endif()
set(CONFIGURE_OPTIONS
"-DCMAKE_INSTALL_PREFIX=${PKG_PREFIX}"
"-DCMAKE_BUILD_TYPE=Release")
if(PYTHON_EXECUTABLE)
list(APPEND CONFIGURE_OPTIONS "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}")
endif()
ctest_start(Experimental)
ctest_configure(OPTIONS "${CONFIGURE_OPTIONS}")
ctest_build(TARGET install)
# How to recompile OTB Python bindings with your Python version?
The OTB Python bindings are made of:
* a Python module `otbApplication.py`
* a Python C++ extension `_otbApplication.so` (or `_otbApplication.pyd` on Windows)
The Python module should be independent from the Python version you are using,
but the compiled C++ extension is linked to a specific Python library. If this
library is not present on your system, you can recompile the extension to use
your Python version.
The procedure is simple, once you have the following prerequisite:
* A C++ compiler:
* On Linux: GCC >= 5.0
* On macos: XCode
* On Windows: either Visual Studio >= 2015 with the desktop C++ workload,
or just the MSVC Build Tools.
* CMake >= 3.10
* Python library and headers for your Python version
* On macos and Windows: they are usually supplied with installers from
official website
* On Linux packages: you should look for a `python-dev` package matching
your Python interpreter version.
* Numpy module for your Python version
The compilation and installation of the C++ extension can be launched from the
command line (on Windows you can use `cmd.exe`):
```
ctest -S build_wrapping.cmake
```
If you want to use a specific Python interpreter:
```
ctest -DPYTHON_EXECUTABLE=/my/own/python -S build_wrapping.cmake
```
Note: on Windows, you have to setup MSVC compiler before calling `ctest`. Use
the `vcvarsall.bat` script with the right architecture (x32 or x64). For
instance:
```
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
```
......@@ -24,6 +24,14 @@ function(install_python_bindings)
PATTERN "*.pyc" EXCLUDE
PATTERN "__pycache__" EXCLUDE
)
install(DIRECTORY ${SUPERBUILD_INSTALL_DIR}/share/otb/swig
DESTINATION ${PKG_STAGE_DIR}/share/otb
)
install(FILES Files/build_wrapping.cmake
DESTINATION ${PKG_STAGE_DIR}/share/otb/swig)
install(FILES Files/build_wrapping.md
DESTINATION ${PKG_STAGE_DIR}/share/otb/swig
RENAME README.md)
endif()
# Handle GDAL python bindings
file(GLOB gdal_python_scripts "${SUPERBUILD_INSTALL_DIR}/bin/gdal*.py")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment