Skip to content

Commit

Permalink
add java swig (#19)
Browse files Browse the repository at this point in the history
* add java swig

* add java binding package
  • Loading branch information
cyjseagull authored Sep 4, 2024
1 parent 0a7d99f commit 0fba05a
Show file tree
Hide file tree
Showing 25 changed files with 904 additions and 23 deletions.
1 change: 0 additions & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ if(BUILD_WEDPR_TOOLKIT)
# fetch the python dependencies
option(FETCH_PYTHON_DEPS "Install python required modules if not available" ON)
message(STATUS "Python fetch dependencies: ${FETCH_PYTHON_DEPS}")
include(python)
if(WIN32)
message(STATUS "Getting SWIG for Windows: ...")
include(swig)
Expand Down
5 changes: 4 additions & 1 deletion cpp/cmake/TargetSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@ set(WEDPR_TRANSPORT_SDK_TARGET wedpr-transport-sdk)

# ==== the swig wrapper =====
set(WEDPR_PYTHON_TRANSPORT "wedpr_python_transport")
set(WEDPR_PYTHON_TRANSPORT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TRANSPORT})
set(WEDPR_PYTHON_TRANSPORT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TRANSPORT})

set(WEDPR_JAVA_TRANSPORT "wedpr_java_transport")
set(WEDPR_JAVA_TRANSPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/java/${WEDPR_JAVA_TRANSPORT})
16 changes: 16 additions & 0 deletions cpp/cmake/java.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
if(NOT BUILD_JAVA)
return()
endif()

# 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()
2 changes: 1 addition & 1 deletion cpp/ppc-framework/front/IFront.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class IFront : public virtual IFrontClient
long timeout) = 0;

virtual ppc::protocol::Message::Ptr pop(std::string const& topic, long timeoutMs) = 0;
virtual ppc::protocol::Message::Ptr peek(std::string const& topic);
virtual ppc::protocol::Message::Ptr peek(std::string const& topic) = 0;


/**
Expand Down
2 changes: 1 addition & 1 deletion cpp/wedpr-protocol/grpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ foreach(proto_file ${MESSAGES_PROTOS})
endforeach()

add_library(${SERVICE_PB_TARGET} ${GRPC_MESSAGES_SRCS})
target_link_libraries(${SERVICE_PB_TARGET} PUBLIC ${PB_PROTOCOL_TARGET} gRPC::grpc++_unsecure)
target_link_libraries(${SERVICE_PB_TARGET} PUBLIC ${PB_PROTOCOL_TARGET} gRPC::grpc++_unsecure gRPC::grpc++_reflection)

add_subdirectory(client)
add_subdirectory(server)
2 changes: 1 addition & 1 deletion cpp/wedpr-protocol/grpc/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
file(GLOB_RECURSE SRCS *.cpp)
add_library(${SERVICE_CLIENT_TARGET} ${SRCS})
target_link_libraries(${SERVICE_CLIENT_TARGET} PUBLIC ${SERVICE_PB_TARGET} ${PB_PROTOCOL_TARGET})
target_link_libraries(${SERVICE_CLIENT_TARGET} PUBLIC ${SERVICE_PB_TARGET} ${PROTOCOL_TARGET} ${PB_PROTOCOL_TARGET})
2 changes: 1 addition & 1 deletion cpp/wedpr-protocol/grpc/server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
file(GLOB_RECURSE SRCS *.cpp)
add_library(${SERVICE_SERVER_TARGET} ${SRCS})
target_link_libraries(${SERVICE_SERVER_TARGET} PUBLIC ${SERVICE_PB_TARGET} ${PB_PROTOCOL_TARGET})
target_link_libraries(${SERVICE_SERVER_TARGET} PUBLIC ${SERVICE_PB_TARGET} ${PROTOCOL_TARGET} ${PB_PROTOCOL_TARGET})
1 change: 1 addition & 0 deletions cpp/wedpr-protocol/protobuf/NodeInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "NodeInfo.pb.h"
#include "ppc-framework/protocol/INodeInfo.h"
#include <memory>

namespace ppc::protocol
{
// the node information
Expand Down
2 changes: 1 addition & 1 deletion cpp/wedpr-protocol/tars/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ target_include_directories(${TARS_PROTOCOL_TARGET} PUBLIC
$<BUILD_INTERFACE:${TARSCPP_ROOT}/include>
$<BUILD_INTERFACE:${TARS_HEADER_DIR}>
$<INSTALL_INTERFACE:include/wedpr-protocol/tars>)
target_link_libraries(${TARS_PROTOCOL_TARGET} PUBLIC ${BCOS_UTILITIES_TARGET} tarscpp::tarsservant tarscpp::tarsutil)
target_link_libraries(${TARS_PROTOCOL_TARGET} PUBLIC ${BCOS_UTILITIES_TARGET} tarscpp::tarsutil)

# ut
if (TESTS)
Expand Down
2 changes: 1 addition & 1 deletion cpp/wedpr-transport/ppc-front/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ aux_source_directory(ppc-front SRCS)


add_library(${FRONT_TARGET} ${SRCS})
target_link_libraries(${FRONT_TARGET} PUBLIC ${TARS_PROTOCOL_TARGET} TCMalloc)
target_link_libraries(${FRONT_TARGET} PUBLIC ${TARS_PROTOCOL_TARGET} TBB::tbb TCMalloc)

if (TESTS)
enable_testing()
Expand Down
7 changes: 3 additions & 4 deletions cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#include "FrontImpl.h"

using namespace ppc::front;
using namespace ppc::tools;
using namespace ppc::protocol;

IFront::Ptr build(ppc::protocol::INodeInfoFactory::Ptr nodeInfoFactory,
ppc::protocol::MessagePayloadBuilder::Ptr messageFactory,
ppc::protocol::MessageOptionalHeaderBuilder::Ptr routerInfoBuilder,
IFront::Ptr FrontFactory::build(INodeInfoFactory::Ptr nodeInfoFactory,
MessagePayloadBuilder::Ptr messageFactory, MessageOptionalHeaderBuilder::Ptr routerInfoBuilder,
ppc::gateway::IGateway::Ptr const& gateway, FrontConfig::Ptr config)
{
auto threadPool = std::make_shared<bcos::ThreadPool>("front", config->threadPoolSize());
Expand Down
14 changes: 6 additions & 8 deletions cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
* @date 2024-9-04
*/
#pragma once
#include "ppc-framework/front/IFront.h"
#include "ppc-tools/src/config/PPCConfig.h"
#include "ppc-framework/front/FrontConfig.h"
#include "ppc-framework/protocol/INodeInfo.h"
#include "ppc-framework/front/IFront.h"
#include "ppc-framework/gateway/IGateway.h"
#include "ppc-framework/protocol/INodeInfo.h"

