Improve and fix bugs in Conanfile

- Fix issues with Conanfile - create a package which can be consumed by
  downstream Conan packages
- TODO: Add standard Conan workflow instructions to README
This commit is contained in:
Jacob Crabill 2023-09-06 13:30:17 -07:00 committed by Patrick Boettcher
parent 349cba9f7e
commit 704a54552d
2 changed files with 37 additions and 24 deletions

View File

@ -10,9 +10,12 @@ endif ()
# Basic project definition # # Basic project definition #
]==============================================================================================] ]==============================================================================================]
# TODO: CMake >= 3.19 can use string(JSON VERSION GET "${METADATA}" "version") to load from JSON
set(PROJECT_VERSION 2.4.0)
# TODO: Version 3, rename the project and namespace to something more compact # TODO: Version 3, rename the project and namespace to something more compact
project(nlohmann_json_schema_validator project(nlohmann_json_schema_validator
VERSION 2.3.0 VERSION ${PROJECT_VERSION}
DESCRIPTION "Json validator for nlohmann::json library" DESCRIPTION "Json validator for nlohmann::json library"
HOMEPAGE_URL "https://github.com/pboettch/json-schema-validator" HOMEPAGE_URL "https://github.com/pboettch/json-schema-validator"
LANGUAGES CXX) LANGUAGES CXX)

View File

@ -1,7 +1,10 @@
import os import os
import re import re
from conans import load, tools, ConanFile, CMake
from conan import ConanFile
from conan.tools.cmake import cmake_layout, CMake, CMakeToolchain
from conans.tools import load
from conans import tools as ctools
def get_version(): def get_version():
try: try:
@ -20,52 +23,59 @@ class JsonSchemaValidatorConan(ConanFile):
version = get_version() version = get_version()
url = 'https://github.com/pboettch/json-schema-validator' url = 'https://github.com/pboettch/json-schema-validator'
license = 'MIT' license = 'MIT'
settings = 'os', 'compiler', 'build_type', 'arch' settings = 'os', 'compiler', 'build_type', 'arch'
options = { options = {
'shared': [True, False], 'shared': [True, False],
'fPIC': [True, False], 'fPIC': [True, False],
'build_examples': [True, False], 'build_examples': [True, False],
'build_tests': [True, False] 'build_tests': [True, False],
'test_coverage': [True, False],
} }
default_options = { default_options = {
'shared': False, 'shared': False,
'fPIC': True, 'fPIC': True,
'build_examples': True, 'build_examples': True,
'build_tests': False 'build_tests': False,
'test_coverage': False,
} }
generators = "CMakeDeps"
generators = 'CMakeDeps', 'CMakeToolchain', 'VirtualBuildEnv', 'VirtualRunEnv'
exports_sources = [ exports_sources = [
'CMakeLists.txt', 'CMakeLists.txt',
'nlohmann_json_schema_validatorConfig.cmake.in', 'conanfile.py',
'cmake/*',
'src/*', 'src/*',
'app/*', 'example/*',
'test/*', 'test/*',
] ]
requires = (
'nlohmann_json/3.11.2'
)
_cmake = None
def _configure_cmake(self): requires = [
if self._cmake: 'nlohmann_json/3.11.2'
return self._cmake ]
self._cmake = CMake(self)
self._cmake.definitions['JSON_VALIDATOR_BUILD_EXAMPLES'] = self.options.build_examples def generate(self):
self._cmake.definitions['JSON_VALIDATOR_BUILD_TESTS'] = self.options.build_tests tc = CMakeToolchain(self)
self._cmake.configure() tc.variables['JSON_VALIDATOR_BUILD_EXAMPLES'] = self.options.build_examples
return self._cmake tc.variables['JSON_VALIDATOR_BUILD_TESTS'] = self.options.build_tests
tc.variables['JSON_VALIDATOR_SHARED_LIBS '] = self.options.shared
tc.variables['JSON_VALIDATOR_TEST_COVERAGE '] = self.options.test_coverage
tc.generate()
def layout(self): def layout(self):
build_type = str(self.settings.build_type).lower() cmake_layout(self)
self.folders.build = "build-{}".format(build_type)
def build(self): def build(self):
cmake = self._configure_cmake() cmake = CMake(self)
cmake.configure() cmake.configure()
cmake.verbose = True
cmake.build() cmake.build()
def package(self): def package(self):
cmake = self._configure_cmake() cmake = CMake(self)
cmake.install() cmake.install()
def package_info(self): def package_info(self):
@ -74,7 +84,7 @@ class JsonSchemaValidatorConan(ConanFile):
libdir = os.path.join(self.package_folder, "lib") libdir = os.path.join(self.package_folder, "lib")
self.cpp_info.libdirs = [libdir] self.cpp_info.libdirs = [libdir]
self.cpp_info.libs += tools.collect_libs(self, libdir) self.cpp_info.libs += ctools.collect_libs(self, libdir)
bindir = os.path.join(self.package_folder, "bin") bindir = os.path.join(self.package_folder, "bin")
self.output.info("Appending PATH environment variable: {}".format(bindir)) self.output.info("Appending PATH environment variable: {}".format(bindir))