0027301: CMake: export targets for OCCT
authorski <ski@opencascade.com>
Mon, 4 Apr 2016 16:51:24 +0000 (19:51 +0300)
committerabv <abv@opencascade.com>
Tue, 5 Apr 2016 12:52:28 +0000 (15:52 +0300)
Enabled creation of CMake target files (one per module) for OCCT at install time.
Variables enumerating available OCCT toolkits by module are added in CMake configuration file.
Added check of availability of OCCT modules specified by OpenCASCADE_FIND_COMPONENTS variable.
Location of CMake configuration and target files in Windows layout is changed to $INSTALL_DIR/cmake (thus common for all configurations), to be found by standard search logic of CMake find_package() function.

CMakeLists.txt
adm/cmake/occt_macros.cmake
adm/cmake/occt_toolkit.cmake
adm/templates/OpenCASCADEConfig.cmake.in

index 3a94c98..a42d003 100644 (file)
@@ -265,7 +265,7 @@ if (NOT DEFINED INSTALL_DIR_CMAKE)
       set (INSTALL_DIR_CMAKE "lib/cmake/opencascade" CACHE PATH "${INSTALL_DIR_CMAKE_DESCR}")
     endif()
   else()
-    set (INSTALL_DIR_CMAKE "${OS_WITH_BIT}/${COMPILER}/lib" CACHE PATH "${INSTALL_DIR_CMAKE_DESCR}")
+    set (INSTALL_DIR_CMAKE "cmake" CACHE PATH "${INSTALL_DIR_CMAKE_DESCR}")
   endif()
 endif()
 
@@ -556,7 +556,7 @@ endif()
 if (3RDPARTY_INCLUDE_DIRS)
   list (REMOVE_DUPLICATES 3RDPARTY_INCLUDE_DIRS)
   string (REGEX REPLACE ";" "\n\t" 3RDPARTY_INCLUDE_DIRS_WITH_ENDS "${3RDPARTY_INCLUDE_DIRS}")
-  message (STATUS "Info: The directories of 3rdparty headers: ${3RDPARTY_INCLUDE_DIRS_WITH_ENDS}")
+  message (STATUS "Info: The directories of 3rdparty headers: \n\t${3RDPARTY_INCLUDE_DIRS_WITH_ENDS}")
   include_directories (${3RDPARTY_INCLUDE_DIRS})
 endif()
 
@@ -566,7 +566,7 @@ include_directories (${CMAKE_BINARY_DIR}/inc)
 if (3RDPARTY_LIBRARY_DIRS)
   list (REMOVE_DUPLICATES 3RDPARTY_LIBRARY_DIRS)
   string (REGEX REPLACE ";" "\n\t" 3RDPARTY_LIBRARY_DIRS_WITH_ENDS "${3RDPARTY_LIBRARY_DIRS}")
-  message (STATUS "Info: The directories of 3rdparty libraries: ${3RDPARTY_LIBRARY_DIRS_WITH_ENDS}")
+  message (STATUS "Info: The directories of 3rdparty libraries: \n\t${3RDPARTY_LIBRARY_DIRS_WITH_ENDS}")
   link_directories (${3RDPARTY_LIBRARY_DIRS})
 endif()
 
@@ -763,6 +763,7 @@ endif()
 # Prepare variables for configuration of OpenCASCADE cmake config file
 set (OCCT_MODULES_ENABLED)
 set (OCCT_LIBRARIES)
+set (SET_OpenCASCADE_MODULES_TOOLKITS "\n# List of available OpenCASCADE libraries for each module\n")
 foreach (OCCT_TOOLKIT ${BUILD_TOOLKITS})
   if (TARGET ${OCCT_TOOLKIT})
 
@@ -770,18 +771,29 @@ foreach (OCCT_TOOLKIT ${BUILD_TOOLKITS})
     get_target_property (${OCCT_TOOLKIT}_TYPE ${OCCT_TOOLKIT} "TYPE")
     if ("${${OCCT_TOOLKIT}_TYPE}" STREQUAL "STATIC_LIBRARY" OR "${${OCCT_TOOLKIT}_TYPE}" STREQUAL "SHARED_LIBRARY" )
       list (APPEND OCCT_LIBRARIES ${OCCT_TOOLKIT})
