Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings/cpp): init cpp binding #2980

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 79 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ members = [
"bindings/dotnet",
"bindings/ocaml",
"bindings/php",
"bindings/cpp",

"bin/oli",
"bin/oay",
Expand Down
3 changes: 3 additions & 0 deletions bindings/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
compile_commands.json
.cache
build
77 changes: 77 additions & 0 deletions bindings/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.20)
project(opendal-cpp CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CARGO_MANIFEST ${CMAKE_SOURCE_DIR}/Cargo.toml)
set(CARGO_TARGET_DIR ${CMAKE_SOURCE_DIR}/../../target)
set(RUST_SOURCE_FILE ${CMAKE_SOURCE_DIR}/src/lib.rs)
set(RUST_BRIDGE_CPP ${CARGO_TARGET_DIR}/cxxbridge/opendal-cpp/src/lib.rs.cc)
set(RUST_LIB ${CARGO_TARGET_DIR}/debug/${CMAKE_STATIC_LIBRARY_PREFIX}opendal_cpp${CMAKE_STATIC_LIBRARY_SUFFIX})
set(CPP_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include ${CARGO_TARGET_DIR}/cxxbridge/opendal-cpp/src)
file(GLOB_RECURSE CPP_SOURCE_FILE src/*.cpp)

add_custom_command(
OUTPUT ${RUST_BRIDGE_CPP} ${RUST_LIB}
COMMAND cargo build --manifest-path ${CARGO_MANIFEST}
DEPENDS ${RUST_SOURCE_FILE}
USES_TERMINAL
COMMENT "Running cargo..."
)

add_library(opendal_cpp STATIC ${CPP_SOURCE_FILE} ${RUST_BRIDGE_CPP})
target_include_directories(opendal_cpp PUBLIC ${CPP_INCLUDE_DIR})
target_link_libraries(opendal_cpp PUBLIC ${RUST_LIB})
set_target_properties(opendal_cpp
PROPERTIES ADDITIONAL_CLEAN_FILES ${CARGO_TARGET_DIR}
)

# Platform-specific test configuration
if(WIN32)
target_link_libraries(opendal_cpp userenv ws2_32 bcrypt)
set_target_properties(
opendal_cpp
PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreadedDLL"
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
)
endif()

# Tests
enable_testing()
find_package(GTest REQUIRED)
file(GLOB_RECURSE TEST_SOURCE_FILE tests/*.cpp)
add_executable(opendal_cpp_test ${TEST_SOURCE_FILE})
target_include_directories(opendal_cpp_test PUBLIC ${CPP_INCLUDE_DIR} ${GTEST_INCLUDE_DIRS})
target_link_libraries(opendal_cpp_test ${GTEST_LDFLAGS} GTest::gtest_main opendal_cpp)
target_compile_options(opendal_cpp_test PRIVATE ${GTEST_CFLAGS})

# Platform-specific test configuration
if(WIN32)
target_link_libraries(opendal_cpp_test userenv ws2_32 bcrypt)
endif()
if(APPLE)
target_link_libraries(opendal_cpp_test "-framework CoreFoundation -framework Security")
endif()

include(GoogleTest)
gtest_discover_tests(opendal_cpp_test)
39 changes: 39 additions & 0 deletions bindings/cpp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

[package]
name = "opendal-cpp"
publish = false
version = "0.1.0"

authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[lib]
crate-type = ["staticlib"]

[dependencies]
opendal.workspace = true
cxx = "1.0"
anyhow = "1.0"

[build-dependencies]
cxx-build = "1.0"
36 changes: 36 additions & 0 deletions bindings/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# OpenDAL CPP Binding (WIP)

![](https://github.com/apache/incubator-opendal/assets/5351546/87bbf6e5-f19e-449a-b368-3e283016c887)

## Example

```cpp
#include "opendal.hpp"
#include <vector>

int main() {
auto op = opendal::Operator("memory");
std::vector<uint8_t> data = {1, 2, 3, 4, 5};
op.write("test", data);
auto result = op.read("test"); // result == data
}
```

## Build

```bash
mkdir build
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved

# Add -DCMAKE_EXPORT_COMPILE_COMMANDS=1 to generate compile_commands.json for clangd
cmake -DCMAKE_BUILD_TYPE=Debug -GNinja ..

ninja
```

## Test

You should build the project first. Then run:

```bash
ninja test
```
22 changes: 22 additions & 0 deletions bindings/cpp/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.

fn main() {
let _ = cxx_build::bridge("src/lib.rs");

println!("cargo:rerun-if-changed=src/lib.rs");
}
53 changes: 53 additions & 0 deletions bindings/cpp/include/opendal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#pragma once
#include "lib.rs.h"

#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

namespace opendal {

class Operator : std::enable_shared_from_this<Operator> {
public:
Operator() = default;
Operator(std::string_view scheme,
const std::unordered_map<std::string, std::string> &config = {});

// Disable copy and assign
Operator(const Operator &) = delete;
Operator &operator=(const Operator &) = delete;

// Enable move
Operator(Operator &&) = default;
Operator &operator=(Operator &&) = default;
~Operator() = default;

bool available() const;
std::vector<uint8_t> read(std::string_view path);
void write(std::string_view path, const std::vector<uint8_t> &data);

private:
std::optional<rust::Box<opendal::ffi::Operator>> operator_;
};

} // namespace opendal