-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c14d5b
commit 0a7d99f
Showing
14 changed files
with
283 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
if(NOT BUILD_PYTHON) | ||
return() | ||
endif() | ||
|
||
# Use latest UseSWIG module (3.14) and Python3 module (3.18) | ||
cmake_minimum_required(VERSION 3.18) | ||
|
||
# Will need swig | ||
set(CMAKE_SWIG_FLAGS) | ||
find_package(SWIG REQUIRED) | ||
include(UseSWIG) | ||
|
||
if(${SWIG_VERSION} VERSION_GREATER_EQUAL 4) | ||
list(APPEND CMAKE_SWIG_FLAGS "-doxygen") | ||
endif() | ||
|
||
if(UNIX AND NOT APPLE) | ||
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64") | ||
endif() | ||
|
||
# Find Python 3 | ||
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module) | ||
list(APPEND CMAKE_SWIG_FLAGS "-py3" "-DPY3") | ||
|
||
function(search_python_module) | ||
set(options NO_VERSION) | ||
set(oneValueArgs NAME PACKAGE) | ||
set(multiValueArgs "") | ||
cmake_parse_arguments(MODULE | ||
"${options}" | ||
"${oneValueArgs}" | ||
"${multiValueArgs}" | ||
${ARGN} | ||
) | ||
message(STATUS "Searching python module: \"${MODULE_NAME}\"") | ||
if(${MODULE_NO_VERSION}) | ||
execute_process( | ||
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}" | ||
RESULT_VARIABLE _RESULT | ||
ERROR_QUIET | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
) | ||
set(MODULE_VERSION "unknown") | ||
else() | ||
execute_process( | ||
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}; print(${MODULE_NAME}.__version__)" | ||
RESULT_VARIABLE _RESULT | ||
OUTPUT_VARIABLE MODULE_VERSION | ||
ERROR_QUIET | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
) | ||
endif() | ||
if(${_RESULT} STREQUAL "0") | ||
message(STATUS "Found python module: \"${MODULE_NAME}\" (found version \"${MODULE_VERSION}\")") | ||
else() | ||
if(FETCH_PYTHON_DEPS) | ||
message(WARNING "Can't find python module: \"${MODULE_NAME}\", install it using pip...") | ||
execute_process( | ||
COMMAND ${Python3_EXECUTABLE} -m pip install --user ${MODULE_PACKAGE} | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
) | ||
else() | ||
message(FATAL_ERROR "Can't find python module: \"${MODULE_NAME}\", please install it using your system package manager.") | ||
endif() | ||
endif() | ||
endfunction() | ||
|
||
# Find if a python builtin module is available. | ||
# e.g | ||
# search_python_internal_module( | ||
# NAME | ||
# mypy_protobuf | ||
# ) | ||
function(search_python_internal_module) | ||
set(options "") | ||
set(oneValueArgs NAME) | ||
set(multiValueArgs "") | ||
cmake_parse_arguments(MODULE | ||
"${options}" | ||
"${oneValueArgs}" | ||
"${multiValueArgs}" | ||
${ARGN} | ||
) | ||
message(STATUS "Searching python module: \"${MODULE_NAME}\"") | ||
execute_process( | ||
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}" | ||
RESULT_VARIABLE _RESULT | ||
ERROR_QUIET | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
) | ||
if(${_RESULT} STREQUAL "0") | ||
message(STATUS "Found python internal module: \"${MODULE_NAME}\"") | ||
else() | ||
message(FATAL_ERROR "Can't find python internal module \"${MODULE_NAME}\", please install it using your system package manager.") | ||
endif() | ||
endfunction() | ||
|
||
# Look for python module wheel | ||
search_python_module( | ||
NAME setuptools | ||
PACKAGE setuptools) | ||
search_python_module( | ||
NAME wheel | ||
PACKAGE wheel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
project(wedpr-transport-swig VERSION ${VERSION}) | ||
|
||
find_package(SWIG REQUIRED) | ||
include(${SWIG_USE_FILE}) | ||
|
||
# Add subdirectories for each language if desired | ||
option(BUILD_PYTHON "Build Python SWIG module" ON) | ||
if(BUILD_PYTHON) | ||
add_subdirectory(python) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
file(GLOB_RECURSE SRCS *.i) | ||
set_source_files_properties(${SRCS} PROPERTIES CPLUSPLUS ON) | ||
|
||
swig_add_library( | ||
${WEDPR_PYTHON_TRANSPORT} | ||
TYPE MODULE | ||
LANGUAGE python | ||
OUTPUT_DIR ${WEDPR_PYTHON_TRANSPORT_DIR} | ||
SOURCES ${SRCS} | ||
) | ||
|
||
message("#### Python3_INCLUDE_DIRS: ${Python3_INCLUDE_DIRS}") | ||
target_include_directories( | ||
${WEDPR_PYTHON_TRANSPORT} | ||
PRIVATE | ||
../include | ||
${Python3_INCLUDE_DIRS} | ||
) | ||
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON) | ||
|
||
# note: macOS is APPLE and also UNIX ! | ||
if(APPLE) | ||
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} APPEND PROPERTY | ||
LINK_FLAGS "-flat_namespace -undefined suppress" | ||
) | ||
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES | ||
SUFFIX ".so" | ||
INSTALL_RPATH "@loader_path;@loader_path/../../${WEDPR_PYTHON_TRANSPORT}/.libs" | ||
) | ||
elseif(UNIX) | ||
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES | ||
INSTALL_RPATH "$ORIGIN:$ORIGIN/../../${WEDPR_PYTHON_TRANSPORT}/.libs" | ||
) | ||
endif() | ||
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${FRONT_TARGET}) | ||
|
||
SET(LIBRARY_OUTPUT_PATH ${WEDPR_PYTHON_TRANSPORT_DIR}/) | ||
|
||
# Variable PYTHON_LIBRARIES can contains keyword `optimized` | ||
# which won't be interpreted inside a generator expression. | ||
# i.e. we can't use: $<$<PLATFORM_ID:Windows>:${PYTHON_LIBRARIES}> | ||
# see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries | ||
if(MSVC) | ||
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${Python3_LIBRARIES}) | ||
endif() | ||
|
||
# Configure setup.py and copy to output directory | ||
file(GENERATE OUTPUT ${WEDPR_PYTHON_TRANSPORT_DIR}/__init__.py CONTENT "__version__ = \"${PYTHON_TOOLKIT_VERSION}\"\n") | ||
set(SETUP_PY_IN ${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in) | ||
set(SETUP_PY_OUT ${WEDPR_PYTHON_TRANSPORT_DIR}/setup.py) | ||
configure_file(${SETUP_PY_IN} ${SETUP_PY_OUT}) | ||
|
||
message(STATUS "CMAKE_INSTALL_INCLUDEDIR => ${CMAKE_INSTALL_INCLUDEDIR}") | ||
message(STATUS "CMAKE_CURRENT_SOURCE_DIR => ${CMAKE_CURRENT_SOURCE_DIR}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import setuptools.command.install | ||
import shutil | ||
from distutils.sysconfig import get_python_lib | ||
|
||
|
||
class CompiledLibInstall(setuptools.command.install.install): | ||
""" | ||
Specialized install to install to python libs | ||
""" | ||
|
||
def run(self): | ||
""" | ||
Run method called by setup | ||
:return: | ||
""" | ||
# Get filenames from CMake variable | ||
filenames = '${PYTHON_INSTALL_FILES}'.split(';') | ||
|
||
# Directory to install to | ||
install_dir = get_python_lib() | ||
|
||
# Install files | ||
[shutil.copy(filename, install_dir) for filename in filenames] | ||
|
||
|
||
if __name__ == '__main__': | ||
setuptools.setup( | ||
name='wedpr-python-transport', | ||
version='1.0.0-rc1', | ||
packages=['wedpr-python-transport'], | ||
license='Apache License 2.0', | ||
author='wedpr', | ||
author_email='[email protected]', | ||
cmdclass={'install': CompiledLibInstall} | ||
) |
49 changes: 49 additions & 0 deletions
49
cpp/wedpr-transport/sdk-wrapper/python/swig/wedpr_python_transport.i
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
%module wedpr_python_toolkit | ||
|
||
%include <stdint.i> | ||
%include <cpointer.i> | ||
%include <std_vector.i> | ||
%include <std_string.i> | ||
%include <std_shared_ptr.i> | ||
|
||
|
||
%shared_ptr(ppc::front::FrontConfig); | ||
%shared_ptr(bcos::Error); | ||
%shared_ptr(ppc::protocol::Message); | ||
%shared_ptr(ppc::protocol::MessageOptionalHeader) | ||
%shared_ptr(ppc::sdk::Transport); | ||
|
||
%{ | ||
#define SWIG_FILE_WITH_INIT | ||
#include <stdint.h> | ||
#include "wedpr-transport/sdk/TransportBuilder.h" | ||
#include "wedpr-transport/sdk/Transport.h" | ||
#include "ppc-framework/front/IFront.h" | ||
#include "ppc-framework/protocol/RouteType.h" | ||
#include "ppc-framework/front/FrontConfig.h" | ||
#include <bcos-utilities/Error.h> | ||
%} | ||
|
||
namespace ppc::front{ | ||
class FrontConfig; | ||
class IFront; | ||
class FrontImpl; | ||
class FrontBuilderImpl; | ||
class RouteType; | ||
class GatewayEndPoint; | ||
} | ||
|
||
namespace ppc::sdk{ | ||
class Transport; | ||
class TransportBuilder; | ||
} | ||
|
||
%template(SharedFrontConfig) std::shared_ptr<ppc::front::FrontConfig>; | ||
%template(SharedBcosError) std::shared_ptr<bcos::Error>; | ||
%template(SharedMessage) std::shared_ptr<ppc::protocol::Message>; | ||
%template(SharedRouteInfo) std::shared_ptr<ppc::protocol::MessageOptionalHeader>; | ||
%template(SharedTransport) std::shared_ptr<ppc::sdk::Transport>; | ||
|
||
%include "wedpr-transport/sdk/TransportBuilder.h" | ||
%include "wedpr-transport/sdk/Transport.h" | ||
%include "ppc-framework/front/IFront.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.