Skip to content

Commit

Permalink
Add conanfile and test_package
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepy9 authored and jorgen committed Sep 13, 2024
1 parent 1fc4672 commit addc61f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ tests/json-tokenizer-partial-test
tests/json-tree-test
tests/json-tree-printer-test

*CMakeUserPresets.json
build/
67 changes: 67 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, cmake_layout
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.scm import Git, Version
from conan.tools.files import copy

import os

class JsonStructLibrary(ConanFile):
name = "json_struct"

# Metadata
license = "MIT"
version = "1.0.0"
author = "Jørgen Lind <[email protected]>"
url = "https://github.com/jorgen/json_struct"
description = "json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa"

topics = ("serialization", "deserialization", "reflection", "json")

settings = "os", "compiler", "build_type", "arch"
pacgake_type = "header-library"
implements = ["auto_header_only"]
exports_sources = "include/*", "cmake/*" "CMakeLists.txt"

options = {
"opt_build_benchmarks": [True, False],
"opt_build_examples": [True, False],
"opt_build_tests": [True, False],
"opt_disable_pch": [True, False],
"opt_install": [True, False],
}

default_options = {
"opt_build_benchmarks": False,
"opt_build_examples": False,
"opt_build_tests": False,
"opt_disable_pch": False,
"opt_install": True,
}

def generate(self):
toolchain = CMakeToolchain(self)

toolchain.variables["JSON_STRUCT_OPT_BUILD_BENCHMARKS"] = self.options.opt_build_benchmarks.value
toolchain.variables["JSON_STRUCT_OPT_BUILD_EXAMPLES"] = self.options.opt_build_examples.value
toolchain.variables["JSON_STRUCT_OPT_BUILD_TESTS"] = self.options.opt_build_tests.value
toolchain.variables["JSON_STRUCT_OPT_DISABLE_PCH"] = self.options.opt_disable_pch.value
toolchain.variables["JSON_STRUCT_OPT_INSTALL"] = self.options.opt_install

toolchain.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
# Invoke cmake --install
cmake = CMake(self)
cmake.install()

def layout(self):
cmake_layout(self)

def package_id(self):
self.info.clear()
11 changes: 11 additions & 0 deletions test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.21)

project(JsonStructTester
DESCRIPTION "Tester package for json_struct"
LANGUAGES C CXX)

find_package(json_struct REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} json_struct::json_struct)
28 changes: 28 additions & 0 deletions test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
from conan.tools.build import can_run


class JsonStructTest(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps", "CMakeToolchain"

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def layout(self):
cmake_layout(self)

def test(self):
if can_run(self):
self.output.info("Checking compiled tester...")
cmd = os.path.join(self.cpp.build.bindir, "JsonStructTester")
self.run(cmd, env="conanrun")

19 changes: 19 additions & 0 deletions test_package/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "json_struct/json_struct.h"
#include <iostream>

struct MyTestStruct
{
std::string name;
unsigned age;
JS_OBJ(name, age);
};

int main()
{
MyTestStruct person{.name="Jonh", .age=23};

std::string person_json = JS::serializeStruct(person);
std::cout << person_json << std::endl;

return 0;
}

0 comments on commit addc61f

Please sign in to comment.