Skip to content

Add conan 2 recipe that exports tool and library #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cmake-build-*/
!.vscode/extensions.json
*.swp
*~
CMakeUserPresets.json

# OS Generated Files
.DS_Store
Expand Down
18 changes: 11 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
71 changes: 71 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -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 = "<Put your name here> <And your email here>"
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"))
14 changes: 0 additions & 14 deletions conanfile.txt

This file was deleted.

6 changes: 3 additions & 3 deletions include/json2cpp/json2cpp_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ SOFTWARE.
#include <string>

#include <utility>
#include <valijson/adapters/adapter.hpp>
#include <valijson/adapters/basic_adapter.hpp>
#include <valijson/adapters/frozen_value.hpp>
#include <valijson/internal/adapter.hpp>
#include <valijson/internal/basic_adapter.hpp>
#include <valijson/internal/frozen_value.hpp>
#include <valijson/exceptions.hpp>

namespace valijson {
Expand Down
45 changes: 39 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<INSTALL_INTERFACE:include>)

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")
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/json2cppConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include(CMakeFindDependencyMacro)

find_dependency(ValiJSON CONFIG)

include(${CMAKE_CURRENT_LIST_DIR}/json2cppTargets.cmake)
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("<document_name>").asString();
std::filesystem::path filename = args.at("<file_name>").asString();
std::filesystem::path output_filename = args.at("<output_base_name>").asString();
const std::string document_name = args.at("<document_name>").asString();
const std::filesystem::path filename = args.at("<file_name>").asString();
const std::filesystem::path output_filename = args.at("<output_base_name>").asString();

compile_to(document_name, filename, output_filename);

Expand Down
14 changes: 14 additions & 0 deletions test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
34 changes: 34 additions & 0 deletions test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -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")
8 changes: 8 additions & 0 deletions test_package/src/example.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string>() == "example glossary");

return 0;
}
25 changes: 25 additions & 0 deletions test_package/test.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}