diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7d9b2fa3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# CMake +.ninja_deps +.ninja_log +CMakeCache.txt +CMakeFiles/ +Makefile +build.ninja +cmake_install.cmake +rules.ninja + +# Build products +librealm-object-store.dylib +tests/tests diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..44b8f2ab --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "external/pegtl"] + path = external/pegtl + url = https://github.com/ColinH/PEGTL +[submodule "external/catch"] + path = external/catch + url = https://github.com/philsquared/Catch diff --git a/CMake/CompilerFlags.cmake b/CMake/CompilerFlags.cmake new file mode 100644 index 00000000..25d78a77 --- /dev/null +++ b/CMake/CompilerFlags.cmake @@ -0,0 +1,15 @@ +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED on) +set(CMAKE_CXX_EXTENSIONS off) +set(CMAKE_BUILD_TYPE CACHE STRING Debug) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") + +if(${CMAKE_GENERATOR} STREQUAL "Ninja") + if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcolor-diagnostics") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics") + elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdiagnostics-color=always") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always") + endif() +endif() diff --git a/CMake/RealmCore.cmake b/CMake/RealmCore.cmake new file mode 100644 index 00000000..daf83eba --- /dev/null +++ b/CMake/RealmCore.cmake @@ -0,0 +1,43 @@ +include(ExternalProject) + +function(download_realm_core core_version) + set(core_url "https://static.realm.io/downloads/core/realm-core-${core_version}.tar.bz2") + set(core_tarball_name "realm-core-${core_version}.tar.bz2") + set(core_temp_tarball "/tmp/${core_tarball_name}") + set(core_directory_parent "${CMAKE_CURRENT_SOURCE_DIR}${CMAKE_FILES_DIRECTORY}") + set(core_directory "${core_directory_parent}/realm-core-${core_version}") + set(core_tarball "${core_directory_parent}/${core_tarball_name}") + + if (NOT EXISTS ${core_tarball}) + if (NOT EXISTS ${core_temp_tarball}) + message("Downloading core ${core_version} from ${core_url}.") + file(DOWNLOAD ${core_url} ${core_temp_tarball}.tmp SHOW_PROGRESS) + file(RENAME ${core_temp_tarball}.tmp ${core_temp_tarball}) + endif() + file(COPY ${core_temp_tarball} DESTINATION ${core_directory_parent}) + endif() + + set(core_library_debug ${core_directory}/librealm-dbg.a) + set(core_library_release ${core_directory}/librealm.a) + set(core_libraries ${core_library_debug} ${core_library_release}) + + add_custom_command( + COMMENT "Extracting ${core_tarball_name}" + OUTPUT ${core_libraries} + DEPENDS ${core_temp_tarball} + COMMAND ${CMAKE_COMMAND} -E copy ${core_temp_tarball} ${core_directory_parent} + COMMAND ${CMAKE_COMMAND} -E tar xf ${core_tarball} + COMMAND ${CMAKE_COMMAND} -E remove_directory ${core_directory} + COMMAND ${CMAKE_COMMAND} -E rename core ${core_directory} + COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${core_libraries}) + + add_custom_target(realm-core DEPENDS ${core_libraries}) + + add_library(realm STATIC IMPORTED) + add_dependencies(realm realm-core) + set_property(TARGET realm PROPERTY IMPORTED_LOCATION_DEBUG ${core_library_debug}) + set_property(TARGET realm PROPERTY IMPORTED_LOCATION_RELEASE ${core_library_release}) + set_property(TARGET realm PROPERTY IMPORTED_LOCATION ${core_library_release}) + + set(REALM_CORE_INCLUDE_DIR ${core_directory}/include PARENT_SCOPE) +endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..85138132 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +project(realm-object-store) + +cmake_minimum_required(VERSION 3.2.0) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake") + +include(CompilerFlags) + +include(RealmCore) +download_realm_core(0.95.5) + +include_directories(${REALM_CORE_INCLUDE_DIR} src external/pegtl) + +add_subdirectory(src) +add_subdirectory(tests) diff --git a/README.md b/README.md index 30ae7900..948a47e6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Realm Object Store -Cross-platform code used accross bindings. Binding developers can choose to use some or all the included functionality +Cross-platform code used accross bindings. Binding developers can choose to use some or all the included functionality: - `object_store`/`schema`/`object_schema`/`property` - contains the structures and logic used to setup and modify realm files and their schema. - `shared_realm` - wraps the object_store apis to provide transactions, notifications, realm caching, migrations, and other higher level functionality. - `object_accessor`/`results`/`list` - accessor classes, object creation/update pipeline, and helpers for creating platform specific property getters and setters. @@ -8,8 +8,28 @@ Cross-platform code used accross bindings. Binding developers can choose to use ## Building -TBD +The object store's build system currently only suports building for OS X. The object store itself can build for all Apple +platforms when integrated into a binding. + +1. Install CMake. You can download an installer for OS X from the [CMake download page], or install via [Homebrew](http://brew.sh): + ``` + brew install cmake + ``` + +2. Generate build files: + + ``` + cmake . + ``` + +3. Build: + + ``` + make + ``` ## Testing -TBD +``` +make run-tests +``` diff --git a/external/catch b/external/catch new file mode 160000 index 00000000..f294c984 --- /dev/null +++ b/external/catch @@ -0,0 +1 @@ +Subproject commit f294c9847272b1b92c5119a6f711e57113b5f231 diff --git a/external/pegtl b/external/pegtl new file mode 160000 index 00000000..49a5b0a4 --- /dev/null +++ b/external/pegtl @@ -0,0 +1 @@ +Subproject commit 49a5b0a49e154b362ef9cf1e756dd8673ddd4efe diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..61204003 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,22 @@ +include_directories(impl) + +set(SOURCES + index_set.cpp + list.cpp + object_schema.cpp + object_store.cpp + results.cpp + schema.cpp + shared_realm.cpp + impl/transact_log_handler.cpp + parser/parser.cpp + parser/query_builder.cpp) + +if(APPLE) + include_directories(impl/apple) + list(APPEND SOURCES impl/apple/external_commit_helper.cpp) + find_library(CF_LIBRARY CoreFoundation) +endif() + +add_library(realm-object-store SHARED ${SOURCES}) +target_link_libraries(realm-object-store realm ${CF_LIBRARY}) diff --git a/binding_context.hpp b/src/binding_context.hpp similarity index 100% rename from binding_context.hpp rename to src/binding_context.hpp diff --git a/impl/apple/external_commit_helper.cpp b/src/impl/apple/external_commit_helper.cpp similarity index 100% rename from impl/apple/external_commit_helper.cpp rename to src/impl/apple/external_commit_helper.cpp diff --git a/impl/apple/external_commit_helper.hpp b/src/impl/apple/external_commit_helper.hpp similarity index 100% rename from impl/apple/external_commit_helper.hpp rename to src/impl/apple/external_commit_helper.hpp diff --git a/impl/transact_log_handler.cpp b/src/impl/transact_log_handler.cpp similarity index 100% rename from impl/transact_log_handler.cpp rename to src/impl/transact_log_handler.cpp diff --git a/impl/transact_log_handler.hpp b/src/impl/transact_log_handler.hpp similarity index 100% rename from impl/transact_log_handler.hpp rename to src/impl/transact_log_handler.hpp diff --git a/index_set.cpp b/src/index_set.cpp similarity index 100% rename from index_set.cpp rename to src/index_set.cpp diff --git a/index_set.hpp b/src/index_set.hpp similarity index 100% rename from index_set.hpp rename to src/index_set.hpp diff --git a/list.cpp b/src/list.cpp similarity index 100% rename from list.cpp rename to src/list.cpp diff --git a/list.hpp b/src/list.hpp similarity index 100% rename from list.hpp rename to src/list.hpp diff --git a/object_accessor.hpp b/src/object_accessor.hpp similarity index 100% rename from object_accessor.hpp rename to src/object_accessor.hpp diff --git a/object_schema.cpp b/src/object_schema.cpp similarity index 100% rename from object_schema.cpp rename to src/object_schema.cpp diff --git a/object_schema.hpp b/src/object_schema.hpp similarity index 98% rename from object_schema.hpp rename to src/object_schema.hpp index 0ede3f5d..4ede6ce5 100644 --- a/object_schema.hpp +++ b/src/object_schema.hpp @@ -25,7 +25,6 @@ #include namespace realm { - class Property; class Group; struct Property; diff --git a/object_store.cpp b/src/object_store.cpp similarity index 100% rename from object_store.cpp rename to src/object_store.cpp diff --git a/object_store.hpp b/src/object_store.hpp similarity index 100% rename from object_store.hpp rename to src/object_store.hpp diff --git a/parser/parser.cpp b/src/parser/parser.cpp similarity index 100% rename from parser/parser.cpp rename to src/parser/parser.cpp diff --git a/parser/parser.hpp b/src/parser/parser.hpp similarity index 100% rename from parser/parser.hpp rename to src/parser/parser.hpp diff --git a/parser/query_builder.cpp b/src/parser/query_builder.cpp similarity index 100% rename from parser/query_builder.cpp rename to src/parser/query_builder.cpp diff --git a/parser/query_builder.hpp b/src/parser/query_builder.hpp similarity index 100% rename from parser/query_builder.hpp rename to src/parser/query_builder.hpp diff --git a/parser/test.sh b/src/parser/test.sh similarity index 100% rename from parser/test.sh rename to src/parser/test.sh diff --git a/property.hpp b/src/property.hpp similarity index 100% rename from property.hpp rename to src/property.hpp diff --git a/results.cpp b/src/results.cpp similarity index 100% rename from results.cpp rename to src/results.cpp diff --git a/results.hpp b/src/results.hpp similarity index 100% rename from results.hpp rename to src/results.hpp diff --git a/schema.cpp b/src/schema.cpp similarity index 100% rename from schema.cpp rename to src/schema.cpp diff --git a/schema.hpp b/src/schema.hpp similarity index 100% rename from schema.hpp rename to src/schema.hpp diff --git a/shared_realm.cpp b/src/shared_realm.cpp similarity index 100% rename from shared_realm.cpp rename to src/shared_realm.cpp diff --git a/shared_realm.hpp b/src/shared_realm.hpp similarity index 100% rename from shared_realm.hpp rename to src/shared_realm.hpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..a7b20889 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,5 @@ +include_directories(../external/catch/single_include) +add_executable(tests main.cpp parser.cpp) +target_link_libraries(tests realm-object-store) + +add_custom_target(run-tests USES_TERMINAL DEPENDS tests COMMAND ./tests) diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 00000000..0c7c351f --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN +#include "catch.hpp" diff --git a/parser/test.cpp b/tests/parser.cpp similarity index 72% rename from parser/test.cpp rename to tests/parser.cpp index f834c505..0815d3d6 100644 --- a/parser/test.cpp +++ b/tests/parser.cpp @@ -1,10 +1,8 @@ - -#include "parser.hpp" +#include "catch.hpp" +#include "parser/parser.hpp" #include #include -#include -#include static std::vector valid_queries = { // true/false predicates @@ -131,36 +129,16 @@ static std::vector invalid_queries = { "truepredicate & truepredicate", }; -namespace realm { -namespace parser { - -bool test_grammar() -{ - bool success = true; - for (auto &query : valid_queries) { - std::cout << "valid query: " << query << std::endl; - try { - realm::parser::parse(query); - } catch (std::exception &ex) { - std::cout << "FAILURE - " << ex.what() << std::endl; - success = false; - } +TEST_CASE("valid queries") { + for (auto& query : valid_queries) { + INFO("query: " << query); + CHECK_NOTHROW(realm::parser::parse(query)); } +} - for (auto &query : invalid_queries) { - std::cout << "invalid query: " << query << std::endl; - try { - realm::parser::parse(query); - } catch (std::exception &ex) { - // std::cout << "message: " << ex.what() << std::endl; - continue; - } - std::cout << "FAILURE - query should throw an exception" << std::endl; - success = false; +TEST_CASE("invalid queries") { + for (auto& query : invalid_queries) { + INFO("query: " << query); + CHECK_THROWS(realm::parser::parse(query)); } - - return success; -} - -} } diff --git a/parser/queryTests.json b/tests/query.json similarity index 100% rename from parser/queryTests.json rename to tests/query.json