diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 9054a717..4ac588da 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -87,7 +87,7 @@ set(ALL_SOURCE_LIST wedpr-helper/libhelper wedpr-helper/ppc-tools wedpr-storage/ppc-io wedpr-storage/ppc-storage wedpr-transport/ppc-gateway wedpr-transport/ppc-front - wedpr-transport/ppc-http wedpr-transport/ppc-rpc + wedpr-transport/ppc-http wedpr-transport/ppc-rpc wedpr-transport/sdk wedpr-computing/ppc-psi wedpr-computing/ppc-mpc wedpr-computing/ppc-pir ${CEM_SOURCE}) if(BUILD_WEDPR_TOOLKIT) @@ -100,7 +100,7 @@ if(BUILD_WEDPR_TOOLKIT) include(swig) message(STATUS "Getting SWIG for Windows: ...DONE") endif() - add_subdirectory(wedpr-toolkit-wrapper) + add_subdirectory(wedpr-transport/sdk-wrapper) endif() if(BUILD_ALL) diff --git a/cpp/cmake/Options.cmake b/cpp/cmake/Options.cmake index bf631af3..9b092e36 100644 --- a/cpp/cmake/Options.cmake +++ b/cpp/cmake/Options.cmake @@ -169,6 +169,7 @@ macro(print_config NAME) message("-- DEMO Enable DEMO ${ENABLE_DEMO}") message("-- BUILD_SDK BUILD SDK ${BUILD_SDK}") message("-- BUILD_UDF BUILD UDF ${BUILD_UDF}") + message("-- BUILD_WEDPR_TOOLKIT BUILD_WEDPR_TOOLKIT ${BUILD_WEDPR_TOOLKIT}") message("-- DEBUG ${DEBUG}") message("------------------------------------------------------------------------") message("") diff --git a/cpp/cmake/TargetSettings.cmake b/cpp/cmake/TargetSettings.cmake index 46735456..519f4d55 100644 --- a/cpp/cmake/TargetSettings.cmake +++ b/cpp/cmake/TargetSettings.cmake @@ -137,5 +137,5 @@ set(BOOST_UNIT_TEST Boost::unit_test_framework) set(WEDPR_TRANSPORT_SDK_TARGET wedpr-transport-sdk) # ==== the swig wrapper ===== -set(WEDPR_PYTHON_TOOLKIT "wedpr_python_toolkit") -set(WEDPR_PYTHON_TOOLKIT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TOOLKIT}) \ No newline at end of file +set(WEDPR_PYTHON_TRANSPORT "wedpr_python_transport") +set(WEDPR_PYTHON_TRANSPORT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TRANSPORT}) \ No newline at end of file diff --git a/cpp/cmake/python.cmake b/cpp/cmake/python.cmake new file mode 100644 index 00000000..8adbf35d --- /dev/null +++ b/cpp/cmake/python.cmake @@ -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) \ No newline at end of file diff --git a/cpp/wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h b/cpp/wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h index 17f4b99d..b48caec7 100644 --- a/cpp/wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h +++ b/cpp/wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h @@ -109,5 +109,10 @@ class MessageOptionalHeaderBuilderImpl : public MessageOptionalHeaderBuilder { return std::make_shared(optionalHeader); } + + MessageOptionalHeader::Ptr build() override + { + return std::make_shared(); + } }; } // namespace ppc::protocol \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk-wrapper/CMakeLists.txt b/cpp/wedpr-transport/sdk-wrapper/CMakeLists.txt new file mode 100644 index 00000000..a56f6cf1 --- /dev/null +++ b/cpp/wedpr-transport/sdk-wrapper/CMakeLists.txt @@ -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() diff --git a/cpp/wedpr-transport/sdk-wrapper/python/CMakeLists.txt b/cpp/wedpr-transport/sdk-wrapper/python/CMakeLists.txt new file mode 100644 index 00000000..1eaab1a7 --- /dev/null +++ b/cpp/wedpr-transport/sdk-wrapper/python/CMakeLists.txt @@ -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: $<$:${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}") \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk-wrapper/python/setup.py.in b/cpp/wedpr-transport/sdk-wrapper/python/setup.py.in new file mode 100644 index 00000000..b2ec003f --- /dev/null +++ b/cpp/wedpr-transport/sdk-wrapper/python/setup.py.in @@ -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='wedpr@webank.com', + cmdclass={'install': CompiledLibInstall} + ) diff --git a/cpp/wedpr-transport/sdk-wrapper/python/swig/wedpr_python_transport.i b/cpp/wedpr-transport/sdk-wrapper/python/swig/wedpr_python_transport.i new file mode 100644 index 00000000..d65250d7 --- /dev/null +++ b/cpp/wedpr-transport/sdk-wrapper/python/swig/wedpr_python_transport.i @@ -0,0 +1,49 @@ +%module wedpr_python_toolkit + +%include +%include +%include +%include +%include + + +%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 +#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 +%} + +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; +%template(SharedBcosError) std::shared_ptr; +%template(SharedMessage) std::shared_ptr; +%template(SharedRouteInfo) std::shared_ptr; +%template(SharedTransport) std::shared_ptr; + +%include "wedpr-transport/sdk/TransportBuilder.h" +%include "wedpr-transport/sdk/Transport.h" +%include "ppc-framework/front/IFront.h" \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/ProTransportImpl.cpp b/cpp/wedpr-transport/sdk/ProTransportImpl.cpp index 223d4c69..1573c0fa 100644 --- a/cpp/wedpr-transport/sdk/ProTransportImpl.cpp +++ b/cpp/wedpr-transport/sdk/ProTransportImpl.cpp @@ -18,16 +18,19 @@ * @date 2024-09-04 */ #include "ProTransportImpl.h" -#include "protobuf/src/v1/MessageImpl.h" +#include "protocol/src/v1/MessageImpl.h" #include "wedpr-protocol/grpc/server/FrontServer.h" using namespace ppc::front; using namespace ppc::protocol; -ProTransportImpl::ProTransportImpl(ppc::Front::FrontConfig::Ptr config) +using namespace ppc::sdk; + + +ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config) : m_config(std::move(config)) { - GrpcServerConfig{config->selfEndPoint()}; - m_server = std::make_shared(GrpcServerConfig); + GrpcServerConfig grpcServerConfig{config->selfEndPoint()}; + m_server = std::make_shared(grpcServerConfig); FrontFactory frontFactory; grpc::ChannelArguments channelConfig; @@ -36,6 +39,9 @@ ProTransportImpl::ProTransportImpl(ppc::Front::FrontConfig::Ptr config) m_front = frontFactory.build(std::make_shared(), std::make_shared(), std::make_shared(), gateway, config); + + auto msgBuilder = + std::make_shared(std::make_shared()); auto frontService = std::make_shared(msgBuilder, m_front); // register the frontService diff --git a/cpp/wedpr-transport/sdk/ProTransportImpl.h b/cpp/wedpr-transport/sdk/ProTransportImpl.h index 628fed53..d32ba9e5 100644 --- a/cpp/wedpr-transport/sdk/ProTransportImpl.h +++ b/cpp/wedpr-transport/sdk/ProTransportImpl.h @@ -29,7 +29,7 @@ class ProTransportImpl : public Transport { public: using Ptr = std::shared_ptr; - ProTransportImpl(ppc::Front::FrontConfig::Ptr config); + ProTransportImpl(ppc::front::FrontConfig::Ptr config); void start() override { @@ -43,6 +43,7 @@ class ProTransportImpl : public Transport } protected: + ppc::front::FrontConfig::Ptr m_config; ppc::protocol::GrpcServer::Ptr m_server; }; } // namespace ppc::sdk \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/Transport.h b/cpp/wedpr-transport/sdk/Transport.h index eddb414f..cf9230fb 100644 --- a/cpp/wedpr-transport/sdk/Transport.h +++ b/cpp/wedpr-transport/sdk/Transport.h @@ -24,6 +24,7 @@ namespace ppc::sdk class Transport { public: + using Ptr = std::shared_ptr; Transport() = default; virtual ~Transport() = default; diff --git a/cpp/wedpr-transport/sdk/TransportBuilder.h b/cpp/wedpr-transport/sdk/TransportBuilder.h index a113310e..f0492822 100644 --- a/cpp/wedpr-transport/sdk/TransportBuilder.h +++ b/cpp/wedpr-transport/sdk/TransportBuilder.h @@ -36,7 +36,7 @@ class TransportBuilder TransportBuilder() = default; virtual ~TransportBuilder() = default; - Transport::Ptr build(SDKMode mode, ppc::Front::FrontConfig::Ptr config, + Transport::Ptr build(SDKMode mode, ppc::front::FrontConfig::Ptr config, ppc::gateway::IGateway::Ptr const& gateway) { switch (mode) @@ -50,7 +50,7 @@ class TransportBuilder return std::make_shared(config); } default: - throw std::exception("Unsupported sdk mode, only support AIR/PRO mode!"); + throw std::runtime_error("Unsupported sdk mode, only support AIR/PRO mode!"); } } }; diff --git a/cpp/wedpr-transport/sdk/TransportImpl.h b/cpp/wedpr-transport/sdk/TransportImpl.h index 47222e4b..c32e20d3 100644 --- a/cpp/wedpr-transport/sdk/TransportImpl.h +++ b/cpp/wedpr-transport/sdk/TransportImpl.h @@ -30,17 +30,17 @@ namespace ppc::sdk class TransportImpl : public Transport { public: - TransportImpl(ppc::Front::FrontConfig::Ptr config, ppc::gateway::IGateway::Ptr const& gateway) + TransportImpl(ppc::front::FrontConfig::Ptr config, ppc::gateway::IGateway::Ptr const& gateway) : m_config(std::move(config)) { - FrontFactory frontFactory; - m_front = frontFactory.build(std::make_shared(), - std::make_shared(), - std::make_shared(), gateway, m_config); + ppc::front::FrontFactory frontFactory; + m_front = frontFactory.build(std::make_shared(), + std::make_shared(), + std::make_shared(), gateway, m_config); } ~TransportImpl() override = default; protected: - ppc::Front::FrontConfig::Ptr m_config; + ppc::front::FrontConfig::Ptr m_config; }; } // namespace ppc::sdk