Skip to content

Commit

Permalink
test/cmake-generators: using cmake script
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-obx committed Aug 6, 2024
1 parent 471969e commit 1375462
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 25 deletions.
23 changes: 14 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ jobs:
path: ./third_party/*
key: ${{ runner.os }}-${{ github.workspace }}-deps-${{ hashFiles('./third_party/*') }}

#- run: make
#- run: make test-depend
#- run: make test
- run: make test-cmake GH_RUNNER_OS=${{ runner.os }}
- run: make
- run: make test-depend
- run: make test
- if: matrix.os == 'windows-2019'
run: cd test/standalone-cmake && cmake -DMULTI="Visual Studio 16 2019" -P test-cmake-generators.cmake
- if: matrix.os == 'ubuntu-22.04'
run: cd test/standalone-cmake && cmake -DSINGLE="Unix Makefiles" -P test-cmake-generators.cmake
- if: matrix.os == 'macos-12'
run: cd test/standalone-cmake && cmake -DMULTI="Xcode" -P test-cmake-generators.cmake

#- name: Upload artifact
# uses: actions/upload-artifact@v4
# with:
# name: objectbox-generator-${{ runner.os }}
# path: objectbox-generator*
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: objectbox-generator-${{ runner.os }}
path: objectbox-generator*
6 changes: 3 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-cmake-generators.cmake

t:cmake:win-x64:
tags: [ windows ]
script:
- make test-cmake
- cd test/standalone-cmake && cmake -DMULTI="Visual Studio 17 2022" -P test-cmake-generators.cmake

t:cmake:mac-x64:
tags: [ mac, x64 ]
script:
- make test-cmake
- cd test/standalone-cmake && cmake -DAUTO=1 -DMULTI="Xcode" -P test-cmake-generators.cmake
19 changes: 19 additions & 0 deletions test/standalone-cmake/common.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
option(DO_INSOURCE "Generate files in-source" FALSE)
option(NO_PREBUILT "Use pre-built generator" FALSE)
include(FetchContent)
FetchContent_Declare(
objectbox
GIT_REPOSITORY https://github.com/objectbox/objectbox-c.git
GIT_TAG v4.0.1
)
FetchContent_MakeAvailable(objectbox)

# Use find module from source tree.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../cmake)

# Use pre-built ObjectBox Generator.
if(NOT NO_PREBUILT)
set(ObjectBoxGenerator_ROOT ${CMAKE_CURRENT_LIST_DIR}/../..)
endif()

find_package(ObjectBoxGenerator 4.0.0 REQUIRED)
15 changes: 2 additions & 13 deletions test/standalone-cmake/cpp-flat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
cmake_minimum_required(VERSION 3.14)

project("objectbox-generator-cpp-flat-layout" CXX)
project("objectbox-generator-cmake-cpp-flat" 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)
include(../common.cmake)

add_executable(objectbox-test main.cpp)
target_link_libraries(objectbox-test objectbox)
Expand All @@ -21,8 +12,6 @@ set_target_properties(objectbox-test PROPERTIES
CXX_STANDARD_REQUIRED YES
)

option(DO_INSOURCE "" FALSE)

if (DO_INSOURCE)
add_obx_schema(
TARGET
Expand Down
39 changes: 39 additions & 0 deletions test/standalone-cmake/cpp-tree/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.14)

project("objectbox-test" CXX)

include(../common.cmake)

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
)

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
)
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()
32 changes: 32 additions & 0 deletions test/standalone-cmake/cpp-tree/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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<Task> box(store);

Task my_task{};
my_task.text = "Buy milk";
obx_id id = box.put(my_task); // Create

std::unique_ptr<Task> 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;
}
6 changes: 6 additions & 0 deletions test/standalone-cmake/cpp-tree/src/schema/another_monster.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
table AnotherMonster {
id: ulong;
name: string;
hp: short;
mana: short;
}
6 changes: 6 additions & 0 deletions test/standalone-cmake/cpp-tree/src/schema/monster.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
table Monster {
id: ulong;
name: string;
hp: short;
mana: short;
}
5 changes: 5 additions & 0 deletions test/standalone-cmake/cpp-tree/src/schema/person.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
table Person {
id: ulong;
name: string;
age: int;
}
6 changes: 6 additions & 0 deletions test/standalone-cmake/cpp-tree/src/schema/task.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
table Task {
id: ulong;
text: string;
date_created: ulong;
date_finished: ulong;
}
91 changes: 91 additions & 0 deletions test/standalone-cmake/test-cmake-generators.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# CMake script to test cmake find module with different CMake generators/toolchains.
#
# Usage: cmake
# [ -DAUTO=1 ]
# [ -DSINGLE="gen1;gen2;..." ]
# [ -DMULTI="gen1;gen2" ]
# [ -DNO_PREBUILT=1 ]
# -P test-cmake-generators.cmake
#
# If AUTO is 1, Ninja/Make tools will be auto-detected.
# MULTI specifies Multi-Config Generators such as "Xcode". (Release and Debug builds will be built)
# SINGLE specifies Single-Config Generators.
# NO_PREBUILT enforces not to look for local prebuilt generators but always download.
#
# Common to all CMake test projects:
# Boilerplate code for fetching objectbox but using current source-tree find module available from "common.cmake".
# Check for DO_INSOURCE to run add_obx_schema with INSOURCE.

# Declare CMake test project directories which should be tested.
set(PROJECTS cpp-flat) #;cpp-tree)

if(NO_PREBUILT)
set(extraFlags "-DNO_PREBUILT=1")
endif()

# overload execute_process: Exit on cperror code.
function(execute_process)
_execute_process(${ARGV} RESULT_VARIABLE error)
if(error)
message(FATAL_ERROR "")
endif()
endfunction()

# configureAndBuild all PROJECTS using two variants: default and insource.
# If multi is true, build Release and Debug configurations.
# generator is passed as is to cmake via -G .
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} ${extraFlags})
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)
# Auto-detect Generators on Path
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()

# Run Multi-Config Generators
foreach(generator ${MULTI})
configureAndBuild(TRUE ${generator})
endforeach()

# Run Single-Config Generators
foreach(generator ${SINGLE})
configureAndBuild(FALSE ${generator})
endforeach()

0 comments on commit 1375462

Please sign in to comment.