-    endif()
 
-    # get all modules
-    get_target_property (${OCCT_TOOLKIT}_FOLDER ${OCCT_TOOLKIT} "FOLDER")
-    string (REGEX MATCH "/(.+)" ${OCCT_TOOLKIT}_MODULE "${${OCCT_TOOLKIT}_FOLDER}")
-    if (NOT "${CMAKE_MATCH_1}" STREQUAL "")
-      list (APPEND OCCT_MODULES_ENABLED ${CMAKE_MATCH_1})
+      # get all modules
+      get_target_property (${OCCT_TOOLKIT}_MODULE ${OCCT_TOOLKIT} "MODULE")
+      list (APPEND OCCT_MODULES_ENABLED ${${OCCT_TOOLKIT}_MODULE})
+      list (APPEND OpenCASCADE_${${OCCT_TOOLKIT}_MODULE}_TOOLKITS ${OCCT_TOOLKIT})
     endif()
   endif()
 endforeach()
 list (REMOVE_DUPLICATES OCCT_MODULES_ENABLED)
 
+foreach (OCCT_MODULE ${OCCT_MODULES})
+  if (BUILD_MODULE_${OCCT_MODULE})
+    set (SET_OpenCASCADE_MODULES_TOOLKITS "${SET_OpenCASCADE_MODULES_TOOLKITS}set (OpenCASCADE_${OCCT_MODULE}_LIBRARIES ${OpenCASCADE_${OCCT_MODULE}_TOOLKITS})\n")
+  endif()
+endforeach()
+
+# Add all targets to the build-tree export set
+export (TARGETS ${OCCT_LIBRARIES} FILE "${CMAKE_BINARY_DIR}/OpenCASCADETargets.cmake")
+
+# Export the package for use from the build-tree
+# (this registers the build-tree with a global CMake-registry)
+export(PACKAGE OpenCASCADE)
+
 if (CMAKE_BUILD_TYPE)
   set (SET_OpenCASCADE_BUILD_TYPE "set (OpenCASCADE_BUILD_TYPE        \"${CMAKE_BUILD_TYPE}\")")
 endif()
@@ -799,8 +811,13 @@ if (APPLE)
   set (SET_OpenCASCADE_WITH_GLX "set (OpenCASCADE_WITH_GLX       ${USE_GLX})")
 endif()
 
+if (NOT SINGLE_GENERATOR)
+  OCCT_INSERT_CODE_FOR_TARGET()
+endif()
+
 # Configure and install cmake config file
-OCCT_CONFIGURE_AND_INSTALL ("adm/templates/OpenCASCADEConfig.cmake.in" "OpenCASCADEConfig.cmake" "OpenCASCADEConfig.cmake" "${INSTALL_DIR}/${INSTALL_DIR_CMAKE}")
+configure_file("${CMAKE_SOURCE_DIR}/adm/templates/OpenCASCADEConfig.cmake.in" "OpenCASCADEConfig.cmake" @ONLY)
+install(FILES "${CMAKE_BINARY_DIR}/OpenCASCADEConfig.cmake" DESTINATION  "${INSTALL_DIR}/${INSTALL_DIR_CMAKE}")
 
 # Configure cmake version file
 include(CMakePackageConfigHelpers)
