Skip to content

Commit

Permalink
(#13959) [json-schema-validator] Conan v2 support
Browse files Browse the repository at this point in the history
* Bump nlohmann_json/3.11.2

* conan v2: ConanFile, tools, ConanInvalidConfiguration

* conan v2: CMakeDeps generator

* conan v2: patch order of project and cmake_minimum_version so toolchain works...

* fix linter complaint about missing patch_type and patch_description

* Delete CMakeLists.txt as no longer required

* Apply suggestions from code review

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* Fix cmake_target_name to complete name

* conan v2 test_package

Co-authored-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
garethsb and uilianries authored Nov 14, 2022
1 parent 31f87ec commit 97175e5
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 68 deletions.
7 changes: 0 additions & 7 deletions recipes/json-schema-validator/all/CMakeLists.txt

This file was deleted.

9 changes: 9 additions & 0 deletions recipes/json-schema-validator/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ sources:
"2.0.0":
url: "https://github.com/pboettch/json-schema-validator/archive/refs/tags/2.0.0.tar.gz"
sha256: "ca8e4ca5a88c49ea52b5f5c2a08a293dbf02b2fc66cb8c09d4cce5810ee98b57"
patches:
"2.1.0":
- patch_file: "patches/2.1.0-cmake_minimum_version.patch"
patch_type: "conan"
patch_description: "CMake: cmake_minimum_version() before project()"
"2.0.0":
- patch_file: "patches/2.0.0-cmake_minimum_version.patch"
patch_type: "conan"
patch_description: "CMake: cmake_minimum_version() before project()"
99 changes: 47 additions & 52 deletions recipes/json-schema-validator/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools import build, files, microsoft, scm
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
import os
import textwrap

required_conan_version = ">=1.43.0"
required_conan_version = ">=1.53.0"


class JsonSchemaValidatorConan(ConanFile):
Expand All @@ -26,88 +28,81 @@ class JsonSchemaValidatorConan(ConanFile):
}

short_paths = True
generators = "cmake", "cmake_find_package"
exports_sources = ["CMakeLists.txt"]
_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

@property
def _build_subfolder(self):
return "build_subfolder"
def export_sources(self):
files.export_conandata_patches(self)

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
self.options.rm_safe("fPIC")

def requirements(self):
self.requires("nlohmann_json/3.10.5")
self.requires("nlohmann_json/3.11.2")

def validate(self):
version = tools.Version(self.version)
version = scm.Version(self.version)
min_vs_version = "16" if version < "2.1.0" else "14"
min_cppstd = "17" if self.settings.compiler == "Visual Studio" and version < "2.1.0" else "11"
if self.settings.get_safe("compiler.cppstd"):
tools.check_min_cppstd(self, min_cppstd)
min_cppstd = "17" if microsoft.is_msvc(self) and version < "2.1.0" else "11"
if self.info.settings.get_safe("compiler.cppstd"):
build.check_min_cppstd(self, min_cppstd)
min_vs_version = "15" if version < "2.1.0" else "14"

compilers = {
"Visual Studio": min_vs_version,
"gcc": "5" if version < "2.1.0" else "4.9",
"clang": "4",
"apple-clang": "9"}
min_version = compilers.get(str(self.settings.compiler))
min_version = compilers.get(str(self.info.settings.compiler))
if not min_version:
self.output.warn("{} recipe lacks information about the {} compiler support.".format(
self.name, self.settings.compiler))
self.output.warn(f"{self.name} recipe lacks information about the {self.info.settings.compiler} compiler support.")
else:
if tools.Version(self.settings.compiler.version) < min_version:
raise ConanInvalidConfiguration("{} requires c++{} support. The current compiler {} {} does not support it.".format(
self.name, min_cppstd, self.settings.compiler, self.settings.compiler.version))
if scm.Version(self.info.settings.compiler.version) < min_version:
raise ConanInvalidConfiguration(f"{self.name} requires c++{min_cppstd} support. The current compiler {self.info.settings.compiler} {self.info.settings.compiler.version} does not support it.")

