Skip to content

Commit

Permalink
ExternalModule: Improve target export to allow build against external…
Browse files Browse the repository at this point in the history
… module

This commit backports the ITK external module infrastructure to allow
building against a collection of VTK module externally built.

In a nutshell, config files [1] associated with external modules are now configured into the VTK module
directory found in the VTK binary directory and they have to be
explicitly listed as components when using `find_package(VTK COMPONENTS NameOfExternalModule REQUIRED)`.

More specifically, it implements the following changes:

* List of enabled VTK module is now configured into `VTKConfig.cmake` instead
  of being set based on the list of `*.cmake` found in the VTK module
  directory. This allows to only consider VTK modules enabled in the original VTK
  build directory when using signature `find_package(VTK REQUIRED)` in user code or VTK remote
  module.

* Each external module is now associated with its own target file
  independent of `VTKTargets.cmake`

[1] module config files:
```
lib/cmake/vtk-8.1/Modules/vtkNameOfExternalModule-Headers.cmake
lib/cmake/vtk-8.1/Modules/vtkNameOfExternalModule.cmake
lib/cmake/vtk-8.1/Modules/vtkNameOfExternalModuleHierarchy.txt
```
  • Loading branch information
jcfr committed Oct 17, 2017
1 parent 074d321 commit 2062959
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
11 changes: 1 addition & 10 deletions CMake/VTKConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@ endif()
# Load requested modules.

# List of available VTK modules.
set(VTK_MODULES_ENABLED)

# Determine list of available VTK-modules by scanning the VTK_MODULES_DIR.
file(GLOB config_files RELATIVE "${VTK_MODULES_DIR}" "${VTK_MODULES_DIR}/*.cmake")
foreach (_file ${config_files})
if (NOT "${_file}" MATCHES "[^\\-]+-[a-zA-Z]+\\.cmake")
string(REGEX REPLACE "\\.cmake$" "" _module "${_file}")
list(APPEND VTK_MODULES_ENABLED "${_module}")
endif()
endforeach()
set(VTK_MODULES_ENABLED "@VTK_CONFIG_MODULES_ENABLED@")

# Import VTK targets.
set(VTK_CONFIG_TARGETS_FILE "@VTK_CONFIG_TARGETS_FILE@")
Expand Down
13 changes: 12 additions & 1 deletion CMake/vtkExternalModuleMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ endif()
if(VTK_MODULES_DIR)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${VTK_MODULES_DIR})
endif()
set(VTK_MODULES_DIR "${CMAKE_BINARY_DIR}/${VTK_INSTALL_PACKAGE_DIR}/Modules")
set(VTK_MODULES_DIR "${VTK_DIR}/${VTK_INSTALL_PACKAGE_DIR}/Modules")

include(vtkModuleMacros)
include(module.cmake OPTIONAL RESULT_VARIABLE _found)
if(_found)
set(${vtk-module}-targets ${vtk-module}Targets)
set(${vtk-module}-targets-install "\${VTK_INSTALL_PREFIX}/${VTK_INSTALL_PACKAGE_DIR}/Modules/Targets/${vtk-module}Targets.cmake")
set(${vtk-module}_TARGETS_FILE_INSTALL "${${vtk-module}-targets-install}")
set(${vtk-module}-targets-build-directory "${VTK_DIR}/${VTK_INSTALL_PACKAGE_DIR}/Modules/Targets")
file(MAKE_DIRECTORY ${${vtk-module}-targets-build-directory})
set(${vtk-module}-targets-build "${${vtk-module}-targets-build-directory}/${vtk-module}Targets.cmake")
set(${vtk-module}_TARGETS_FILE_BUILD "${${vtk-module}-targets-build}")
file(WRITE "${${vtk-module}_TARGETS_FILE_BUILD}" "") # Clear targets
endif()
20 changes: 19 additions & 1 deletion CMake/vtkModuleAPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ macro(vtk_module_load mod)
if(NOT ${mod}_LOADED)
message(FATAL_ERROR "No such module: \"${mod}\"")
endif()
# Include the targets file if it has been defined. Targets files other
# than VTKTargets.cmake are created when modules are built externally. Do not
# include the targets file inside the module itself -- which occurs in a module's
# test configuration.
if(EXISTS "${${mod}_TARGETS_FILE}" AND NOT vtk-module STREQUAL mod)
include("${${mod}_TARGETS_FILE}")
endif()
endif()
endmacro()

Expand Down Expand Up @@ -108,9 +115,20 @@ endmacro()
#
# Do not name a module as the namespace.
macro(vtk_module_config ns)

