0030948: Configuration, CMake - problem building with CMake 3.15.2
[occt.git] / adm / cmake / occt_macros.cmake
index 0e87d9a..8220ecd 100644 (file)
@@ -1,4 +1,11 @@
-#
+##
+
+if(OCCT_MACROS_ALREADY_INCLUDED)
+  return()
+endif()
+set(OCCT_MACROS_ALREADY_INCLUDED 1)
+
+
 macro (OCCT_CHECK_AND_UNSET VARNAME)
   if (DEFINED ${VARNAME})
     unset (${VARNAME} CACHE)
@@ -19,6 +26,18 @@ macro (OCCT_CHECK_AND_UNSET_GROUP VARNAME)
   endif()
 endmacro()
 
+macro (OCCT_CHECK_AND_UNSET_INSTALL_DIR_SUBDIRS)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_BIN)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_SCRIPT)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_LIB)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_INCLUDE)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_RESOURCE)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_DATA)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_SAMPLES)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_TESTS)
+  OCCT_CHECK_AND_UNSET (INSTALL_DIR_DOC)
+endmacro()
+
 # COMPILER_BITNESS variable
 macro (OCCT_MAKE_COMPILER_BITNESS)
   math (EXPR COMPILER_BITNESS "32 + 32*(${CMAKE_SIZEOF_VOID_P}/8)")
@@ -41,20 +60,27 @@ endmacro()
 # COMPILER variable
 macro (OCCT_MAKE_COMPILER_SHORT_NAME)
   if (MSVC)
-    if (MSVC70)
+    if ((MSVC_VERSION EQUAL 1300) OR (MSVC_VERSION EQUAL 1310))
       set (COMPILER vc7)
-    elseif (MSVC80)
+    elseif (MSVC_VERSION EQUAL 1400)
       set (COMPILER vc8)
-    elseif (MSVC90)
+    elseif (MSVC_VERSION EQUAL 1500)
       set (COMPILER vc9)
-    elseif (MSVC10)
+    elseif (MSVC_VERSION EQUAL 1600)
       set (COMPILER vc10)
-    elseif (MSVC11)
+    elseif (MSVC_VERSION EQUAL 1700)
       set (COMPILER vc11)
-    elseif (MSVC12)
+    elseif (MSVC_VERSION EQUAL 1800)
       set (COMPILER vc12)
-    elseif (MSVC14)
+    elseif (MSVC_VERSION EQUAL 1900)
+      set (COMPILER vc14)
+    elseif ((MSVC_VERSION GREATER 1900) AND (MSVC_VERSION LESS 2000))
+      # Since Visual Studio 15 (2017), its version diverged from version of
+      # compiler which is 14.1; as that compiler uses the same run-time as 14.0,
+      # we keep its id as "vc14" to be compatibille
       set (COMPILER vc14)
+    else()
+      message (FATAL_ERROR "Unrecognized MSVC_VERSION")
     endif()
   elseif (DEFINED CMAKE_COMPILER_IS_GNUCC)
     set (COMPILER gcc)
@@ -82,24 +108,51 @@ function (SUBDIRECTORY_NAMES MAIN_DIRECTORY RESULT)
   set (${RESULT} ${LOCAL_RESULT} PARENT_SCOPE)
 endfunction()
 
+function (FIND_SUBDIRECTORY ROOT_DIRECTORY DIRECTORY_SUFFIX SUBDIRECTORY_NAME)
+  #message("Trying to find directory with suffix ${DIRECTORY_SUFFIX} in ${ROOT_DIRECTORY}")
+  SUBDIRECTORY_NAMES ("${ROOT_DIRECTORY}" SUBDIR_NAME_LIST)
+  #message("Subdirectories: ${SUBDIR_NAME_LIST}")
+
+  #set(${SUBDIRECTORY_NAME} "${SUBDIR_NAME_LIST}" PARENT_SCOPE)
+
+  foreach (SUBDIR_NAME ${SUBDIR_NAME_LIST})
+    #message("Subdir: ${SUBDIR_NAME}, ${DIRECTORY_SUFFIX}")
+    # REGEX failed if the directory name contains '++' combination, so we replace it
+    string(REPLACE "+" "\\+" SUBDIR_NAME_ESCAPED ${SUBDIR_NAME})
+    string (REGEX MATCH "${SUBDIR_NAME_ESCAPED}" DOES_PATH_CONTAIN "${DIRECTORY_SUFFIX}")
+    if (DOES_PATH_CONTAIN)
+      set(${SUBDIRECTORY_NAME} "${ROOT_DIRECTORY}/${SUBDIR_NAME}" PARENT_SCOPE)
+      #message("Subdirectory is found: ${SUBDIRECTORY_NAME}")
+      BREAK()
+    else()
+      #message("Check directory: ${ROOT_DIRECTORY}/${SUBDIR_NAME}")
+      FIND_SUBDIRECTORY ("${ROOT_DIRECTORY}/${SUBDIR_NAME}" "${DIRECTORY_SUFFIX}" SUBDIR_REC_NAME)
+      if (NOT "${SUBDIR_REC_NAME}" STREQUAL "")
+        set(${SUBDIRECTORY_NAME} "${SUBDIR_REC_NAME}" PARENT_SCOPE)
+        #message("Subdirectory is found: ${SUBDIRECTORY_NAME}")
+        BREAK()
+      endif()
+    endif()
+  endforeach()
+endfunction()
+
 function (OCCT_ORIGIN_AND_PATCHED_FILES RELATIVE_PATH SEARCH_TEMPLATE RESULT)
 
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${RELATIVE_PATH}")
-    file (GLOB FOUND_FILES "${APPLY_OCCT_PATCH_DIR}/${RELATIVE_PATH}/${SEARCH_TEMPLATE}")
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${RELATIVE_PATH}")
+    file (GLOB FOUND_FILES "${BUILD_PATCH}/${RELATIVE_PATH}/${SEARCH_TEMPLATE}")
   endif()
 
   file (GLOB ORIGIN_FILES "${CMAKE_SOURCE_DIR}/${RELATIVE_PATH}/${SEARCH_TEMPLATE}")
   foreach (ORIGIN_FILE ${ORIGIN_FILES})
     # check for existence of patched version of current file
