[DO-987] catch recipe (!3)

Co-authored-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
Reviewed-on: https://git.avroid.tech/Conan/conan_build/pulls/3
Reviewed-by: Denis Patrakeev <denis.patrakeev@avroid.team>
Co-authored-by: Aleksandr Vodyanov <aleksandr.vodyanov@avroid.team>
Co-committed-by: Aleksandr Vodyanov <aleksandr.vodyanov@avroid.team>
This commit is contained in:
Aleksandr Vodyanov
2024-11-21 17:23:08 +03:00
committed by Denis Patrakeev
parent bb6197bed7
commit e3106d807c
27 changed files with 928 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
sources:
"2.13.10":
url: "https://github.com/catchorg/Catch2/archive/v2.13.10.tar.gz"
sha256: "d54a712b7b1d7708bc7a819a8e6e47b2fde9536f487b89ccbca295072a7d9943"
"2.13.9":
url: "https://github.com/catchorg/Catch2/archive/v2.13.9.tar.gz"
sha256: "06dbc7620e3b96c2b69d57bf337028bf245a211b3cddb843835bfe258f427a52"
"2.13.8":
url: "https://github.com/catchorg/Catch2/archive/v2.13.8.tar.gz"
sha256: "b9b592bd743c09f13ee4bf35fc30eeee2748963184f6bea836b146e6cc2a585a"
"2.13.7":
url: "https://github.com/catchorg/Catch2/archive/v2.13.7.tar.gz"
sha256: "3cdb4138a072e4c0290034fe22d9f0a80d3bcfb8d7a8a5c49ad75d3a5da24fae"
"2.12.4":
url: "https://github.com/catchorg/Catch2/archive/v2.12.4.tar.gz"
sha256: "5436725bbc6ee131a0bc9545bef31f0adabbb21fbc39fb6f1b2a42c12e4f8107"
"2.11.3":
url: "https://github.com/catchorg/Catch2/archive/v2.11.3.tar.gz"
sha256: "9a6967138062688f04374698fce4ce65908f907d8c0fe5dfe8dc33126bd46543"

View File

@@ -0,0 +1,133 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir
from conan.tools.scm import Version
import os
required_conan_version = ">=1.53.0"
class Catch2Conan(ConanFile):
name = "catch2"
description = "A modern, C++-native, header-only, framework for unit-tests, TDD and BDD"
topics = ("header-only", "unit-test", "tdd", "bdd")
homepage = "https://github.com/catchorg/Catch2"
url = "https://github.com/conan-io/conan-center-index"
license = "BSL-1.0"
settings = "os", "arch", "compiler", "build_type"
options = {
"fPIC": [True, False],
"with_main": [True, False],
"with_benchmark": [True, False],
"with_prefix": [True, False],
"default_reporter": [None, "ANY"],
}
default_options = {
"fPIC": True,
"with_main": False,
"with_benchmark": False,
"with_prefix": False,
"default_reporter": None,
}
@property
def _default_reporter_str(self):
return str(self.options.default_reporter).strip('"')
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def configure(self):
if not self.options.with_main:
self.options.rm_safe("fPIC")
self.options.rm_safe("with_benchmark")
def layout(self):
cmake_layout(self, src_folder="src")
def package_id(self):
if not self.info.options.with_main:
self.info.clear()
def validate(self):
if Version(self.version) < "2.13.1" and self.settings.arch == "armv8":
raise ConanInvalidConfiguration("ARMv8 is not supported by versions < 2.13.1+")
if self.options.get_safe("with_main") and Version(self.version) < "2.13.4":
raise ConanInvalidConfiguration("Option with_main not supported by versions < 2.13.4")
def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)
def generate(self):
tc = CMakeToolchain(self)
tc.variables["BUILD_TESTING"] = False
tc.cache_variables["CATCH_INSTALL_DOCS"] = False # these are cmake options, so use cache_variables
tc.cache_variables["CATCH_INSTALL_HELPERS"] = "ON" # these are cmake options, so use cache_variables
tc.cache_variables["CATCH_BUILD_STATIC_LIBRARY"] = str(self.options.with_main) # these are cmake options, so use cache_variables (str() is required for conan 1.52)
if self.options.with_prefix:
tc.preprocessor_definitions["CATCH_CONFIG_PREFIX_ALL"] = 1
if self.options.get_safe("with_benchmark", False):
tc.preprocessor_definitions["CATCH_CONFIG_ENABLE_BENCHMARKING"] = 1
if self.options.default_reporter:
tc.variables["CATCH_CONFIG_DEFAULT_REPORTER"] = self._default_reporter_str
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
if self.options.with_main:
cmake.build()
def package(self):
copy(self, pattern="LICENSE.txt", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
cmake = CMake(self)
cmake.install()
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "share"))
for cmake_file in ["ParseAndAddCatchTests.cmake", "Catch.cmake", "CatchAddTests.cmake"]:
copy(self,
cmake_file,
src=os.path.join(self.source_folder, "contrib"),
dst=os.path.join(self.package_folder, "lib", "cmake", "Catch2"),
)
def package_info(self):
self.cpp_info.set_property("cmake_file_name", "Catch2")
self.cpp_info.set_property("cmake_target_name", "Catch2::Catch2{}".format("WithMain" if self.options.with_main else ""))
self.cpp_info.set_property("pkg_config_name", "catch2{}".format("-with-main" if self.options.with_main else ""))
defines = []
if self.options.get_safe("with_benchmark", False):
defines.append("CATCH_CONFIG_ENABLE_BENCHMARKING")
if self.options.with_prefix:
defines.append("CATCH_CONFIG_PREFIX_ALL")
if self.options.default_reporter:
defines.append(f"CATCH_CONFIG_DEFAULT_REPORTER={self._default_reporter_str}")
if self.options.with_main:
self.cpp_info.components["_catch2"].set_property("cmake_target_name", "Catch2::Catch2")
self.cpp_info.components["_catch2"].set_property("pkg_config_name", "catch2")
self.cpp_info.components["_catch2"].defines = defines
self.cpp_info.components["catch2_with_main"].builddirs.append(os.path.join("lib", "cmake", "Catch2"))
self.cpp_info.components["catch2_with_main"].libs = ["Catch2WithMain"]
self.cpp_info.components["catch2_with_main"].system_libs = ["log"] if self.settings.os == "Android" else []
self.cpp_info.components["catch2_with_main"].set_property("cmake_target_name", "Catch2::Catch2WithMain")
self.cpp_info.components["catch2_with_main"].set_property("pkg_config_name", "catch2-with-main")
self.cpp_info.components["catch2_with_main"].defines = defines
else:
self.cpp_info.builddirs = [os.path.join("lib", "cmake", "Catch2")]
self.cpp_info.system_libs = ["log"] if self.settings.os == "Android" else []
self.cpp_info.defines = defines
# TODO: to remove in conan v2 once legacy generators removed
self.cpp_info.names["cmake_find_package"] = "Catch2"
self.cpp_info.names["cmake_find_package_multi"] = "Catch2"
if self.options.with_main:
self.cpp_info.components["_catch2"].names["cmake_find_package"] = "Catch2"
self.cpp_info.components["_catch2"].names["cmake_find_package_multi"] = "Catch2"
self.cpp_info.components["catch2_with_main"].names["cmake_find_package"] = "Catch2WithMain"
self.cpp_info.components["catch2_with_main"].names["cmake_find_package_multi"] = "Catch2WithMain"

