Repository: conan-io/conan-center-index

Issue:
[package] openexr/all: CMake generators does not reflect installed config files and targets
<!-- 
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.
-->

Yes, this recipe needs Conan components but it can't be fully modeled on CMake side because two config files should be generated, with two different namespaces:

`OpenEXRConfig.cmake`: `OpenEXR::IlmImfConfig`, `OpenEXR::IlmImf`, `OpenEXR::IlmImfUtil`
`IlmBaseConfig.cmake`: `IlmBase::IlmBaseConfig`, `IlmBase::Half`, `IlmBase::Iex`, `IlmBase::IexMath`, `IlmBase::Imath`, `IlmBase::IlmThread`

_Originally posted by @SpaceIm in https://github.com/conan-io/conan-center-index/pull/3454#issuecomment-723994868_

### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **openexr/1.2.8**
  * Operating System+version: **Linux Ubuntu 18.04**
  * Compiler+version: **GCC 8**
  * Docker image: **conanio/gcc8**
  * Conan version: **conan 1.30.0**
  * Python version: **Python 3.7.4**


### Conan profile (output of `conan profile show default` or `conan profile show <profile>` if custom profile is in use)
https://c3i.jfrog.io/c3i/misc/logs/pr/3454/1/openexr/2.5.3/10ebe76393760b5ddd19f7b1726da594a3050f23/profile.txt

### Steps to reproduce (Include if Applicable)
_CI is sufficient_


### Logs (Include/Attach if Applicable)
<details><summary>Click to expand log</summary>

https://c3i.jfrog.io/c3i/misc/logs/pr/3454/1/openexr/2.5.3/10ebe76393760b5ddd19f7b1726da594a3050f23/create_stdout.txt

</details>



Discussion:

Resolving patch:
diff --git a/recipes/openexr/3.x/conanfile.py b/recipes/openexr/3.x/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/openexr/3.x/conanfile.py
@@ -0,0 +1,127 @@
+from conans import ConanFile, CMake, tools
+import os, functools
+
+class OpenEXRConan(ConanFile):
+    name = "openexr"
+    description = "OpenEXR is a high dynamic-range (HDR) image file format developed by Industrial Light & " \
+                  "Magic for use in computer imaging applications."
+    topics = ("conan", "openexr", "hdr", "image", "picture")
+    license = "BSD-3-Clause"
+    homepage = "https://github.com/AcademySoftwareFoundation/openexr"
+    url = "https://github.com/conan-io/conan-center-index"
+    settings = "os", "arch", "compiler", "build_type"
+    options = {"shared": [True, False], "fPIC": [True, False]}
+    default_options = {"shared": False, "fPIC": True}
+    generators = "cmake", "cmake_find_package"
+    exports_sources = ["CMakeLists.txt", "patches/*.patch"]
+
+    @property
+    def _source_subfolder(self):
+        return "source_subfolder"
+
+    @property
+    def _build_subfolder(self):
+        return "build_subfolder"
+
+    def config_options(self):
+        if self.settings.os == "Windows":
+            del self.options.fPIC
+
+    def configure(self):
+        if self.options.shared:
+            del self.options.fPIC
+
+    def validate(self):
+        if self.settings.compiler.get_safe("cppstd"):
+            tools.check_min_cppstd(self, 11)
+
+    def requirements(self):
+        self.requires("zlib/1.2.11")
+
+        # Note: OpenEXR and Imath are versioned independently.
+        self.requires("imath/3.1.4")
+
+    def source(self):
+        tools.get(**self.conan_data["sources"][self.version],
+                  destination=self._source_subfolder, strip_root=True)
+
+    @functools.lru_cache(1)
+    def _configure_cmake(self):
+        cmake = CMake(self)
+        cmake.definitions["OPENEXR_INSTALL_EXAMPLES"] = False
+        cmake.definitions["DOCS"] = False
+        cmake.configure(build_folder=self._build_subfolder)
+        return cmake
+
+    def build(self):
+        for patch in self.conan_data["patches"][self.version]:
+            tools.patch(**patch)
+
+        cmake = self._configure_cmake()
+        cmake.build()
+
+    def package(self):
+        self.copy("LICENSE.md", src=self._source_subfolder, dst="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, "lib", "cmake"))
+
+    def _add_component(self, name):
+        component = self.cpp_info.components[name]
+        component.names["cmake_find_package"] = name
+        component.names["cmake_find_package_multi"] = name
+        return component
+
+    def package_info(self):
+        self.cpp_info.names["cmake_find_package"] = "OpenEXR"
+        self.cpp_info.names["cmake_find_package_multi"] = "OpenEXR"
+        self.cpp_info.names["pkg_config"] = "openexr"
+
+        openexr_version = tools.Version(self.version)
+        lib_suffix = "-{}_{}".format(openexr_version.major, openexr_version.minor)
+        if self.settings.build_type == "Debug":
+            lib_suffix += "_d"
+
+        # OpenEXR::OpenEXRConfig
+        OpenEXRConfig = self._add_component("OpenEXRConfig")
+        OpenEXRConfig.includedirs.append(os.path.join("include", "OpenEXR"))
+
+        # OpenEXR::IexConfig
+        IexConfig = self._add_component("IexConfig")
+        IexConfig.includedirs = OpenEXRConfig.includedirs
+
+        # OpenEXR::IlmThreadConfig
+        IlmThreadConfig = self._add_component("IlmThreadConfig")
+        IlmThreadConfig.includedirs = OpenEXRConfig.includedirs
+
+        # OpenEXR::Iex
+        Iex = self._add_component("Iex")
+        Iex.libs = ["Iex{}".format(lib_suffix)]
+        Iex.requires = ["IexConfig"]
+
+        # OpenEXR::IlmThread
+        IlmThread = self._add_component("IlmThread")
+        IlmThread.libs = ["IlmThread{}".format(lib_suffix)]
+        IlmThread.requires = ["IlmThreadConfig", "Iex"]
+        if self.settings.os == "Linux":
+            IlmThread.system_libs = ["pthread"]
+
+        # OpenEXR::OpenEXRCore
+        OpenEXRCore = self._add_component("OpenEXRCore")
+        OpenEXRCore.libs = ["OpenEXRCore{}".format(lib_suffix)]
+        OpenEXRCore.requires = ["OpenEXRConfig", "zlib::zlib"]
+
+        # OpenEXR::OpenEXR
+        OpenEXR = self._add_component("OpenEXR")
+        OpenEXR.libs = ["OpenEXR{}".format(lib_suffix)]
+        OpenEXR.requires = ["OpenEXRCore", "IlmThread", "Iex", "imath::imath"]
+
+        # OpenEXR::OpenEXRUtil
+        OpenEXRUtil = self._add_component("OpenEXRUtil")
+        OpenEXRUtil.libs = ["OpenEXRUtil{}".format(lib_suffix)]
+        OpenEXRUtil.requires = ["OpenEXR"]
+
+        # Add tools directory to PATH
+        self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))


