From ce70bbd7305ece9276a81676578b9d7cf2d69ada Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Achard?= <remiachard@gmail.com>
Date: Tue, 15 Jun 2021 05:19:53 +0100
Subject: [PATCH] Add OpenColorIOConfig.cmake generation (#1397)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Initial OpenColorIO Config CMake implementation

Signed-off-by: Rémi Achard <remiachard@gmail.com>

* Remove macro, improve documentation

Signed-off-by: Rémi Achard <remiachard@gmail.com>

Co-authored-by: Patrick Hodoul <patrick.hodoul@autodesk.com>
---
 CMakeLists.txt                     | 39 ++++++++++++++++++++++++++++++
 include/OpenColorIO/CMakeLists.txt | 21 +++++++++++++---
 src/OpenColorIO/CMakeLists.txt     |  8 +++---
 src/bindings/python/CMakeLists.txt |  2 +-
 src/cmake/Config.cmake.in          |  5 ++++
 tests/cpu/CMakeLists.txt           |  2 +-
 6 files changed, 66 insertions(+), 11 deletions(-)
 create mode 100644 src/cmake/Config.cmake.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c33067646d..b8e18214b4 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -265,3 +265,42 @@ configure_file(${CMAKE_SOURCE_DIR}/share/ocio/${OCIO_SETUP_NAME}.in
     ${CMAKE_CURRENT_BINARY_DIR}/share/ocio/${OCIO_SETUP_NAME} @ONLY)
 
 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/share/ocio/${OCIO_SETUP_NAME} DESTINATION share/ocio/)
