From 7cce6b04943007a0074149096bbfee181ae9f654 Mon Sep 17 00:00:00 2001
From: ski <ski@opencascade.com>
Date: Thu, 24 Aug 2017 13:11:41 +0300
Subject: [PATCH] 0027409: Provide API header files for OCCT

Added possibility to generate API header files for toolkits and modules.
---
 CMakeLists.txt              |  6 +++
 adm/cmake/occt_macros.cmake | 82 ++++++++++++++++++++++++++++++++++++-
 adm/cmake/vardescr.cmake    |  3 ++
 adm/templates/headerAPI.in  | 13 ++++++
 4 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 adm/templates/headerAPI.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index eac91c2e8f..182892b9ab 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -112,6 +112,12 @@ if (BUILD_USE_PCH)
 
 endif()
 
+# Generate Toolkits/Modules API header files
+if (NOT BUILD_API_HEADER_FILES)
+  set (BUILD_API_HEADER_FILES "Do not generate" CACHE STRING "${BUILD_API_HEADER_FILES_DESCR}" FORCE)
+  set_property(CACHE BUILD_API_HEADER_FILES PROPERTY STRINGS "Do not generate" "One per toolkit" "One per module" "One per toolkit and module")
+endif()
+
 # copy samples to install directory
 set (INSTALL_SAMPLES OFF CACHE BOOL "${INSTALL_SAMPLES_DESCR}")
 
diff --git a/adm/cmake/occt_macros.cmake b/adm/cmake/occt_macros.cmake
index 65ac4d4043..c4ea2fb215 100644
--- a/adm/cmake/occt_macros.cmake
+++ b/adm/cmake/occt_macros.cmake
@@ -223,6 +223,20 @@ macro (OCCT_CONFIGURE_AND_INSTALL BEING_CONGIRUGED_FILE BUILD_NAME INSTALL_NAME
 endmacro()
 
 macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOOLKITS OCCT_COLLECT_SOURCE_DIR OCCT_INSTALL_DIR_PREFIX)
+  # Remove all API header files
+  foreach (OCCT_MODULE ${OCCT_MODULES})
+    set (OCCT_MODULE_API_HEADER_FILE ${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_MODULE}API.h)
+    if (EXISTS "${OCCT_MODULE_API_HEADER_FILE}")
+      file (REMOVE "${OCCT_MODULE_API_HEADER_FILE}")
+    endif()
+    foreach(OCCT_TOOLKIT ${${OCCT_MODULE}_TOOLKITS})
+      set (OCCT_TOOLKIT_API_HEADER_FILE ${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_TOOLKIT}API.h)
+      if (EXISTS "${OCCT_TOOLKIT_API_HEADER_FILE}")
+        file (REMOVE "${OCCT_TOOLKIT_API_HEADER_FILE}")
+      endif()
+    endforeach()
+  endforeach()
+
   set (OCCT_USED_PACKAGES)
 
   # consider patched header.in template
@@ -243,6 +257,21 @@ macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOO
     endif()
 
     list (APPEND OCCT_USED_PACKAGES ${OCCT_TOOLKIT_PACKAGES})
+
+    # Get packages parent toolkits
+    set (OCCT_${OCCT_USED_TOOLKIT}_PACKAGES ${OCCT_TOOLKIT_PACKAGES})
+    foreach (OCCT_PACKAGE ${OCCT_TOOLKIT_PACKAGES})
+      set (OCCT_PACKAGE_${OCCT_PACKAGE}_PARENT ${OCCT_USED_TOOLKIT})
+    endforeach()
+
+    # Get toolkits parent modules
+    foreach (MODULE ${OCCT_MODULES})
+      set (TOOLKITS ${${MODULE}_TOOLKITS})
+      list (FIND TOOLKITS ${OCCT_USED_TOOLKIT} FOUND)
+      if (NOT ${FOUND} EQUAL -1)
+        set (OCCT_${OCCT_USED_TOOLKIT}_PARENT ${MODULE})
+      endif()
+    endforeach(MODULE)
   endforeach()
 
   list (REMOVE_DUPLICATES OCCT_USED_PACKAGES)
@@ -255,6 +284,9 @@ macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOO
   message (STATUS "Info: \(${CURRENT_TIME}\) Compare FILES with files in package directories...")
 
   foreach (OCCT_PACKAGE ${OCCT_USED_PACKAGES})
+    if (NOT "x${BUILD_API_HEADER_FILES}" STREQUAL "xDo not generate")
+      set (OCCT_${OCCT_PACKAGE_${OCCT_PACKAGE}_PARENT}_HEADER_FILES "${OCCT_${OCCT_PACKAGE_${OCCT_PACKAGE}_PARENT}_HEADER_FILES}\n// ${OCCT_PACKAGE}")
+    endif()
     if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES")
       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" OCCT_ALL_FILE_NAMES)
     elseif (EXISTS "${OCCT_COLLECT_SOURCE_DIR}/${OCCT_PACKAGE}/FILES")
@@ -299,6 +331,10 @@ macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOO
           if (IS_HEADER_FOUND)
             list (APPEND OCCT_HEADER_FILES_COMPLETE ${OCCT_FILE_IN_DIR})
 
+            if (NOT "x${BUILD_API_HEADER_FILES}" STREQUAL "xDo not generate")
+              set (OCCT_${OCCT_PACKAGE_${OCCT_PACKAGE}_PARENT}_HEADER_FILES "${OCCT_${OCCT_PACKAGE_${OCCT_PACKAGE}_PARENT}_HEADER_FILES}\n#include \"${OCCT_FILE_IN_DIR_NAME}\"")
+            endif()
+
             # collect header files with name that does not contain its package one
             string (FIND "${OCCT_FILE_NAME}" "${OCCT_PACKAGE}_" FOUND_INDEX)
             if (NOT ${FOUND_INDEX} EQUAL 0)
