This is an automated email from the ASF dual-hosted git repository.
silver pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/main by this push:
new c5b617780 docs(bindings/cpp): add CONTRIBUTING.md (#2984)
c5b617780 is described below
commit c5b61778071a60149224cac271570c2a530353c8
Author: Mingzhuo Yin <[email protected]>
AuthorDate: Thu Aug 31 16:08:17 2023 +0800
docs(bindings/cpp): add CONTRIBUTING.md (#2984)
* docs(bindings/cpp): add CONTRIBUTING.md
Signed-off-by: silver-ymz <[email protected]>
* add address sanitizer to check memory error
Signed-off-by: silver-ymz <[email protected]>
* fix poes_create.sh php binding
Signed-off-by: silver-ymz <[email protected]>
* download zigup in temp dir
Signed-off-by: silver-ymz <[email protected]>
* disable sandboxing for ocaml
Signed-off-by: silver-ymz <[email protected]>
* remove incorrect space in mktemp
Signed-off-by: silver-ymz <[email protected]>
* add libdl to linked library to fix codespace error
Signed-off-by: silver-ymz <[email protected]>
---------
Signed-off-by: silver-ymz <[email protected]>
---
.devcontainer/post_create.sh | 12 +++--
bindings/cpp/.gitignore | 2 +-
bindings/cpp/CMakeLists.txt | 10 +++-
bindings/cpp/CONTRIBUTING.md | 103 +++++++++++++++++++++++++++++++++++++++
bindings/cpp/include/opendal.hpp | 1 +
bindings/cpp/src/opendal.cpp | 12 ++++-
6 files changed, 131 insertions(+), 9 deletions(-)
diff --git a/.devcontainer/post_create.sh b/.devcontainer/post_create.sh
index 1f9b84a1d..7e7db75fa 100644
--- a/.devcontainer/post_create.sh
+++ b/.devcontainer/post_create.sh
@@ -45,8 +45,9 @@ sudo apt install -y libgtest-dev cmake
# Setup for Zig binding
sudo apt install -y wget
-wget -q
https://github.com/marler8997/zigup/releases/download/v2023_07_27/zigup.ubuntu-latest-x86_64.zip
-sudo unzip -o zigup.ubuntu-latest-x86_64.zip -d /usr/bin
+tmp=$(mktemp -d)
+wget -q
https://github.com/marler8997/zigup/releases/download/v2023_07_27/zigup.ubuntu-latest-x86_64.zip
-O $tmp/zigup.ubuntu-latest-x86_64.zip
+sudo unzip -o $tmp/zigup.ubuntu-latest-x86_64.zip -d /usr/bin
sudo chmod +x /usr/bin/zigup
sudo zigup 0.11.0
@@ -55,7 +56,7 @@ sudo apt install -y ghc cabal-install
cabal update
# Setup for PHP binding
-sudo apt install software-properties-common
+sudo apt install -y software-properties-common
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee
/etc/apt/sources.list.d/sury-php.list
wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
sudo apt update -y
@@ -63,5 +64,8 @@ sudo apt install -y php8.2 php8.2-dev
# Setup for OCaml binding
sudo apt install -y opam
-opam init --auto-setup --yes
+opam init --auto-setup --yes --disable-sandboxing # container can't use
sandboxing
opam install -y dune ounit2 ocamlformat
+
+# Setup for Cpp binding
+sudo apt install -y ninja-build
\ No newline at end of file
diff --git a/bindings/cpp/.gitignore b/bindings/cpp/.gitignore
index c1d315b0e..20ded089e 100644
--- a/bindings/cpp/.gitignore
+++ b/bindings/cpp/.gitignore
@@ -1,3 +1,3 @@
compile_commands.json
.cache
-build
\ No newline at end of file
+build*
\ No newline at end of file
diff --git a/bindings/cpp/CMakeLists.txt b/bindings/cpp/CMakeLists.txt
index b8b5abc1d..429fbc18f 100644
--- a/bindings/cpp/CMakeLists.txt
+++ b/bindings/cpp/CMakeLists.txt
@@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
-cmake_minimum_required(VERSION 3.20)
+cmake_minimum_required(VERSION 3.10)
project(opendal-cpp CXX)
set(CMAKE_CXX_STANDARD 17)
@@ -39,7 +39,7 @@ add_custom_command(
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})
+target_link_libraries(opendal_cpp PUBLIC ${RUST_LIB} ${CMAKE_DL_LIBS})
set_target_properties(opendal_cpp
PROPERTIES ADDITIONAL_CLEAN_FILES ${CARGO_TARGET_DIR}
)
@@ -65,6 +65,12 @@ target_include_directories(opendal_cpp_test PUBLIC
${CPP_INCLUDE_DIR} ${GTEST_IN
target_link_libraries(opendal_cpp_test ${GTEST_LDFLAGS} GTest::gtest_main
opendal_cpp)
target_compile_options(opendal_cpp_test PRIVATE ${GTEST_CFLAGS})
+# enable address sanitizers
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES
"GNU")
+ target_compile_options(opendal_cpp_test PRIVATE
-fsanitize=leak,address,undefined -fno-omit-frame-pointer -fno-common -O1)
+ target_link_options(opendal_cpp_test PRIVATE
-fsanitize=leak,address,undefined)
+endif()
+
# Platform-specific test configuration
if(WIN32)
target_link_libraries(opendal_cpp_test userenv ws2_32 bcrypt)
diff --git a/bindings/cpp/CONTRIBUTING.md b/bindings/cpp/CONTRIBUTING.md
new file mode 100644
index 000000000..f6a934c94
--- /dev/null
+++ b/bindings/cpp/CONTRIBUTING.md
@@ -0,0 +1,103 @@
+# Contributing
+
+- [Contributing](#contributing)
+ - [Setup](#setup)
+ - [Using a dev container environment](#using-a-dev-container-environment)
+ - [Bring your own toolbox](#bring-your-own-toolbox)
+ - [Build](#build)
+ - [Test](#test)
+
+## Setup
+
+### Using a dev container environment
+
+OpenDAL provides a pre-configured [dev container](https://containers.dev/)
that could be used in [Github
Codespaces](https://github.com/features/codespaces),
[VSCode](https://code.visualstudio.com/),
[JetBrains](https://www.jetbrains.com/remote-development/gateway/),
[JuptyerLab](https://jupyterlab.readthedocs.io/en/stable/). Please pick up your
favourite runtime environment.
+
+The fastest way is:
+
+[](https://codespaces.new/apache/incubator-opendal?quickstart=1&machine=standardLinux32gb)
+
+### Bring your own toolbox
+
+To build OpenDAL C++ binding, the following is all you need:
+
+- **C++ toolchain** that supports **c++17**, _e.g._ clang++ and g++
+
+- **CMake**. It is used to generate the build system.
+
+- **Ninja**. It is used to build the project. You can also use other build
systems supported by CMake, _e.g._ make, bazel, etc. You just need to make
corresponding changes to following commands.
+
+- To format the code, you need to install **clang-format**
+
+- **GTest(Google Test)**. It is used to run the tests. To see how to build,
check [here](https://github.com/google/googletest).
+
+For Ubuntu and Debian:
+
+```shell
+# install C/C++ toolchain (can be replaced by other toolchains)
+sudo apt install build-essential
+
+# install CMake and Ninja
+sudo apt install cmake ninja-build
+
+# install clang-format
+sudo apt install clang-format
+
+# install and build GTest library under /usr/lib and softlink to /usr/local/lib
+sudo apt-get install libgtest-dev
+cd /usr/src/gtest
+sudo cmake CMakeLists.txt
+sudo make
+sudo cp lib/*.a /usr/lib
+sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a
+sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
+```
+
+For MacOS:
+
+```shell
+# install C/C++ toolchain (can be replaced by other toolchains)
+xcode-select --install
+
+# install CMake and Ninja
+brew install cmake ninja
+
+# install clang-format
+brew install clang-format
+
+# install GTest library
+brew install googletest
+```
+
+## Build
+
+To build the library and header file.
+
+```shell
+mkdir build
+cd build
+
+# Add -DCMAKE_EXPORT_COMPILE_COMMANDS=1 to generate compile_commands.json for
clangd
+cmake -DCMAKE_BUILD_TYPE=Debug -GNinja ..
+
+ninja
+```
+
+- The header file `opendal.hpp` is under `./include`.
+- The library is under `build` after building.
+
+To clean the build results.
+
+```shell
+ninja clean
+```
+
+## Test
+
+To build and run the tests. (Note that you need to install GTest)
+
+You should build the project first. Then run:
+
+```shell
+ninja test
+```
diff --git a/bindings/cpp/include/opendal.hpp b/bindings/cpp/include/opendal.hpp
index 2d42b8390..6fd4c2379 100644
--- a/bindings/cpp/include/opendal.hpp
+++ b/bindings/cpp/include/opendal.hpp
@@ -20,6 +20,7 @@
#pragma once
#include "lib.rs.h"
+#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
diff --git a/bindings/cpp/src/opendal.cpp b/bindings/cpp/src/opendal.cpp
index 92c240c3e..49a1fa227 100644
--- a/bindings/cpp/src/opendal.cpp
+++ b/bindings/cpp/src/opendal.cpp
@@ -38,8 +38,16 @@ Operator::Operator(std::string_view scheme,
bool Operator::available() const { return operator_.has_value(); }
std::vector<uint8_t> Operator::read(std::string_view path) {
- auto res = operator_.value()->read(rust::Str(path.data()));
- return std::vector<uint8_t>(res.data(), res.data() + res.size());
+ auto rust_vec = operator_.value()->read(rust::Str(path.data()));
+
+ // Convert rust::Vec<uint8_t> to std::vector<uint8_t>
+ // This cannot use rust vector pointer to init std::vector because
+ // rust::Vec owns the memory and will free it when it goes out of scope.
+ std::vector<uint8_t> res;
+ res.reserve(rust_vec.size());
+ std::copy(rust_vec.cbegin(), rust_vec.cend(), std::back_inserter(res));
+
+ return res;
}
void Operator::write(std::string_view path, const std::vector<uint8_t> &data) {