Skip to content
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

[json-schema-validator] Conan v2 support #13959

Merged
merged 9 commits into from
Nov 14, 2022
Merged
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):
uilianries marked this conversation as resolved.
Show resolved Hide resolved
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)
uilianries marked this conversation as resolved.
Show resolved Hide resolved

@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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a regression, target name was correct and aligned with upstream imported target.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing.

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)