def layout(self):
cmake_layout(self, src_folder="src")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["BUILD_TESTS"] = False
self._cmake.definitions["BUILD_EXAMPLES"] = False
if tools.Version(self.version) < "2.1.0":
self._cmake.definitions["NLOHMANN_JSON_DIR"] = ";".join(self.deps_cpp_info["nlohmann_json"].include_paths)
self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
files.get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["BUILD_TESTS"] = False
tc.variables["BUILD_EXAMPLES"] = False
if scm.Version(self.version) < "2.1.0":
tc.variables["NLOHMANN_JSON_DIR"] = ";".join(self.deps_cpp_info["nlohmann_json"].include_paths).replace("\\", "/")
tc.generate()
deps = CMakeDeps(self)
deps.generate()

def build(self):
cmake = self._configure_cmake()
files.apply_conandata_patches(self)
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
files.copy(self, "LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
cmake = CMake(self)
cmake.install()
if tools.Version(self.version) < "2.1.0":
self.copy("json-schema.hpp",
dst=os.path.join("include", "nlohmann"),
src=os.path.join(self._source_subfolder, "src"))
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
if scm.Version(self.version) < "2.1.0":
files.copy(self, "json-schema.hpp",
dst=os.path.join(self.package_folder, "include", "nlohmann"),
src=os.path.join(self.source_folder, "src"))
files.rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._module_file_rel_path),
{"nlohmann_json_schema_validator": "nlohmann_json_schema_validator::nlohmann_json_schema_validator"}
)

@staticmethod
def _create_cmake_module_alias_targets(module_file, targets):
def _create_cmake_module_alias_targets(self, module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent("""\
Expand All @@ -116,16 +111,16 @@ def _create_cmake_module_alias_targets(module_file, targets):
set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})
endif()
""".format(alias=alias, aliased=aliased))
tools.save(module_file, content)
files.save(self, module_file, content)

@property
def _module_file_rel_path(self):
return os.path.join("lib", "cmake", "conan-official-{}-targets.cmake".format(self.name))
return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake")

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "nlohmann_json_schema_validator")
self.cpp_info.set_property("cmake_target_name", "nlohmann_json_schema_validator")
self.cpp_info.libs = ["json-schema-validator" if tools.Version(self.version) < "2.1.0" else "nlohmann_json_schema_validator"]
self.cpp_info.set_property("cmake_target_name", "nlohmann_json_schema_validator::nlohmann_json_schema_validator")
self.cpp_info.libs = ["json-schema-validator" if scm.Version(self.version) < "2.1.0" else "nlohmann_json_schema_validator"]

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self.cpp_info.names["cmake_find_package"] = "nlohmann_json_schema_validator"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--- a/CMakeLists.txt 2019-04-02 10:39:55.000000000 -0000
+++ b/CMakeLists.txt 2022-11-07 17:09:53.295000000 -0000
@@ -1,10 +1,10 @@
+cmake_minimum_required(VERSION 3.2)
+
project(json-schema-validator
LANGUAGES CXX)

set(PROJECT_VERSION 2.0.0)

-cmake_minimum_required(VERSION 3.2)
-
option(BUILD_TESTS "Build tests" ON)
option(BUILD_EXAMPLES "Build examples" ON)

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--- a/CMakeLists.txt 2020-05-15 09:04:12.000000000 -0000
+++ b/CMakeLists.txt 2022-11-07 16:57:29.655000000 -0000
@@ -1,10 +1,10 @@
+cmake_minimum_required(VERSION 3.2)
+
project(nlohmann_json_schema_validator
LANGUAGES CXX)

set(PROJECT_VERSION 2.1.0)

-cmake_minimum_required(VERSION 3.2)
-
option(BUILD_TESTS "Build tests" ON)
option(BUILD_EXAMPLES "Build examples" ON)

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(nlohmann_json_schema_validator CONFIG REQUIRED)

add_executable(${PROJECT_NAME} test_package.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
target_link_libraries(${PROJECT_NAME} nlohmann_json_schema_validator)
target_link_libraries(${PROJECT_NAME} nlohmann_json_schema_validator::nlohmann_json_schema_validator)
19 changes: 14 additions & 5 deletions recipes/json-schema-validator/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

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

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

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
11 changes: 11 additions & 0 deletions recipes/json-schema-validator/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.1)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(nlohmann_json_schema_validator CONFIG REQUIRED)

add_executable(${PROJECT_NAME} ../test_package/test_package.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
target_link_libraries(${PROJECT_NAME} nlohmann_json_schema_validator)
17 changes: 17 additions & 0 deletions recipes/json-schema-validator/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

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

def test(self):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit 97175e5

Please sign in to comment.