Repository: conan-io/conan-center-index

Issue:
[request] catch2/v3.0.1
### Package Details
  * Package Name/Version: **catch2/v3.0.1**
  * Changelog: **https://github.com/catchorg/Catch2/releases/tag/v3.0.1**


The above mentioned version is newly released by the upstream project and not yet available as a recipe. Please add this version.



Discussion:

Resolving patch:
diff --git a/recipes/catch2/2.x.x/conanfile.py b/recipes/catch2/2.x.x/conanfile.py
--- a/recipes/catch2/2.x.x/conanfile.py
+++ b/recipes/catch2/2.x.x/conanfile.py
@@ -9,11 +9,11 @@
 class ConanRecipe(ConanFile):
     name = "catch2"
     description = "A modern, C++-native, header-only, framework for unit-tests, TDD and BDD"
-    topics = ("conan", "catch2", "header-only", "unit-test", "tdd", "bdd")
+    topics = ("catch2", "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"
-    exports_sources = ["CMakeLists.txt", "patches/**"]
+    exports_sources = ["CMakeLists.txt"]
     generators = "cmake"
     settings = "os", "compiler", "build_type", "arch"
     options = {
@@ -78,12 +78,7 @@ def _configure_cmake(self):
         self._cmake.configure(build_folder=self._build_subfolder)
         return self._cmake
 
-    def _patch_sources(self):
-        for patch in self.conan_data.get("patches", {}).get(self.version, []):
-            tools.patch(**patch)
-
     def build(self):
-        self._patch_sources()
         # Catch2 does skip install if included as subproject:
         # https://github.com/catchorg/Catch2/blob/79a5cd795c387e2da58c13e9dcbfd9ea7a2cfb30/CMakeLists.txt#L100-L102
         main_cml = os.path.join(self._source_subfolder, "CMakeLists.txt")
diff --git a/recipes/catch2/3.x.x/conanfile.py b/recipes/catch2/3.x.x/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/conanfile.py
@@ -0,0 +1,126 @@
+import os
+import functools
+from conans import ConanFile, CMake, tools
+from conans.errors import ConanInvalidConfiguration
+
+required_conan_version = ">=1.43.0"
+
+class ConanRecipe(ConanFile):
+    name = "catch2"
+    description = "A modern, C++-native, header-only, framework for unit-tests, TDD and BDD"
+    topics = ("catch2", "unit-test", "tdd", "bdd")
+    license = "BSL-1.0"
+    homepage = "https://github.com/catchorg/Catch2"
+    url = "https://github.com/conan-io/conan-center-index"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {
+        "fPIC": [True, False],
+        "with_prefix": [True, False],
+        "default_reporter": "ANY",
+    }
+    default_options = {
+        "fPIC": True,
+        "with_prefix": False,
+        "default_reporter": None,
+    }
+    generators = "cmake"
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _build_subfolder(self):
+        return "build_subfolder"
+
+    @property
+    def _default_reporter_str(self):
+        return '"{}"'.format(str(self.options.default_reporter).strip('"'))
+
+    def export_sources(self):
+        self.copy("CMakeLists.txt")
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            self.copy(patch["patch_file"])
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    @property
+    def _compilers_minimum_version(self):
+        return {
+            "gcc": "7",
+            "Visual Studio": "15",
+            "clang": "5",
+            "apple-clang": "10",
+        }
+
+    def validate(self):
+        if self.settings.compiler.get_safe("cppstd"):
+            tools.check_min_cppstd(self, "14")
+        minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
+        if minimum_version:
+            if tools.Version(self.settings.compiler.version) < minimum_version:
+                raise ConanInvalidConfiguration("{}/{}: Unsupported compiler: {}-{} "
+                                                "(https://github.com/p-ranav/structopt#compiler-compatibility)."
+                                                .format(self.name, self.version, self.settings.compiler, self.settings.compiler.version))
+        else:
+            self.output.warn("{}/{} requires C++14. Your compiler is unknown. Assuming it supports C++14.".format(self.name, self.version))
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version], strip_root=True, destination=self._source_subfolder)
+
+    @functools.lru_cache(1)
+    def _configure_cmake(self):
+        cmake = CMake(self)
+        cmake.definitions["BUILD_TESTING"] = "OFF"
+        cmake.definitions["CATCH_INSTALL_DOCS"] = "OFF"
+        cmake.definitions["CATCH_INSTALL_HELPERS"] = "ON"
+        cmake.definitions["CATCH_CONFIG_PREFIX_ALL"] = self.options.with_prefix
+        if self.options.default_reporter:
+            cmake.definitions["CATCH_CONFIG_DEFAULT_REPORTER"] = self._default_reporter_str
+        cmake.configure(build_folder=self._build_subfolder)
+        return cmake
+
+    def _patch_sources(self):
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            tools.patch(**patch)
+
+    def build(self):
+        self._patch_sources()
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        self.copy(pattern="LICENSE.txt", dst="licenses", src=self._source_subfolder)
+        cmake = self._configure_cmake()
+        cmake.install()
+        tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
+        tools.rmdir(os.path.join(self.package_folder, "share"))
+
+    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.get_safe("with_main") else ""))
+        self.cpp_info.names["cmake_find_package"] = "Catch2"
+        self.cpp_info.names["cmake_find_package_multi"] = "Catch2"
+
+        lib_suffix = "d" if self.settings.build_type == "Debug" else ""
+        self.cpp_info.components["Catch2"].set_property("cmake_target_name", "Catch2::Catch2")
+        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"].libs = ["Catch2" + lib_suffix]
+
+        self.cpp_info.components["Catch2WithMain"].builddirs = [os.path.join("lib", "cmake", "Catch2")]
+        self.cpp_info.components["Catch2WithMain"].libs = ["Catch2Main" + lib_suffix]
+        self.cpp_info.components["Catch2WithMain"].requires = ["Catch2"]
+        self.cpp_info.components["Catch2WithMain"].system_libs = ["log"] if self.settings.os == "Android" else []
+        self.cpp_info.components["Catch2WithMain"].set_property("cmake_target_name", "Catch2::Catch2WithMain")
+        self.cpp_info.components["Catch2WithMain"].names["cmake_find_package"] = "Catch2WithMain"
+        self.cpp_info.components["Catch2WithMain"].names["cmake_find_package_multi"] = "Catch2WithMain"
+        defines = self.cpp_info.components["Catch2WithMain"].defines
+
+        if self.options.with_prefix:
+            defines.append("CATCH_CONFIG_PREFIX_ALL")
+        if self.options.default_reporter:
+            defines.append("CATCH_CONFIG_DEFAULT_REPORTER={}".format(self._default_reporter_str))