@@ -810,3 +827,14 @@ write_basic_package_version_file( ${CMAKE_BINARY_DIR}/OpenCASCADEConfigVersion.c
 
 # Install cmake version file
 install (FILES "${CMAKE_BINARY_DIR}/OpenCASCADEConfigVersion.cmake" DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_CMAKE}")
+
+# Install the export set for use with the install-tree for each configuration
+foreach (OCCT_MODULE ${OCCT_MODULES})
+  if (BUILD_MODULE_${OCCT_MODULE})
+    install(EXPORT OpenCASCADE${OCCT_MODULE}Targets DESTINATION "${INSTALL_DIR_CMAKE}")
+  endif()
+endforeach()
+
+# Update generated OpenCASCADETargets-*.cmake files 
+# to have correct paths to libraries depending on the configuration
+OCCT_UPDATE_TARGET_FILE ()
index 03b778a..b8c8359 100644 (file)
@@ -539,3 +539,40 @@ macro (CHECK_PATH_FOR_CONSISTENCY THE_ROOT_PATH_NAME THE_BEING_CHECKED_PATH_NAME
   endif()
 
 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 (NOT SINGLE_GENERATOR)
+    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()
index b721e4c..9e87057 100644 (file)
@@ -127,65 +127,41 @@ if (MSVC)
   endif()
 endif()
 
+set (CURRENT_MODULE)
+foreach (OCCT_MODULE ${OCCT_MODULES})
+  list (FIND ${OCCT_MODULE}_TOOLKITS ${PROJECT_NAME} CURRENT_PROJECT_IS_BUILT)
+  if (NOT ${CURRENT_PROJECT_IS_BUILT} EQUAL -1)
+    set (CURRENT_MODULE ${OCCT_MODULE})
+  endif()
+endforeach()
+
+if (NOT SINGLE_GENERATOR)
+  OCCT_INSERT_CODE_FOR_TARGET ()
+endif()
+
 if ("${PROJECT_NAME}" STREQUAL "DRAWEXE")
   add_executable (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE})
 
-  if (SINGLE_GENERATOR)
-    install (TARGETS ${PROJECT_NAME} DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}")
-  else()
-    install (TARGETS ${PROJECT_NAME}
-             CONFIGURATIONS Release
-             DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}")
-    install (TARGETS ${PROJECT_NAME}
-             CONFIGURATIONS RelWithDebInfo
-             DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}i")
-    install (TARGETS ${PROJECT_NAME}
-             CONFIGURATIONS Debug
-             DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d")
-  endif()
+  install (TARGETS ${PROJECT_NAME}
+           DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
 else()
   add_library (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE})
 
-  if (SINGLE_GENERATOR)
-    install (TARGETS ${PROJECT_NAME}
-             RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}"
-             ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}"
-             LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}")
-  else()
-    install (TARGETS ${PROJECT_NAME}
-             CONFIGURATIONS Release
-             RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}"
-             ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}"
-             LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}")
-    install (TARGETS ${PROJECT_NAME}
-             CONFIGURATIONS RelWithDebInfo
-             RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}i"
-             ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}i"
-             LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}i")
-    install (TARGETS ${PROJECT_NAME}
+  if (MSVC)
+    install (FILES  ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bind/${PROJECT_NAME}.pdb
              CONFIGURATIONS Debug
-             RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d"
-             ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}d"
-             LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}d")
-
-    if (MSVC)
-      install (FILES  ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bind/${PROJECT_NAME}.pdb
-               CONFIGURATIONS Debug
-               DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d")
-    endif()
+             DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
   endif()
+  install (TARGETS ${PROJECT_NAME}
+           EXPORT OpenCASCADE${CURRENT_MODULE}Targets
+           RUNTIME DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}"
+           ARCHIVE DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}"
+           LIBRARY DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}")
 endif()
 
-set (CURRENT_MODULE)
-foreach (OCCT_MODULE ${OCCT_MODULES})
-  list (FIND ${OCCT_MODULE}_TOOLKITS ${PROJECT_NAME} CURRENT_PROJECT_IS_BUILT)
-  if (NOT ${CURRENT_PROJECT_IS_BUILT} EQUAL -1)
-    set (CURRENT_MODULE ${OCCT_MODULE})
-  endif()
-endforeach()
-
 if (CURRENT_MODULE)
   set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Modules/${CURRENT_MODULE}")
+  set_target_properties (${PROJECT_NAME} PROPERTIES MODULE "${CURRENT_MODULE}")
 endif()
 
 get_property (OCC_VERSION_MAJOR GLOBAL PROPERTY OCC_VERSION_MAJOR)
index f11e574..8cf9061 100644 (file)
@@ -5,20 +5,30 @@
 # This file is configured by OpenCASCADE.
 #
 