-    if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${RELATIVE_PATH}")
+    if (NOT BUILD_PATCH OR NOT EXISTS "${BUILD_PATCH}/${RELATIVE_PATH}")
+      list (APPEND FOUND_FILES ${ORIGIN_FILE})
+    else()
       get_filename_component (ORIGIN_FILE_NAME "${ORIGIN_FILE}" NAME)
-      if (EXISTS "${APPLY_OCCT_PATCH_DIR}/${RELATIVE_PATH}/${ORIGIN_FILE_NAME}")
-        continue()
+      if (NOT EXISTS "${BUILD_PATCH}/${RELATIVE_PATH}/${ORIGIN_FILE_NAME}")
+        list (APPEND FOUND_FILES ${ORIGIN_FILE})
       endif()
     endif()
-
-    # append origin version if patched one is not found
-    list (APPEND FOUND_FILES ${ORIGIN_FILE})
   endforeach()
 
   set (${RESULT} ${FOUND_FILES} PARENT_SCOPE)
@@ -110,17 +163,18 @@ function (FIND_PRODUCT_DIR ROOT_DIR PRODUCT_NAME RESULT)
   OCCT_MAKE_COMPILER_BITNESS()
 
   string (TOLOWER "${PRODUCT_NAME}" lower_PRODUCT_NAME)
-
-  list (APPEND SEARCH_TEMPLATES "^${lower_PRODUCT_NAME}.*${COMPILER}.*${COMPILER_BITNESS}")
-  list (APPEND SEARCH_TEMPLATES "^${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER}.*${COMPILER_BITNESS}")
-  list (APPEND SEARCH_TEMPLATES "^${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER_BITNESS}")
-  list (APPEND SEARCH_TEMPLATES "^${lower_PRODUCT_NAME}.*[0-9.]+")
-  list (APPEND SEARCH_TEMPLATES "^${lower_PRODUCT_NAME}")
-  list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*${COMPILER}.*${COMPILER_BITNESS}")
-  list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER}.*${COMPILER_BITNESS}")
-  list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER_BITNESS}")
-  list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+")
-  list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}")
+  if ("${lower_PRODUCT_NAME}" STREQUAL "egl")
+    string (SUBSTRING "${lower_PRODUCT_NAME}" 1 -1 lower_PRODUCT_NAME)
+    list (APPEND SEARCH_TEMPLATES "[^gl]+${lower_PRODUCT_NAME}.*")
+  else()
+    list (APPEND SEARCH_TEMPLATES "^[^a-zA-Z]*${lower_PRODUCT_NAME}[^a-zA-Z]*${COMPILER}.*${COMPILER_BITNESS}")
+    list (APPEND SEARCH_TEMPLATES "^[^a-zA-Z]*${lower_PRODUCT_NAME}[^a-zA-Z]*[0-9.]+.*${COMPILER}.*${COMPILER_BITNESS}")
+    list (APPEND SEARCH_TEMPLATES "^[a-zA-Z]*[0-9]*-${lower_PRODUCT_NAME}[^a-zA-Z]*[0-9.]+.*${COMPILER}.*${COMPILER_BITNESS}")
+    list (APPEND SEARCH_TEMPLATES "^[^a-zA-Z]*${lower_PRODUCT_NAME}[^a-zA-Z]*[0-9.]+.*${COMPILER_BITNESS}")
+    list (APPEND SEARCH_TEMPLATES "^[^a-zA-Z]*${lower_PRODUCT_NAME}[^a-zA-Z]*.*${COMPILER_BITNESS}")
+    list (APPEND SEARCH_TEMPLATES "^[^a-zA-Z]*${lower_PRODUCT_NAME}[^a-zA-Z]*[0-9.]+")
+    list (APPEND SEARCH_TEMPLATES "^[^a-zA-Z]*${lower_PRODUCT_NAME}[^a-zA-Z]*")
+  endif()
 
   SUBDIRECTORY_NAMES ("${ROOT_DIR}" SUBDIR_NAME_LIST)
 
@@ -140,23 +194,21 @@ function (FIND_PRODUCT_DIR ROOT_DIR PRODUCT_NAME RESULT)
   endforeach()
 
   if (LOCAL_RESULT)
