diff --git a/recipes/opencv/2.x/conandata.yml b/recipes/opencv/2.x/conandata.yml index 9bd9ee8e16d813..c5a3a4fe4a0119 100644 --- a/recipes/opencv/2.x/conandata.yml +++ b/recipes/opencv/2.x/conandata.yml @@ -5,4 +5,3 @@ sources: patches: "2.4.13.7": - patch_file: "patches/0001-jasper.patch" - base_path: "src" diff --git a/recipes/opencv/2.x/conanfile.py b/recipes/opencv/2.x/conanfile.py index 175420ede91f73..8b6447069350b6 100644 --- a/recipes/opencv/2.x/conanfile.py +++ b/recipes/opencv/2.x/conanfile.py @@ -1,10 +1,13 @@ -from conan.tools.microsoft import msvc_runtime_flag -from conans import ConanFile, CMake, tools -from conans.errors import ConanInvalidConfiguration +from conan import ConanFile +from conan.errors import ConanInvalidConfiguration +from conan.tools.cmake import cmake_layout +from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rm, rmdir, save +from conan.tools.microsoft import is_msvc, is_msvc_static_runtime +from conans import CMake import os import textwrap -required_conan_version = ">=1.43.0" +required_conan_version = ">=1.52.0" class OpenCVConan(ConanFile): @@ -46,22 +49,9 @@ class OpenCVConan(ConanFile): generators = "cmake", "cmake_find_package" _cmake = None - @property - def _source_subfolder(self): - return "src" - - @property - def _build_subfolder(self): - return "build" - - @property - def _is_msvc(self): - return str(self.settings.compiler) in ["Visual Studio", "msvc"] - def export_sources(self): - self.copy("CMakeLists.txt") - for patch in self.conan_data.get("patches", {}).get(self.version, []): - self.copy(patch["patch_file"]) + copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder) + export_conandata_patches(self) def config_options(self): if self.settings.os == "Windows": @@ -71,7 +61,13 @@ def config_options(self): def configure(self): if self.options.shared: - del self.options.fPIC + try: + del self.options.fPIC + except Exception: + pass + + def layout(self): + cmake_layout(self, src_folder="src") def requirements(self): self.requires("zlib/1.2.12") @@ -93,29 +89,28 @@ def requirements(self): self.requires("gtk/system") def validate(self): - if self.options.shared and self._is_msvc and "MT" in msvc_runtime_flag(self): + if self.info.options.shared and is_msvc(self) and is_msvc_static_runtime(self): raise ConanInvalidConfiguration("Visual Studio with static runtime is not supported for shared library.") 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], + destination=self.source_folder, strip_root=True) - def _patch_opencv(self): - for patch in self.conan_data.get("patches", {}).get(self.version, []): - tools.patch(**patch) - tools.rmdir(os.path.join(self._source_subfolder, "3rdparty")) + def _patch_sources(self): + apply_conandata_patches(self) + rmdir(self, os.path.join(self.source_folder, "3rdparty")) - cmakelists = os.path.join(self._source_subfolder, "CMakeLists.txt") + cmakelists = os.path.join(self.source_folder, "CMakeLists.txt") # allow to find conan-supplied OpenEXR if self.options.with_openexr: - find_openexr = os.path.join(self._source_subfolder, "cmake", "OpenCVFindOpenEXR.cmake") - tools.replace_in_file(find_openexr, + find_openexr = os.path.join(self.source_folder, "cmake", "OpenCVFindOpenEXR.cmake") + replace_in_file(self, find_openexr, r'SET(OPENEXR_ROOT "C:/Deploy" CACHE STRING "Path to the OpenEXR \"Deploy\" folder")', "") - tools.replace_in_file(find_openexr, r'set(OPENEXR_ROOT "")', "") - tools.replace_in_file(find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)", "") - tools.replace_in_file(find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)", "") + replace_in_file(self, find_openexr, r'set(OPENEXR_ROOT "")', "") + replace_in_file(self, find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)", "") + replace_in_file(self, find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)", "") def openexr_library_names(name): # OpenEXR library may have different names, depends on namespace versioning, static, debug, etc. @@ -124,42 +119,44 @@ def openexr_library_names(name): version = version_name.split("/")[1] version_tokens = version.split(".") major, minor = version_tokens[0], version_tokens[1] - suffix = "%s_%s" % (major, minor) - names = ["%s-%s" % (name, suffix), - "%s-%s_s" % (name, suffix), - "%s-%s_d" % (name, suffix), - "%s-%s_s_d" % (name, suffix), - "%s" % name, - "%s_s" % name, - "%s_d" % name, - "%s_s_d" % name] + suffix = f"{major}_{minor}" + names = [ + f"{name}-{suffix}", + f"{name}-{suffix}_s", + f"{name}-{suffix}_d", + f"{name}-{suffix}_s_d", + name, + f"{name}_s", + f"{name}_d", + f"{name}_s_d", + ] return " ".join(names) for lib in ["Half", "Iex", "Imath", "IlmImf", "IlmThread"]: - tools.replace_in_file(find_openexr, "NAMES %s" % lib, "NAMES %s" % openexr_library_names(lib)) + replace_in_file(self, find_openexr, f"NAMES {lib}", f"NAMES {openexr_library_names(lib)}") - tools.replace_in_file(cmakelists, + replace_in_file(self, cmakelists, "project(OpenCV CXX C)", "project(OpenCV CXX C)\nset(CMAKE_CXX_STANDARD 11)") for cascade in ["lbpcascades", "haarcascades"]: - tools.replace_in_file(os.path.join(self._source_subfolder, "data", "CMakeLists.txt"), - "share/OpenCV/%s" % cascade, "res/%s" % cascade) + replace_in_file(self, os.path.join(self.source_folder, "data", "CMakeLists.txt"), + f"share/OpenCV/{cascade}", f"res/{cascade}") - tools.replace_in_file(cmakelists, "staticlib", "lib") - tools.replace_in_file(cmakelists, "ANDROID OR NOT UNIX", "FALSE") - tools.replace_in_file(cmakelists, "${OpenCV_ARCH}/${OpenCV_RUNTIME}/", "") - tools.replace_in_file(os.path.join(self._source_subfolder, "modules", "highgui", "CMakeLists.txt"), "JASPER_", "Jasper_") + replace_in_file(self, cmakelists, "staticlib", "lib") + replace_in_file(self, cmakelists, "ANDROID OR NOT UNIX", "FALSE") + replace_in_file(self, cmakelists, "${OpenCV_ARCH}/${OpenCV_RUNTIME}/", "") + replace_in_file(self, os.path.join(self.source_folder, "modules", "highgui", "CMakeLists.txt"), "JASPER_", "Jasper_") # relocatable shared lib on macOS - tools.replace_in_file(cmakelists, "cmake_policy(SET CMP0042 OLD)", "cmake_policy(SET CMP0042 NEW)") + replace_in_file(self, cmakelists, "cmake_policy(SET CMP0042 OLD)", "cmake_policy(SET CMP0042 NEW)") # Cleanup RPATH - tools.replace_in_file(cmakelists, + replace_in_file(self, cmakelists, "set(CMAKE_INSTALL_RPATH \"${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}\")", "") - tools.replace_in_file(cmakelists, "set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)", "") + replace_in_file(self, cmakelists, "set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)", "") # Do not try to detect Python - tools.replace_in_file(cmakelists, "include(cmake/OpenCVDetectPython.cmake)", "") + replace_in_file(self, cmakelists, "include(cmake/OpenCVDetectPython.cmake)", "") def _configure_cmake(self): if self._cmake: @@ -200,25 +197,25 @@ def _configure_cmake(self): self._cmake.definitions["ENABLE_CCACHE"] = False - if self._is_msvc: - self._cmake.definitions["BUILD_WITH_STATIC_CRT"] = "MT" in msvc_runtime_flag(self) - self._cmake.configure(build_folder=self._build_subfolder) + if is_msvc(self): + self._cmake.definitions["BUILD_WITH_STATIC_CRT"] = is_msvc_static_runtime(self) + self._cmake.configure(source_folder=os.path.join(self.source_folder, os.pardir), build_folder=self.build_folder) return self._cmake def build(self): - self._patch_opencv() + self._patch_sources() cmake = self._configure_cmake() cmake.build() def package(self): - self.copy("LICENSE", dst="licenses", src=self._source_subfolder) + copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) cmake = self._configure_cmake() cmake.install() - tools.rmdir(os.path.join(self.package_folder, "share")) - tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig")) - tools.rmdir(os.path.join(self.package_folder, "staticlib")) - tools.remove_files_by_mask(self.package_folder, "*.cmake") + rmdir(self, os.path.join(self.package_folder, "share")) + rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig")) + rmdir(self, os.path.join(self.package_folder, "staticlib")) + rm(self, "*.cmake", self.package_folder) # TODO: to remove in conan v2 once cmake_find_package* generators removed self._create_cmake_module_alias_targets( @@ -226,21 +223,20 @@ def package(self): {component["target"]:"opencv::{}".format(component["target"]) for component in self._opencv_components} ) - @staticmethod - def _create_cmake_module_alias_targets(module_file, targets): + def _create_cmake_module_alias_targets(self, module_file, targets): content = "" for alias, aliased in targets.items(): - content += textwrap.dedent("""\ + content += textwrap.dedent(f"""\ if(TARGET {aliased} AND NOT TARGET {alias}) add_library({alias} INTERFACE IMPORTED) set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased}) endif() - """.format(alias=alias, aliased=aliased)) - tools.save(module_file, content) + """) + save(self, module_file, content) @property def _module_file_rel_path(self): - return os.path.join("lib", "cmake", "conan-official-{}-targets.cmake".format(self.name)) + return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake") @property def _opencv_components(self): @@ -297,10 +293,10 @@ def nonfree(): def package_info(self): version = self.version.split(".")[:-1] # last version number is not used version = "".join(version) if self.settings.os == "Windows" else "" - debug = "d" if self.settings.build_type == "Debug" and self._is_msvc else "" + debug = "d" if self.settings.build_type == "Debug" and is_msvc(self) else "" def get_lib_name(module): - return "opencv_%s%s%s" % (module, version, debug) + return f"opencv_{module}{version}{debug}" def add_components(components): for component in components: @@ -314,7 +310,7 @@ def add_components(components): self.cpp_info.components[conan_component].set_property("cmake_target_name", cmake_target) self.cpp_info.components[conan_component].libs = [lib_name] self.cpp_info.components[conan_component].requires = requires - if self.settings.os == "Linux": + if self.settings.os in ["Linux", "FreeBSD"]: self.cpp_info.components[conan_component].system_libs = ["dl", "m", "pthread", "rt"] # TODO: to remove in conan v2 once cmake_find_package* generators removed @@ -327,11 +323,9 @@ def add_components(components): self.cpp_info.components[conan_component_alias].names["cmake_find_package"] = cmake_component self.cpp_info.components[conan_component_alias].names["cmake_find_package_multi"] = cmake_component self.cpp_info.components[conan_component_alias].requires = [conan_component] + self.cpp_info.components[conan_component_alias].bindirs = [] self.cpp_info.components[conan_component_alias].includedirs = [] self.cpp_info.components[conan_component_alias].libdirs = [] - self.cpp_info.components[conan_component_alias].resdirs = [] - self.cpp_info.components[conan_component_alias].bindirs = [] - self.cpp_info.components[conan_component_alias].frameworkdirs = [] self.cpp_info.set_property("cmake_file_name", "OpenCV") diff --git a/recipes/opencv/2.x/test_package/CMakeLists.txt b/recipes/opencv/2.x/test_package/CMakeLists.txt index f35b448655b4f3..d43070a4b5daf4 100644 --- a/recipes/opencv/2.x/test_package/CMakeLists.txt +++ b/recipes/opencv/2.x/test_package/CMakeLists.txt @@ -1,10 +1,7 @@ cmake_minimum_required(VERSION 3.1) -project(test_package) - -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup(TARGETS) +project(test_package LANGUAGES CXX) find_package(OpenCV REQUIRED core CONFIG) add_executable(${PROJECT_NAME} test_package.cpp) -target_link_libraries(${PROJECT_NAME} opencv_core) +target_link_libraries(${PROJECT_NAME} PRIVATE opencv_core) diff --git a/recipes/opencv/2.x/test_package/conanfile.py b/recipes/opencv/2.x/test_package/conanfile.py index 38f4483872d47f..0a6bc68712d901 100644 --- a/recipes/opencv/2.x/test_package/conanfile.py +++ b/recipes/opencv/2.x/test_package/conanfile.py @@ -1,10 +1,19 @@ -from conans import ConanFile, CMake, tools +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import CMake, cmake_layout import os class TestPackageConan(ConanFile): settings = "os", "arch", "compiler", "build_type" - generators = "cmake", "cmake_find_package_multi" + generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv" + test_type = "explicit" + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires(self.tested_reference_str) def build(self): cmake = CMake(self) @@ -12,6 +21,6 @@ def build(self): cmake.build() def test(self): - if not tools.cross_building(self): - bin_path = os.path.join("bin", "test_package") - self.run(bin_path, run_environment=True) + 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/opencv/2.x/test_v1_package/CMakeLists.txt b/recipes/opencv/2.x/test_v1_package/CMakeLists.txt new file mode 100644 index 00000000000000..aac6d95ea41d5a --- /dev/null +++ b/recipes/opencv/2.x/test_v1_package/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.1) +project(test_package LANGUAGES CXX) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup(TARGETS) + +find_package(OpenCV REQUIRED core CONFIG) + +add_executable(${PROJECT_NAME} ../test_package/test_package.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE opencv_core) diff --git a/recipes/opencv/2.x/test_v1_package/conanfile.py b/recipes/opencv/2.x/test_v1_package/conanfile.py new file mode 100644 index 00000000000000..38f4483872d47f --- /dev/null +++ b/recipes/opencv/2.x/test_v1_package/conanfile.py @@ -0,0 +1,17 @@ +from conans import ConanFile, CMake, tools +import os + + +class TestPackageConan(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 tools.cross_building(self): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True) diff --git a/recipes/opencv/3.x/conanfile.py b/recipes/opencv/3.x/conanfile.py index 93b2622292a642..b1d041abe3caf6 100644 --- a/recipes/opencv/3.x/conanfile.py +++ b/recipes/opencv/3.x/conanfile.py @@ -1,10 +1,14 @@ -from conan.tools.microsoft import msvc_runtime_flag -from conans import ConanFile, CMake, tools -from conans.errors import ConanInvalidConfiguration +from conan import ConanFile +from conan.errors import ConanInvalidConfiguration +from conan.tools.cmake import cmake_layout +from conan.tools.files import copy, get, rename, replace_in_file, rmdir, save +from conan.tools.microsoft import is_msvc, is_msvc_static_runtime +from conan.tools.scm import Version +from conans import CMake import os import textwrap -required_conan_version = ">=1.43.0" +required_conan_version = ">=1.52.0" class OpenCVConan(ConanFile): @@ -52,21 +56,9 @@ class OpenCVConan(ConanFile): generators = "cmake", "cmake_find_package" _cmake = None - @property - def _source_subfolder(self): - return "source_subfolder" - - @property - def _build_subfolder(self): - return "build_subfolder" - @property def _contrib_folder(self): - return "contrib" - - @property - def _is_msvc(self): - return str(self.settings.compiler) in ["Visual Studio", "msvc"] + return os.path.join(self.source_folder, os.pardir, "contrib") def config_options(self): if self.settings.os == "Windows": @@ -76,11 +68,17 @@ def config_options(self): def configure(self): if self.options.shared: - del self.options.fPIC + try: + del self.options.fPIC + except Exception: + pass self.options["*"].jpeg = self.options.with_jpeg self.options["*"].with_libjpeg = self.options.with_jpeg self.options["*"].with_jpeg = self.options.with_jpeg + def layout(self): + cmake_layout(self, src_folder="src") + def requirements(self): self.requires("zlib/1.2.12") if self.options.with_jpeg == "libjpeg": @@ -110,53 +108,51 @@ def requirements(self): self.requires("gtk/system") def validate(self): - if self.settings.compiler.get_safe("cppstd") and self.options.with_openexr: - tools.check_min_cppstd(self, 11) - if self.options.shared and self._is_msvc and "MT" in msvc_runtime_flag(self): + if self.info.options.shared and is_msvc(self) and is_msvc_static_runtime(self): raise ConanInvalidConfiguration("Visual Studio with static runtime is not supported for shared library.") - if self.settings.compiler == "clang" and tools.Version(self.settings.compiler.version) < "4": + if self.info.settings.compiler == "clang" and Version(self.info.settings.compiler.version) < "4": raise ConanInvalidConfiguration("Clang 3.x cannot build OpenCV 3.x due an internal bug.") def source(self): - tools.get(**self.conan_data["sources"][self.version][0], - destination=self._source_subfolder, strip_root=True) + get(self, **self.conan_data["sources"][self.version][0], + destination=self.source_folder, strip_root=True) - tools.get(**self.conan_data["sources"][self.version][1], - destination=self._contrib_folder, strip_root=True) + get(self, **self.conan_data["sources"][self.version][1], + destination=self._contrib_folder, strip_root=True) - def _patch_opencv(self): - tools.rmdir(os.path.join(self._source_subfolder, "3rdparty")) + def _patch_sources(self): + rmdir(self, os.path.join(self.source_folder, "3rdparty")) if self.options.contrib: freetype_cmake = os.path.join(self._contrib_folder, "modules", "freetype", "CMakeLists.txt") - tools.replace_in_file(freetype_cmake, "ocv_check_modules(FREETYPE freetype2)", "find_package(Freetype REQUIRED)") - tools.replace_in_file(freetype_cmake, "FREETYPE_", "Freetype_") + replace_in_file(self, freetype_cmake, "ocv_check_modules(FREETYPE freetype2)", "find_package(Freetype REQUIRED)") + replace_in_file(self, freetype_cmake, "FREETYPE_", "Freetype_") - tools.replace_in_file(freetype_cmake, "ocv_check_modules(HARFBUZZ harfbuzz)", "find_package(harfbuzz REQUIRED)") - tools.replace_in_file(freetype_cmake, "HARFBUZZ_", "harfbuzz_") + replace_in_file(self, freetype_cmake, "ocv_check_modules(HARFBUZZ harfbuzz)", "find_package(harfbuzz REQUIRED)") + replace_in_file(self, freetype_cmake, "HARFBUZZ_", "harfbuzz_") - tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"), "ANDROID OR NOT UNIX", "FALSE") - tools.replace_in_file(os.path.join(self._source_subfolder, "modules", "imgcodecs", "CMakeLists.txt"), "JASPER_", "Jasper_") + replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "ANDROID OR NOT UNIX", "FALSE") + replace_in_file(self, os.path.join(self.source_folder, "modules", "imgcodecs", "CMakeLists.txt"), "JASPER_", "Jasper_") # Cleanup RPATH - if tools.Version(self.version) < "3.4.8": - install_layout_file = os.path.join(self._source_subfolder, "CMakeLists.txt") + if Version(self.version) < "3.4.8": + install_layout_file = os.path.join(self.source_folder, "CMakeLists.txt") else: - install_layout_file = os.path.join(self._source_subfolder, "cmake", "OpenCVInstallLayout.cmake") - tools.replace_in_file(install_layout_file, + install_layout_file = os.path.join(self.source_folder, "cmake", "OpenCVInstallLayout.cmake") + replace_in_file(self, install_layout_file, "ocv_update(CMAKE_INSTALL_RPATH \"${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}\")", "") - tools.replace_in_file(install_layout_file, "set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)", "") + replace_in_file(self, install_layout_file, "set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)", "") - if self.options.contrib and tools.Version(self.version) <= "3.4.12": + if self.options.contrib and Version(self.version) <= "3.4.12": sfm_cmake = os.path.join(self._contrib_folder, "modules", "sfm", "CMakeLists.txt") - search = ' find_package(Glog QUIET)\nendif()' - tools.replace_in_file(sfm_cmake, search, """{} + search = " find_package(Glog QUIET)\nendif()" + replace_in_file(self, sfm_cmake, search, f"""{search} if(NOT GFLAGS_LIBRARIES AND TARGET gflags::gflags) set(GFLAGS_LIBRARIES gflags::gflags) endif() if(NOT GLOG_LIBRARIES AND TARGET glog::glog) set(GLOG_LIBRARIES glog::glog) - endif()""".format(search)) + endif()""") def _configure_cmake(self): if self._cmake: @@ -259,9 +255,9 @@ def _configure_cmake(self): self._cmake.definitions["WITH_OPENEXR"] = self.options.with_openexr self._cmake.definitions["WITH_EIGEN"] = self.options.with_eigen self._cmake.definitions["WITH_WEBP"] = self.options.with_webp - self._cmake.definitions["WITH_DSHOW"] = self._is_msvc - self._cmake.definitions["WITH_MSMF"] = self._is_msvc - self._cmake.definitions["WITH_MSMF_DXVA"] = self._is_msvc + self._cmake.definitions["WITH_DSHOW"] = is_msvc(self) + self._cmake.definitions["WITH_MSMF"] = is_msvc(self) + self._cmake.definitions["WITH_MSMF_DXVA"] = is_msvc(self) self._cmake.definitions["WITH_GTK"] = self.options.get_safe("with_gtk", False) self._cmake.definitions["WITH_GTK_2_X"] = self.options.get_safe("with_gtk", False) @@ -272,32 +268,32 @@ def _configure_cmake(self): self._cmake.definitions["WITH_OPENMP"] = self.options.parallel == "openmp" if self.options.contrib: - self._cmake.definitions["OPENCV_EXTRA_MODULES_PATH"] = os.path.join(self.build_folder, self._contrib_folder, 'modules') + self._cmake.definitions["OPENCV_EXTRA_MODULES_PATH"] = os.path.join(self._contrib_folder, "modules") - if self._is_msvc: - self._cmake.definitions["BUILD_WITH_STATIC_CRT"] = "MT" in msvc_runtime_flag(self) + if is_msvc(self): + self._cmake.definitions["BUILD_WITH_STATIC_CRT"] = is_msvc_static_runtime(self) if self.options.with_openexr: - self._cmake.definitions["OPENEXR_ROOT"] = self.deps_cpp_info['openexr'].rootpath.replace("\\", "/") + self._cmake.definitions["OPENEXR_ROOT"] = self.deps_cpp_info["openexr"].rootpath.replace("\\", "/") self._cmake.definitions["ENABLE_PIC"] = self.options.get_safe("fPIC", True) self._cmake.definitions["ENABLE_CCACHE"] = False - self._cmake.configure(build_folder=self._build_subfolder) + self._cmake.configure(source_folder=os.path.join(self.source_folder, os.pardir), build_folder=self.build_folder) return self._cmake def build(self): - self._patch_opencv() + self._patch_sources() cmake = self._configure_cmake() cmake.build() def package(self): - self.copy("LICENSE", dst="licenses", src=self._source_subfolder) + copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) cmake = self._configure_cmake() cmake.install() - tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig")) - tools.rmdir(os.path.join(self.package_folder, "cmake")) + rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig")) + rmdir(self, os.path.join(self.package_folder, "cmake")) if os.path.isfile(os.path.join(self.package_folder, "setup_vars_opencv3.cmd")): - os.rename(os.path.join(self.package_folder, "setup_vars_opencv3.cmd"), - os.path.join(self.package_folder, "res", "setup_vars_opencv3.cmd")) + rename(self, os.path.join(self.package_folder, "setup_vars_opencv3.cmd"), + os.path.join(self.package_folder, "res", "setup_vars_opencv3.cmd")) # TODO: to remove in conan v2 once cmake_find_package* generators removed self._create_cmake_module_alias_targets( @@ -305,21 +301,20 @@ def package(self): {component["target"]:"opencv::{}".format(component["target"]) for component in self._opencv_components} ) - @staticmethod - def _create_cmake_module_alias_targets(module_file, targets): + def _create_cmake_module_alias_targets(self, module_file, targets): content = "" for alias, aliased in targets.items(): - content += textwrap.dedent("""\ + content += textwrap.dedent(f"""\ if(TARGET {aliased} AND NOT TARGET {alias}) add_library({alias} INTERFACE IMPORTED) set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased}) endif() - """.format(alias=alias, aliased=aliased)) - tools.save(module_file, content) + """) + save(self, module_file, content) @property def _module_file_rel_path(self): - return os.path.join("lib", "cmake", "conan-official-{}-targets.cmake".format(self.name)) + return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake") @property def _opencv_components(self): @@ -413,13 +408,13 @@ def gtk(): def package_info(self): version = self.version.split(".") version = "".join(version) if self.settings.os == "Windows" else "" - debug = "d" if self.settings.build_type == "Debug" and self._is_msvc else "" + debug = "d" if self.settings.build_type == "Debug" and is_msvc(self) else "" def get_lib_name(module): if module in ("correspondence", "multiview", "numeric"): return module else: - return "opencv_%s%s%s" % (module, version, debug) + return f"opencv_{module}{version}{debug}" def add_components(components): for component in components: @@ -434,7 +429,7 @@ def add_components(components): self.cpp_info.components[conan_component].libs = [lib_name] self.cpp_info.components[conan_component].libs = [lib_name] self.cpp_info.components[conan_component].requires = requires - if self.settings.os == "Linux": + if self.settings.os in ["Linux", "FreeBSD"]: self.cpp_info.components[conan_component].system_libs = ["dl", "m", "pthread", "rt"] # TODO: to remove in conan v2 once cmake_find_package* generators removed @@ -447,11 +442,9 @@ def add_components(components): self.cpp_info.components[conan_component_alias].names["cmake_find_package"] = cmake_component self.cpp_info.components[conan_component_alias].names["cmake_find_package_multi"] = cmake_component self.cpp_info.components[conan_component_alias].requires = [conan_component] + self.cpp_info.components[conan_component_alias].bindirs = [] self.cpp_info.components[conan_component_alias].includedirs = [] self.cpp_info.components[conan_component_alias].libdirs = [] - self.cpp_info.components[conan_component_alias].resdirs = [] - self.cpp_info.components[conan_component_alias].bindirs = [] - self.cpp_info.components[conan_component_alias].frameworkdirs = [] self.cpp_info.set_property("cmake_file_name", "OpenCV") @@ -460,7 +453,7 @@ def add_components(components): if self.settings.os == "Windows": self.cpp_info.components["opencv_imgcodecs"].system_libs = ["comctl32", "gdi32", "ole32", "setupapi", "ws2_32", "vfw32"] elif self.settings.os == "Macos": - self.cpp_info.components["opencv_imgcodecs"].frameworks = ['OpenCL', 'Accelerate', 'CoreMedia', 'CoreVideo', 'CoreGraphics', 'AVFoundation', 'QuartzCore', 'Cocoa'] + self.cpp_info.components["opencv_imgcodecs"].frameworks = ["OpenCL", "Accelerate", "CoreMedia", "CoreVideo", "CoreGraphics", "AVFoundation", "QuartzCore", "Cocoa"] # TODO: to remove in conan v2 once cmake_find_package* generators removed self.cpp_info.filenames["cmake_find_package"] = "OpenCV" diff --git a/recipes/opencv/3.x/test_package/CMakeLists.txt b/recipes/opencv/3.x/test_package/CMakeLists.txt index 18a5ba5919ce81..69d824a0783ee0 100644 --- a/recipes/opencv/3.x/test_package/CMakeLists.txt +++ b/recipes/opencv/3.x/test_package/CMakeLists.txt @@ -1,8 +1,5 @@ cmake_minimum_required(VERSION 3.1) -project(test_package) - -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup(TARGETS) +project(test_package LANGUAGES CXX) find_package(OpenCV REQUIRED core imgproc CONFIG) @@ -12,4 +9,4 @@ if(built_contrib) endif() add_executable(${PROJECT_NAME} test_package.cpp) -target_link_libraries(${PROJECT_NAME} opencv_core opencv_imgproc $) +target_link_libraries(${PROJECT_NAME} PRIVATE opencv_core opencv_imgproc $) diff --git a/recipes/opencv/3.x/test_package/conanfile.py b/recipes/opencv/3.x/test_package/conanfile.py index 3f79fd5e038ff1..c7db2feac4ede9 100644 --- a/recipes/opencv/3.x/test_package/conanfile.py +++ b/recipes/opencv/3.x/test_package/conanfile.py @@ -1,18 +1,31 @@ -from conans import ConanFile, CMake, tools +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout import os class TestPackageConan(ConanFile): settings = "os", "arch", "compiler", "build_type" - generators = "cmake", "cmake_find_package_multi" + 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["built_contrib"] = self.dependencies["opencv"].options.contrib + tc.generate() def build(self): cmake = CMake(self) - cmake.definitions["built_contrib"] = self.options["opencv"].contrib cmake.configure() cmake.build() def test(self): - if not tools.cross_building(self): - bin_path = os.path.join("bin", "test_package") - self.run(bin_path, run_environment=True) + 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/opencv/3.x/test_v1_package/CMakeLists.txt b/recipes/opencv/3.x/test_v1_package/CMakeLists.txt new file mode 100644 index 00000000000000..d745112aac4303 --- /dev/null +++ b/recipes/opencv/3.x/test_v1_package/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.1) +project(test_package LANGUAGES CXX) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup(TARGETS) + +find_package(OpenCV REQUIRED core imgproc CONFIG) + +option(built_contrib "Enabled if opencv is built contrib sfm" OFF) +if(built_contrib) + add_definitions(-DBUILT_CONTRIB) +endif() + +add_executable(${PROJECT_NAME} ../test_package/test_package.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE opencv_core opencv_imgproc $) diff --git a/recipes/opencv/3.x/test_v1_package/conanfile.py b/recipes/opencv/3.x/test_v1_package/conanfile.py new file mode 100644 index 00000000000000..3f79fd5e038ff1 --- /dev/null +++ b/recipes/opencv/3.x/test_v1_package/conanfile.py @@ -0,0 +1,18 @@ +from conans import ConanFile, CMake, tools +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["built_contrib"] = self.options["opencv"].contrib + cmake.configure() + cmake.build() + + def test(self): + if not tools.cross_building(self): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True) diff --git a/recipes/opencv/4.x/conandata.yml b/recipes/opencv/4.x/conandata.yml index 88bdca68c62a1a..593305f1e5516d 100644 --- a/recipes/opencv/4.x/conandata.yml +++ b/recipes/opencv/4.x/conandata.yml @@ -32,6 +32,4 @@ sources: patches: "4.1.2": - patch_file: "patches/0001-jasper.patch" - base_path: "source_subfolder" - patch_file: "patches/0002-openexr.patch" - base_path: "source_subfolder" diff --git a/recipes/opencv/4.x/conanfile.py b/recipes/opencv/4.x/conanfile.py index 6f6d6d31aeb8f3..d87afa805bb698 100644 --- a/recipes/opencv/4.x/conanfile.py +++ b/recipes/opencv/4.x/conanfile.py @@ -1,10 +1,15 @@ -from conan.tools.microsoft import msvc_runtime_flag -from conans import ConanFile, CMake, tools -from conans.errors import ConanInvalidConfiguration +from conan import ConanFile +from conan.errors import ConanInvalidConfiguration +from conan.tools.build import cross_building +from conan.tools.cmake import cmake_layout +from conan.tools.files import apply_conandata_patches, collect_libs, copy, export_conandata_patches, get, rename, replace_in_file, rmdir, save +from conan.tools.microsoft import is_msvc, is_msvc_static_runtime +from conan.tools.scm import Version +from conans import CMake, tools as tools_legacy import os import textwrap -required_conan_version = ">=1.43.0" +required_conan_version = ">=1.52.0" class OpenCVConan(ConanFile): @@ -47,9 +52,9 @@ class OpenCVConan(ConanFile): "neon": [True, False], "dnn": [True, False], "dnn_cuda": [True, False], - "cuda_arch_bin": "ANY", - "cpu_baseline": "ANY", - "cpu_dispatch": "ANY", + "cuda_arch_bin": [None, "ANY"], + "cpu_baseline": [None, "ANY"], + "cpu_dispatch": [None, "ANY"], "nonfree": [True, False], } default_options = { @@ -93,21 +98,9 @@ class OpenCVConan(ConanFile): generators = "cmake", "cmake_find_package" _cmake = None - @property - def _source_subfolder(self): - return "source_subfolder" - - @property - def _build_subfolder(self): - return "build_subfolder" - @property def _contrib_folder(self): - return "contrib" - - @property - def _is_msvc(self): - return str(self.settings.compiler) in ["Visual Studio", "msvc"] + return os.path.join(self.source_folder, os.pardir, "contrib") @property def _has_with_jpeg2000_option(self): @@ -123,12 +116,11 @@ def _has_with_ffmpeg_option(self): @property def _protobuf_version(self): - return "protobuf/3.21.4" + return "3.21.4" def export_sources(self): - self.copy("CMakeLists.txt") - for patch in self.conan_data.get("patches", {}).get(self.version, []): - self.copy(patch["patch_file"]) + copy(self, "CMakeLists.txt", src=self.recipe_folder, dst=self.export_sources_folder) + export_conandata_patches(self) def config_options(self): if self.settings.os == "Windows": @@ -154,7 +146,10 @@ def config_options(self): def configure(self): if self.options.shared: - del self.options.fPIC + try: + del self.options.fPIC + except Exception: + pass if not self.options.contrib: del self.options.contrib_freetype del self.options.contrib_sfm @@ -175,6 +170,9 @@ def configure(self): if self.settings.os == "Android": self.options.with_openexr = False # disabled because this forces linkage to libc++_shared.so + def layout(self): + cmake_layout(self, src_folder="src") + def requirements(self): self.requires("zlib/1.2.12") if self.options.with_jpeg == "libjpeg": @@ -212,70 +210,68 @@ def requirements(self): if self.options.get_safe("with_gtk"): self.requires("gtk/system") if self.options.dnn: - self.requires(self._protobuf_version) + self.requires(f"protobuf/{self._protobuf_version}") if self.options.with_ade: self.requires("ade/0.1.2a") def validate(self): - if self.options.shared and self._is_msvc and "MT" in msvc_runtime_flag(self): + if self.info.options.shared and is_msvc(self) and is_msvc_static_runtime(self): raise ConanInvalidConfiguration("Visual Studio with static runtime is not supported for shared library.") - if self.settings.compiler == "clang" and tools.Version(self.settings.compiler.version) < "4": + if self.info.settings.compiler == "clang" and Version(self.info.settings.compiler.version) < "4": raise ConanInvalidConfiguration("Clang 3.x can't build OpenCV 4.x due to an internal bug.") - if self.options.with_cuda and not self.options.contrib: + if self.info.options.with_cuda and not self.info.options.contrib: raise ConanInvalidConfiguration("contrib must be enabled for cuda") - if self.options.get_safe("dnn_cuda", False) and \ - (not self.options.with_cuda or not self.options.contrib or not self.options.with_cublas or not self.options.with_cudnn): + if self.info.options.get_safe("dnn_cuda", False) and \ + (not self.info.options.with_cuda or not self.info.options.contrib or not self.info.options.with_cublas or not self.info.options.with_cudnn): raise ConanInvalidConfiguration("with_cublas, with_cudnn and contrib must be enabled for dnn_cuda") - if self.options.with_ipp == "opencv-icv" and \ - (not str(self.settings.arch) in ["x86", "x86_64"] or \ - not str(self.settings.os) in ["Linux", "Macos", "Windows"]): - raise ConanInvalidConfiguration("opencv-icv is not available for %s/%s" % \ - (str(self.settings.os), str(self.settings.arch))) + if self.info.options.with_ipp == "opencv-icv" and \ + (not str(self.info.settings.arch) in ["x86", "x86_64"] or \ + not str(self.info.settings.os) in ["Linux", "Macos", "Windows"]): + raise ConanInvalidConfiguration(f"opencv-icv is not available for {self.info.settings.os}/{self.info.settings.arch}") def build_requirements(self): if self.options.dnn and hasattr(self, "settings_build"): - self.build_requires(self._protobuf_version) + self.tool_requires(f"protobuf/{self._protobuf_version}") def source(self): - tools.get(**self.conan_data["sources"][self.version][0], - destination=self._source_subfolder, strip_root=True) + get(self, **self.conan_data["sources"][self.version][0], + destination=self.source_folder, strip_root=True) - tools.get(**self.conan_data["sources"][self.version][1], - destination=self._contrib_folder, strip_root=True) + get(self, **self.conan_data["sources"][self.version][1], + destination=self._contrib_folder, strip_root=True) - def _patch_opencv(self): - for patch in self.conan_data.get("patches", {}).get(self.version, []): - tools.patch(**patch) - for directory in ['libjasper', 'libjpeg-turbo', 'libjpeg', 'libpng', 'libtiff', 'libwebp', 'openexr', 'protobuf', 'zlib', 'quirc']: - tools.rmdir(os.path.join(self._source_subfolder, '3rdparty', directory)) + def _patch_sources(self): + apply_conandata_patches(self) + for directory in ["libjasper", "libjpeg-turbo", "libjpeg", "libpng", "libtiff", "libwebp", "openexr", "protobuf", "zlib", "quirc"]: + rmdir(self, os.path.join(self.source_folder, "3rdparty", directory)) if self.options.with_openexr: - find_openexr = os.path.join(self._source_subfolder, "cmake", "OpenCVFindOpenEXR.cmake") - tools.replace_in_file(find_openexr, + find_openexr = os.path.join(self.source_folder, "cmake", "OpenCVFindOpenEXR.cmake") + replace_in_file(self, find_openexr, r'SET(OPENEXR_ROOT "C:/Deploy" CACHE STRING "Path to the OpenEXR \"Deploy\" folder")', "") - tools.replace_in_file(find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)", "") - tools.replace_in_file(find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)", "") + replace_in_file(self, find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES x64/Release x64 x64/Debug)", "") + replace_in_file(self, find_openexr, "SET(OPENEXR_LIBSEARCH_SUFFIXES Win32/Release Win32 Win32/Debug)", "") - tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"), "ANDROID OR NOT UNIX", "FALSE") - tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"), "elseif(EMSCRIPTEN)", "elseif(QNXNTO)\nelseif(EMSCRIPTEN)") - tools.replace_in_file(os.path.join(self._source_subfolder, "modules", "imgcodecs", "CMakeLists.txt"), "JASPER_", "Jasper_") + replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "ANDROID OR NOT UNIX", "FALSE") + replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "elseif(EMSCRIPTEN)", "elseif(QNXNTO)\nelseif(EMSCRIPTEN)") + replace_in_file(self, os.path.join(self.source_folder, "modules", "imgcodecs", "CMakeLists.txt"), "JASPER_", "Jasper_") # Cleanup RPATH - if tools.Version(self.version) < "4.1.2": - install_layout_file = os.path.join(self._source_subfolder, "CMakeLists.txt") + if Version(self.version) < "4.1.2": + install_layout_file = os.path.join(self.source_folder, "CMakeLists.txt") else: - install_layout_file = os.path.join(self._source_subfolder, "cmake", "OpenCVInstallLayout.cmake") - tools.replace_in_file(install_layout_file, + install_layout_file = os.path.join(self.source_folder, "cmake", "OpenCVInstallLayout.cmake") + replace_in_file(self, install_layout_file, "ocv_update(CMAKE_INSTALL_RPATH \"${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}\")", "") - tools.replace_in_file(install_layout_file, "set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)", "") + replace_in_file(self, install_layout_file, "set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)", "") if self.options.dnn: - find_protobuf = os.path.join(self._source_subfolder, "cmake", "OpenCVFindProtobuf.cmake") + find_protobuf = os.path.join(self.source_folder, "cmake", "OpenCVFindProtobuf.cmake") # variables generated by protobuf recipe have all lowercase prefixes - tools.replace_in_file(find_protobuf, - 'find_package(Protobuf QUIET)', - '''find_package(Protobuf QUIET) + replace_in_file(self, find_protobuf, + "find_package(Protobuf QUIET)", + """find_package(Protobuf QUIET) if(NOT DEFINED Protobuf_LIBRARIES) set(Protobuf_LIBRARIES ${protobuf_LIBRARIES}) endif() @@ -284,37 +280,36 @@ def _patch_opencv(self): endif() if(NOT DEFINED Protobuf_INCLUDE_DIR) set(Protobuf_INCLUDE_DIR ${protobuf_INCLUDE_DIR}) - endif()''') + endif()""") # in 'if' block, get_target_property() produces an error - if tools.Version(self.version) >= "4.4.0": - tools.replace_in_file(find_protobuf, + if Version(self.version) >= "4.4.0": + replace_in_file(self, find_protobuf, 'if(TARGET "${Protobuf_LIBRARIES}")', 'if(FALSE) # patch: disable if(TARGET "${Protobuf_LIBRARIES}")') if self.options.with_ade: - ade_cmake = os.path.join(self._source_subfolder, "modules", "gapi", - "cmake", "init.cmake") - replacement = '''find_package(ade REQUIRED) - if(ade_DIR)''' - tools.replace_in_file(ade_cmake, 'if(ade_DIR)', replacement, strict=False) - tools.replace_in_file(ade_cmake, 'if (ade_DIR)', replacement, strict=False) - tools.replace_in_file(ade_cmake, "TARGET ade", "TARGET ade::ade") - gapi_cmake = os.path.join(self._source_subfolder, "modules", "gapi", "CMakeLists.txt") - tools.replace_in_file(gapi_cmake, " ade)", " ade::ade)") - - if self.options.contrib and self.options.contrib_sfm and tools.Version(self.version) <= "4.5.2": + ade_cmake = os.path.join(self.source_folder, "modules", "gapi", "cmake", "init.cmake") + replacement = """find_package(ade REQUIRED) + if(ade_DIR)""" + replace_in_file(self, ade_cmake, "if(ade_DIR)", replacement, strict=False) + replace_in_file(self, ade_cmake, "if (ade_DIR)", replacement, strict=False) + replace_in_file(self, ade_cmake, "TARGET ade", "TARGET ade::ade") + gapi_cmake = os.path.join(self.source_folder, "modules", "gapi", "CMakeLists.txt") + replace_in_file(self, gapi_cmake, " ade)", " ade::ade)") + + if self.options.contrib and self.options.contrib_sfm and Version(self.version) <= "4.5.2": sfm_cmake = os.path.join(self._contrib_folder, "modules", "sfm", "CMakeLists.txt") - ver = tools.Version(self.version) + ver = Version(self.version) if ver <= "4.5.0": - search = ' find_package(Glog QUIET)\nendif()' + search = " find_package(Glog QUIET)\nendif()" else: search = ' set(GLOG_INCLUDE_DIRS "${GLOG_INCLUDE_DIR}")\nendif()' - tools.replace_in_file(sfm_cmake, search, """{} + replace_in_file(self, sfm_cmake, search, f"""{search} if(NOT GFLAGS_LIBRARIES AND TARGET gflags::gflags) set(GFLAGS_LIBRARIES gflags::gflags) endif() if(NOT GLOG_LIBRARIES AND TARGET glog::glog) set(GLOG_LIBRARIES glog::glog) - endif()""".format(search)) + endif()""") def _configure_cmake(self): if self._cmake: @@ -436,9 +431,9 @@ def _configure_cmake(self): self._cmake.definitions["WITH_OPENEXR"] = self.options.with_openexr self._cmake.definitions["WITH_EIGEN"] = self.options.with_eigen self._cmake.definitions["HAVE_QUIRC"] = self.options.with_quirc # force usage of quirc requirement - self._cmake.definitions["WITH_DSHOW"] = self._is_msvc - self._cmake.definitions["WITH_MSMF"] = self._is_msvc - self._cmake.definitions["WITH_MSMF_DXVA"] = self._is_msvc + self._cmake.definitions["WITH_DSHOW"] = is_msvc(self) + self._cmake.definitions["WITH_MSMF"] = is_msvc(self) + self._cmake.definitions["WITH_MSMF_DXVA"] = is_msvc(self) self._cmake.definitions["OPENCV_MODULES_PUBLIC"] = "opencv" self._cmake.definitions["OPENCV_ENABLE_NONFREE"] = self.options.nonfree @@ -458,14 +453,14 @@ def _configure_cmake(self): self._cmake.definitions["OPENCV_DNN_CUDA"] = self.options.get_safe("dnn_cuda", False) if self.options.contrib: - self._cmake.definitions['OPENCV_EXTRA_MODULES_PATH'] = os.path.join(self.build_folder, self._contrib_folder, 'modules') - self._cmake.definitions['BUILD_opencv_freetype'] = self.options.get_safe("contrib_freetype", False) - self._cmake.definitions['BUILD_opencv_sfm'] = self.options.get_safe("contrib_sfm", False) + self._cmake.definitions["OPENCV_EXTRA_MODULES_PATH"] = os.path.join(self._contrib_folder, "modules") + self._cmake.definitions["BUILD_opencv_freetype"] = self.options.get_safe("contrib_freetype", False) + self._cmake.definitions["BUILD_opencv_sfm"] = self.options.get_safe("contrib_sfm", False) if self.options.with_openexr: self._cmake.definitions["OPENEXR_ROOT"] = self.deps_cpp_info["openexr"].rootpath if self.options.get_safe("with_jpeg2000") == "openjpeg": - openjpeg_version = tools.Version(self.deps_cpp_info["openjpeg"].version) + openjpeg_version = Version(self.deps_cpp_info["openjpeg"].version) self._cmake.definitions["OPENJPEG_MAJOR_VERSION"] = openjpeg_version.major self._cmake.definitions["OPENJPEG_MINOR_VERSION"] = openjpeg_version.minor self._cmake.definitions["OPENJPEG_BUILD_VERSION"] = openjpeg_version.patch @@ -487,18 +482,18 @@ def _configure_cmake(self): self._cmake.definitions["ENABLE_PIC"] = self.options.get_safe("fPIC", True) self._cmake.definitions["ENABLE_CCACHE"] = False - if self._is_msvc: - self._cmake.definitions["BUILD_WITH_STATIC_CRT"] = "MT" in msvc_runtime_flag(self) + if is_msvc(self): + self._cmake.definitions["BUILD_WITH_STATIC_CRT"] = is_msvc_static_runtime(self) if self.settings.os == "Android": self._cmake.definitions["ANDROID_STL"] = "c++_static" self._cmake.definitions["ANDROID_NATIVE_API_LEVEL"] = self.settings.os.api_level - self._cmake.definitions["ANDROID_ABI"] = tools.to_android_abi(str(self.settings.arch)) + self._cmake.definitions["ANDROID_ABI"] = tools_legacy.to_android_abi(str(self.settings.arch)) self._cmake.definitions["BUILD_ANDROID_EXAMPLES"] = False if "ANDROID_NDK_HOME" in os.environ: self._cmake.definitions["ANDROID_NDK"] = os.environ.get("ANDROID_NDK_HOME") - if tools.cross_building(self): + if cross_building(self): # FIXME: too specific and error prone, should be delegated to CMake helper cmake_system_processor = { "armv8": "aarch64", @@ -510,21 +505,21 @@ def _configure_cmake(self): self._cmake.definitions["CMAKE_FIND_ROOT_PATH_MODE_INCLUDE"] = "BOTH" self._cmake.definitions["CMAKE_FIND_ROOT_PATH_MODE_LIBRARY"] = "BOTH" - self._cmake.configure(build_folder=self._build_subfolder) + self._cmake.configure(source_folder=os.path.join(self.source_folder, os.pardir), build_folder=self.build_folder) return self._cmake def build(self): - self._patch_opencv() + self._patch_sources() cmake = self._configure_cmake() cmake.build() def package(self): - self.copy("LICENSE", dst="licenses", src=self._source_subfolder) + copy(self, "LICENSE", src=self._source_subfolder, dst=os.path.join(self.package_folder, "licenses")) cmake = self._configure_cmake() cmake.install() - tools.rmdir(os.path.join(self.package_folder, "cmake")) + rmdir(self, os.path.join(self.package_folder, "cmake")) if os.path.isfile(os.path.join(self.package_folder, "setup_vars_opencv4.cmd")): - tools.rename(os.path.join(self.package_folder, "setup_vars_opencv4.cmd"), + rename(self, os.path.join(self.package_folder, "setup_vars_opencv4.cmd"), os.path.join(self.package_folder, "res", "setup_vars_opencv4.cmd")) # TODO: to remove in conan v2 once cmake_find_package* generators removed @@ -533,21 +528,20 @@ def package(self): {component["target"]:"opencv::{}".format(component["target"]) for component in self._opencv_components} ) - @staticmethod - def _create_cmake_module_alias_targets(module_file, targets): + def _create_cmake_module_alias_targets(self, module_file, targets): content = "" for alias, aliased in targets.items(): - content += textwrap.dedent("""\ + content += textwrap.dedent(f"""\ if(TARGET {aliased} AND NOT TARGET {alias}) add_library({alias} INTERFACE IMPORTED) set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased}) endif() - """.format(alias=alias, aliased=aliased)) - tools.save(module_file, content) + """) + save(self, module_file, content) @property def _module_file_rel_path(self): - return os.path.join("lib", "cmake", "conan-official-{}-targets.cmake".format(self.name)) + return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake") # returns true if GTK2 is selected. To do this, the version option # of the gtk/system package is checked or the conan package version @@ -560,7 +554,7 @@ def _is_gtk_version2(self): if gtk_version == "system": return self.options["gtk"].version == 2 else: - return tools.Version(gtk_version) < "3.0.0" + return Version(gtk_version) < "3.0.0" @property def _opencv_components(self): @@ -737,11 +731,11 @@ def package_info(self): def get_lib_name(module): if module == "ippiw": - return "%s%s" % (module, debug) + return f"{module}{debug}" elif module in ("correspondence", "multiview", "numeric"): return module else: - return "opencv_%s%s%s" % (module, version, debug) + return f"opencv_{module}{version}{debug}" def add_components(components): for component in components: @@ -770,15 +764,15 @@ def add_components(components): self.cpp_info.components[conan_component].system_libs.append("mediandk") if not self.options.shared: self.cpp_info.components[conan_component].libdirs.append( - os.path.join("sdk", "native", "staticlibs", tools.to_android_abi(str(self.settings.arch)))) + os.path.join("sdk", "native", "staticlibs", tools_legacy.to_android_abi(str(self.settings.arch)))) if conan_component == "opencv_core": self.cpp_info.components[conan_component].libdirs.append("lib") - self.cpp_info.components[conan_component].libs += tools.collect_libs(self) + self.cpp_info.components[conan_component].libs += collect_libs(self) if self.settings.os in ["iOS", "Macos", "Linux", "Neutrino"]: if not self.options.shared: if conan_component == "opencv_core": - libs = list(filter(lambda x: not x.startswith("opencv"), tools.collect_libs(self))) + libs = list(filter(lambda x: not x.startswith("opencv"), collect_libs(self))) self.cpp_info.components[conan_component].libs += libs # TODO: to remove in conan v2 once cmake_find_package* generators removed @@ -791,11 +785,9 @@ def add_components(components): self.cpp_info.components[conan_component_alias].names["cmake_find_package"] = cmake_component self.cpp_info.components[conan_component_alias].names["cmake_find_package_multi"] = cmake_component self.cpp_info.components[conan_component_alias].requires = [conan_component] + self.cpp_info.components[conan_component_alias].bindirs = [] self.cpp_info.components[conan_component_alias].includedirs = [] self.cpp_info.components[conan_component_alias].libdirs = [] - self.cpp_info.components[conan_component_alias].resdirs = [] - self.cpp_info.components[conan_component_alias].bindirs = [] - self.cpp_info.components[conan_component_alias].frameworkdirs = [] self.cpp_info.set_property("cmake_file_name", "OpenCV") diff --git a/recipes/opencv/4.x/test_package/CMakeLists.txt b/recipes/opencv/4.x/test_package/CMakeLists.txt index 3361bc4c26e1f3..87f27765c5a052 100644 --- a/recipes/opencv/4.x/test_package/CMakeLists.txt +++ b/recipes/opencv/4.x/test_package/CMakeLists.txt @@ -1,8 +1,5 @@ -cmake_minimum_required(VERSION 3.1) -project(test_package) - -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup(TARGETS) +cmake_minimum_required(VERSION 3.8) +project(test_package LANGUAGES CXX) find_package(OpenCV REQUIRED imgcodecs highgui objdetect CONFIG) @@ -22,7 +19,7 @@ if(built_contrib_sfm) endif() add_executable(${PROJECT_NAME} test_package.cpp) -target_link_libraries(${PROJECT_NAME} +target_link_libraries(${PROJECT_NAME} PRIVATE opencv_imgcodecs opencv_highgui opencv_objdetect @@ -30,4 +27,4 @@ target_link_libraries(${PROJECT_NAME} $ $ ) -set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11) diff --git a/recipes/opencv/4.x/test_package/conanfile.py b/recipes/opencv/4.x/test_package/conanfile.py index 1c034b84e089a1..8cbff65ecd5a3a 100644 --- a/recipes/opencv/4.x/test_package/conanfile.py +++ b/recipes/opencv/4.x/test_package/conanfile.py @@ -1,20 +1,33 @@ -from conans import ConanFile, CMake, tools +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout import os class TestPackageConan(ConanFile): settings = "os", "arch", "compiler", "build_type" - generators = "cmake", "cmake_find_package_multi" + 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["built_with_ade"] = self.dependencies["opencv"].options.with_ade + tc.variables["built_with_ffmpeg"] = self.dependencies["opencv"].options.with_ffmpeg + tc.variables["built_contrib_sfm"] = self.dependencies["opencv"].options.contrib and self.dependencies["opencv"].options.contrib_sfm + tc.generate() def build(self): cmake = CMake(self) - cmake.definitions["built_with_ade"] = self.options["opencv"].with_ade - cmake.definitions["built_with_ffmpeg"] = self.options["opencv"].with_ffmpeg - cmake.definitions["built_contrib_sfm"] = self.options["opencv"].contrib and self.options["opencv"].contrib_sfm cmake.configure() cmake.build() def test(self): - if not tools.cross_building(self): - bin_path = os.path.join("bin", "test_package") - self.run(bin_path, run_environment=True) + 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/opencv/4.x/test_v1_package/CMakeLists.txt b/recipes/opencv/4.x/test_v1_package/CMakeLists.txt new file mode 100644 index 00000000000000..bc5a0cfe8a3fc6 --- /dev/null +++ b/recipes/opencv/4.x/test_v1_package/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.8) +project(test_package LANGUAGES CXX) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup(TARGETS) + +find_package(OpenCV REQUIRED imgcodecs highgui objdetect CONFIG) + +option(built_with_ade "Enabled if opencv is built with ade" OFF) +if(built_with_ade) + add_definitions(-DBUILT_WITH_ADE) +endif() + +option(built_with_ffmpeg "Enabled if opencv is built with ffmpeg" OFF) +if(built_with_ffmpeg) + add_definitions(-DBUILT_WITH_FFMPEG) +endif() + +option(built_contrib_sfm "Enabled if opencv is built contrib sfm" OFF) +if(built_contrib_sfm) + add_definitions(-DBUILT_CONTRIB_SFM) +endif() + +add_executable(${PROJECT_NAME} ../test_package/test_package.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE + opencv_imgcodecs + opencv_highgui + opencv_objdetect + $ + $ + $ +) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11) diff --git a/recipes/opencv/4.x/test_v1_package/conanfile.py b/recipes/opencv/4.x/test_v1_package/conanfile.py new file mode 100644 index 00000000000000..1c034b84e089a1 --- /dev/null +++ b/recipes/opencv/4.x/test_v1_package/conanfile.py @@ -0,0 +1,20 @@ +from conans import ConanFile, CMake, tools +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["built_with_ade"] = self.options["opencv"].with_ade + cmake.definitions["built_with_ffmpeg"] = self.options["opencv"].with_ffmpeg + cmake.definitions["built_contrib_sfm"] = self.options["opencv"].contrib and self.options["opencv"].contrib_sfm + cmake.configure() + cmake.build() + + def test(self): + if not tools.cross_building(self): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True)