Test patch:
diff --git a/recipes/openexr/3.x/test_package/CMakeLists.txt b/recipes/openexr/3.x/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/openexr/3.x/test_package/CMakeLists.txt
@@ -0,0 +1,11 @@
+cmake_minimum_required(VERSION 3.1)
+project(test_package)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup(TARGETS)
+
+find_package(OpenEXR REQUIRED CONFIG)
+
+add_executable(${PROJECT_NAME} test_package.cpp)
+target_link_libraries(${PROJECT_NAME} OpenEXR::OpenEXR)
+set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
diff --git a/recipes/openexr/3.x/test_package/comp_short_decode_piz.exr b/recipes/openexr/3.x/test_package/comp_short_decode_piz.exr
new file mode 100644
Binary files /dev/null and b/recipes/openexr/3.x/test_package/comp_short_decode_piz.exr differ
diff --git a/recipes/openexr/3.x/test_package/conanfile.py b/recipes/openexr/3.x/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/openexr/3.x/test_package/conanfile.py
@@ -0,0 +1,17 @@
+from conans import ConanFile, CMake, tools
+import os
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "arch", "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.settings):
+            bin_path = os.path.join("bin", "test_package")
+            imgfile = os.path.join(self.source_folder, "comp_short_decode_piz.exr")
+            self.run("{} {}".format(bin_path, imgfile), run_environment=True)
diff --git a/recipes/openexr/3.x/test_package/test_package.cpp b/recipes/openexr/3.x/test_package/test_package.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/openexr/3.x/test_package/test_package.cpp
@@ -0,0 +1,18 @@
+#include <iostream>
+#include <cstdlib>
+
+#include <ImfRgbaFile.h>
+#include <ImfArray.h>
+#include <half.h>
+
+int main (int argc, char *argv[])
+{
+    if (argc < 2)
+        return EXIT_FAILURE;
+    Imf::RgbaInputFile input_file(argv[1]);
+    const Imf::Header& header = input_file.header();
+    size_t width = header.dataWindow().max.x - header.dataWindow().min.x + 1;
+    size_t height = header.dataWindow().max.y - header.dataWindow().min.y + 1;
+    std::cout << "OpenEXR images size is " << width << "x" << height << std::endl;
+    return EXIT_SUCCESS;
+}