-    list (LENGTH "${LOCAL_RESULT}" LOC_LEN)
-    math (EXPR LAST_ELEMENT_INDEX "${LOC_LEN}-1")
-    list (GET LOCAL_RESULT ${LAST_ELEMENT_INDEX} DUMMY)
+    list (GET LOCAL_RESULT -1 DUMMY)
     set (${RESULT} ${DUMMY} PARENT_SCOPE)
   endif()
 endfunction()
 
 macro (OCCT_INSTALL_FILE_OR_DIR BEING_INSTALLED_OBJECT DESTINATION_PATH)
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${BEING_INSTALLED_OBJECT}")
-    if (IS_DIRECTORY "${APPLY_OCCT_PATCH_DIR}/${BEING_INSTALLED_OBJECT}")
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${BEING_INSTALLED_OBJECT}")
+    if (IS_DIRECTORY "${BUILD_PATCH}/${BEING_INSTALLED_OBJECT}")
       # first of all, install original files
       install (DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
 
       # secondly, rewrite original files with patched ones
-      install (DIRECTORY "${APPLY_OCCT_PATCH_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
+      install (DIRECTORY "${BUILD_PATCH}/${BEING_INSTALLED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
     else()
-      install (FILES     "${APPLY_OCCT_PATCH_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
+      install (FILES     "${BUILD_PATCH}/${BEING_INSTALLED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
     endif()
   else()
     if (IS_DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}")
@@ -168,8 +220,8 @@ macro (OCCT_INSTALL_FILE_OR_DIR BEING_INSTALLED_OBJECT DESTINATION_PATH)
 endmacro()
 
 macro (OCCT_CONFIGURE_AND_INSTALL BEING_CONGIRUGED_FILE BUILD_NAME INSTALL_NAME DESTINATION_PATH)
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${BEING_CONGIRUGED_FILE}")
-    configure_file("${APPLY_OCCT_PATCH_DIR}/${BEING_CONGIRUGED_FILE}" "${BUILD_NAME}" @ONLY)
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${BEING_CONGIRUGED_FILE}")
+    configure_file("${BUILD_PATCH}/${BEING_CONGIRUGED_FILE}" "${BUILD_NAME}" @ONLY)
   else()
     configure_file("${CMAKE_SOURCE_DIR}/${BEING_CONGIRUGED_FILE}" "${BUILD_NAME}" @ONLY)
   endif()
@@ -177,55 +229,151 @@ macro (OCCT_CONFIGURE_AND_INSTALL BEING_CONGIRUGED_FILE BUILD_NAME INSTALL_NAME
   install(FILES "${OCCT_BINARY_DIR}/${BUILD_NAME}" DESTINATION  "${DESTINATION_PATH}" RENAME ${INSTALL_NAME})
 endmacro()
 
-macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOOLKITS)
-  set (OCCT_SOURCE_DIRS)
+macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_TARGET_OCCT_DIR OCCT_BUILD_TOOLKITS OCCT_COLLECT_SOURCE_DIR OCCT_INSTALL_DIR_PREFIX)
+  set (OCCT_USED_PACKAGES)
 
   # consider patched header.in template
   set (TEMPLATE_HEADER_PATH "${CMAKE_SOURCE_DIR}/adm/templates/header.in")
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/adm/templates/header.in")
-    set (TEMPLATE_HEADER_PATH "${APPLY_OCCT_PATCH_DIR}/adm/templates/header.in")
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/adm/templates/header.in")
+    set (TEMPLATE_HEADER_PATH "${BUILD_PATCH}/adm/templates/header.in")
   endif()
 
   set (ROOT_OCCT_DIR ${CMAKE_SOURCE_DIR})
 
   foreach (OCCT_USED_TOOLKIT ${OCCT_BUILD_TOOLKITS})
-    # append parent folder
-    list (APPEND OCCT_SOURCE_DIRS ${OCCT_USED_TOOLKIT})
-
     # append all required package folders
-    set (OCCT_USED_TOOLKIT_DEPS)
-    if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/src/${OCCT_USED_TOOLKIT}/PACKAGES")
-      file (STRINGS "${APPLY_OCCT_PATCH_DIR}/src/${OCCT_USED_TOOLKIT}/PACKAGES" OCCT_USED_TOOLKIT_DEPS)
-    elseif (EXISTS "${CMAKE_SOURCE_DIR}/src/${OCCT_USED_TOOLKIT}/PACKAGES")
-      file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_USED_TOOLKIT}/PACKAGES" OCCT_USED_TOOLKIT_DEPS)
+    set (OCCT_TOOLKIT_PACKAGES)
+    if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_USED_TOOLKIT}/PACKAGES")
+      file (STRINGS "${BUILD_PATCH}/src/${OCCT_USED_TOOLKIT}/PACKAGES" OCCT_TOOLKIT_PACKAGES)
+    elseif (EXISTS "${OCCT_COLLECT_SOURCE_DIR}/${OCCT_USED_TOOLKIT}/PACKAGES")
+      file (STRINGS "${OCCT_COLLECT_SOURCE_DIR}/${OCCT_USED_TOOLKIT}/PACKAGES" OCCT_TOOLKIT_PACKAGES)
     endif()
 
-    foreach (OCCT_USED_TOOLKIT_DEP ${OCCT_USED_TOOLKIT_DEPS})
-      list (APPEND OCCT_SOURCE_DIRS ${OCCT_USED_TOOLKIT_DEP})
-    endforeach()
+    list (APPEND OCCT_USED_PACKAGES ${OCCT_TOOLKIT_PACKAGES})
   endforeach()
 
-  foreach (OCCT_SOURCE_DIR ${OCCT_SOURCE_DIRS})
-    # get all header files from each src folder
-    file (GLOB OCCT_HEADER_FILES "${CMAKE_SOURCE_DIR}/src/${OCCT_SOURCE_DIR}/*.[hgl]xx" "${CMAKE_SOURCE_DIR}/src/${OCCT_SOURCE_DIR}/*.h")
-    install (FILES ${OCCT_HEADER_FILES} DESTINATION "${INSTALL_DIR}/inc")
+  list (REMOVE_DUPLICATES OCCT_USED_PACKAGES)
 
-    # create new file including found header
-    foreach (OCCT_HEADER_FILE ${OCCT_HEADER_FILES})
-      get_filename_component (HEADER_FILE_NAME ${OCCT_HEADER_FILE} NAME)
-      configure_file ("${TEMPLATE_HEADER_PATH}" "${ROOT_TARGET_OCCT_DIR}/inc/${HEADER_FILE_NAME}" @ONLY)
+  set (OCCT_HEADER_FILES_COMPLETE)
+  set (OCCT_HEADER_FILE_NAMES_NOT_IN_FILES)
+  set (OCCT_HEADER_FILE_WITH_PROPER_NAMES)
+
+  string(TIMESTAMP CURRENT_TIME "%H:%M:%S")
+  message (STATUS "Info: \(${CURRENT_TIME}\) Compare FILES with files in package directories...")
+
+  foreach (OCCT_PACKAGE ${OCCT_USED_PACKAGES})
+    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")
+      file (STRINGS "${OCCT_COLLECT_SOURCE_DIR}/${OCCT_PACKAGE}/FILES" OCCT_ALL_FILE_NAMES)
+    else()
+      message (WARNING "FILES has not been found in ${OCCT_COLLECT_SOURCE_DIR}/${OCCT_PACKAGE}")
+      continue()
+    endif()
+
+    list (LENGTH OCCT_ALL_FILE_NAMES ALL_FILES_NB)
+    math (EXPR ALL_FILES_NB "${ALL_FILES_NB} - 1" )
+
+    # emit warnings if there are unprocessed headers
+    file (GLOB OCCT_ALL_FILES_IN_DIR "${OCCT_COLLECT_SOURCE_DIR}/${OCCT_PACKAGE}/*.*")
+    file (GLOB OCCT_ALL_FILES_IN_PATCH_DIR "${BUILD_PATCH}/src/${OCCT_PACKAGE}/*.*")
+
+    # use patched header files
+    foreach (OCCT_FILE_IN_PATCH_DIR ${OCCT_ALL_FILES_IN_PATCH_DIR})
+      get_filename_component (OCCT_FILE_IN_PATCH_DIR_NAME ${OCCT_FILE_IN_PATCH_DIR} NAME)
+      list (REMOVE_ITEM OCCT_ALL_FILES_IN_DIR "${OCCT_COLLECT_SOURCE_DIR}/${OCCT_PACKAGE}/${OCCT_FILE_IN_PATCH_DIR_NAME}")
+      list (APPEND OCCT_ALL_FILES_IN_DIR "${OCCT_FILE_IN_PATCH_DIR}")
     endforeach()
 
-    # consider pathed the source files
-    if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/src/${OCCT_SOURCE_DIR}")
-      file (GLOB PATCHED_OCCT_HEADER_FILES "${APPLY_OCCT_PATCH_DIR}/src/${OCCT_SOURCE_DIR}/*.[hgl]xx" "${APPLY_OCCT_PATCH_DIR}/src/${OCCT_SOURCE_DIR}/*.h")
-      install (FILES ${PATCHED_OCCT_HEADER_FILES} DESTINATION "${INSTALL_DIR}/inc")
+    foreach (OCCT_FILE_IN_DIR ${OCCT_ALL_FILES_IN_DIR})
+      get_filename_component (OCCT_FILE_IN_DIR_NAME ${OCCT_FILE_IN_DIR} NAME)
+
+      set (OCCT_FILE_IN_DIR_STATUS OFF)
+
+      if (${ALL_FILES_NB} LESS 0)
+        break()
+      endif()
+
+      foreach (FILE_INDEX RANGE ${ALL_FILES_NB})
+        list (GET OCCT_ALL_FILE_NAMES ${FILE_INDEX} OCCT_FILE_NAME)
+
+        string (REGEX REPLACE "[^:]+:+" "" OCCT_FILE_NAME "${OCCT_FILE_NAME}")
+
+        if ("${OCCT_FILE_IN_DIR_NAME}" STREQUAL "${OCCT_FILE_NAME}")
+          set (OCCT_FILE_IN_DIR_STATUS ON)
+
+          string (REGEX MATCH ".+\\.[hlg]xx|.+\\.h$" IS_HEADER_FOUND "${OCCT_FILE_NAME}")
+          if (IS_HEADER_FOUND)
+            list (APPEND OCCT_HEADER_FILES_COMPLETE ${OCCT_FILE_IN_DIR})
 
-      # create new patched file including found header
-      foreach (OCCT_HEADER_FILE ${PATCHED_OCCT_HEADER_FILES})
-        get_filename_component (HEADER_FILE_NAME ${OCCT_HEADER_FILE} NAME)
-        configure_file ("${TEMPLATE_HEADER_PATH}" "${ROOT_TARGET_OCCT_DIR}/inc/${HEADER_FILE_NAME}" @ONLY)
+            # collect header files with name that does not contain its package one
+            string (REGEX MATCH "^${OCCT_PACKAGE}[_.]" IS_HEADER_MATHCING_PACKAGE "${OCCT_FILE_NAME}")
+            if (NOT IS_HEADER_MATHCING_PACKAGE)
+              list (APPEND OCCT_HEADER_FILE_WITH_PROPER_NAMES "${OCCT_FILE_NAME}")
+            endif()
+          endif()
+
+          # remove found element from list
+          list (REMOVE_AT OCCT_ALL_FILE_NAMES ${FILE_INDEX})
+          math (EXPR ALL_FILES_NB "${ALL_FILES_NB} - 1" ) # decrement number
+
+          break()
+        endif()
       endforeach()
+
+      if (NOT OCCT_FILE_IN_DIR_STATUS)
+        message (STATUS "Warning. File ${OCCT_FILE_IN_DIR} is not listed in ${OCCT_COLLECT_SOURCE_DIR}/${OCCT_PACKAGE}/FILES")
+
+        string (REGEX MATCH ".+\\.[hlg]xx|.+\\.h$" IS_HEADER_FOUND "${OCCT_FILE_NAME}")
+        if (IS_HEADER_FOUND)
+          list (APPEND OCCT_HEADER_FILE_NAMES_NOT_IN_FILES ${OCCT_FILE_NAME})
+        endif()
+      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...")
+
+  foreach (OCCT_HEADER_FILE ${OCCT_HEADER_FILES_COMPLETE})
+    get_filename_component (HEADER_FILE_NAME ${OCCT_HEADER_FILE} NAME)
+    set (OCCT_HEADER_FILE_CONTENT "#include \"${OCCT_HEADER_FILE}\"")
+    configure_file ("${TEMPLATE_HEADER_PATH}" "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/${HEADER_FILE_NAME}" @ONLY)
+  endforeach()
+  
+  install (FILES ${OCCT_HEADER_FILES_COMPLETE} DESTINATION "${INSTALL_DIR}/${OCCT_INSTALL_DIR_PREFIX}")
+  
+  string(TIMESTAMP CURRENT_TIME "%H:%M:%S")
+  message (STATUS "Info: \(${CURRENT_TIME}\) Checking headers in inc folder...")
+    
+  file (GLOB OCCT_HEADER_FILES_OLD "${ROOT_TARGET_OCCT_DIR}/${OCCT_INSTALL_DIR_PREFIX}/*")
+  foreach (OCCT_HEADER_FILE_OLD ${OCCT_HEADER_FILES_OLD})
+    get_filename_component (HEADER_FILE_NAME ${OCCT_HEADER_FILE_OLD} NAME)
+    string (REGEX MATCH "^[a-zA-Z0-9]+" PACKAGE_NAME "${HEADER_FILE_NAME}")
+    
+    list (FIND OCCT_USED_PACKAGES ${PACKAGE_NAME} IS_HEADER_FOUND)
+    if (NOT ${IS_HEADER_FOUND} EQUAL -1)
+      if (NOT EXISTS "${OCCT_COLLECT_SOURCE_DIR}/${PACKAGE_NAME}/${HEADER_FILE_NAME}")
+        message (STATUS "Warning. ${OCCT_HEADER_FILE_OLD} is not present in the sources and will be removed from ${ROOT_TARGET_OCCT_DIR}/inc")
+        file (REMOVE "${OCCT_HEADER_FILE_OLD}")
+      else()
+        list (FIND OCCT_HEADER_FILE_NAMES_NOT_IN_FILES ${PACKAGE_NAME} IS_HEADER_FOUND)
+        if (NOT ${IS_HEADER_FOUND} EQUAL -1)
+          message (STATUS "Warning. ${OCCT_HEADER_FILE_OLD} is present in the sources but not involved in FILES and will be removed from ${ROOT_TARGET_OCCT_DIR}/inc")
+          file (REMOVE "${OCCT_HEADER_FILE_OLD}")
+        endif()
+      endif()
+    else()
+      set (IS_HEADER_FOUND -1)
+      if (NOT "${OCCT_HEADER_FILE_WITH_PROPER_NAMES}" STREQUAL "")
+        list (FIND OCCT_HEADER_FILE_WITH_PROPER_NAMES ${HEADER_FILE_NAME} IS_HEADER_FOUND)
+      endif()
+      
+      if (${IS_HEADER_FOUND} EQUAL -1)
+        message (STATUS "Warning. \(${PACKAGE_NAME}\) ${OCCT_HEADER_FILE_OLD} is not used and will be removed from ${ROOT_TARGET_OCCT_DIR}/inc")
+        file (REMOVE "${OCCT_HEADER_FILE_OLD}")
+      endif()
     endif()
   endforeach()
 endmacro()
@@ -236,23 +384,23 @@ macro (OCCT_COPY_FILE_OR_DIR BEING_COPIED_OBJECT DESTINATION_PATH)
     file (COPY "${CMAKE_SOURCE_DIR}/${BEING_COPIED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
   endif()
 
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${BEING_COPIED_OBJECT}")
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${BEING_COPIED_OBJECT}")
     # secondly, rewrite original files with patched ones
-    file (COPY "${APPLY_OCCT_PATCH_DIR}/${BEING_COPIED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
+    file (COPY "${BUILD_PATCH}/${BEING_COPIED_OBJECT}" DESTINATION  "${DESTINATION_PATH}")
   endif()
 endmacro()
 
 macro (OCCT_CONFIGURE BEING_CONGIRUGED_FILE FINAL_NAME)
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${BEING_CONGIRUGED_FILE}")
-    configure_file("${APPLY_OCCT_PATCH_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${BEING_CONGIRUGED_FILE}")
+    configure_file("${BUILD_PATCH}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
   else()
     configure_file("${CMAKE_SOURCE_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
   endif()
 endmacro()
 
 macro (OCCT_ADD_SUBDIRECTORY BEING_ADDED_DIRECTORY)
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${BEING_ADDED_DIRECTORY}/CMakeLists.txt")
-    add_subdirectory(${APPLY_OCCT_PATCH_DIR}/${BEING_ADDED_DIRECTORY})
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${BEING_ADDED_DIRECTORY}/CMakeLists.txt")
+    add_subdirectory(${BUILD_PATCH}/${BEING_ADDED_DIRECTORY})
   elseif (EXISTS "${CMAKE_SOURCE_DIR}/${BEING_ADDED_DIRECTORY}/CMakeLists.txt")
     add_subdirectory (${CMAKE_SOURCE_DIR}/${BEING_ADDED_DIRECTORY})
   else()
@@ -267,8 +415,8 @@ function (OCCT_IS_PRODUCT_REQUIRED CSF_VAR_NAME USE_PRODUCT)
     message(STATUS "Warning: the list of being used toolkits is empty")
   else()
     foreach (USED_TOOLKIT ${BUILD_TOOLKITS})
-      if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/src/${USED_TOOLKIT}/EXTERNLIB")
-        file (READ "${APPLY_OCCT_PATCH_DIR}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
+      if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${USED_TOOLKIT}/EXTERNLIB")
+        file (READ "${BUILD_PATCH}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
       elseif (EXISTS "${CMAKE_SOURCE_DIR}/src/${USED_TOOLKIT}/EXTERNLIB")
         file (READ "${CMAKE_SOURCE_DIR}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
       endif()
@@ -285,8 +433,8 @@ endfunction()
 
 function (FILE_TO_LIST FILE_NAME FILE_CONTENT)
   set (LOCAL_FILE_CONTENT)
-  if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/${FILE_NAME}")
-    file (STRINGS "${APPLY_OCCT_PATCH_DIR}/${FILE_NAME}" LOCAL_FILE_CONTENT)
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${FILE_NAME}")
+    file (STRINGS "${BUILD_PATCH}/${FILE_NAME}" LOCAL_FILE_CONTENT)
   elseif (EXISTS "${CMAKE_SOURCE_DIR}/${FILE_NAME}")
     file (STRINGS "${CMAKE_SOURCE_DIR}/${FILE_NAME}" LOCAL_FILE_CONTENT)
   endif()
@@ -316,7 +464,7 @@ function (OCCT_TOOLKIT_DEP TOOLKIT_NAME TOOLKIT_DEPS)
   set (LOCAL_TOOLKIT_DEPS)
   foreach (FILE_CONTENT_LINE ${FILE_CONTENT})
     string (REGEX MATCH "^TK" TK_FOUND ${FILE_CONTENT_LINE})
-    if ("${FILE_CONTENT_LINE}" STREQUAL "DRAWEXE" OR NOT "${TK_FOUND}" STREQUAL "")
+    if ("x${FILE_CONTENT_LINE}" STREQUAL "xDRAWEXE" OR NOT "${TK_FOUND}" STREQUAL "")
       list (APPEND LOCAL_TOOLKIT_DEPS ${FILE_CONTENT_LINE})
     endif()
   endforeach()
@@ -359,11 +507,11 @@ function (OCCT_TOOLKIT_FULL_DEP TOOLKIT_NAME TOOLKIT_FULL_DEPS)
   set (${TOOLKIT_FULL_DEPS} ${LOCAL_TOOLKIT_FULL_DEPS} PARENT_SCOPE)
 endfunction()
 
-# Function to get list of modules and toolkits from file adm/MODULES.
+# Function to get list of modules/toolkits/samples from file adm/${FILE_NAME}.
 # Creates list <$MODULE_LIST> to store list of MODULES and
-# <NAME_OF_MODULE>_TOOLKITS foreach module to store its toolkits.
-function (OCCT_MODULES_AND_TOOLKITS MODULE_LIST)
-  FILE_TO_LIST ("adm/MODULES" FILE_CONTENT)
+# <NAME_OF_MODULE>_TOOLKITS foreach module to store its toolkits, where "TOOLKITS" is defined by TOOLKITS_NAME_SUFFIX.
+function (OCCT_MODULES_AND_TOOLKITS FILE_NAME TOOLKITS_NAME_SUFFIX MODULE_LIST)
+  FILE_TO_LIST ("adm/${FILE_NAME}" FILE_CONTENT)
 
   foreach (CONTENT_LINE ${FILE_CONTENT})
     string (REPLACE " " ";" CONTENT_LINE ${CONTENT_LINE})
@@ -371,33 +519,126 @@ function (OCCT_MODULES_AND_TOOLKITS MODULE_LIST)
     list (REMOVE_AT CONTENT_LINE 0)
     list (APPEND ${MODULE_LIST} ${MODULE_NAME})
     # (!) REMOVE THE LINE BELOW (implicit variables)
-    set (${MODULE_NAME}_TOOLKITS ${CONTENT_LINE} PARENT_SCOPE)
+    set (${MODULE_NAME}_${TOOLKITS_NAME_SUFFIX} ${CONTENT_LINE} PARENT_SCOPE)
   endforeach()
 
   set (${MODULE_LIST} ${${MODULE_LIST}} PARENT_SCOPE)
 endfunction()
 
-# Returns OCCT version string from file Standard_Version.hxx (if available)
-function (OCCT_VERSION OCCT_VERSION_VAR)
-  set (OCC_VERSION_COMPLETE "7.1.0")
-  set (OCC_VERSION_DEVELOPMENT "")
-  
-  set (OCCT_VERSION_LOCALVAR "${OCC_VERSION_COMPLETE}.${OCC_VERSION_DEVELOPMENT}")
+# Returns OCC version string from file Standard_Version.hxx (if available)
+function (OCC_VERSION OCC_VERSION_MAJOR OCC_VERSION_MINOR OCC_VERSION_MAINTENANCE OCC_VERSION_DEVELOPMENT OCC_VERSION_STRING_EXT)
 
+  set (OCC_VERSION_MAJOR         7)
+  set (OCC_VERSION_MINOR         0)
+  set (OCC_VERSION_MAINTENANCE   0)
+  set (OCC_VERSION_DEVELOPMENT   dev)
+  set (OCC_VERSION_COMPLETE      "7.0.0")
   set (STANDARD_VERSION_FILE "${CMAKE_SOURCE_DIR}/src/Standard/Standard_Version.hxx")
+  if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/Standard/Standard_Version.hxx")
+    set (STANDARD_VERSION_FILE "${BUILD_PATCH}/src/Standard/Standard_Version.hxx")
+  endif()
+
   if (EXISTS "${STANDARD_VERSION_FILE}")
-    foreach (SOUGHT_VERSION OCC_VERSION_COMPLETE OCC_VERSION_DEVELOPMENT)
+    foreach (SOUGHT_VERSION OCC_VERSION_MAJOR OCC_VERSION_MINOR OCC_VERSION_MAINTENANCE)
       file (STRINGS "${STANDARD_VERSION_FILE}" ${SOUGHT_VERSION} REGEX "^#define ${SOUGHT_VERSION} .*")
-      string (REGEX REPLACE ".*${SOUGHT_VERSION} .*\"([^ ]+)\".*" "\\1" ${SOUGHT_VERSION} "${${SOUGHT_VERSION}}" )
+      string (REGEX REPLACE ".*${SOUGHT_VERSION} .*([^ ]+).*" "\\1" ${SOUGHT_VERSION} "${${SOUGHT_VERSION}}" )
     endforeach()
     
-    if (NOT "${OCC_VERSION_DEVELOPMENT}" STREQUAL "")
-      set (OCCT_VERSION_LOCALVAR "${OCC_VERSION_COMPLETE}.${OCC_VERSION_DEVELOPMENT}")
-    else()
-      set (OCCT_VERSION_LOCALVAR "${OCC_VERSION_COMPLETE}")
+    foreach (SOUGHT_VERSION OCC_VERSION_DEVELOPMENT OCC_VERSION_COMPLETE)
+      file (STRINGS "${STANDARD_VERSION_FILE}" ${SOUGHT_VERSION} REGEX "^#define ${SOUGHT_VERSION} .*")
+      string (REGEX REPLACE ".*${SOUGHT_VERSION} .*\"([^ ]+)\".*" "\\1" ${SOUGHT_VERSION} "${${SOUGHT_VERSION}}" )
+    endforeach()
+  endif()
+  set (OCC_VERSION_MAJOR "${OCC_VERSION_MAJOR}" PARENT_SCOPE)
+  set (OCC_VERSION_MINOR "${OCC_VERSION_MINOR}" PARENT_SCOPE)
+  set (OCC_VERSION_MAINTENANCE "${OCC_VERSION_MAINTENANCE}" PARENT_SCOPE)
+  set (OCC_VERSION_DEVELOPMENT "${OCC_VERSION_DEVELOPMENT}" PARENT_SCOPE)
+  
+  if (OCC_VERSION_DEVELOPMENT AND OCC_VERSION_COMPLETE)
+    set (OCC_VERSION_STRING_EXT "${OCC_VERSION_COMPLETE}.${OCC_VERSION_DEVELOPMENT}" PARENT_SCOPE)
+  else()
+    set (OCC_VERSION_STRING_EXT "${OCC_VERSION_COMPLETE}" PARENT_SCOPE)
+  endif()
+endfunction()
+
+macro (CHECK_PATH_FOR_CONSISTENCY THE_ROOT_PATH_NAME THE_BEING_CHECKED_PATH_NAME THE_VAR_TYPE THE_MESSAGE_OF_BEING_CHECKED_PATH)
+  
+  set (THE_ROOT_PATH "${${THE_ROOT_PATH_NAME}}")
+  set (THE_BEING_CHECKED_PATH "${${THE_BEING_CHECKED_PATH_NAME}}")
+
+  if (THE_BEING_CHECKED_PATH OR EXISTS "${THE_BEING_CHECKED_PATH}")
+    get_filename_component (THE_ROOT_PATH_ABS "${THE_ROOT_PATH}" ABSOLUTE)
+    get_filename_component (THE_BEING_CHECKED_PATH_ABS "${THE_BEING_CHECKED_PATH}" ABSOLUTE)
+
+    string (REGEX MATCH "${THE_ROOT_PATH_ABS}" DOES_PATH_CONTAIN "${THE_BEING_CHECKED_PATH_ABS}")
+
+    if (NOT DOES_PATH_CONTAIN) # if cmake found the being checked path at different place from THE_ROOT_PATH_ABS
+      set (${THE_BEING_CHECKED_PATH_NAME} "" CACHE ${THE_VAR_TYPE} "${THE_MESSAGE_OF_BEING_CHECKED_PATH}" FORCE)
     endif()
+  else()
+    set (${THE_BEING_CHECKED_PATH_NAME} "" CACHE ${THE_VAR_TYPE} "${THE_MESSAGE_OF_BEING_CHECKED_PATH}" FORCE)
   endif()
 
-  set (${OCCT_VERSION_VAR} "${OCCT_VERSION_LOCALVAR}" PARENT_SCOPE)
-endfunction()
+endmacro()
+
+# Adds OCCT_INSTALL_BIN_LETTER variable ("" for Release, "d" for Debug and 
+# "i" for RelWithDebInfo) in OpenCASCADETargets-*.cmake files during 
+# installation process.
+# This and the following macros are used to overcome limitation of CMake
+# prior to version 3.3 not supporting per-configuration install paths
+# for install target files (see https://cmake.org/Bug/view.php?id=14317)
+macro (OCCT_UPDATE_TARGET_FILE)
+  if (MSVC)
+    OCCT_INSERT_CODE_FOR_TARGET ()
+  endif()
 
+  install (CODE
+  "cmake_policy(PUSH)
+  cmake_policy(SET CMP0007 NEW)
+  string (TOLOWER \"\${CMAKE_INSTALL_CONFIG_NAME}\" CMAKE_INSTALL_CONFIG_NAME_LOWERCASE)
+  file (GLOB ALL_OCCT_TARGET_FILES \"${INSTALL_DIR}/${INSTALL_DIR_CMAKE}/OpenCASCADE*Targets-\${CMAKE_INSTALL_CONFIG_NAME_LOWERCASE}.cmake\")
+  foreach(TARGET_FILENAME \${ALL_OCCT_TARGET_FILES})
+    file (STRINGS \"\${TARGET_FILENAME}\" TARGET_FILE_CONTENT)
+    file (REMOVE \"\${TARGET_FILENAME}\")
+    foreach (line IN LISTS TARGET_FILE_CONTENT)
+      string (REGEX REPLACE \"[\\\\]?[\\\$]{OCCT_INSTALL_BIN_LETTER}\" \"\${OCCT_INSTALL_BIN_LETTER}\" line \"\${line}\")
+      file (APPEND \"\${TARGET_FILENAME}\" \"\${line}\\n\")
+    endforeach()
+  endforeach()
+  cmake_policy(POP)")
+endmacro()
+
+macro (OCCT_INSERT_CODE_FOR_TARGET)
+  install(CODE "if (\"\${CMAKE_INSTALL_CONFIG_NAME}\" MATCHES \"^([Rr][Ee][Ll][Ee][Aa][Ss][Ee])$\")
+    set (OCCT_INSTALL_BIN_LETTER \"\")
+  elseif (\"\${CMAKE_INSTALL_CONFIG_NAME}\" MATCHES \"^([Rr][Ee][Ll][Ww][Ii][Tt][Hh][Dd][Ee][Bb][Ii][Nn][Ff][Oo])$\")
+    set (OCCT_INSTALL_BIN_LETTER \"i\")
+  elseif (\"\${CMAKE_INSTALL_CONFIG_NAME}\" MATCHES \"^([Dd][Ee][Bb][Uu][Gg])$\")
+    set (OCCT_INSTALL_BIN_LETTER \"d\")
+  endif()")
+endmacro()
+
+macro (OCCT_UPDATE_DRAW_DEFAULT_FILE)
+  install(CODE "cmake_policy(PUSH)
+  cmake_policy(SET CMP0007 NEW)
+  set (DRAW_DEFAULT_FILE_NAME \"${INSTALL_DIR}/${INSTALL_DIR_RESOURCE}/DrawResources/DrawPlugin\")
+  file (STRINGS \"\${DRAW_DEFAULT_FILE_NAME}\" DRAW_DEFAULT_CONTENT)
+  file (REMOVE \"\${DRAW_DEFAULT_FILE_NAME}\")
+  foreach (line IN LISTS DRAW_DEFAULT_CONTENT)
+    string (REGEX MATCH \": TK\([a-zA-Z]+\)$\" IS_TK_LINE \"\${line}\")
+    string (REGEX REPLACE \": TK\([a-zA-Z]+\)$\" \": TK\${CMAKE_MATCH_1}${BUILD_SHARED_LIBRARY_NAME_POSTFIX}\" line \"\${line}\")
+    file (APPEND \"\${DRAW_DEFAULT_FILE_NAME}\" \"\${line}\\n\")
+  endforeach()
+  cmake_policy(POP)")
+endmacro()
+
+macro (OCCT_CREATE_SYMLINK_TO_FILE LIBRARY_NAME LINK_NAME)
+  if (NOT WIN32)
+    install (CODE "if (EXISTS \"${LIBRARY_NAME}\")
+        execute_process (COMMAND ln -s \"${LIBRARY_NAME}\" \"${LINK_NAME}\")
+      endif()
+    ")
+  endif()
+endmacro()