diff --git a/CMake/UseSWIGLocal.cmake b/CMake/UseSWIGLocal.cmake index 639143ccb0a5bb14d70d3dd7805430915023eca7..3f7b7984b6a30472e7412e7aaba2321d9ff66358 100644 --- a/CMake/UseSWIGLocal.cmake +++ b/CMake/UseSWIGLocal.cmake @@ -122,10 +122,6 @@ macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile) set(swig_generated_file_fullname "${CMAKE_CURRENT_BINARY_DIR}") - if(swig_source_file_relative_path) - set(swig_generated_file_fullname - "${swig_generated_file_fullname}/${swig_source_file_relative_path}") - endif() # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir if(CMAKE_SWIG_OUTDIR) set(swig_outdir ${CMAKE_SWIG_OUTDIR}) diff --git a/CMakeLists.txt b/CMakeLists.txt index 147b46694827c7899b9b1fd2fda1b0907e2e695e..56ebefc0406a952844fb2a2594754094baefa5e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,6 +145,9 @@ endif() if(NOT OTB_INSTALL_PYTHON_DIR) set(OTB_INSTALL_PYTHON_DIR "lib/otb/python") endif() +if(NOT OTB_INSTALL_PYTHON3_DIR) + set(OTB_INSTALL_PYTHON3_DIR "lib/otb/python3") +endif() if(NOT OTB_INSTALL_JAVA_DIR) set(OTB_INSTALL_JAVA_DIR "lib/otb/java") endif() diff --git a/Documentation/Cookbook/rst/Installation_Linux.txt b/Documentation/Cookbook/rst/Installation_Linux.txt index 3ca41e0b28b92d2953e5f05fa034bde6a23b0209..f8b0dd652b72b8b57dd58982af4bdaea3030eaf5 100644 --- a/Documentation/Cookbook/rst/Installation_Linux.txt +++ b/Documentation/Cookbook/rst/Installation_Linux.txt @@ -84,8 +84,10 @@ necessary to install one of the following packages: Python bindings ~~~~~~~~~~~~~~~ -Starting from OTB 5.8.0, OTB Python bindings are distributed with binary package. -Currently only Python 2.x is supported and if no compatible Python version is found a +Starting from OTB 5.8.0, OTB bindings for Python 2.7 are distributed with binary +package. With OTB 6.4.0, additional bindings for Python 3.5 are also included. +Please note that using a different Python version may not be compatible with +OTB wrappings. If no compatible Python 2.x version is found a notification is generated during the installation process. If the installation completes without issue, information relating to your Python bindings will be provided. @@ -93,6 +95,10 @@ You must have Python numpy bindings installed in your system. They can be instal without admin rights as follows: "pip install --user numpy". This is to give users the option to select their own existing Python installation rather than the one dibstributed by the OTB package. +By default, bindings for Python 2.7 will be enabled with the ``otbenv`` script. +If you want to use bindings for Python 3.5, you can copy this script and modify: + +- ``lib/python`` into ``lib/python3``, for variable PYTHONPATH Notes: diff --git a/Documentation/Cookbook/rst/Installation_Macx.txt b/Documentation/Cookbook/rst/Installation_Macx.txt index a3ad2d568f6e1fd84e001fb10860e0d04011bc78..1d227e256ed025b774f8b18cd63281e0df6ac6e2 100644 --- a/Documentation/Cookbook/rst/Installation_Macx.txt +++ b/Documentation/Cookbook/rst/Installation_Macx.txt @@ -44,14 +44,21 @@ supplied script ``tools/uninstall_otb.sh``. Python bindings ~~~~~~~~~~~~~~~ -Starting from OTB 5.8.0, OTB python bindings are distributed with binary package. -currently only Python 2.x is supported. If no compatible python is found, installation -notify you about it. If everything works fine, you will be given information about -using python bindings. - -You must have python numpy bindings installed in your system. you can install it locally -without admin rights with "pip install --user numpy". This is to give users to choose -their own existing python installation rather than distributing one in OTB package. +Starting from OTB 5.8.0, OTB bindings for Python 2.7 are distributed with binary +package. With OTB 6.4.0, additional bindings for Python 3.5 are also included. +Please note that using a different Python version may not be compatible with +OTB wrappings. If no compatible Python 2.x version is found a +notification is generated during the installation process. If the installation completes +without issue, information relating to your Python bindings will be provided. + +You must have Python numpy bindings installed in your system. They can be installed locally +without admin rights as follows: "pip install --user numpy". This is to give users the option +to select their own existing Python installation rather than the one dibstributed by the OTB package. + +By default, bindings for Python 2.7 will be enabled with the ``otbenv`` script. +If you want to use bindings for Python 3.5, you can copy this script and modify: + +- ``lib/python`` into ``lib/python3``, for variable PYTHONPATH Notes: diff --git a/Documentation/Cookbook/rst/Installation_Windows.txt b/Documentation/Cookbook/rst/Installation_Windows.txt index 004238b4e46b6a511b6b61a8d71133afeeedfd23..24b3a0c92c3bc58b8a86c9fec163002dcf318076 100644 --- a/Documentation/Cookbook/rst/Installation_Windows.txt +++ b/Documentation/Cookbook/rst/Installation_Windows.txt @@ -43,8 +43,10 @@ start Visual Studio. Python bindings ~~~~~~~~~~~~~~~ -Starting from OTB 5.8.0, OTB Python bindings are distributed with binary package. -Currently only Python 2.x is supported and if no compatible Python version is found a +Starting from OTB 5.8.0, OTB bindings for Python 2.7 are distributed with binary +package. With OTB 6.4.0, additional bindings for Python 3.5 are also included. +Please note that using a different Python version may not be compatible with +OTB wrappings. If no compatible Python 2.x version is found a notification is generated during the installation process. If the installation completes without issue, information relating to your Python bindings will be provided. @@ -52,6 +54,11 @@ You must have Python numpy bindings installed in your system. They can be instal without admin rights as follows: "pip install --user numpy". This is to give users the option to select their own existing Python installation rather than the one dibstributed by the OTB package. +By default, bindings for Python 2.7 will be enabled with the ``otbenv`` script. +If you want to use bindings for Python 3.5, you can copy this script and modify: + +- ``lib/python`` into ``lib/python3``, for variable PYTHONPATH + Notes ~~~~~ diff --git a/Modules/Wrappers/SWIG/otb-module-init.cmake b/Modules/Wrappers/SWIG/otb-module-init.cmake index 413ba564b509344f251404841d81df2ac0df3914..8f02b520e99a34b04c006a819f128e3ff9f400b0 100644 --- a/Modules/Wrappers/SWIG/otb-module-init.cmake +++ b/Modules/Wrappers/SWIG/otb-module-init.cmake @@ -19,9 +19,10 @@ # option ( OTB_WRAP_PYTHON "Wrap Python" OFF ) +option ( OTB_WRAP_PYTHON3 "Wrap Python 3" OFF ) option ( OTB_WRAP_JAVA "Wrap Java" OFF ) -if ( OTB_WRAP_PYTHON OR OTB_WRAP_JAVA ) +if ( OTB_WRAP_PYTHON OR OTB_WRAP_JAVA OR OTB_WRAP_PYTHON3) find_package ( SWIG REQUIRED ) mark_as_advanced(SWIG_DIR) mark_as_advanced(SWIG_EXECUTABLE) @@ -43,11 +44,67 @@ endmacro() if ( OTB_WRAP_PYTHON ) check_PIC_flag ( Python ) - find_package ( PythonLibs REQUIRED ) find_package ( PythonInterp REQUIRED ) + find_package ( PythonLibs REQUIRED ) find_package ( Numpy ) endif() +macro(swap_cache_variable var1 var2) + set(_backup ${${var1}}) + get_property(_var1_type CACHE ${var1} PROPERTY TYPE) + get_property(_var1_help CACHE ${var1} PROPERTY HELPSTRING) + get_property(_var2_type CACHE ${var2} PROPERTY TYPE) + get_property(_var2_help CACHE ${var2} PROPERTY HELPSTRING) + set(${var1} ${${var2}} CACHE ${_var1_type} "${_var1_help}" FORCE) + set(${var2} ${_backup} CACHE ${_var2_type} "${_var2_help}" FORCE) +endmacro() + +macro(swap_variable var1 var2) + set(_backup ${${var1}}) + set(${var1} ${${var2}}) + set(${var2} ${_backup}) +endmacro() + +if ( OTB_WRAP_PYTHON3 ) + set(PYTHON3_EXECUTABLE "PYTHON3_EXECUTABLE-NOTFOUND" CACHE FILEPATH "Path to python 3 interpreter") + set(PYTHON3_INCLUDE_DIR "PYTHON3_INCLUDE_DIR-NOTFOUND" CACHE PATH "Path to python 3 include directory") + set(PYTHON3_LIBRARY "PYTHON3_LIBRARY-NOTFOUND" CACHE FILEPATH "Path to python 3 library") + set(PYTHON3_LIBRARY_DEBUG "PYTHON3_LIBRARY_DEBUG-NOTFOUND" CACHE FILEPATH "Path to python 3 library (debug)") + set(PYTHON3_LIBRARY_RELEASE "PYTHON3_LIBRARY_RELEASE-NOTFOUND" CACHE FILEPATH "Path to python 3 library (release)") + set(NUMPY_PYTHON3_INCLUDE_DIR "NUMPY_PYTHON3_INCLUDE_DIR" CACHE PATH "Path to numpy module for Python 3") + + # Swap cache variables between python and python3 + swap_cache_variable(PYTHON_EXECUTABLE PYTHON3_EXECUTABLE) + swap_cache_variable(PYTHON_INCLUDE_DIR PYTHON3_INCLUDE_DIR) + swap_cache_variable(PYTHON_LIBRARY PYTHON3_LIBRARY) + swap_cache_variable(PYTHON_LIBRARY_DEBUG PYTHON3_LIBRARY_DEBUG) + swap_cache_variable(PYTHON_LIBRARY_RELEASE PYTHON3_LIBRARY_RELEASE) + swap_cache_variable(NUMPY_INCLUDE_DIR NUMPY_PYTHON3_INCLUDE_DIR) + + swap_variable(PYTHON_LIBRARIES PYTHON3_LIBRARIES) + swap_variable(NUMPY_INCLUDE_DIRS NUMPY_PYTHON3_INCLUDE_DIRS) + swap_variable(NUMPY_FOUND NUMPY_PYTHON3_FOUND) + swap_variable(PYTHON_VERSION_STRING PYTHON3_VERSION_STRING) + + check_PIC_flag ( Python ) + find_package ( PythonInterp 3 REQUIRED ) + find_package ( PythonLibs 3 REQUIRED ) + find_package ( Numpy ) + + # Swap cache variables between python and python3 + swap_cache_variable(PYTHON_EXECUTABLE PYTHON3_EXECUTABLE) + swap_cache_variable(PYTHON_INCLUDE_DIR PYTHON3_INCLUDE_DIR) + swap_cache_variable(PYTHON_LIBRARY PYTHON3_LIBRARY) + swap_cache_variable(PYTHON_LIBRARY_DEBUG PYTHON3_LIBRARY_DEBUG) + swap_cache_variable(PYTHON_LIBRARY_RELEASE PYTHON3_LIBRARY_RELEASE) + swap_cache_variable(NUMPY_INCLUDE_DIR NUMPY_PYTHON3_INCLUDE_DIR) + + swap_variable(PYTHON_LIBRARIES PYTHON3_LIBRARIES) + swap_variable(NUMPY_INCLUDE_DIRS NUMPY_PYTHON3_INCLUDE_DIRS) + swap_variable(NUMPY_FOUND NUMPY_PYTHON3_FOUND) + swap_variable(PYTHON_VERSION_STRING PYTHON3_VERSION_STRING) +endif() + # # JAVA SWIG configuration # diff --git a/Modules/Wrappers/SWIG/src/CMakeLists.txt b/Modules/Wrappers/SWIG/src/CMakeLists.txt index 8c0b302ddab973791be241d2a08fc647cdcf3537..34a3d58689511182bc7d50bff2085f82757328ba 100644 --- a/Modules/Wrappers/SWIG/src/CMakeLists.txt +++ b/Modules/Wrappers/SWIG/src/CMakeLists.txt @@ -18,124 +18,28 @@ # limitations under the License. # -if ( OTB_WRAP_PYTHON OR OTB_WRAP_JAVA ) +if ( OTB_WRAP_PYTHON OR OTB_WRAP_JAVA OR OTB_WRAP_PYTHON3) include ( UseSWIGLocal ) - set_source_files_properties ( otbApplication.i PROPERTIES CPLUSPLUS ON ) - 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() - -# # Python SWIG configuration -# if ( OTB_WRAP_PYTHON ) - include_directories ( ${PYTHON_INCLUDE_DIR} ) - - # Run swig - set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_GLOBAL_FLAGS}) - if(NUMPY_FOUND) - include_directories(${NUMPY_INCLUDE_DIRS}) - list(APPEND CMAKE_SWIG_FLAGS "-DOTB_SWIGNUMPY=1") - endif() - set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR}/${OTB_INSTALL_PYTHON_DIR}) - set(SWIG_MODULE_otbApplication_EXTRA_DEPS - ${CMAKE_CURRENT_SOURCE_DIR}/Python.i - ${CMAKE_CURRENT_SOURCE_DIR}/PyCommand.i - itkPyCommand.h - OTBApplicationEngine) - SWIG_add_module( otbApplication python otbApplication.i otbApplicationPYTHON_wrap.cxx itkPyCommand.cxx ) - SWIG_link_libraries( otbApplication ${PYTHON_LIBRARIES} OTBApplicationEngine ) - set_target_properties(_otbApplication PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR}) - if(MSVC) - set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "/wd4005" ) - else() - set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "-w" ) - endif() - - # byte-compile the resulting python file - add_custom_command( - TARGET _otbApplication - 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 - ) - - otb_module_target_label( _otbApplication ) - - install( TARGETS _otbApplication - DESTINATION ${OTB_INSTALL_PYTHON_DIR} - COMPONENT RuntimeLibraries ) - - install( FILES ${CMAKE_SWIG_OUTDIR}/otbApplication.py - ${CMAKE_SWIG_OUTDIR}/otbApplication.pyc - DESTINATION ${OTB_INSTALL_PYTHON_DIR} - COMPONENT RuntimeLibraries ) + add_subdirectory(python) +endif() +# Python 3 SWIG configuration +if ( OTB_WRAP_PYTHON3 ) + add_subdirectory(python3) endif() -# # JAVA SWIG configuration -# if ( OTB_WRAP_JAVA ) - include_directories ( ${JAVA_INCLUDE_PATH} ${JNI_INCLUDE_DIRS} ) - - # Make sure the nested directory structure exists - set(JAVA_SOURCE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/org/otb/application) - set(JAVA_BINARY_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build) - file(MAKE_DIRECTORY ${JAVA_SOURCE_DIRECTORY}) - file(MAKE_DIRECTORY ${JAVA_BINARY_DIRECTORY}) - - # Nicely write the bridge code in org/itk/simple - set(CMAKE_SWIG_OUTDIR ${JAVA_SOURCE_DIRECTORY}) - set(CMAKE_SWIG_FLAGS -package "org.otb.application" ${CMAKE_SWIG_GLOBAL_FLAGS}) - set(SWIG_MODULE_otbApplicationJava_EXTRA_DEPS - ${CMAKE_CURRENT_SOURCE_DIR}/Java.i - OTBApplicationEngine) - SWIG_add_module ( otbApplicationJava java otbApplication.i otbApplicationJAVA_wrap.cxx) - SWIG_link_libraries(otbApplicationJava OTBApplicationEngine ) - - if(MSVC) - set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationJAVA_wrap.cxx COMPILE_FLAGS "/wd4005" ) - else() - set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/otbApplicationJAVA_wrap.cxx COMPILE_FLAGS "-w") - endif() - - # Add target for org.otb.Application.jar - add_custom_target(org_otb_Application_jar ALL DEPENDS org.otb.application.jar) - - otb_module_target_label( org_otb_Application_jar ) - - # Add custom command and target to compile the generated files and put them in a jar file - # Make sure the commands depend on the output library from SWIG - add_custom_command( - OUTPUT org.otb.application.jar - COMMENT "Creating jar file..." - COMMAND ${Java_JAVAC_EXECUTABLE} -d ${JAVA_BINARY_DIRECTORY} ${JAVA_SOURCE_DIRECTORY}/*.java - COMMAND ${Java_JAR_EXECUTABLE} cf ${CMAKE_CURRENT_BINARY_DIR}/org.otb.application.jar -C ${JAVA_BINARY_DIRECTORY} org - DEPENDS ${SWIG_MODULE_otbApplication_REAL_NAME} otbApplicationJava OTBApplicationEngine - ) - - # Get the location of the extension directory - string(REGEX REPLACE "include" "jre/lib/ext" JAVA_EXTENSION_DIR ${JAVA_INCLUDE_PATH} ) - - # Add the install target - # -- install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.otb.application.jar DESTINATION ${JAVA_EXTENSION_DIR}) - # Prefer using OTB_INSTALL_JAVA_DIR which defaults to something that honors CMAKE_INSTALL_PREFIX - # Using the system-wide Java extension dir usually require admin privileges, - # and shall better be left to packagers. - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.otb.application.jar - DESTINATION ${OTB_INSTALL_JAVA_DIR} - COMPONENT RuntimeLibraries ) - + add_subdirectory(java) endif() diff --git a/Modules/Wrappers/SWIG/src/java/CMakeLists.txt b/Modules/Wrappers/SWIG/src/java/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..a4df75c5c60e7d82f0b48f75d12b7a3c6e75676b --- /dev/null +++ b/Modules/Wrappers/SWIG/src/java/CMakeLists.txt @@ -0,0 +1,70 @@ +# +# Copyright (C) 2005-2017 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. +# + +include_directories ( ${JAVA_INCLUDE_PATH} ${JNI_INCLUDE_DIRS} ) +set_source_files_properties ( ../otbApplication.i PROPERTIES CPLUSPLUS ON ) + +# Make sure the nested directory structure exists +set(JAVA_SOURCE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/org/otb/application) +set(JAVA_BINARY_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build) +file(MAKE_DIRECTORY ${JAVA_SOURCE_DIRECTORY}) +file(MAKE_DIRECTORY ${JAVA_BINARY_DIRECTORY}) + +# Nicely write the bridge code in org/itk/simple +set(CMAKE_SWIG_OUTDIR ${JAVA_SOURCE_DIRECTORY}) +set(CMAKE_SWIG_FLAGS -package "org.otb.application" ${CMAKE_SWIG_GLOBAL_FLAGS}) +set(SWIG_MODULE_otbApplicationJava_EXTRA_DEPS + ${CMAKE_CURRENT_SOURCE_DIR}/../Java.i + OTBApplicationEngine) +SWIG_add_module ( otbApplicationJava java ../otbApplication.i otbApplicationJAVA_wrap.cxx) +SWIG_link_libraries(otbApplicationJava OTBApplicationEngine ) + +if(MSVC) + set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationJAVA_wrap.cxx COMPILE_FLAGS "/wd4005" ) +else() + set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/otbApplicationJAVA_wrap.cxx COMPILE_FLAGS "-w") +endif() + +# Add target for org.otb.Application.jar +add_custom_target(org_otb_Application_jar ALL DEPENDS org.otb.application.jar) + +otb_module_target_label( org_otb_Application_jar ) + +# Add custom command and target to compile the generated files and put them in a jar file +# Make sure the commands depend on the output library from SWIG +add_custom_command( + OUTPUT org.otb.application.jar + COMMENT "Creating jar file..." + COMMAND ${Java_JAVAC_EXECUTABLE} -d ${JAVA_BINARY_DIRECTORY} ${JAVA_SOURCE_DIRECTORY}/*.java + COMMAND ${Java_JAR_EXECUTABLE} cf ${CMAKE_CURRENT_BINARY_DIR}/org.otb.application.jar -C ${JAVA_BINARY_DIRECTORY} org + DEPENDS ${SWIG_MODULE_otbApplication_REAL_NAME} otbApplicationJava OTBApplicationEngine + ) + +# Get the location of the extension directory +string(REGEX REPLACE "include" "jre/lib/ext" JAVA_EXTENSION_DIR ${JAVA_INCLUDE_PATH} ) + +# Add the install target +# -- install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.otb.application.jar DESTINATION ${JAVA_EXTENSION_DIR}) +# Prefer using OTB_INSTALL_JAVA_DIR which defaults to something that honors CMAKE_INSTALL_PREFIX +# Using the system-wide Java extension dir usually require admin privileges, +# and shall better be left to packagers. +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/org.otb.application.jar + DESTINATION ${OTB_INSTALL_JAVA_DIR} + COMPONENT RuntimeLibraries ) diff --git a/Modules/Wrappers/SWIG/src/python/CMakeLists.txt b/Modules/Wrappers/SWIG/src/python/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6177a6682026d9291a2a30a404bfbb719f06bdca --- /dev/null +++ b/Modules/Wrappers/SWIG/src/python/CMakeLists.txt @@ -0,0 +1,72 @@ +# +# Copyright (C) 2005-2017 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. +# + +include_directories ( ${PYTHON_INCLUDE_DIR} ) +include_directories ( ${CMAKE_CURRENT_SOURCE_DIR} ) +set_source_files_properties ( ../otbApplication.i PROPERTIES CPLUSPLUS ON ) +# Run swig +set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_GLOBAL_FLAGS}) +if(NUMPY_FOUND) + include_directories(${NUMPY_INCLUDE_DIRS}) + list(APPEND CMAKE_SWIG_FLAGS "-DOTB_SWIGNUMPY=1") +endif() +set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR}/${OTB_INSTALL_PYTHON_DIR}) +set(SWIG_MODULE_otbApplication_EXTRA_DEPS + ${CMAKE_CURRENT_SOURCE_DIR}/../Python.i + ${CMAKE_CURRENT_SOURCE_DIR}/../PyCommand.i + itkPyCommand.h + OTBApplicationEngine) +SWIG_add_module( otbApplication python ../otbApplication.i otbApplicationPYTHON_wrap.cxx itkPyCommand.cxx ) +SWIG_link_libraries( otbApplication ${PYTHON_LIBRARIES} OTBApplicationEngine ) +set_target_properties(_otbApplication PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR}) +if(MSVC) + set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "/wd4005" ) +else() + set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "-w" ) +endif() + +# byte-compile the resulting python file +add_custom_command( + TARGET _otbApplication + 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 + ) + +otb_module_target_label( _otbApplication ) + +install( TARGETS _otbApplication + DESTINATION ${OTB_INSTALL_PYTHON_DIR} + COMPONENT RuntimeLibraries ) + +install( FILES ${CMAKE_SWIG_OUTDIR}/otbApplication.py + DESTINATION ${OTB_INSTALL_PYTHON_DIR} + COMPONENT RuntimeLibraries ) + +if(PYTHON_VERSION_STRING VERSION_LESS "3.2.0") + install( FILES ${CMAKE_SWIG_OUTDIR}/otbApplication.pyc + DESTINATION ${OTB_INSTALL_PYTHON_DIR} + COMPONENT RuntimeLibraries ) +else() + # TODO : pyc file is in a subfolder. +endif() diff --git a/Modules/Wrappers/SWIG/src/itkPyCommand.cxx b/Modules/Wrappers/SWIG/src/python/itkPyCommand.cxx similarity index 100% rename from Modules/Wrappers/SWIG/src/itkPyCommand.cxx rename to Modules/Wrappers/SWIG/src/python/itkPyCommand.cxx diff --git a/Modules/Wrappers/SWIG/src/itkPyCommand.h b/Modules/Wrappers/SWIG/src/python/itkPyCommand.h similarity index 100% rename from Modules/Wrappers/SWIG/src/itkPyCommand.h rename to Modules/Wrappers/SWIG/src/python/itkPyCommand.h diff --git a/Modules/Wrappers/SWIG/src/python3/CMakeLists.txt b/Modules/Wrappers/SWIG/src/python3/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..97953712dd2cef134e08b6aa566e83d097b08f0c --- /dev/null +++ b/Modules/Wrappers/SWIG/src/python3/CMakeLists.txt @@ -0,0 +1,73 @@ +# +# Copyright (C) 2005-2017 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. +# + +include_directories ( ${PYTHON3_INCLUDE_DIR} ) +include_directories ( ${CMAKE_CURRENT_SOURCE_DIR}/../python ) +set_source_files_properties ( ../otbApplication.i PROPERTIES CPLUSPLUS ON ) +# Run swig +set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_GLOBAL_FLAGS}) +if(NUMPY_PYTHON3_FOUND) + include_directories(${NUMPY_PYTHON3_INCLUDE_DIRS}) + list(APPEND CMAKE_SWIG_FLAGS "-DOTB_SWIGNUMPY=1") +endif() +set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR}/${OTB_INSTALL_PYTHON3_DIR}) +set(SWIG_MODULE_otbApplication_EXTRA_DEPS + ${CMAKE_CURRENT_SOURCE_DIR}/../Python.i + ${CMAKE_CURRENT_SOURCE_DIR}/../PyCommand.i + itkPyCommand.h + OTBApplicationEngine) +SWIG_add_module( otbApplicationPy3 python ../otbApplication.i otbApplicationPYTHON_wrap.cxx ../python/itkPyCommand.cxx ) +SWIG_link_libraries( otbApplicationPy3 ${PYTHON3_LIBRARIES} OTBApplicationEngine ) +set_target_properties(_otbApplicationPy3 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR}) +set_target_properties(_otbApplicationPy3 PROPERTIES LIBRARY_OUTPUT_NAME _otbApplication) +if(MSVC) + set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "/wd4005" ) +else() + set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/otbApplicationPYTHON_wrap.cxx COMPILE_FLAGS "-w" ) +endif() + +# byte-compile the resulting python file +add_custom_command( + TARGET _otbApplicationPy3 + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo "Byte-compiling otbApplication.py" + COMMAND ${PYTHON3_EXECUTABLE} + ${CMAKE_SOURCE_DIR}/CMake/PythonCompile.py + ${CMAKE_SWIG_OUTDIR}/otbApplication.py + DEPENDS _otbApplicationPy3 + ) + +otb_module_target_label( _otbApplicationPy3 ) + +install( TARGETS _otbApplicationPy3 + DESTINATION ${OTB_INSTALL_PYTHON3_DIR} + COMPONENT RuntimeLibraries ) + +install( FILES ${CMAKE_SWIG_OUTDIR}/otbApplication.py + DESTINATION ${OTB_INSTALL_PYTHON3_DIR} + COMPONENT RuntimeLibraries ) + +if(PYTHON3_VERSION_STRING VERSION_LESS "3.2.0") + install( FILES ${CMAKE_SWIG_OUTDIR}/otbApplication.pyc + DESTINATION ${OTB_INSTALL_PYTHON3_DIR} + COMPONENT RuntimeLibraries ) +else() + # TODO : pyc file is in a subfolder. +endif() diff --git a/Packaging/CMakeLists.txt b/Packaging/CMakeLists.txt index e2ef02debc16ac329c37da880217b9ab0eb21a2c..e18cd06250102a5a3cfc70f4e77b048b5709642f 100644 --- a/Packaging/CMakeLists.txt +++ b/Packaging/CMakeLists.txt @@ -181,6 +181,11 @@ if(EXISTS "${SUPERBUILD_INSTALL_DIR}/lib/otb/python/_otbApplication${PYMODULE_EX set(HAVE_PYTHON TRUE) endif() +set(HAVE_PYTHON3 FALSE CACHE INTERNAL "HAVE_PYTHON3") +if(EXISTS "${SUPERBUILD_INSTALL_DIR}/lib/otb/python3/_otbApplication${PYMODULE_EXT}") + set(HAVE_PYTHON3 TRUE) +endif() + #only for *nix if(UNIX) file(WRITE ${CMAKE_BINARY_DIR}/make_symlinks "#!/bin/sh\n") diff --git a/Packaging/install_python_bindings.cmake b/Packaging/install_python_bindings.cmake index 451894bb09aa00a844d46f985419cd3955322400..9c62934bb45304c0c967a6a45759440043b149e7 100644 --- a/Packaging/install_python_bindings.cmake +++ b/Packaging/install_python_bindings.cmake @@ -22,6 +22,14 @@ function(install_python_bindings) install(DIRECTORY ${SUPERBUILD_INSTALL_DIR}/lib/otb/python DESTINATION ${PKG_STAGE_DIR}/lib PATTERN "*.pyc" EXCLUDE + PATTERN "__pycache__" EXCLUDE + ) + endif() + if(HAVE_PYTHON3) + install(DIRECTORY ${SUPERBUILD_INSTALL_DIR}/lib/otb/python3 + DESTINATION ${PKG_STAGE_DIR}/lib + PATTERN "*.pyc" EXCLUDE + PATTERN "__pycache__" EXCLUDE ) endif() endfunction() diff --git a/SuperBuild/CMake/External_otb.cmake b/SuperBuild/CMake/External_otb.cmake index 6890d46d7163864f57a6934ab2cf438e8074a903..38a097ddb6fee45a2abb290378ae0b0f8dda65a6 100644 --- a/SuperBuild/CMake/External_otb.cmake +++ b/SuperBuild/CMake/External_otb.cmake @@ -74,7 +74,7 @@ if(OTB_USE_MUPARSERX) ADD_SUPERBUILD_CMAKE_VAR(OTB MUPARSERX_LIBRARY) endif() -if(OTB_WRAP_PYTHON OR OTB_WRAP_JAVA) +if(OTB_WRAP_PYTHON OR OTB_WRAP_JAVA OR OTB_WRAP_PYTHON3) ADD_SUPERBUILD_CMAKE_VAR(OTB SWIG_EXECUTABLE) ADDTO_DEPENDENCIES_IF_NOT_SYSTEM(OTB SWIG) endif() @@ -85,6 +85,12 @@ if(OTB_WRAP_PYTHON) ADD_SUPERBUILD_CMAKE_VAR(OTB PYTHON_EXECUTABLE) endif() +if(OTB_WRAP_PYTHON3) + ADD_SUPERBUILD_CMAKE_VAR(OTB PYTHON3_INCLUDE_DIR) + ADD_SUPERBUILD_CMAKE_VAR(OTB PYTHON3_LIBRARY) + ADD_SUPERBUILD_CMAKE_VAR(OTB PYTHON3_EXECUTABLE) +endif() + if(OTB_USE_QT4) ADDTO_DEPENDENCIES_IF_NOT_SYSTEM(OTB QT4) endif() @@ -186,6 +192,7 @@ ExternalProject_Add(OTB -DOTB_USE_QWT:BOOL=${OTB_USE_QWT} -DOTB_USE_OPENMP:BOOL=${OTB_USE_OPENMP} -DOTB_WRAP_PYTHON:BOOL=${OTB_WRAP_PYTHON} + -DOTB_WRAP_PYTHON3:BOOL=${OTB_WRAP_PYTHON3} -DOTB_WRAP_JAVA:BOOL=${OTB_WRAP_JAVA} ${OTB_ADDITIONAL_CACHE} CMAKE_ARGS ${OTB_SB_CONFIG} diff --git a/SuperBuild/CMakeLists.txt b/SuperBuild/CMakeLists.txt index affdfdb14115e716864c3ac60205785975b13fe6..c9d3106c6aa53f8c179f2b1594d255b793438aee 100644 --- a/SuperBuild/CMakeLists.txt +++ b/SuperBuild/CMakeLists.txt @@ -333,6 +333,7 @@ find_path(OTB_DATA_ROOT README-OTB-Data # SWIG Wrapping option(OTB_WRAP_PYTHON "Enable python wrappers for OTB applications (requires SWIG)" ON) +option(OTB_WRAP_PYTHON3 "Enable python 3 wrappers for OTB applications (requires SWIG)" OFF) option(OTB_WRAP_JAVA "Enable java wrappers for OTB applications (requires SWIG)" OFF) configure_file(${CMAKE_SOURCE_DIR}/CMake/CTestCustom.cmake.in