(#12413) nlohmann_json: Add support for Conan V2

* nlohmann_json: Add support for Conan V2

* Fix cross_building call in test package

* Use src directory for source folder in layout
This commit is contained in:
Jordan Williams
2022-08-22 06:58:24 -05:00
committed by GitHub
parent 544c359d10
commit f3e554f4e3
5 changed files with 61 additions and 21 deletions

View File

@@ -1,32 +1,37 @@
from conans import ConanFile, tools
import os
required_conan_version = ">=1.33.0"
from conan import ConanFile
from conan.tools.files import copy, get
from conan.tools.layout import basic_layout
required_conan_version = ">=1.50.0"
class NlohmannJsonConan(ConanFile):
name = "nlohmann_json"
homepage = "https://github.com/nlohmann/json"
description = "JSON for Modern C++ parser and generator."
topics = ("conan", "jsonformoderncpp",
"nlohmann_json", "json", "header-only")
topics = ("jsonformoderncpp", "nlohmann_json", "json", "header-only")
url = "https://github.com/conan-io/conan-center-index"
no_copy_source = True
license = "MIT"
settings = "os", "arch", "compiler", "build_type"
no_copy_source = True
@property
def _source_subfolder(self):
return "source_subfolder"
def layout(self):
basic_layout(self, src_folder="src")
def package_id(self):
self.info.header_only()
self.info.clear()
def generate(self):
pass
def source(self):
tools.get(**self.conan_data["sources"][self.version], destination=self._source_subfolder, strip_root=True)
get(self, **self.conan_data["sources"][self.version], strip_root=True)
def package(self):
self.copy(pattern="LICENSE*", src=self._source_subfolder, dst="licenses")
self.copy("*", src=os.path.join(self._source_subfolder, "include"), dst="include")
copy(self, "LICENSE*", self.source_folder, os.path.join(self.package_folder, "licenses"))
copy(self, "*", os.path.join(self.source_folder, "include"), os.path.join(self.package_folder, "include"))
def package_info(self):
self.cpp_info.names["cmake_find_package"] = "nlohmann_json"

View File

@@ -1,9 +1,6 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.15)
project(test_package)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
find_package(nlohmann_json REQUIRED CONFIG)
add_executable(${PROJECT_NAME} test_package.cpp)

View File

@@ -1,10 +1,19 @@
from conans import ConanFile, CMake, tools
import os
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv"
def requirements(self):
self.requires(self.tested_reference_str)
def layout(self):
cmake_layout(self)
def build(self):
cmake = CMake(self)
@@ -12,6 +21,5 @@ class TestPackageConan(ConanFile):
cmake.build()
def test(self):
if not tools.cross_building(self.settings):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)
if can_run(self):
self.run(os.path.join(self.cpp.build.bindirs[0], "test_package"), env="conanrun")

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.1.2)
project(test_package)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
find_package(nlohmann_json REQUIRED CONFIG)
add_executable(${PROJECT_NAME} ../test_package/test_package.cpp)
target_link_libraries(${PROJECT_NAME} nlohmann_json::nlohmann_json)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 11)

View File

@@ -0,0 +1,19 @@
import os
from conan.tools.build import cross_building
from conans import ConanFile, CMake
class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
if not cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)