@@ -324,7 +360,7 @@ macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOO
       endif()
     endforeach()
   endforeach()
-  
+
   # create new file including found header
   string(TIMESTAMP CURRENT_TIME "%H:%M:%S")
   message (STATUS "Info: \(${CURRENT_TIME}\) Create header-links in inc folder...")
@@ -369,6 +405,50 @@ macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOO
       endif()
     endif()
   endforeach()
+
+  string(TIMESTAMP CURRENT_TIME "%H:%M:%S")
+  message (STATUS "Info: \(${CURRENT_TIME}\) Creating API headers in inc folder...")
+
+  if ( "x${BUILD_API_HEADER_FILES}" STREQUAL "xOne per toolkit" OR
+       "x${BUILD_API_HEADER_FILES}" STREQUAL "xOne per toolkit and module")
+    foreach (OCCT_USED_TOOLKIT ${OCCT_BUILD_TOOLKITS})
+      set (OCCT_API_HEADER_FILE_CONTENT "${OCCT_${OCCT_USED_TOOLKIT}_HEADER_FILES}")
+      configure_file("${CMAKE_SOURCE_DIR}/adm/templates/headerAPI.in" "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_USED_TOOLKIT}API.h" @ONLY)
+      install (FILES "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_USED_TOOLKIT}API.h" DESTINATION "${INSTALL_DIR_INCLUDE}")
+    endforeach()
+  endif()
+
+  if ( "x${BUILD_API_HEADER_FILES}" STREQUAL "xOne per toolkit and module")
+    set (OCCT_USED_MODULES)
+    foreach (OCCT_USED_TOOLKIT ${OCCT_BUILD_TOOLKITS})
+      list (APPEND OCCT_USED_MODULES ${OCCT_${OCCT_USED_TOOLKIT}_PARENT})
+      set (${OCCT_${OCCT_USED_TOOLKIT}_PARENT}_HEADER_FILES "${${OCCT_${OCCT_USED_TOOLKIT}_PARENT}_HEADER_FILES}\n#include \"${OCCT_USED_TOOLKIT}API.h\"")
+    endforeach()
+
+    list (REMOVE_DUPLICATES OCCT_USED_MODULES)
+    foreach (OCCT_USED_MODULE ${OCCT_USED_MODULES})
+      set (OCCT_API_HEADER_FILE_CONTENT "${${OCCT_USED_MODULE}_HEADER_FILES}")
+      configure_file("${CMAKE_SOURCE_DIR}/adm/templates/headerAPI.in" "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_USED_MODULE}API.h" @ONLY)
+      install (FILES "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_USED_MODULE}API.h" DESTINATION "${INSTALL_DIR_INCLUDE}")
+    endforeach()
+  endif()
+
+  if ( "x${BUILD_API_HEADER_FILES}" STREQUAL "xOne per module")
+    set (OCCT_USED_MODULES)
+    set (LINE_BREAK "")
+    foreach (OCCT_USED_TOOLKIT ${OCCT_BUILD_TOOLKITS})
+      list (APPEND OCCT_USED_MODULES ${OCCT_${OCCT_USED_TOOLKIT}_PARENT})
+      set (${OCCT_${OCCT_USED_TOOLKIT}_PARENT}_HEADER_FILES "${${OCCT_${OCCT_USED_TOOLKIT}_PARENT}_HEADER_FILES}${LINE_BREAK}\n// ${OCCT_USED_TOOLKIT}\n${OCCT_${OCCT_USED_TOOLKIT}_HEADER_FILES}")
+      set (LINE_BREAK "\n")
+    endforeach()
+
+    list (REMOVE_DUPLICATES OCCT_USED_MODULES)
+    foreach (OCCT_USED_MODULE ${OCCT_USED_MODULES})
+      set (OCCT_API_HEADER_FILE_CONTENT "${${OCCT_USED_MODULE}_HEADER_FILES}")
+      configure_file("${CMAKE_SOURCE_DIR}/adm/templates/headerAPI.in" "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_USED_MODULE}API.h" @ONLY)
+      install (FILES "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${OCCT_USED_MODULE}API.h" DESTINATION "${INSTALL_DIR_INCLUDE}")
+    endforeach()
+  endif()
 endmacro()
 
 macro (OCCT_COPY_FILE_OR_DIR BEING_COPIED_OBJECT DESTINATION_PATH)
diff --git a/adm/cmake/vardescr.cmake b/adm/cmake/vardescr.cmake
index a21807038f..54ec4e5324 100644
--- a/adm/cmake/vardescr.cmake
+++ b/adm/cmake/vardescr.cmake
@@ -37,6 +37,9 @@ set (BUILD_USE_PCH_DESCR
 "Use precompiled headers to accelerate the build.
 Precompiled headers are generated automatically by Cotire tool.")
 
+set (BUILD_API_HEADER_FILES_DESCR
+"Generate API header files, one per toolkit and / or module, including all API headers in corresponding toolkit or module.")
+
 # install variables
 set (INSTALL_DIR_DESCR 
 "The place where built OCCT libraries, headers, test cases (INSTALL_TEST_CASES variable),
diff --git a/adm/templates/headerAPI.in b/adm/templates/headerAPI.in
new file mode 100644
index 0000000000..938ca696a3
--- /dev/null
+++ b/adm/templates/headerAPI.in
@@ -0,0 +1,13 @@
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+@OCCT_API_HEADER_FILE_CONTENT@
-- 
2.19.1.windows.1