+if(OpenCASCADE_ALREADY_INCLUDED)
+  return()
+endif()
+set(OpenCASCADE_ALREADY_INCLUDED 1)
+
 # The OpenCASCADE version number
 set (OpenCASCADE_MAJOR_VERSION       "@OCC_VERSION_MAJOR@")
 set (OpenCASCADE_MINOR_VERSION       "@OCC_VERSION_MINOR@")
 set (OpenCASCADE_MAINTENANCE_VERSION "@OCC_VERSION_MAINTENANCE@")
 set (OpenCASCADE_DEVELOPMENT_VERSION "@OCC_VERSION_DEVELOPMENT@")
 
-# Compute the installation prefix from this OpenCASCADEConfig.cmake file location.
-# This file should be always installed three levels below install dir:
-# - in UNIX style: lib/cmake/opencascade-<version>
-# - in Windows style: win64/vc10/lib
+# Compute the installation prefix from this OpenCASCADEConfig.cmake file 
+# location, by going up one level + one level if "cmake" + one level if "lib".
+# This is made to support different locations of CMake files:
+# - in UNIX style: $INSTALL_DIR/lib/cmake/opencascade-<version>
+# - in Windows style: $INSTALL_DIR/cmake
 get_filename_component (OpenCASCADE_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
 get_filename_component (OpenCASCADE_INSTALL_PREFIX "${OpenCASCADE_INSTALL_PREFIX}" PATH)
-get_filename_component (OpenCASCADE_INSTALL_PREFIX "${OpenCASCADE_INSTALL_PREFIX}" PATH)
-get_filename_component (OpenCASCADE_INSTALL_PREFIX "${OpenCASCADE_INSTALL_PREFIX}" PATH)
+if (OpenCASCADE_INSTALL_PREFIX MATCHES "/cmake$")
+  get_filename_component (OpenCASCADE_INSTALL_PREFIX "${OpenCASCADE_INSTALL_PREFIX}" PATH)
+endif()
+if (OpenCASCADE_INSTALL_PREFIX MATCHES "/lib$")
+  get_filename_component (OpenCASCADE_INSTALL_PREFIX "${OpenCASCADE_INSTALL_PREFIX}" PATH)
+endif()
 
 # Set OpenCASCADE paths to headers, binaries, libraries, resources, tests, samples, data
 set (OpenCASCADE_BINARY_DIR   "${OpenCASCADE_INSTALL_PREFIX}/@INSTALL_DIR_BIN@")
@@ -34,7 +44,7 @@ set (OpenCASCADE_CXX_FLAGS    "@CMAKE_CXX_FLAGS@")
 
 # List of available OpenCASCADE modules.
 set (OpenCASCADE_MODULES @OCCT_MODULES_ENABLED@)
-
+@SET_OpenCASCADE_MODULES_TOOLKITS@
 # List of available OpenCASCADE libraries.
 set (OpenCASCADE_LIBRARIES @OCCT_LIBRARIES@)
 
@@ -53,3 +63,20 @@ set (OpenCASCADE_WITH_TBB       @USE_TBB@)
 set (OpenCASCADE_WITH_VTK       @USE_VTK@)
 @SET_OpenCASCADE_WITH_D3D@
 @SET_OpenCASCADE_WITH_GLX@
+
+if (NOT OpenCASCADE_FIND_COMPONENTS)
+  set (OpenCASCADE_FIND_COMPONENTS ${OpenCASCADE_MODULES})
+endif ()
+
+# Import OpenCASCADE targets.
+foreach(_comp ${OpenCASCADE_FIND_COMPONENTS})
+  if (NOT ";${OpenCASCADE_MODULES};" MATCHES "${_comp}")
+    set(OpenCASCADE_FOUND False)
+    set(OpenCASCADE_NOTFOUND_MESSAGE "Specified unsupported component: ${_comp}")
+    if (NOT OpenCASCADE_FIND_QUIETLY)
+      message (ERROR ": ${OpenCASCADE_NOTFOUND_MESSAGE}")
+    endif()
+  else()
+    include("${CMAKE_CURRENT_LIST_DIR}/OpenCASCADE${_comp}Targets.cmake")
+  endif()
+endforeach()