Test patch:
diff --git a/recipes/catch2/3.x.x/test_package/100-standalone.cpp b/recipes/catch2/3.x.x/test_package/100-standalone.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/test_package/100-standalone.cpp
@@ -0,0 +1,5 @@
+#include <catch2/catch_test_macros.hpp>
+
+TEST_CASE( "compiles and runs" ) {
+    REQUIRE( true == !false );
+}
diff --git a/recipes/catch2/3.x.x/test_package/200-benchmark.cpp b/recipes/catch2/3.x.x/test_package/200-benchmark.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/test_package/200-benchmark.cpp
@@ -0,0 +1,12 @@
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/benchmark/catch_benchmark.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);
+   };
+}
diff --git a/recipes/catch2/3.x.x/test_package/300-standalone-with-prefix.cpp b/recipes/catch2/3.x.x/test_package/300-standalone-with-prefix.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/test_package/300-standalone-with-prefix.cpp
@@ -0,0 +1,5 @@
+#include <catch2/catch_test_macros.hpp>
+
+CATCH_TEST_CASE( "compiles and runs" ) {
+    CATCH_REQUIRE( true == !false );
+}
diff --git a/recipes/catch2/3.x.x/test_package/400-benchmark-with-prefix.cpp b/recipes/catch2/3.x.x/test_package/400-benchmark-with-prefix.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/test_package/400-benchmark-with-prefix.cpp
@@ -0,0 +1,12 @@
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/benchmark/catch_benchmark.hpp>
+
+unsigned int Factorial( unsigned int number ) {
+  return number > 1 ? Factorial(number-1)*number : 1;
+}
+
+CATCH_TEST_CASE( "compiles and runs" ) {
+   CATCH_BENCHMARK("factorial 25"){
+      return Factorial(25);
+   };
+}
diff --git a/recipes/catch2/3.x.x/test_package/CMakeLists.txt b/recipes/catch2/3.x.x/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/test_package/CMakeLists.txt
@@ -0,0 +1,22 @@
+cmake_minimum_required(VERSION 3.12)
+project(test_package)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+find_package(Catch2 REQUIRED CONFIG)
+
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+if(NOT WITH_PREFIX)
+  add_executable(standalone 100-standalone.cpp)
+  target_link_libraries(standalone PRIVATE Catch2::Catch2WithMain)
+  add_executable(benchmark 200-benchmark.cpp)
+  target_link_libraries(benchmark PRIVATE Catch2::Catch2WithMain)
+else()
+  add_executable(standalone 300-standalone-with-prefix.cpp)
+  target_link_libraries(standalone PRIVATE Catch2::Catch2WithMain)
+  add_executable(benchmark 400-benchmark-with-prefix.cpp)
+  target_link_libraries(benchmark PRIVATE Catch2::Catch2WithMain)
+endif()
diff --git a/recipes/catch2/3.x.x/test_package/conanfile.py b/recipes/catch2/3.x.x/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/catch2/3.x.x/test_package/conanfile.py
@@ -0,0 +1,18 @@
+from conans import ConanFile, CMake, tools
+from conans.tools import Version
+import os
+
+class TestPackageConan(ConanFile):
+    settings = "os", "arch", "compiler", "build_type"
+    generators = "cmake", "cmake_find_package_multi"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.definitions["WITH_PREFIX"] = self.options["catch2"].with_prefix
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self):
+            self.run(os.path.join("bin", "standalone"), run_environment=True)
+            self.run(os.path.join("bin", "benchmark"), run_environment=True)