Skip to content

Commit addc61f

Browse files
sheepy9jorgen
authored andcommitted
Add conanfile and test_package
1 parent 1fc4672 commit addc61f

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ tests/json-tokenizer-partial-test
2929
tests/json-tree-test
3030
tests/json-tree-printer-test
3131

32+
*CMakeUserPresets.json
33+
build/

conanfile.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from conan import ConanFile
2+
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, cmake_layout
3+
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
4+
from conan.tools.scm import Git, Version
5+
from conan.tools.files import copy
6+
7+
import os
8+
9+
class JsonStructLibrary(ConanFile):
10+
name = "json_struct"
11+
12+
# Metadata
13+
license = "MIT"
14+
version = "1.0.0"
15+
author = "Jørgen Lind <jorgen.lind@gmail.com>"
16+
url = "https://github.com/jorgen/json_struct"
17+
description = "json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa"
18+
19+
topics = ("serialization", "deserialization", "reflection", "json")
20+
21+
settings = "os", "compiler", "build_type", "arch"
22+
pacgake_type = "header-library"
23+
implements = ["auto_header_only"]
24+
exports_sources = "include/*", "cmake/*" "CMakeLists.txt"
25+
26+
options = {
27+
"opt_build_benchmarks": [True, False],
28+
"opt_build_examples": [True, False],
29+
"opt_build_tests": [True, False],
30+
"opt_disable_pch": [True, False],
31+
"opt_install": [True, False],
32+
}
33+
34+
default_options = {
35+
"opt_build_benchmarks": False,
36+
"opt_build_examples": False,
37+
"opt_build_tests": False,
38+
"opt_disable_pch": False,
39+
"opt_install": True,
40+
}
41+
42+
def generate(self):
43+
toolchain = CMakeToolchain(self)
44+
45+
toolchain.variables["JSON_STRUCT_OPT_BUILD_BENCHMARKS"] = self.options.opt_build_benchmarks.value
46+
toolchain.variables["JSON_STRUCT_OPT_BUILD_EXAMPLES"] = self.options.opt_build_examples.value
47+
toolchain.variables["JSON_STRUCT_OPT_BUILD_TESTS"] = self.options.opt_build_tests.value
48+
toolchain.variables["JSON_STRUCT_OPT_DISABLE_PCH"] = self.options.opt_disable_pch.value
49+
toolchain.variables["JSON_STRUCT_OPT_INSTALL"] = self.options.opt_install
50+
51+
toolchain.generate()
52+
53+
def build(self):
54+
cmake = CMake(self)
55+
cmake.configure()
56+
cmake.build()
57+
58+
def package(self):
59+
# Invoke cmake --install
60+
cmake = CMake(self)
61+
cmake.install()
62+
63+
def layout(self):
64+
cmake_layout(self)
65+
66+
def package_id(self):
67+
self.info.clear()

test_package/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
3+
project(JsonStructTester
4+
DESCRIPTION "Tester package for json_struct"
5+
LANGUAGES C CXX)
6+
7+
find_package(json_struct REQUIRED)
8+
9+
add_executable(${PROJECT_NAME} main.cpp)
10+
11+
target_link_libraries(${PROJECT_NAME} json_struct::json_struct)

test_package/conanfile.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
3+
from conan import ConanFile
4+
from conan.tools.cmake import CMake, cmake_layout
5+
from conan.tools.build import can_run
6+
7+
8+
class JsonStructTest(ConanFile):
9+
settings = "os", "compiler", "build_type", "arch"
10+
generators = "CMakeDeps", "CMakeToolchain"
11+
12+
def requirements(self):
13+
self.requires(self.tested_reference_str)
14+
15+
def build(self):
16+
cmake = CMake(self)
17+
cmake.configure()
18+
cmake.build()
19+
20+
def layout(self):
21+
cmake_layout(self)
22+
23+
def test(self):
24+
if can_run(self):
25+
self.output.info("Checking compiled tester...")
26+
cmd = os.path.join(self.cpp.build.bindir, "JsonStructTester")
27+
self.run(cmd, env="conanrun")
28+

test_package/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "json_struct/json_struct.h"
2+
#include <iostream>
3+
4+
struct MyTestStruct
5+
{
6+
std::string name;
7+
unsigned age;
8+
JS_OBJ(name, age);
9+
};
10+
11+
int main()
12+
{
13+
MyTestStruct person{.name="Jonh", .age=23};
14+
15+
std::string person_json = JS::serializeStruct(person);
16+
std::cout << person_json << std::endl;
17+
18+
return 0;
19+
}

0 commit comments

Comments
 (0)