diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 00000000..f0bc9328 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,70 @@ +name: test + +on: [ push, pull_request ] + +jobs: + linux-gcc: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Run vcpkg + uses: lukka/run-vcpkg@v11 + - name: install dependencies + run: | + sudo apt update + sudo apt install -y g++ + - name: Run test + run: | + g++ --version + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DCMAKE_BUILD_TYPE=Release + cmake --build build -j 4 + ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests + ./build/tests/json/reflect-cpp-json-tests + + linux-clang: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + submodules: true + - name: Run vcpkg + uses: lukka/run-vcpkg@v11 + - uses: KyleMayes/install-llvm-action@v1 + with: + version: "16" + - name: link clang lib + run: | + sudo mkdir -p /usr/lib/llvm-16/lib + sudo ln -s clang /usr/lib/llvm-16/lib/clang + working-directory: ${{ env.LLVM_PATH }}/lib + - name: Run test + env: + CC: clang + CXX: clang++ + run: | + clang --version + cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DCMAKE_BUILD_TYPE=Release + cmake --build build -j 4 + ./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests + ./build/tests/json/reflect-cpp-json-tests + + +# The latest MSVC version on GitHub Actions has a bug, and it's difficult to switch to another version. +# Re-enable this when the bug is fixed. + +# windows-msvc: +# runs-on: windows-latest +# steps: +# - uses: actions/checkout@v3 +# with: +# submodules: true +# - uses: ilammy/msvc-dev-cmd@v1 +# - uses: lukka/run-vcpkg@v11 +# - name: Run test +# run: | +# cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON +# cmake --build build -j 4 +# .\build\tests\flexbuffers\Release\reflect-cpp-flexbuffers-tests.exe +# .\build\tests\json\Release\reflect-cpp-json-tests.exe \ No newline at end of file diff --git a/.gitignore b/.gitignore index 02def0a0..c75e7162 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ # Output files *.json *.fb +!vcpkg.json \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..a0a57f3d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vcpkg"] + path = vcpkg + url = https://github.com/microsoft/vcpkg.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a86c810..455205c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,35 @@ -cmake_minimum_required(VERSION 3.4) +cmake_minimum_required(VERSION 3.15) + +option(REFLECTCPP_BUILD_SHARED "Build shared library" OFF) +option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF) + +option(REFLECTCPP_BUILD_TESTS "Build tests" OFF) + + +# enable vcpkg if require features other than JSON +if (REFLECTCPP_FLEXBUFFERS) +set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file") +endif () project(reflectcpp) -include_directories( - "include" -) +set(CMAKE_CXX_STANDARD 20) -add_library(reflectcpp SHARED "src/yyjson.c") -add_library(reflectcpp_static STATIC "src/yyjson.c") +if (REFLECTCPP_BUILD_SHARED) + add_library(reflectcpp SHARED src/yyjson.c) +else () + add_library(reflectcpp STATIC src/yyjson.c) +endif () +target_include_directories(reflectcpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") + +if (REFLECTCPP_FLEXBUFFERS) + find_package(flatbuffers CONFIG REQUIRED) + target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers) +endif () + +target_compile_options(reflectcpp PRIVATE -Wall) + +if (REFLECTCPP_BUILD_TESTS) + add_subdirectory(tests) +endif () \ No newline at end of file diff --git a/README.md b/README.md index 26806ecb..7c1f78d8 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ This increases user experience and developer experience, it makes your code safe For a more in-depth theoretical discussions of these topics, the following books are warmly recommended: -- *Category Theory for Programmers* by Bartosz Milewski (https://github.com/hmemcpy/milewski-ctfp-pdf/releases) +- *Category Theory for Programmers* by Bartosz Milewski (https://github.com/hmemcpy/milewski-ctfp-pdf/releases) - *Domain Modeling Made Functional* by Scott Wlaschin ## Simple Example @@ -218,7 +218,7 @@ Other forms of tagging are supported as well. Refer to the [documentation](https ## Reflective programming -Beyond serialization and deserialization, reflect-cpp also supports reflective programming in general. +Beyond serialization and deserialization, reflect-cpp also supports reflective programming in general. For instance: @@ -301,24 +301,24 @@ const auto c2 = rfl::replace(c, a); reflect-cpp supports the following containers from the C++ standard library: -- `std::array` -- `std::deque` -- `std::forward_list` +- `std::array` +- `std::deque` +- `std::forward_list` - `std::map` - `std::multimap` - `std::multiset` -- `std::list` +- `std::list` - `std::optional` - `std::pair` - `std::set` - `std::shared_ptr` - `std::string` - `std::tuple` -- `std::unique_ptr` +- `std::unique_ptr` - `std::unordered_map` - `std::unordered_multimap` - `std::unordered_multiset` -- `std::unordered_set` +- `std::unordered_set` - `std::variant` - `std::vector` @@ -364,47 +364,65 @@ The following compilers are supported: If you **do not** need JSON support or you want to link YYJSON yourself, then reflect-cpp is header-only. Simply copy the contents of the folder `include` into your source repository or add it to your include path. ### Option 2: Include source files into your own build - Simply copy the contents of the folder `include` into your source repository or add it to your include path and also add `src/yyjson.c` to your source files for compilation. If you need support for other serialization formats like flexbuffers, you should also include and link the respective libraries, as listed in the previous section. -### Option 3: Compilation using cmake +### Option 3: Compilation using cmake -For clang and GCC: - -``` -mkdir build -cd build -cmake .. -make -j4 +```bash +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build -j 4 # gcc, clang +cmake --build build --config Release -j 4 # MSVC ``` -For MSVC replace `make -j4` with: +### Option 4: Compilation using cmake and vcpkg -``` -cmake --build . --config Release +If you want serialization formats other than JSON, you can either install them manually or use vcpkg. + +To install vcpkg: + +```bash +git submodule update --init +./vcpkg/bootstrap-vcpkg.sh # Linux +./vcpkg/bootstrap-vcpkg.bat # Windows +# You may be prompted to install additional dependencies. ``` +To use reflect-cpp in your project: + +```cmake +add_subdirectory(reflect-cpp) # Add this project as a subdirectory + +set(REFLECTCPP_FLEXBUFFERS ON) # Optional + +target_link_libraries(your_project PRIVATE reflectcpp) # Link against the library +``` ## Compiling the tests To compile the tests, do the following: -For clang and GCC: - ``` -cd tests/json -mkdir build -cd build -cmake .. -make -j4 +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DREFLECTCPP_BUILD_TESTS=ON +cmake --build build -j 4 +./build/tests/json/reflect-cpp-json-tests ``` -For MSVC replace `make -j4` with: +To compile the tests with serialization formats other than JSON, do the following: -``` -cmake --build . --config Release +```bash +# bootstrap vcpkg if you haven't done so already +git submodule update --init +./vcpkg/bootstrap-vcpkg.sh # Linux +./vcpkg/bootstrap-vcpkg.bat # Windows +# You may be prompted to install additional dependencies. + +cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DCMAKE_BUILD_TYPE=Release +cmake --build build -j 4 # gcc, clang +cmake --build build --config Release -j 4 # MSVC +./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests +./build/tests/json/reflect-cpp-json-tests ``` diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..5e1d99cc --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,14 @@ +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") + +if (MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++20") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0 -fsanitize=undefined") +endif() + + +add_subdirectory(json) + +if (REFLECTCPP_FLEXBUFFERS) + add_subdirectory(flexbuffers) +endif() \ No newline at end of file diff --git a/tests/flexbuffers/CMakeLists.txt b/tests/flexbuffers/CMakeLists.txt index 27e0b29e..63b3366c 100644 --- a/tests/flexbuffers/CMakeLists.txt +++ b/tests/flexbuffers/CMakeLists.txt @@ -1,56 +1,7 @@ -cmake_minimum_required(VERSION 3.4) +project(reflect-cpp-json-tests) -project(reflect-cpp-tests) +file(GLOB_RECURSE SOURCES "*.cpp") -include_directories( - "../../include" - "../../../flatbuffers/include" -) +add_executable(reflect-cpp-flexbuffers-tests ${SOURCES}) -add_executable( - reflect-cpp-tests - "tests.cpp" - "test_all_of.cpp" - "test_anonymous_fields.cpp" - "test_box.cpp" - "test_custom_class1.cpp" - "test_custom_class2.cpp" - "test_custom_class3.cpp" - "test_custom_constructor.cpp" - "test_deque.cpp" - "test_field_variant.cpp" - "test_flatten_anonymous.cpp" - "test_forward_list.cpp" - "test_list.cpp" - "test_literal.cpp" - "test_map.cpp" - "test_monster_example1.cpp" - "test_monster_example2.cpp" - "test_multimap.cpp" - "test_multiset.cpp" - "test_optional_fields.cpp" - "test_readme_example.cpp" - "test_ref.cpp" - "test_save_load.cpp" - "test_set.cpp" - "test_string_map.cpp" - "test_string_unordered_map.cpp" - "test_tagged_union.cpp" - "test_unique_ptr.cpp" - "test_unique_ptr2.cpp" - "test_unordered_map.cpp" - "test_unordered_multimap.cpp" - "test_unordered_multiset.cpp" - "test_unordered_set.cpp" - "test_variant.cpp" - "../../src/yyjson.c" -) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0 -fsanitize=undefined") - -target_link_libraries( - reflect-cpp-tests - /home/reflectcpp/flatbuffers/build/libflatbuffers.a -) +target_link_libraries(reflect-cpp-flexbuffers-tests PRIVATE reflectcpp) diff --git a/tests/flexbuffers/Dockerfile b/tests/flexbuffers/Dockerfile deleted file mode 100644 index 97f250af..00000000 --- a/tests/flexbuffers/Dockerfile +++ /dev/null @@ -1,25 +0,0 @@ -FROM ubuntu:22.04 - -ENV TZ=Europe/Berlin -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone - -RUN useradd reflectcpp - -RUN apt-get update -RUN apt-get install -y git cmake -RUN apt install gcc-11 g++-11 - -WORKDIR /home/reflectcpp - -RUN git clone https://github.com/google/flatbuffers.git -WORKDIR /home/reflectcpp/flatbuffers -RUN git checkout v23.5.26 -RUN mkdir build -WORKDIR /home/reflectcpp/flatbuffers/build -RUN cmake .. -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 -RUN make - -WORKDIR /home/reflectcpp -COPY compile_on_docker.sh . - -USER reflectcpp diff --git a/tests/flexbuffers/README.md b/tests/flexbuffers/README.md index 1c62f9f5..9acf80e9 100644 --- a/tests/flexbuffers/README.md +++ b/tests/flexbuffers/README.md @@ -17,7 +17,7 @@ Compile the tests: Run the tests: ``` -./build/reflect-cpp-tests +./build/reflect-cpp-flatbuffers-tests ``` To run the scripts, you may have to run `chmod +x build_docker.sh` and `chmod +x compile.sh` first. diff --git a/tests/flexbuffers/build_docker.sh b/tests/flexbuffers/build_docker.sh deleted file mode 100755 index 3db3c555..00000000 --- a/tests/flexbuffers/build_docker.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -docker build --tag=reflectcpp_test_flexbuffers . diff --git a/tests/flexbuffers/compile.sh b/tests/flexbuffers/compile.sh deleted file mode 100755 index e182e01f..00000000 --- a/tests/flexbuffers/compile.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -docker run -v $PWD/../../:"/home/reflectcpp/reflect-cpp" --rm -it -t reflectcpp_test_flexbuffers bash compile_on_docker.sh diff --git a/tests/flexbuffers/compile_on_docker.sh b/tests/flexbuffers/compile_on_docker.sh deleted file mode 100644 index d9b05ed4..00000000 --- a/tests/flexbuffers/compile_on_docker.sh +++ /dev/null @@ -1,5 +0,0 @@ -cd reflect-cpp/tests/flexbuffers -mkdir build -cd build -cmake .. -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 -make -j4 diff --git a/tests/json/CMakeLists.txt b/tests/json/CMakeLists.txt index 1dc15e16..8a32cc38 100644 --- a/tests/json/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -1,86 +1,7 @@ -cmake_minimum_required(VERSION 3.4) +project(reflect-cpp-json-tests) -project(reflect-cpp-tests) +file(GLOB_RECURSE SOURCES "*.cpp") -include_directories( - "../../include" -) - -add_executable( - reflect-cpp-tests - "tests.cpp" - "test_all_of.cpp" - "test_alphanumeric_map.cpp" - "test_alphanumeric_unordered_map.cpp" - "test_anonymous_fields.cpp" - "test_any_of.cpp" - "test_array.cpp" - "test_as.cpp" - "test_as2.cpp" - "test_as_flatten.cpp" - "test_box.cpp" - "test_custom_class1.cpp" - "test_custom_class2.cpp" - "test_custom_class3.cpp" - "test_custom_class4.cpp" - "test_custom_constructor.cpp" - "test_default_values.cpp" - "test_deque.cpp" - "test_email.cpp" - "test_enum.cpp" - "test_error_messages.cpp" - "test_field_variant.cpp" - "test_flag_enum.cpp" - "test_flag_enum_with_int.cpp" - "test_flatten.cpp" - "test_flatten_anonymous.cpp" - "test_forward_list.cpp" - "test_list.cpp" - "test_literal.cpp" - "test_literal_map.cpp" - "test_literal_unordered_map.cpp" - "test_map.cpp" - "test_map_with_key_validation.cpp" - "test_meta_fields.cpp" - "test_monster_example.cpp" - "test_move_replace.cpp" - "test_multimap.cpp" - "test_multiset.cpp" - "test_one_of.cpp" - "test_optional_fields.cpp" - "test_readme_example.cpp" - "test_ref.cpp" - "test_replace.cpp" - "test_replace2.cpp" - "test_replace_flatten.cpp" - "test_replace_flatten2.cpp" - "test_replace_with_other_struct.cpp" - "test_replace_with_other_struct2.cpp" - "test_result.cpp" - "test_save_load.cpp" - "test_set.cpp" - "test_size.cpp" - "test_std_ref.cpp" - "test_string_map.cpp" - "test_string_unordered_map.cpp" - "test_tagged_union.cpp" - "test_tagged_union2.cpp" - "test_timestamp.cpp" - "test_unique_ptr.cpp" - "test_unique_ptr2.cpp" - "test_unordered_map.cpp" - "test_unordered_multimap.cpp" - "test_unordered_multiset.cpp" - "test_unordered_set.cpp" - "test_variant.cpp" - "../../src/yyjson.c" -) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") - -if (MSVC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++20") -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0 -fsanitize=undefined") -endif() +add_executable(reflect-cpp-json-tests ${SOURCES}) +target_link_libraries(reflect-cpp-json-tests PRIVATE reflectcpp) diff --git a/vcpkg b/vcpkg new file mode 160000 index 00000000..50bffcc6 --- /dev/null +++ b/vcpkg @@ -0,0 +1 @@ +Subproject commit 50bffcc62d7f6571eb32bc1a0b1807e77af1166c diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 00000000..c2810a64 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,15 @@ +{ + "name" : "reflectcpp", + "version-string" : "1.0.0", + "builtin-baseline" : "50bffcc62d7f6571eb32bc1a0b1807e77af1166c", + "dependencies" : [ { + "name" : "vcpkg-cmake-config", + "version>=" : "2022-02-06#1" + }, { + "name" : "vcpkg-cmake", + "version>=" : "2023-05-04" + }, { + "name" : "flatbuffers", + "version>=" : "23.5.26#1" + } ] +} \ No newline at end of file