namespace ppc::front
{
Expand All @@ -33,10 +32,9 @@ class FrontFactory
FrontFactory() = default;
virtual ~FrontFactory() = default;

IFront::Ptr build(ppc::protocol::INodeInfoFactory::Ptr nodeInfoFactory,
IFront::Ptr build(ppc::protocol::INodeInfoFactory::Ptr nodeInfoFactory,
ppc::protocol::MessagePayloadBuilder::Ptr messageFactory,
ppc::protocol::MessageOptionalHeaderBuilder::Ptr routerInfoBuilder,
ppc::gateway::IGateway::Ptr const& gateway,
FrontConfig::Ptr config);
ppc::protocol::MessageOptionalHeaderBuilder::Ptr routerInfoBuilder,
ppc::gateway::IGateway::Ptr const& gateway, FrontConfig::Ptr config);
};
}
} // namespace ppc::front
8 changes: 8 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ include(${SWIG_USE_FILE})
# Add subdirectories for each language if desired
option(BUILD_PYTHON "Build Python SWIG module" ON)
if(BUILD_PYTHON)
include(python)
add_subdirectory(python)
endif()

# Add subdirectories for each language if desired
option(BUILD_JAVA "Build JAVA SWIG module" ON)
if(BUILD_JAVA)
include(java)
add_subdirectory(java)
endif()
24 changes: 24 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
file(GLOB_RECURSE SRCS *.i)
set_source_files_properties(${SRCS} PROPERTIES CPLUSPLUS ON)

set(WEDPR_TRANSPORT_PACKAGE "com.webank.wedpr.sdk.jni.transport")

set_property(SOURCE swig/wedpr_java_transport.i PROPERTY COMPILE_OPTIONS
-package ${WEDPR_TRANSPORT_PACKAGE})

swig_add_library(${WEDPR_JAVA_TRANSPORT}
TYPE MODULE
LANGUAGE java
OUTPUT_DIR ${WEDPR_JAVA_TRANSPORT_DIR}
SOURCES ${SRCS})

SET(LIBRARY_OUTPUT_PATH ${WEDPR_JAVA_TRANSPORT_DIR})
message("LIBRARY_OUTPUT_PATH: ${WEDPR_JAVA_TRANSPORT_DIR}")