View File

@@ -0,0 +1,3 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

View File

@@ -0,0 +1,5 @@
#include <catch2/catch.hpp>
TEST_CASE( "compiles and runs" ) {
REQUIRE( true == !false );
}

View File

@@ -0,0 +1,11 @@
#include <catch2/catch.hpp>
unsigned int Factorial( unsigned int number ) {
return number > 1 ? Factorial(number-1)*number : 1;
}
TEST_CASE( "compiles and runs" ) {
BENCHMARK("factorial 25"){
return Factorial(25);
};
}

View File

@@ -0,0 +1,5 @@
#include <catch2/catch.hpp>
CATCH_TEST_CASE( "compiles and runs" ) {
CATCH_REQUIRE( true == !false );
}

View File

@@ -0,0 +1,11 @@
#include <catch2/catch.hpp>
unsigned int Factorial( unsigned int number ) {
return number > 1 ? Factorial(number-1)*number : 1;
}
TEST_CASE( "compiles and runs" ) {
BENCHMARK("factorial 25"){
return Factorial(25);
};
}

View File

@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES CXX)
enable_testing()
find_package(Catch2 REQUIRED CONFIG)
if(WITH_PREFIX)
set(SRC_STANDALONE 300-standalone-with-prefix.cpp)
set(SRC_BENCHMARK 400-benchmark-with-prefix.cpp)
else()
set(SRC_STANDALONE 100-standalone.cpp)
set(SRC_BENCHMARK 200-benchmark.cpp)
endif()
add_executable(standalone 000-CatchMain.cpp ${SRC_STANDALONE})
target_link_libraries(standalone PRIVATE Catch2::Catch2)
target_compile_features(standalone PRIVATE cxx_std_11)
add_test(NAME standalone COMMAND standalone)
if(WITH_MAIN)
add_executable(standalone_with_main ${SRC_STANDALONE})
target_link_libraries(standalone_with_main PRIVATE Catch2::Catch2WithMain)
target_compile_features(standalone_with_main PRIVATE cxx_std_11)
add_test(NAME standalone_with_main COMMAND standalone_with_main)
endif()
if(WITH_BENCHMARK)
add_executable(benchmark ${SRC_BENCHMARK})
target_link_libraries(benchmark PRIVATE Catch2::Catch2WithMain)
target_compile_features(benchmark PRIVATE cxx_std_11)
add_test(NAME benchmark COMMAND benchmark)
endif()

View File

@@ -0,0 +1,33 @@
from conan import ConanFile
from conan.tools.build import build_jobs, can_run
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import chdir
class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
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.variables["WITH_MAIN"] = self.dependencies["catch2"].options.with_main
tc.variables["WITH_BENCHMARK"] = self.dependencies["catch2"].options.get_safe("with_benchmark", False)
tc.variables["WITH_PREFIX"] = self.dependencies["catch2"].options.with_prefix
tc.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
if can_run(self):
with chdir(self, self.build_folder):
self.run(f"ctest --output-on-failure -C {self.settings.build_type} -j {build_jobs(self)}", env="conanrun")

View File

@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.1)
project(test_v1_package)
enable_testing()
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)

View File

@@ -0,0 +1,18 @@
from conans import ConanFile, CMake, tools
class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
def build(self):
cmake = CMake(self)
cmake.definitions["WITH_MAIN"] = self.options["catch2"].with_main
cmake.definitions["WITH_BENCHMARK"] = self.options["catch2"].with_main and self.options["catch2"].with_benchmark
cmake.definitions["WITH_PREFIX"] = self.options["catch2"].with_prefix
cmake.configure()
cmake.build()
def test(self):
if not tools.cross_building(self):
self.run(f"ctest --output-on-failure -C {self.settings.build_type} -j {tools.cpu_count()}", run_environment=True)