From daed818b64d739e39ca8883611c6e572e3496cc2 Mon Sep 17 00:00:00 2001 From: Daniel Adler Date: Tue, 6 Aug 2024 12:32:01 +0200 Subject: [PATCH] test/cmake-generators: using cmake script --- .github/workflows/ci.yml | 7 +- .gitlab-ci.yml | 6 +- test/standalone-cmake/cpp-tree/CMakeLists.txt | 51 +++++++++++++ test/standalone-cmake/cpp-tree/src/main.cpp | 32 ++++++++ .../cpp-tree/src/schema/another_monster.fbs | 6 ++ .../cpp-tree/src/schema/monster.fbs | 6 ++ .../cpp-tree/src/schema/person.fbs | 5 ++ .../cpp-tree/src/schema/task.fbs | 6 ++ test/standalone-cmake/test-generators.cmake | 75 +++++++++++++++++++ 9 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 test/standalone-cmake/cpp-tree/CMakeLists.txt create mode 100644 test/standalone-cmake/cpp-tree/src/main.cpp create mode 100644 test/standalone-cmake/cpp-tree/src/schema/another_monster.fbs create mode 100644 test/standalone-cmake/cpp-tree/src/schema/monster.fbs create mode 100644 test/standalone-cmake/cpp-tree/src/schema/person.fbs create mode 100644 test/standalone-cmake/cpp-tree/src/schema/task.fbs create mode 100644 test/standalone-cmake/test-generators.cmake diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a33f5d..ac92617 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,7 +35,12 @@ jobs: #- run: make #- run: make test-depend #- run: make test - - run: make test-cmake GH_RUNNER_OS=${{ runner.os }} + - if: matrix.os == 'windows-2019' + run: cd test/standalone-cmake && cmake -DMULTI="Visual Studio 16 2019" -P test-generators.cmake + - if: matrix.os == 'ubuntu-22.04' + run: cd test/standalone-cmake && cmake -DSINGLE="Unix Makefiles" -P test-generators.cmake + - if: matrix.os == 'macos-12' + run: cd test/standalone-cmake && cmake -DMULTI="Xcode" -P test-generators.cmake #- name: Upload artifact # uses: actions/upload-artifact@v4 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b68fbc6..7fd81a4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -46,14 +46,14 @@ t:cmake:linux-x64: image: name: objectboxio/buildenv-core:2024-07-11 script: - - make test-cmake + - cd test/standalone-cmake && cmake -DAUTO=1 -P test-generators.cmake t:cmake:win-x64: tags: [ windows ] script: - - make test-cmake + - cd test/standalone-cmake && cmake -DMULTI="Visual Studio 17 2022" -P test-generators.cmake t:cmake:mac-x64: tags: [ mac, x64 ] script: - - make test-cmake + - cd test/standalone-cmake && cmake -DAUTO=1 -DMULTI="Xcode" -P test-generators.cmake diff --git a/test/standalone-cmake/cpp-tree/CMakeLists.txt b/test/standalone-cmake/cpp-tree/CMakeLists.txt new file mode 100644 index 0000000..9db7a03 --- /dev/null +++ b/test/standalone-cmake/cpp-tree/CMakeLists.txt @@ -0,0 +1,51 @@ +cmake_minimum_required(VERSION 3.14) + +project("objectbox-test" CXX) + +include(FetchContent) +FetchContent_Declare( + objectbox + GIT_REPOSITORY https://github.com/objectbox/objectbox-c.git + GIT_TAG v4.0.1 +) +FetchContent_MakeAvailable(objectbox) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../../cmake) +find_package(ObjectBoxGenerator 4.0.0 REQUIRED) + +add_executable("objectbox-test" + "src/main.cpp" +) +target_link_libraries("objectbox-test" objectbox) + +set_target_properties("objectbox-test" PROPERTIES + CXX_STANDARD 14 + CXX_STANDARD_REQUIRED YES +) + +option(DO_INSOURCE "" TRUE) + +if (DO_INSOURCE) + add_obx_schema( + TARGET + objectbox-test + SCHEMA_FILES + src/schema/person.fbs + src/schema/task.fbs + src/schema/monster.fbs + src/schema/another_monster.fbs + INSOURCE + CXX_STANDARD 17 + ) +else() + add_obx_schema( + TARGET + objectbox-test + SCHEMA_FILES + src/schema/person.fbs + src/schema/task.fbs + src/schema/monster.fbs + src/schema/another_monster.fbs + ) + target_include_directories(objectbox-test PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/src) +endif() diff --git a/test/standalone-cmake/cpp-tree/src/main.cpp b/test/standalone-cmake/cpp-tree/src/main.cpp new file mode 100644 index 0000000..f61df5a --- /dev/null +++ b/test/standalone-cmake/cpp-tree/src/main.cpp @@ -0,0 +1,32 @@ + +#define OBX_CPP_FILE + + +#include "objectbox.hpp" + +#include "schema/objectbox-model.h" +#include "schema/task.obx.hpp" + +int main(int argc, char* args[]) +{ + // create_obx_model() provided by objectbox-model.h + // obx interface contents provided by objectbox.hpp + obx::Store store(create_obx_model()); + obx::Box box(store); + + Task my_task{}; + my_task.text = "Buy milk"; + obx_id id = box.put(my_task); // Create + + std::unique_ptr task = box.get(id); // Read + if (task) { + task->text += " & some bread"; + box.put(*task); // Update + } + + printf("Your task has ID=%llu, text=%s\n", + id, + box.get(id)->text.c_str()); + + return 0; +} diff --git a/test/standalone-cmake/cpp-tree/src/schema/another_monster.fbs b/test/standalone-cmake/cpp-tree/src/schema/another_monster.fbs new file mode 100644 index 0000000..2f2cdea --- /dev/null +++ b/test/standalone-cmake/cpp-tree/src/schema/another_monster.fbs @@ -0,0 +1,6 @@ +table AnotherMonster { + id: ulong; + name: string; + hp: short; + mana: short; +} diff --git a/test/standalone-cmake/cpp-tree/src/schema/monster.fbs b/test/standalone-cmake/cpp-tree/src/schema/monster.fbs new file mode 100644 index 0000000..681574e --- /dev/null +++ b/test/standalone-cmake/cpp-tree/src/schema/monster.fbs @@ -0,0 +1,6 @@ +table Monster { + id: ulong; + name: string; + hp: short; + mana: short; +} diff --git a/test/standalone-cmake/cpp-tree/src/schema/person.fbs b/test/standalone-cmake/cpp-tree/src/schema/person.fbs new file mode 100644 index 0000000..07a6c45 --- /dev/null +++ b/test/standalone-cmake/cpp-tree/src/schema/person.fbs @@ -0,0 +1,5 @@ +table Person { + id: ulong; + name: string; + age: int; +} diff --git a/test/standalone-cmake/cpp-tree/src/schema/task.fbs b/test/standalone-cmake/cpp-tree/src/schema/task.fbs new file mode 100644 index 0000000..d7d852f --- /dev/null +++ b/test/standalone-cmake/cpp-tree/src/schema/task.fbs @@ -0,0 +1,6 @@ +table Task { + id: ulong; + text: string; + date_created: ulong; + date_finished: ulong; +} diff --git a/test/standalone-cmake/test-generators.cmake b/test/standalone-cmake/test-generators.cmake new file mode 100644 index 0000000..b4273af --- /dev/null +++ b/test/standalone-cmake/test-generators.cmake @@ -0,0 +1,75 @@ +# Specify CMake test projects +set(PROJECTS cpp-flat;cpp-tree) + +# overload execute_process: Exit on cperror code. +function(execute_process) + _execute_process(${ARGV} RESULT_VARIABLE error) + if(error) + message(FATAL_ERROR "") + endif() +endfunction() + +function(configureAndBuild multi generator) + string(REPLACE " " "_" generatorLabel ${generator}) + foreach(project ${PROJECTS}) + set(srcdir ${CMAKE_CURRENT_LIST_DIR}/${project}) + foreach(insource TRUE;FALSE) + if (insource) + set(variant "insource") + set(configureFlags "-DDO_INSOURCE=TRUE") + else() + set(variant "default") + set(configureFlags) + endif() + set(builddir ${CMAKE_CURRENT_LIST_DIR}/build/${generatorLabel}/${project}/${variant}) + # Remove all auto-generated files from sources + file(GLOB_RECURSE auto_generated "*/objectbox-model.*" "*/*.obx.*") + list(FILTER auto_generated EXCLUDE REGEX "/build/") + if (auto_generated) + file(REMOVE ${auto_generated}) + endif() + message(STATUS "-------------------------------------------------------") + message(STATUS "**** Test Generator: ${generator} with test project '${project}' and variant '${variant}'.") + message(STATUS "-------------------------------------------------------") + execute_process(COMMAND ${CMAKE_COMMAND} -S ${srcdir} -B ${builddir} -G ${generator} ${configureFlags}) + if (multi) + execute_process(COMMAND ${CMAKE_COMMAND} --build ${builddir} --config Release) + execute_process(COMMAND ${CMAKE_COMMAND} --build ${builddir} --config Debug) + else() + execute_process(COMMAND ${CMAKE_COMMAND} --build ${builddir}) + endif() + endforeach() + endforeach() +endfunction() + +if (AUTO) + find_program(NINJA ninja) + if(NINJA) + list(APPEND SINGLE "Ninja") + list(APPEND MULTI "Ninja Multi-Config") + endif() + find_program(MAKE make) + if(MAKE) + list(APPEND SINGLE "Unix Makefiles") + endif() +endif() + +foreach(generator ${MULTI}) + configureAndBuild(TRUE ${generator}) +endforeach() + +foreach(generator ${SINGLE}) + configureAndBuild(FALSE ${generator}) +endforeach() + +return() + +# configureAndBuild("Unix Makefiles;Ninja" FALSE) +configureAndBuild("Ninja Multi-Config" TRUE) +return() + +#foreach(SingleGenerators) +#endforeach() + +#foreach(MultiGenerators) +#endforeach() \ No newline at end of file