set_target_properties(${WEDPR_JAVA_TRANSPORT} PROPERTIES
SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON
POSITION_INDEPENDENT_CODE ON)

target_include_directories(${WEDPR_JAVA_TRANSPORT} PRIVATE ${JNI_INCLUDE_DIRS})

target_link_libraries(${WEDPR_JAVA_TRANSPORT} PRIVATE ${WEDPR_TRANSPORT_SDK_TARGET} ${JAVA_LIBRARIES})
199 changes: 199 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/java/bindings/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
plugins {
id 'java'
id 'idea'
id 'java-library'
id 'maven-publish'
//id 'org.ajoberstar.grgit' version '4.1.1'
//id "de.undercouch.download" version "4.1.2"
// id 'com.github.sherter.google-java-format' version '0.9'
}

println("Notice: current gradle version is " + gradle.gradleVersion)
sourceCompatibility = 1.8
targetCompatibility = 1.8

[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'

repositories {
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven { url "https://oss.sonatype.org/service/local/staging/deploy/maven2"}
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
maven {url "https://plugins.gradle.org/m2/"}
mavenCentral()
mavenLocal()
}

// googleJavaFormat {
// toolVersion = '1.7'
// options style: 'AOSP'
// source = sourceSets*.allJava
// include '**/*.java'
// exclude '**/*Test.java'
// exclude '**/Test*.java'
// exclude '**/Mock*.java'
// }

sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/main/resources'
}
}

jmh {
java {
srcDir 'src/jmh/java'
}
resources {
srcDir 'src/jmh/resources'
}

runtimeClasspath += sourceSets.main.runtimeClasspath
compileClasspath += sourceSets.main.compileClasspath

compileClasspath += main.output
runtimeClasspath += main.output
}
}
dependencies {
api ('org.slf4j:slf4j-api:1.7.36')

jmhImplementation 'org.openjdk.jmh:jmh-core:1.36'
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.36'

testImplementation ('org.slf4j:slf4j-log4j12:1.7.36')
testImplementation ('junit:junit:4.13.2')
}

archivesBaseName = 'wedpr-java-transport-jni'
group = 'com.webank.wedpr'
version = '1.0.0'

// Additional attribute definition
ext {
if (!project.hasProperty("ossrhUsername")) {
ossrhUsername="xxx"
}

if (!project.hasProperty("ossrhPassword")) {
ossrhPassword="xxx"
}
}

jar {
exclude '**/*.xml'
exclude '**/*.properties'
/*manifest {
try {
def repo = grgit.open(currentDir: project.rootDir)
if (repo != null) {
def date = new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
def branch = repo.branch.getCurrent().getName()
def commit = repo.head().getAbbreviatedId(40)
attributes(["Implementation-Timestamp": date,
"Git-Branch" : branch,
"Git-Commit" : commit])
}
} catch (Exception e) {
e.printStackTrace()
}
} from sourceSets.main.output*/

doLast {
copy {
from destinationDirectory
into 'dist/apps'
}

copy {
from configurations.runtimeClasspath
into 'dist/lib'
}
}
}

javadoc {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
archiveClassifier = 'sources'
duplicatesStrategy 'exclude'
}
tasks.withType(Copy).all {
duplicatesStrategy 'exclude'
}


task javadocJar(type: Jar) {
from javadoc
archiveClassifier = 'javadoc'
}

task jmh(type: JavaExec, dependsOn: jmhClasses) {
main = 'com.webank.wedpr.sdk.jni.jmh.BenchmarkMain'
classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath
}

publishing {
publications {
mavenJava(MavenPublication) {

artifactId project.name
groupId project.group
version project.version

from components.java
artifact sourcesJar
artifact javadocJar

pom {
name = 'wedpr'
description = 'wedpr java transport jni'
url = 'http://www.fisco-bcos.org'

licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
scm {
connection = 'http://github.com/WeBankBlockchain/wedpr-components.git'
url = 'http://github.com/WeBankBlockchain/wedpr-components.git'
}

developers {
developer {
id = 'zhangsan'
name = 'zhangsan'
email = '[email protected]'
}
}
}
}
}
repositories {
maven {
def releasesRepoURL = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
def snapshotsRepoURL = "https://oss.sonatype.org/content/repositories/snapshots"
allowInsecureProtocol = true
url = !version.endsWith("SNAPSHOT") ? releasesRepoURL : snapshotsRepoURL

credentials {
username 'deployment'
password 'deployment123'
}
}
}

}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
# distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https://mirrors.cloud.tencent.com/gradle/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 0fba05a

Please sign in to comment.