From 31f8c574234d157b4b6e76f6a5abb2534c8b2f89 Mon Sep 17 00:00:00 2001 From: Grzegorz Antoniak Date: Mon, 3 Jul 2023 19:27:24 +0200 Subject: [PATCH] (#14543) Add `loguru/2.1.0` recipe * Add `loguru/2.1.0` recipe Loguru: a lightweight and flexible C++ logging library. Website: https://emilk.github.io/loguru/index.html Source code: https://github.com/emilk/loguru Closes #14542 * Simplify CMake and native features usage (#1) Simplifications and Conan v2 fixes * Update recipes/loguru/all/conanfile.py * Update recipes/loguru/all/conanfile.py * Update recipes/loguru/all/conanfile.py * loguru: use latest commit with CMake support * fixup: missing comma * fixup: typo * chore: rm config files * fixup: clean up defines * add includedirs, simplify test_package * add suffix * add m * fix windows shared * remove pdbs --------- Co-authored-by: Uilian Ries Co-authored-by: Chris Mc Co-authored-by: czoido --- recipes/loguru/all/conandata.yml | 4 + recipes/loguru/all/conanfile.py | 139 ++++++++++++++++++ .../loguru/all/test_package/CMakeLists.txt | 8 + recipes/loguru/all/test_package/conanfile.py | 31 ++++ .../loguru/all/test_package/test_package.cpp | 29 ++++ .../loguru/all/test_v1_package/CMakeLists.txt | 8 + .../loguru/all/test_v1_package/conanfile.py | 18 +++ recipes/loguru/config.yml | 3 + 8 files changed, 240 insertions(+) create mode 100644 recipes/loguru/all/conandata.yml create mode 100644 recipes/loguru/all/conanfile.py create mode 100644 recipes/loguru/all/test_package/CMakeLists.txt create mode 100644 recipes/loguru/all/test_package/conanfile.py create mode 100644 recipes/loguru/all/test_package/test_package.cpp create mode 100644 recipes/loguru/all/test_v1_package/CMakeLists.txt create mode 100644 recipes/loguru/all/test_v1_package/conanfile.py create mode 100644 recipes/loguru/config.yml diff --git a/recipes/loguru/all/conandata.yml b/recipes/loguru/all/conandata.yml new file mode 100644 index 0000000000000..779d9ad6aa366 --- /dev/null +++ b/recipes/loguru/all/conandata.yml @@ -0,0 +1,4 @@ +sources: + "cci.20230406": + url: "https://github.com/emilk/loguru/archive/4adaa185883e3c04da25913579c451d3c32cfac1.tar.gz" + sha256: "1424f3ce814fa413e5fbdf2949994d455e3914560f958d2931ba869349a686a8" diff --git a/recipes/loguru/all/conanfile.py b/recipes/loguru/all/conanfile.py new file mode 100644 index 0000000000000..098d77e4ec8b7 --- /dev/null +++ b/recipes/loguru/all/conanfile.py @@ -0,0 +1,139 @@ +import os + +from conan import ConanFile +from conan.errors import ConanInvalidConfiguration +from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout +from conan.tools.microsoft import is_msvc +from conan.tools.files import get, load, save, rmdir, rm +from conan.tools.build import check_min_cppstd + + +required_conan_version = ">=1.53.0" + + +class LoguruConan(ConanFile): + name = "loguru" + url = "https://github.com/conan-io/conan-center-index" + homepage = "https://github.com/emilk/loguru" + license = "Unlicense" + topics = ("logging", "log", "fmt") + description = "Loguru is a C++11 logging library." + package_type = "library" + settings = "os", "arch", "compiler", "build_type" + options = { + "shared": [True, False], + "fPIC": [True, False], + "with_fmt": [True, False], + "verbose_scope_endings": [True, False], + "redefine_assert": [True, False], + "enable_streams": [True, False], + "enable_fileabs": [True, False], + "replace_glog": [True, False], + + "scope_text_size": [None, "ANY"], + "scope_time_precision": [None, "ANY"], + "filename_width": [None, "ANY"], + "threadname_width": [None, "ANY"], + } + + default_options = { + "shared": False, + "fPIC": True, + "with_fmt": False, + "verbose_scope_endings": True, + "redefine_assert": False, + "enable_streams": False, + "enable_fileabs": False, + "replace_glog": False, + "scope_text_size": 196, + "scope_time_precision": 3, + "filename_width": 23, + "threadname_width": 16, + } + + def config_options(self): + if self.settings.os == "Windows": + del self.options.fPIC + + def configure(self): + if self.options.shared: + self.options.rm_safe("fPIC") + + def requirements(self): + if self.options.with_fmt: + self.requires("fmt/9.1.0", transitive_headers=True) + + def validate(self): + if self.settings.compiler.cppstd: + check_min_cppstd(self, 11) + + if self.options.replace_glog and not self.options.enable_streams: + # https://github.com/emilk/loguru/blob/4adaa185883e3c04da25913579c451d3c32cfac1/docs/index.html#L692 + raise ConanInvalidConfiguration(f"{self.ref}:replace_glog needs {self.ref}:enable_streams=True") + + def source(self): + get(self, **self.conan_data["sources"][self.version], strip_root=True) + + def layout(self): + cmake_layout(self, src_folder='src') + + def generate(self): + tc = CMakeToolchain(self) + tc.variables["LOGURU_USE_FMTLIB"] = self.options.with_fmt + tc.variables["LOGURU_VERBOSE_SCOPE_ENDINGS"] = self.options.verbose_scope_endings + tc.variables["LOGURU_REDEFINE_ASSERT"] = self.options.redefine_assert + tc.variables["LOGURU_WITH_STREAMS"] = self.options.enable_streams + tc.variables["LOGURU_WITH_FILEABS"] = self.options.enable_fileabs + tc.variables["LOGURU_REPLACE_GLOG"] = self.options.replace_glog + tc.variables["LOGURU_SCOPE_TEXT_SIZE"] = self.options.scope_text_size + tc.variables["LOGURU_SCOPE_TIME_PRECISION"] = self.options.scope_time_precision + tc.variables["LOGURU_FILENAME_WIDTH"] = self.options.filename_width + tc.variables["LOGURU_THREADNAME_WIDTH"] = self.options.threadname_width + if is_msvc(self) and self.options.shared: + tc.preprocessor_definitions["LOGURU_EXPORT"] = "__declspec(dllexport)" + tc.generate() + + deps = CMakeDeps(self) + deps.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + @property + def _extracted_license(self): + tmp = load(self, os.path.join(self.source_folder, 'loguru.hpp')) + return tmp[2:tmp.find("# Inspiration", 0)].strip() + + def package(self): + save(self, os.path.join(self.package_folder, 'licenses', 'LICENSE'), self._extracted_license) + cmake = CMake(self) + cmake.install() + + rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig")) + rmdir(self, os.path.join(self.package_folder, "lib", "cmake")) + rm(self, "*.pdb", os.path.join(self.package_folder, "lib")) + rm(self, "*.pdb", os.path.join(self.package_folder, "bin")) + + def package_info(self): + suffix = "d" if self.settings.build_type == "Debug" else "" + self.cpp_info.libs = [f"loguru{suffix}"] + self.cpp_info.includedirs = [os.path.join("include", "loguru")] + # https://github.com/emilk/loguru/blob/4adaa185883e3c04da25913579c451d3c32cfac1/CMakeLists.txt#L301 + self.cpp_info.set_property("cmake_file_name", "loguru") + self.cpp_info.set_property("cmake_target_name", "loguru::loguru") + + self.cpp_info.defines.append(f"LOGURU_USE_FMTLIB={self.options.with_fmt}") + self.cpp_info.defines.append(f"LOGURU_SCOPE_TEXT_SIZE={self.options.scope_text_size}") + self.cpp_info.defines.append(f"LOGURU_SCOPE_TIME_PRECISION={self.options.scope_time_precision}") + self.cpp_info.defines.append(f"LOGURU_FILENAME_WIDTH={self.options.filename_width}") + self.cpp_info.defines.append(f"LOGURU_THREADNAME_WIDTH={self.options.threadname_width}") + self.cpp_info.defines.append(f"LOGURU_VERBOSE_SCOPE_ENDINGS={self.options.verbose_scope_endings}") + self.cpp_info.defines.append(f"LOGURU_REDEFINE_ASSERT={self.options.redefine_assert}") + self.cpp_info.defines.append(f"LOGURU_WITH_STREAMS={self.options.enable_streams}") + self.cpp_info.defines.append(f"LOGURU_WITH_FILEABS={self.options.enable_fileabs}") + self.cpp_info.defines.append(f"LOGURU_REPLACE_GLOG={self.options.replace_glog}") + + if self.settings.os in ["Linux", "FreeBSD"]: + self.cpp_info.system_libs = ["pthread", "dl", "m"] diff --git a/recipes/loguru/all/test_package/CMakeLists.txt b/recipes/loguru/all/test_package/CMakeLists.txt new file mode 100644 index 0000000000000..aba0bad284220 --- /dev/null +++ b/recipes/loguru/all/test_package/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.8) +project(test_package LANGUAGES CXX) + +find_package(loguru REQUIRED CONFIG) + +add_executable(${PROJECT_NAME} test_package.cpp) +target_link_libraries(${PROJECT_NAME} PUBLIC loguru::loguru) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11) diff --git a/recipes/loguru/all/test_package/conanfile.py b/recipes/loguru/all/test_package/conanfile.py new file mode 100644 index 0000000000000..543315cd0fbb8 --- /dev/null +++ b/recipes/loguru/all/test_package/conanfile.py @@ -0,0 +1,31 @@ +import os +from conan import ConanFile +from conan.tools.cmake import CMake, CMakeToolchain +from conan.tools.build import can_run +from conan.tools.cmake import cmake_layout + + +class TestPackageConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "CMakeDeps", "VirtualRunEnv" + test_type = "explicit" + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires(self.tested_reference_str) + + def generate(self): + tc = CMakeToolchain(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + if can_run(self): + bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package") + self.run(bin_path, env="conanrun") diff --git a/recipes/loguru/all/test_package/test_package.cpp b/recipes/loguru/all/test_package/test_package.cpp new file mode 100644 index 0000000000000..58460d1478be0 --- /dev/null +++ b/recipes/loguru/all/test_package/test_package.cpp @@ -0,0 +1,29 @@ +// Partially taken from: https://github.com/emilk/loguru/tree/master/loguru_example + +#include +#include +#include +#include + +inline void sleep_ms(int ms) +{ + VLOG_F(2, "Sleeping for %d ms", ms); + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); +} + +inline void complex_calculation() +{ + LOG_SCOPE_F(INFO, "complex_calculation"); + LOG_F(INFO, "Starting time machine..."); + LOG_F(WARNING, "The flux capacitor is not getting enough power!"); + LOG_F(INFO, "Lighting strike!"); + VLOG_F(1, "Found 1.21 gigawatts..."); +} + +int main(int argc, char *argv[]) +{ + loguru::init(argc, argv); + LOG_F(INFO, "Hello from main.cpp!"); + complex_calculation(); + LOG_F(INFO, "main function about to end!"); +} diff --git a/recipes/loguru/all/test_v1_package/CMakeLists.txt b/recipes/loguru/all/test_v1_package/CMakeLists.txt new file mode 100644 index 0000000000000..925ecbe19e448 --- /dev/null +++ b/recipes/loguru/all/test_v1_package/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.1) +project(test_package) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup(TARGETS) + +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/ + ${CMAKE_CURRENT_BINARY_DIR}/test_package/) diff --git a/recipes/loguru/all/test_v1_package/conanfile.py b/recipes/loguru/all/test_v1_package/conanfile.py new file mode 100644 index 0000000000000..5a05af3c2dfd2 --- /dev/null +++ b/recipes/loguru/all/test_v1_package/conanfile.py @@ -0,0 +1,18 @@ +from conans import ConanFile, CMake +from conan.tools.build import cross_building +import os + + +class TestPackageV1Conan(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 cross_building(self): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True) diff --git a/recipes/loguru/config.yml b/recipes/loguru/config.yml new file mode 100644 index 0000000000000..82675897e9768 --- /dev/null +++ b/recipes/loguru/config.yml @@ -0,0 +1,3 @@ +versions: + "cci.20230406": + folder: all