+
+
+###############################################################################
+# Generate OpenColorIO-config.cmake
+# Targets exported to ${PROJECT_NAME}_EXPORTED_TARGETS will be available for
+# import using find_package(OpenColorIO).
+
+include(CMakePackageConfigHelpers)
+
+set(OCIO_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets.cmake")
+set(OCIO_VERSION_CONFIG "${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
+set(OCIO_PROJECT_CONFIG "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake")
+set(OCIO_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
+
+# Version fetched from the top level project()
+write_basic_package_version_file(
+    ${OCIO_VERSION_CONFIG}
+    # Version range supported only if generated by CMake 3.19.2 or newer.
+    COMPATIBILITY SameMajorVersion
+)
+
+configure_package_config_file(
+    ${CMAKE_CURRENT_LIST_DIR}/src/cmake/Config.cmake.in ${OCIO_PROJECT_CONFIG}
+    INSTALL_DESTINATION ${OCIO_CONFIG_INSTALL_DIR}
+    NO_SET_AND_CHECK_MACRO
+    NO_CHECK_REQUIRED_COMPONENTS_MACRO
+)
+
+install(
+    EXPORT ${PROJECT_NAME}_EXPORTED_TARGETS
+    DESTINATION ${OCIO_CONFIG_INSTALL_DIR}
+    NAMESPACE ${PROJECT_NAME}::
+    FILE ${OCIO_TARGETS_EXPORT_NAME}
+)
+
+install(
+    FILES "${OCIO_PROJECT_CONFIG}" "${OCIO_VERSION_CONFIG}"
+    DESTINATION "${OCIO_CONFIG_INSTALL_DIR}"
+)
diff --git a/include/OpenColorIO/CMakeLists.txt b/include/OpenColorIO/CMakeLists.txt
index e4c8a79ffb..64980f661d 100644
--- a/include/OpenColorIO/CMakeLists.txt
+++ b/include/OpenColorIO/CMakeLists.txt
@@ -2,13 +2,22 @@
 # Copyright Contributors to the OpenColorIO Project.
 
 configure_file("OpenColorABI.h.in" "${CMAKE_CURRENT_BINARY_DIR}/OpenColorABI.h" @ONLY)
-add_library(public_api INTERFACE)
+add_library(OpenColorIOHeaders INTERFACE)
 
-set_property(TARGET public_api 
-	PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/..;${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_BINARY_DIR}/OpenColorIO
+set(BUILD_INCLUDES
+    ${CMAKE_CURRENT_SOURCE_DIR}/..
+    ${CMAKE_CURRENT_SOURCE_DIR}
+    ${CMAKE_CURRENT_BINARY_DIR}
+    ${CMAKE_CURRENT_BINARY_DIR}/OpenColorIO
 )
 
-add_dependencies(public_api "${CMAKE_CURRENT_BINARY_DIR}/OpenColorABI.h")
+target_include_directories(OpenColorIOHeaders
+    INTERFACE
+        "$<BUILD_INTERFACE:${BUILD_INCLUDES}>"
+        "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+)
+
+add_dependencies(OpenColorIOHeaders "${CMAKE_CURRENT_BINARY_DIR}/OpenColorABI.h")
 
 # public interface
 
@@ -17,3 +26,7 @@ list(APPEND core_export_headers ${CMAKE_CURRENT_BINARY_DIR}/OpenColorABI.h)
 
 install(FILES ${core_export_headers}
     DESTINATION include/OpenColorIO)
+
+install(TARGETS OpenColorIOHeaders
+    EXPORT ${PROJECT_NAME}_EXPORTED_TARGETS
+)
diff --git a/src/OpenColorIO/CMakeLists.txt b/src/OpenColorIO/CMakeLists.txt
index 2ce801e680..48061251b4 100755
--- a/src/OpenColorIO/CMakeLists.txt
+++ b/src/OpenColorIO/CMakeLists.txt
@@ -182,9 +182,6 @@ if(NOT WIN32)
     configure_file(res/OpenColorIO.pc.in ${CMAKE_CURRENT_BINARY_DIR}/OpenColorIO.pc @ONLY)
     install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenColorIO.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
 endif()
-
-# TODO: Install the OpenColorIO-config.cmake.
-
 add_library(OpenColorIO ${SOURCES})
 
 if(BUILD_SHARED_LIBS AND WIN32)
@@ -199,12 +196,12 @@ if(BUILD_SHARED_LIBS AND WIN32)
 endif()
 
 target_include_directories(OpenColorIO
-	PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
+    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
 )
 
 target_link_libraries(OpenColorIO
 	PUBLIC
-		public_api
+		OpenColorIOHeaders
 	PRIVATE
 		expat::expat
 		IlmBase::Half
@@ -279,6 +276,7 @@ if(MSVC AND BUILD_SHARED_LIBS)
 endif()
 
 install(TARGETS OpenColorIO
+	EXPORT ${PROJECT_NAME}_EXPORTED_TARGETS
 	LIBRARY DESTINATION lib
 	RUNTIME DESTINATION bin
 	ARCHIVE DESTINATION lib
diff --git a/src/bindings/python/CMakeLists.txt b/src/bindings/python/CMakeLists.txt
index 3e9d70fd46..8690ea4253 100644
--- a/src/bindings/python/CMakeLists.txt
+++ b/src/bindings/python/CMakeLists.txt
@@ -191,7 +191,7 @@ target_include_directories(PyOpenColorIO
 # macOS, to prevent segfaults when potentially working with multiple Python 
 # installations. See note in:
 #   https://pybind11.readthedocs.io/en/stable/compiling.html#building-manually
-set(PYOCIO_LINK_PUBLIC public_api)
+set(PYOCIO_LINK_PUBLIC OpenColorIOHeaders)
 if(WIN32)
 	list(INSERT PYOCIO_LINK_PUBLIC 0 ${Python_LIBRARIES})
 endif()
diff --git a/src/cmake/Config.cmake.in b/src/cmake/Config.cmake.in
new file mode 100644
index 0000000000..eebb2c1900
--- /dev/null
+++ b/src/cmake/Config.cmake.in
@@ -0,0 +1,5 @@
+@PACKAGE_INIT@
+
+include(CMakeFindDependencyMacro)
+
+include(${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake)
diff --git a/tests/cpu/CMakeLists.txt b/tests/cpu/CMakeLists.txt
index 6738996281..cb03e1da95 100755
--- a/tests/cpu/CMakeLists.txt
+++ b/tests/cpu/CMakeLists.txt
@@ -15,7 +15,7 @@ function(add_ocio_test NAME SOURCES PRIVATE_INCLUDES)
     )
     target_link_libraries(${TEST_BINARY}
         PUBLIC
-            public_api
+            OpenColorIOHeaders
         PRIVATE
             expat::expat
             IlmBase::Half