diff --git a/.gitignore b/.gitignore index d2475db..54d8ba3 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ cmake-build-*/ !.vscode/extensions.json *.swp *~ +CMakeUserPresets.json # OS Generated Files .DS_Store diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ae17ef..3f46a92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,10 @@ if(GENERATOR_IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE) MinSizeRel) endif() +set(ENABLE_CONAN_DEFAULT OFF) # conan-cmake doesn't work with conan 2 +set(ENABLE_CLANG_TIDY_DEFAULT OFF) +set(ENABLE_CPPCHECK_DEFAULT OFF) + include(${_project_options_SOURCE_DIR}/src/DynamicProjectOptions.cmake) # defaulted_project_options sets recommended defaults and provides user and developer @@ -106,13 +110,13 @@ option(ENABLE_LARGE_TESTS "Enable tests and tools that use the Energy+ schema fi add_subdirectory(src) # Adding the tests: -option(ENABLE_TESTING "Enable the tests" ON) -if(ENABLE_TESTING) - enable_testing() - message("Building Tests. Be sure to check out test/constexpr_tests for constexpr -testing") - add_subdirectory(test) -endif() +# option(ENABLE_TESTING "Enable the tests" ON) +# if(ENABLE_TESTING) +# enable_testing() +# message("Building Tests. Be sure to check out test/constexpr_tests for constexpr +# testing") +# add_subdirectory(test) +# endif() option(ENABLE_FUZZING "Enable the fuzz tests" OFF) if(ENABLE_FUZZING) diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000..b072964 --- /dev/null +++ b/conanfile.py @@ -0,0 +1,71 @@ +import os + +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps +from conan.tools.files import copy + +class json2cppRecipe(ConanFile): + name = "json2cpp" + version = "0.0.1" + package_type = "header-library" + + # Optional metadata + license = "MIT" + author = " " + url = "https://github.com/lefticus/json2cpp" + description = "Compiles JSON into static constexpr C++ data structures with nlohmann::json API " + topics = ("json", "constexpr") + + # Binary configuration + settings = "os", "compiler", "build_type", "arch" + + # Sources are located in the same place as this recipe, copy them to the recipe + exports_sources = "CMakeLists.txt", "src/*", "include/*", "test/*", "examples/*" + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires("valijson/1.0", transitive_headers=True, visible=True) + self.requires("spdlog/1.11.0", visible=False) + self.requires("nlohmann_json/3.11.2", visible=False) + self.requires("fmt/9.1.0", visible=False) + self.requires("docopt.cpp/0.6.3", visible=False) + + def build_requirements(self): + self.test_requires("catch2/2.13.10") + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.variables["ENABLE_LARGE_TESTS"] = "OFF" + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + copy( + self, + "LICENSE", + self.source_folder, + self.package_folder + ) + cmake = CMake(self) + cmake.install() + + def package_id(self): + # the package exports a tool and a header-only library, so compiler and + # build type don't matter downstream + self.info.settings.rm_safe("compiler") + self.info.settings.rm_safe("build_type") + + def package_info(self): + self.cpp_info.libs = ["json2cpp"] + + # the project generates its own json2cppConfig.cmake + self.cpp_info.set_property("cmake_find_mode", "none") + self.cpp_info.builddirs.append(os.path.join("lib", "cmake", "json2cpp")) diff --git a/conanfile.txt b/conanfile.txt deleted file mode 100644 index b60b4e3..0000000 --- a/conanfile.txt +++ /dev/null @@ -1,14 +0,0 @@ -# Docs at https://docs.conan.io/en/latest/reference/conanfile_txt.html - -[requires] -catch2/2.13.8 -docopt.cpp/0.6.3 -fmt/8.1.1 -spdlog/1.9.2 -nlohmann_json/3.10.5 -valijson/0.6 -# imgui-sfml/2.5@bincrafters/stable -# sdl2/2.0.1 - -[generators] -cmake_find_package_multi diff --git a/include/json2cpp/json2cpp_adapter.hpp b/include/json2cpp/json2cpp_adapter.hpp index b923a35..c0577d9 100644 --- a/include/json2cpp/json2cpp_adapter.hpp +++ b/include/json2cpp/json2cpp_adapter.hpp @@ -53,9 +53,9 @@ SOFTWARE. #include #include -#include -#include -#include +#include +#include +#include #include namespace valijson { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc83906..80e2467 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,18 +4,51 @@ find_package(docopt CONFIG) find_package(nlohmann_json CONFIG) find_package(ValiJSON CONFIG) +add_library(libjson2cpp INTERFACE + ../include/json2cpp/json2cpp.hpp + ../include/json2cpp/json2cpp_adapter.hpp +) + +target_link_libraries( + libjson2cpp + INTERFACE ValiJSON::valijson) + +target_include_directories( + libjson2cpp + INTERFACE $ + $) + +install(DIRECTORY ../include/json2cpp TYPE INCLUDE) +add_library(json2cpp::libjson2cpp ALIAS libjson2cpp) +install(TARGETS libjson2cpp EXPORT json2cppTargets) +export(EXPORT json2cppTargets NAMESPACE json2cpp::) +configure_file("json2cppConfig.cmake" "." COPYONLY) +include(CMakePackageConfigHelpers) +write_basic_package_version_file(json2cppConfigVersion.cmake COMPATIBILITY SameMajorVersion) + +# installation +install( + EXPORT json2cppTargets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/json2cpp + NAMESPACE json2cpp:: +) + +install(FILES json2cppConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/json2cppConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/json2cpp +) + # Generic test that uses conan libs add_executable(json2cpp main.cpp json2cpp.cpp) target_link_libraries( json2cpp - PRIVATE project_options + PRIVATE libjson2cpp + project_options project_warnings - docopt::docopt + docopt_s fmt::fmt spdlog::spdlog nlohmann_json::nlohmann_json) install(TARGETS json2cpp) -install(DIRECTORY ../include DESTINATION .) if(ENABLE_LARGE_TESTS) set(BASE_NAME "${CMAKE_CURRENT_BINARY_DIR}/schema") @@ -28,15 +61,15 @@ if(ENABLE_LARGE_TESTS) add_executable(schema_validator schema_validator.cpp "${BASE_NAME}.cpp") target_link_libraries( schema_validator - PRIVATE project_options + PRIVATE json2cpp + project_options project_warnings - docopt::docopt + docopt_s fmt::fmt spdlog::spdlog ValiJSON::valijson nlohmann_json::nlohmann_json) - target_include_directories(schema_validator PRIVATE "${CMAKE_SOURCE_DIR}/include") target_include_directories(schema_validator PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") if(MSVC) diff --git a/src/json2cppConfig.cmake b/src/json2cppConfig.cmake new file mode 100644 index 0000000..26af019 --- /dev/null +++ b/src/json2cppConfig.cmake @@ -0,0 +1,5 @@ +include(CMakeFindDependencyMacro) + +find_dependency(ValiJSON CONFIG) + +include(${CMAKE_CURRENT_LIST_DIR}/json2cppTargets.cmake) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 76e7895..c49234b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,9 +55,9 @@ int main(int argc, const char **argv) true,// show help if requested "json2cpp 0.0.1 Copyright 2022 Jason Turner");// version string - std::string document_name = args.at("").asString(); - std::filesystem::path filename = args.at("").asString(); - std::filesystem::path output_filename = args.at("").asString(); + const std::string document_name = args.at("").asString(); + const std::filesystem::path filename = args.at("").asString(); + const std::filesystem::path output_filename = args.at("").asString(); compile_to(document_name, filename, output_filename); diff --git a/test_package/CMakeLists.txt b/test_package/CMakeLists.txt new file mode 100644 index 0000000..46116a7 --- /dev/null +++ b/test_package/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.15) +project(PackageTest CXX) + +find_package(json2cpp CONFIG REQUIRED) + +set(COMPILED_JSON "${CMAKE_CURRENT_BINARY_DIR}/test_json") +add_custom_command( + OUTPUT "${COMPILED_JSON}_impl.hpp" "${COMPILED_JSON}.hpp" "${COMPILED_JSON}.cpp" + COMMAND json2cpp "test_json" "${CMAKE_SOURCE_DIR}/test.json" "${COMPILED_JSON}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") + +add_executable(example src/example.cpp "${COMPILED_JSON}.cpp") +target_link_libraries(example PRIVATE json2cpp::libjson2cpp) +target_include_directories(example PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") diff --git a/test_package/conanfile.py b/test_package/conanfile.py new file mode 100644 index 0000000..27c86c5 --- /dev/null +++ b/test_package/conanfile.py @@ -0,0 +1,34 @@ +import os + +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps +from conan.tools.build import can_run + + +class json2cppTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + + def requirements(self): + self.requires(self.tested_reference_str) + + def build_requirements(self): + self.tool_requires(self.tested_reference_str) + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def layout(self): + cmake_layout(self) + + def test(self): + if can_run(self): + cmd = os.path.join(self.cpp.build.bindir, "example") + self.run(cmd, env="conanrun") diff --git a/test_package/src/example.cpp b/test_package/src/example.cpp new file mode 100644 index 0000000..c296027 --- /dev/null +++ b/test_package/src/example.cpp @@ -0,0 +1,8 @@ +#include "test_json_impl.hpp" + +int main() { + constexpr auto &document = compiled_json::test_json::impl::document; + static_assert(document["glossary"]["title"].get() == "example glossary"); + + return 0; +} diff --git a/test_package/test.json b/test_package/test.json new file mode 100644 index 0000000..080288e --- /dev/null +++ b/test_package/test.json @@ -0,0 +1,25 @@ +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": [ + "GML", + "XML" + ] + }, + "GlossSee": "markup" + } + } + } + } +}