# Determine list of available VTK-modules by scanning the VTK_MODULES_DIR.
set(VTK_MODULES_AVAILABLE)
file(GLOB config_files RELATIVE "${VTK_MODULES_DIR}" "${VTK_MODULES_DIR}/*.cmake")
foreach (_file ${config_files})
if (NOT "${_file}" MATCHES "[^\\-]+-[a-zA-Z]+\\.cmake")
string(REGEX REPLACE "\\.cmake$" "" _module "${_file}")
list(APPEND VTK_MODULES_AVAILABLE "${_module}")
endif()
endforeach()

set(_${ns}_MISSING ${ARGN})
if(_${ns}_MISSING)
list(REMOVE_ITEM _${ns}_MISSING ${VTK_MODULES_ENABLED})
list(REMOVE_ITEM _${ns}_MISSING ${VTK_MODULES_AVAILABLE})
endif()
if(_${ns}_MISSING)
set(msg "")
Expand Down
1 change: 1 addition & 0 deletions CMake/vtkModuleInfo.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ set(@vtk-module@_LIBRARY_DIRS "@vtk-module-LIBRARY_DIRS@")
set(@vtk-module@_RUNTIME_LIBRARY_DIRS "@vtk-module-RUNTIME_LIBRARY_DIRS@")
set(@vtk-module@_WRAP_HIERARCHY_FILE "@vtk-module-WRAP_HIERARCHY_FILE@")
set(@vtk-module@_KIT "@vtk-module-KIT@")
set(@vtk-module@_TARGETS_FILE "@vtk-module-TARGETS_FILE@")
@vtk-module-EXPORT_CODE@
24 changes: 23 additions & 1 deletion CMake/vtkModuleMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ macro(vtk_module_export_info)
endif()
set(vtk-module-EXPORT_CODE-build "${_code}${${vtk-module}_EXPORT_CODE_BUILD}\n")
set(vtk-module-EXPORT_CODE-install "${_code}${${vtk-module}_EXPORT_CODE_INSTALL}\n")
if(VTK_SOURCE_DIR)
# Uses VTKTargets.cmake
set(vtk-module-TARGETS_FILE-build "")
set(vtk-module-TARGETS_FILE-install "")
else()
set(vtk-module-TARGETS_FILE-build "${${vtk-module}_TARGETS_FILE_BUILD}")
set(vtk-module-TARGETS_FILE-install "${${vtk-module}_TARGETS_FILE_INSTALL}")
endif()

if(${vtk-module}_WRAP_HINTS)
set(vtk-module-EXPORT_CODE-build
Expand Down Expand Up @@ -336,13 +344,15 @@ macro(vtk_module_export_info)
set(vtk-module-RUNTIME_LIBRARY_DIRS "${vtk-module-RUNTIME_LIBRARY_DIRS-build}")
set(vtk-module-INCLUDE_DIRS "${vtk-module-INCLUDE_DIRS-build}")
set(vtk-module-EXPORT_CODE "${vtk-module-EXPORT_CODE-build}")
set(vtk-module-TARGETS_FILE "${vtk-module-TARGETS_FILE-build}")
set(vtk-module-WRAP_HIERARCHY_FILE "${${vtk-module}_WRAP_HIERARCHY_FILE}")
set(vtk-module-KIT "${${vtk-module}_KIT}")
configure_file(${_VTKModuleMacros_DIR}/vtkModuleInfo.cmake.in
${VTK_MODULES_DIR}/${vtk-module}.cmake @ONLY)
set(vtk-module-INCLUDE_DIRS "${vtk-module-INCLUDE_DIRS-install}")
set(vtk-module-RUNTIME_LIBRARY_DIRS "${vtk-module-RUNTIME_LIBRARY_DIRS-install}")
set(vtk-module-EXPORT_CODE "${vtk-module-EXPORT_CODE-install}")
set(vtk-module-TARGETS_FILE "${vtk-module-TARGETS_FILE-install}")
set(vtk-module-WRAP_HIERARCHY_FILE
"\${CMAKE_CURRENT_LIST_DIR}/${vtk-module}Hierarchy.txt")
configure_file(${_VTKModuleMacros_DIR}/vtkModuleInfo.cmake.in
Expand Down Expand Up @@ -481,7 +491,19 @@ function(vtk_target_name _name)
endfunction()

function(vtk_target_export _name)
set_property(GLOBAL APPEND PROPERTY VTK_TARGETS ${_name})
set(is_insource_module 0)
if(VTK_SOURCE_DIR)
set(is_insource_module 1)
endif()
set(is_local_project 0)
if("${${vtk-module}-targets-build}" STREQUAL "")
set(is_local_project 1) # E.g Examples/Build/vtkMy or Examples/Build/vtkLocal
endif()
if(is_insource_module OR is_local_module )
set_property(GLOBAL APPEND PROPERTY VTK_TARGETS ${_name})
else()
export(TARGETS ${_name} APPEND FILE ${${vtk-module}-targets-build}) # External VTK module
endif()
endfunction()

function(vtk_target_install _name)
Expand Down

0 comments on commit 2062959

Please sign in to comment.