Skip to content

Commit

Permalink
conan 2 support (and the removal of ctti
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaragon committed Oct 2, 2024
1 parent 4c9eff5 commit 420f13a
Show file tree
Hide file tree
Showing 23 changed files with 173 additions and 173 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 3.2)
cmake_minimum_required(VERSION 3.21)
project(cxxmetrics C CXX)

include("${CMAKE_BINARY_DIR}/build/${CMAKE_BUILD_TYPE}/generators/conan_toolchain.cmake")

message(${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

set(CMAKE_CXX_STANDARD 14)
Expand All @@ -16,8 +19,8 @@ if("${CMAKE_BUILD_TYPE}" STREQUAL "Coverage")
link_libraries(gcov)
endif()

include(Conan)
conan_include("conanfile.py")
set(cxxmetrics_DIR "${CMAKE_SOURCE_DIR}/cxxmetrics" CACHE FILEPATH "cxxmetrics directory" FORCE)
set(cxxmetrics_FOUND TRUE CACHE BOOL "cxxmetrics found" FORCE)

add_subdirectory(cxxmetrics)
add_subdirectory(cxxmetrics_prometheus)
Expand Down
57 changes: 41 additions & 16 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
from conan import ConanFile, tools
from conan.tools.layout import basic_layout
from conan.tools.files import copy
from conan.errors import ConanInvalidConfiguration
from conan.tools.build import check_min_cppstd
import os

from conans import ConanFile, tools


class CxxmetricsConan(ConanFile):
name = "cxxmetrics"
version = "0.0.8"
description = (
"A smallish header-only C++14 library inspired by dropwizard metrics (codahale)"
)
license = "Apache 2.0"
settings = "os"
url = "https://github.com/kmaragon/cxxmetrics"
description = "A smallish header-only C++14 library inspired by dropwizard metrics (codahale)"
requires = "ctti/0.0.1@manu343726/testing"
options = { "prometheus": [True, False] }
default_options = "prometheus=True"
exports_sources = "cxxmetrics*"
settings = ("compiler", "os")
options = { "with_prometheus": [True, False] }
default_options = { "with_prometheus": True }
package_type = "header-library"
exports_sources = "CMakeLists.txt", "cxxmetrics*"
no_copy_source = True
# No settings/options are necessary, this is header only

@property
def _min_cppstd(self):
return "14"

def layout(self):
basic_layout(self, src_folder=".")

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, self._min_cppstd)

def requirements(self):
pass

def package(self):
self.copy("*.hpp", src="cxxmetrics", dst="include/cxxmetrics")
if self.options.prometheus:
self.copy("*.hpp", src="cxxmetrics_prometheus", dst="include/cxxmetrics_prometheus")
copy(self,
"*.hpp",
os.path.join(self.source_folder, "cxxmetrics"),
os.path.join(self.package_folder, "include/cxxmetrics"))
if self.options.with_prometheus:
copy(self,
"*.hpp",
os.path.join(self.source_folder, "cxxmetrics_prometheus"),
os.path.join(self.package_folder, "include/cxxmetrics_prometheus"))

def package_info(self):
self.cpp_info.includedirs = ['include']
self.cpp_info.set_property("cmake_file_name", "cxxmetrics")
self.cpp_info.components["cxxmetrics"].names["cmake_find_package"] = "cxxmetrics"
self.cpp_info.components["cxxmetrics"].includedirs = ['include']

if self.settings.os == 'Linux':
self.cpp_info.libs = ["atomic"]
self.cpp_info.components["cxxmetrics"].system_libs = ["atomic"]

16 changes: 5 additions & 11 deletions cxxmetrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ macro(target_sources_local target) # https://gitlab.kitware.com/cmake/cmake/issu
endif()
list(APPEND _srcList ${src})
endforeach()
message("SOURCES: ${_srcList}")
target_sources(${target} ${_srcList})
endmacro()

Expand Down Expand Up @@ -39,14 +38,9 @@ set(HEADERS
uniform_reservoir.hpp
)

add_library(cxxmetrics INTERFACE)
target_include_directories(cxxmetrics INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/../")
target_sources_local(cxxmetrics INTERFACE ${HEADERS})
target_link_libraries(cxxmetrics INTERFACE atomic CONAN_PKG::ctti)
add_library(cxxmetrics::cxxmetrics INTERFACE IMPORTED GLOBAL)
target_include_directories(cxxmetrics::cxxmetrics INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/../")
target_sources_local(cxxmetrics::cxxmetrics INTERFACE ${HEADERS})
target_link_libraries(cxxmetrics::cxxmetrics INTERFACE atomic)

install(FILES ${HEADERS} DESTINATION "include/cxxmetrics")

install(TARGETS cxxmetrics
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
install(FILES ${HEADERS} DESTINATION "include/cxxmetrics")
Empty file.
7 changes: 3 additions & 4 deletions cxxmetrics/metric.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CXXMETRICS_METRIC_HPP
#define CXXMETRICS_METRIC_HPP

#include <ctti/type_id.hpp>
#include "snapshots.hpp"

#if __cplusplus < 201700L
Expand Down Expand Up @@ -57,9 +56,9 @@ class metric : public internal::metric
*
* \return the compile time type name of the metric
*/
static constexpr ctti::detail::cstring type_name()
static constexpr const char* type_name()
{
return ctti::type_id<TMetricType>().name();
return typeid(TMetricType).name();
}

/**
Expand All @@ -73,7 +72,7 @@ class metric : public internal::metric
template<typename TMetricType>
std::string metric<TMetricType>::metric_type() const noexcept
{
return ctti::type_id<TMetricType>().name().cppstring();
return typeid(TMetricType).name();
}

}
Expand Down
10 changes: 5 additions & 5 deletions cxxmetrics/metrics_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class basic_registered_metric
get_or_create_publish_data(TConstructArgs&&... args)
{
std::lock_guard<std::mutex> lock(pubdatalock_);
auto key = ctti::nameof<TDataType>().str();
auto key = typeid(TDataType).name();
auto& ptr = pubdata_[key];

if (!ptr)
Expand All @@ -165,7 +165,7 @@ class basic_registered_metric
try_get_publish_data() const
{
std::lock_guard<std::mutex> lock(pubdatalock_);
auto fnd = pubdata_.find(ctti::nameof<TDataType>().str());
auto fnd = pubdata_.find(typeid(TDataType).name());
if (fnd == pubdata_.end())
return nullptr;

Expand Down Expand Up @@ -403,7 +403,7 @@ typename std::enable_if<std::is_base_of<basic_publish_options, TDataType>::value
basic_default_repository<TAlloc>::get_publish_data(TConstructArgs&&... args)
{
std::lock_guard<std::mutex> lock(datalock_);
auto key = ctti::nameof<TDataType>().str();
auto key = typeid(TDataType).name();
auto& ptr = data_[key];

if (!ptr)
Expand All @@ -418,8 +418,8 @@ typename std::enable_if<std::is_base_of<basic_publish_options, TDataType>::value
basic_default_repository<TAlloc>::get_publish_data() const
{
std::lock_guard<std::mutex> lock(datalock_);
auto key = ctti::nameof<TDataType>().str();
auto fnd = data_.find(ctti::nameof<TDataType>().str());
auto key = typeid(TDataType).name();
auto fnd = data_.find(typeid(TDataType).name());
if (fnd == data_.end())
return nullptr;

Expand Down
27 changes: 6 additions & 21 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
project("cxxmetrics_test" CXX)

if (NOT CONAN_EXPORTED)
include(Conan)
include(Coverage)
conan_include("conanfile.py")
cmake_minimum_required(VERSION 3.21)

add_library(CONAN_PKG::cxxmetrics INTERFACE IMPORTED)
target_link_libraries(CONAN_PKG::cxxmetrics INTERFACE cxxmetrics cxxmetrics_prometheus)
else()
include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
conan_basic_setup(TARGETS NO_OUTPUT_DIRS)
endif()
project("cxxmetrics_test" CXX)
find_package(Catch2 REQUIRED)
find_package(cxxmetrics REQUIRED)

set(CMAKE_CXX_STANDARD 14)

Expand All @@ -28,26 +20,19 @@ set(SOURCES
#skiplist_test.cpp
histogram_test.cpp
timer_test.cpp
main.cpp
)

set(PROMETHEUS_SOURCES
prometheus_publish_test.cpp
main.cpp
)

add_executable(cxxmetrics_test ${SOURCES})
target_include_directories(cxxmetrics_test PUBLIC ${CONAN_INCLUDES})
target_link_libraries(cxxmetrics_test CONAN_PKG::catch2 CONAN_PKG::cxxmetrics -pthread)
target_link_libraries(cxxmetrics_test Catch2::Catch2 Catch2::Catch2WithMain cxxmetrics::cxxmetrics -pthread)

add_executable(cxxmetrics_prometheus_test ${PROMETHEUS_SOURCES})
target_include_directories(cxxmetrics_prometheus_test PUBLIC ${CONAN_INCLUDES})
target_link_libraries(cxxmetrics_prometheus_test CONAN_PKG::catch2 CONAN_PKG::cxxmetrics)

if (NOT CONAN_EXPORTED)
add_coverage_run(cxxmetrics_coverage cxxmetrics_test)
add_coverage_run(cxxmetrics_prometheus_coverage cxxmetrics_prometheus_test)
endif()
target_link_libraries(cxxmetrics_prometheus_test Catch2::Catch2 Catch2::Catch2WithMain cxxmetrics::cxxmetrics)

enable_testing()
add_test(NAME cxxmetrics
Expand Down
37 changes: 17 additions & 20 deletions test/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import os

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

class CxxmetricsTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
exports = "CMakeLists.txt", "../test*"
build_requires = "catch2/2.13.9"
generators = "CMakeDeps", "CMakeToolchain"

def source(self):
tools.replace_in_file("CMakeLists.txt", "project(\"cxxmetrics_test\" CXX)", '''project("cxxmetrics_test" CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS NO_OUTPUT_DIRS)''')
def requirements(self):
self.requires(self.tested_reference_str)
self.requires("catch2/3.7.1")

def build(self):
cmake = CMake(self)
# Current dir is "test_package/build/<build_id>" and CMakeLists.txt is
# in "test_package"
cmake.configure()
cmake.build()
def layout(self):
cmake_layout(self)

def imports(self):
self.copy("*.dll", dst="bin", src="bin")
self.copy("*.dylib*", dst="bin", src="lib")
self.copy('*.so*', dst='bin', src='lib')
def build(self):
if can_run(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not tools.cross_building(self.settings):
self.run(".%scxxmetrics_test" % os.sep)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "cxxmetrics_test")
self.run(bin_path, env="conanrun")
4 changes: 2 additions & 2 deletions test/counter_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <cxxmetrics/counter.hpp>

using namespace cxxmetrics;
Expand All @@ -21,7 +21,7 @@ TEST_CASE("Counter incr and wrappers work", "[counter]")
a = 10;
REQUIRE(a == 10);

REQUIRE(a.metric_type().substr(0, 20) == "cxxmetrics::counter<");
REQUIRE(a.metric_type().find("counter") != std::string::npos);
}

TEST_CASE("Counter excercise snapshot", "[counter]")
Expand Down
2 changes: 1 addition & 1 deletion test/ewma_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <thread>
#include <cxxmetrics/ewma.hpp>
#include "helpers.hpp"
Expand Down
10 changes: 5 additions & 5 deletions test/gauge_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <cxxmetrics/gauge.hpp>

using namespace std;
Expand Down Expand Up @@ -30,8 +30,8 @@ TEST_CASE("Functional Gauge works", "[gauge]")
REQUIRE(g.get() == 99.81);

value = 10000;
REQUIRE_THAT(g.get(), Catch::WithinULP(10000.0, 1));
REQUIRE_THAT(g.snapshot().value(), Catch::WithinULP(10000.0, 1));
REQUIRE_THAT(g.get(), Catch::Matchers::WithinULP(10000.0, 1));
REQUIRE_THAT(g.snapshot().value(), Catch::Matchers::WithinULP(10000.0, 1));
}

TEST_CASE("Pointer Gauge works", "[gauge]")
Expand All @@ -41,8 +41,8 @@ TEST_CASE("Pointer Gauge works", "[gauge]")
REQUIRE(g.get() == 70);

v = 500.017f;
REQUIRE_THAT(g.get(), Catch::WithinULP(500.017f, 1));
REQUIRE_THAT(g.snapshot().value(), Catch::WithinULP(500.017f, 1));
REQUIRE_THAT(g.get(), Catch::Matchers::WithinULP(500.017f, 1));
REQUIRE_THAT(g.snapshot().value(), Catch::Matchers::WithinULP(500.017f, 1));

float x = 0;
gauge<float *> h(&x);
Expand Down
8 changes: 4 additions & 4 deletions test/histogram_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <cxxmetrics/histogram.hpp>
#include <cxxmetrics/simple_reservoir.hpp>

Expand All @@ -20,10 +20,10 @@ TEST_CASE("Histogram sanity check", "[histogram]")

auto s = h.snapshot();

REQUIRE_THAT(s.min(), Catch::WithinULP(10.0, 1));
REQUIRE_THAT(s.max(), Catch::WithinULP(45.0, 1));
REQUIRE_THAT(s.min(), Catch::Matchers::WithinULP(10.0, 1));
REQUIRE_THAT(s.max(), Catch::Matchers::WithinULP(45.0, 1));
REQUIRE(std::abs(static_cast<double>(s.value<99_p>()) - 45.0) < 1);
REQUIRE(std::abs(static_cast<double>(s.value<60_p>()) - 35.0) <= 1);
REQUIRE_THAT(s.mean(), Catch::WithinULP(28.0, 1));
REQUIRE_THAT(s.mean(), Catch::Matchers::WithinULP(28.0, 1));
REQUIRE(s.count() == 8);
}
2 changes: 1 addition & 1 deletion test/internal/atomic_lifo_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <cxxmetrics/internal/atomic_lifo.hpp>
#include <set>
#include <thread>
Expand Down
2 changes: 0 additions & 2 deletions test/main.cpp

This file was deleted.

5 changes: 2 additions & 3 deletions test/meter_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <thread>
#include <cxxmetrics/meter.hpp>
#include <ctti/type_id.hpp>
#include "helpers.hpp"

using namespace std;
Expand Down Expand Up @@ -64,7 +63,7 @@ TEST_CASE("Meter rates are passed on", "[meter]")
REQUIRE(m.get_rate<1>()> m.get_rate<8>());
REQUIRE(m.get_rate<8>()> m.get_rate<20>());
REQUIRE(m.get_rate<20>()> m.get_rate<50>());
REQUIRE_THAT(m.mean(), Catch::WithinULP(1100.0 / 111.0, 1));
REQUIRE_THAT(m.mean(), Catch::Matchers::WithinULP(1100.0 / 111.0, 1));
}

TEST_CASE("Meter snapshot", "[meter]")
Expand Down
2 changes: 1 addition & 1 deletion test/metrics_registry_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_all.hpp>
#include <thread>
#include <cxxmetrics/metrics_registry.hpp>
#include <cxxmetrics/simple_reservoir.hpp>
Expand Down
Loading

0 comments on commit 420f13a

Please sign in to comment.