diff --git a/CMakeLists.txt b/CMakeLists.txt index bb1c421..920139f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,8 +19,6 @@ project( VERSION "${T8PROJECT_VERSION_MAJOR}.${T8PROJECT_VERSION_MINOR}.${T8PROJECT_VERSION_PATCH}" ) -option( T8PROJECT_ENABLE_VTK "Link against VTK. Must be used if t8code is linked against VTK." OFF ) - # # Enable "make test" to run all the tests # @@ -37,19 +35,13 @@ set(CMAKE_CXX_COMPILER mpicxx) # -DSC_DIR=PATH, find_package ( T8CODE REQUIRED) -# -# Find VTK library. -# If t8code is linked against VTK then t8project should link against it as well. -# -if( T8PROJECT_ENABLE_VTK ) - find_package( VTK REQUIRED COMPONENTS - IOXML CommonExecutionModel CommonDataModel - IOGeometry IOXMLParser IOParallelXML IOPLY - ParallelMPI FiltersCore vtksys CommonCore zlib IOLegacy) - if(VTK_FOUND) - message("Found VTK") - endif (VTK_FOUND) -endif( T8PROJECT_ENABLE_VTK ) +# if t8code is linked against VTK, this project will +# automatically detect it and link against VTK as well. +# We set T8PROJECT_ENABLE_VTK to 1 in order to define our own VTK dependent macros in src/CMakeLists.txt +if (T8CODE_ENABLE_VTK) + message ("t8code links against VTK with version ${T8CODE_VTK_VERSION_USED}") + set (T8PROJECT_ENABLE_VTK "1") +endif () # Recurse into the "test" subdirectory. This does not actually # cause another cmake executable to run. The same process will walk through diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2777570..901296d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,11 +22,16 @@ install( FILES # VTK dependent settings. if( T8PROJECT_ENABLE_VTK ) - # Define T8PROJECT_VTK_MAJOR/MINOR_VERSION to true, such that get defined in t8project_vtk_linkage.hxx - set ( T8PROJECT_VTK_MAJOR_VERSION 1 ) - set ( T8PROJECT_VTK_MINOR_VERSION 1 ) + set ( T8PROJECT_VTK_VERSION_USED "${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}" ) + if (NOT ${T8PROJECT_VTK_VERSION_USED} STREQUAL ${T8CODE_VTK_VERSION_USED}) + message (FATAL_ERROR "VTK version mismatch. t8code uses ${T8CODE_VTK_VERSION_USED} and this project uses ${T8PROJECT_VTK_VERSION_USED}.") + endif () # Set CMake variables for lib target - target_compile_definitions( t8project PUBLIC T8PROJECT_VTK_VERSION_USED="${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}" ) + + # Define T8PROJECT_VTK_VERSION_USED for internal and external use in source files. + # Note, external libraries linking against t8code (i.e. using find_package ( T8CODE REQUIRED )) can + # use T8PROJECT_VTK_VERSION_USED in source code and in CMake scripts (triggered by config.cmake.in). + target_compile_definitions( t8project PUBLIC T8PROJECT_VTK_VERSION_USED="${T8PROJECT_VTK_VERSION_USED}" ) target_compile_definitions( t8project PUBLIC T8PROJECT_ENABLE_VTK=1 ) target_include_directories( t8project PUBLIC ${VTK_INCLUDE_DIR} ) target_link_libraries( t8project PUBLIC ${VTK_LIBRARIES} ) @@ -39,23 +44,8 @@ if( T8PROJECT_ENABLE_VTK ) # install( FILES # put/header/file/here # DESTINATION include) -else () - unset ( T8PROJECT_VTK_MAJOR_VERSION ) - unset ( T8PROJECT_VTK_MINOR_VERSION ) endif() -# -# Setup the file t8project_vtk_linkage.hxx and write macros for the VTK version -# -configure_file (t8project_vtk_linkage.hxx.in t8project_vtk_linkage.hxx @ONLY) - - -# install the automatically created t8project_vtk_linkage.hxx into the include folder -install (FILES - ${CMAKE_CURRENT_BINARY_DIR}/t8project_vtk_linkage.hxx - DESTINATION include -) - # Make this package available as a cmake install package, so # that other projects can find it with find_package. include( CMakePackageConfigHelpers ) diff --git a/src/config.cmake.in b/src/config.cmake.in index 3a12389..8b6a794 100644 --- a/src/config.cmake.in +++ b/src/config.cmake.in @@ -6,6 +6,13 @@ find_dependency( T8CODE CONFIG ) find_dependency( P4EST CONFIG ) find_dependency( SC CONFIG ) +if(T8PROJECT_ENABLE_VTK) + find_dependency(VTK) + # Expose the variable T8PROJECT_VTK_VERSION_USED to external CMake scripts linking against t8code + # for example with find_package ( T8PROJECT REQUIRED ) + set (T8PROJECT_VTK_VERSION_USED "@T8PROJECT_VTK_VERSION_USED@") +endif() + include( "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake" ) check_required_components( @PROJECT_NAME@ ) diff --git a/src/t8project_vtk_linkage.hxx.in b/src/t8project_vtk_linkage.hxx.in deleted file mode 100644 index 5c5fc31..0000000 --- a/src/t8project_vtk_linkage.hxx.in +++ /dev/null @@ -1,31 +0,0 @@ -/* - This file is part of t8project. - Copyright (C) 2025 the developers - - t8project is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - t8project is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with t8project; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - - -#ifndef T8PROJECT_VTK_LINKAGE_HXX -#define T8PROJECT_VTK_LINKAGE_HXX - -/** Macro that is 1 if we link against VTK on 0 if we don't. */ -#cmakedefine01 T8PROJECT_ENABLE_VTK -/** VTK major version number */ -#cmakedefine T8PROJECT_VTK_MAJOR_VERSION @VTK_MAJOR_VERSION@ -/** VTK minor version number */ -#cmakedefine T8PROJECT_VTK_MINOR_VERSION @VTK_MINOR_VERSION@ - -#endif // T8PROJECT_VTK_LINKAGE_HXX \ No newline at end of file diff --git a/test/t8project_gtest_vtk_linkage.cxx b/test/t8project_gtest_vtk_linkage.cxx index c1c543d..7a9b95f 100644 --- a/test/t8project_gtest_vtk_linkage.cxx +++ b/test/t8project_gtest_vtk_linkage.cxx @@ -29,55 +29,12 @@ * does nothing and is always passed. */ #include -#include #include -#include #if T8PROJECT_ENABLE_VTK #include -#include #include #endif -/* Test correct macro dependencies. - * Will throw a compile time error if T8PROJECT_ENABLE_VTK is O - * but T8PROJECT_VTK_VERSION_USED or T8PROJECT_VTK_MAJOR_VERSION or T8PROJECT_VTK_MINOR_VERSION is defined. */ -#if not T8PROJECT_ENABLE_VTK -#ifdef T8PROJECT_VTK_VERSION_USED -#error Configuration error: T8PROJECT_VTK_VERSION_USED is defined despite \ - T8PROJECT_ENABLE_VTK not being defined. -#endif - -#ifdef T8PROJECT_VTK_MAJOR_VERSION -#error Configuration error: T8PROJECT_VTK_MAJOR_VERSION is defined despite \ - T8PROJECT_ENABLE_VTK not being defined. -#endif - -#ifdef T8PROJECT_VTK_MINOR_VERSION -#error Configuration error: T8PROJECT_VTK_MINOR_VERSION is defined despite \ - T8PROJECT_ENABLE_VTK not being defined. -#endif -#endif - - -/* Test correct macro dependencies. - * Will throw a compile time error if T8PROJECT_ENABLE_VTK is 1 - * but one of T8PROJECT_VTK_VERSION_USED, T8PROJECT_VTK_MAJOR_VERSION, T8PROJECT_VTK_MINOR_VERSION is not defined. - */ -#if T8PROJECT_ENABLE_VTK -#ifndef T8PROJECT_VTK_VERSION_USED -#error Configuration error: T8PROJECT_ENABLE_VTK is defined despite \ - T8PROJECT_VTK_VERSION_USED not being defined. -#endif -#ifndef T8PROJECT_VTK_MAJOR_VERSION -#error Configuration error: T8PROJECT_ENABLE_VTK is defined despite \ - T8PROJECT_VTK_MAJOR_VERSION not being defined. -#endif -#ifndef T8PROJECT_VTK_MINOR_VERSION -#error Configuration error: T8PROJECT_ENABLE_VTK is defined despite \ - T8PROJECT_VTK_MINOR_VERSION not being defined. -#endif -#endif - /* Check whether T8PROJECT_VTK_VERSION_USED equals VTK_MAJOR_VERSION.VTK_MINOR_VERSION */ TEST (t8project_gtest_vtk_linkage, t8project_test_vtk_version_number) { @@ -90,20 +47,28 @@ TEST (t8project_gtest_vtk_linkage, t8project_test_vtk_version_number) if (!strcmp (T8PROJECT_VTK_VERSION_USED, vtk_version)) { std::cout << "Using vtk version " << vtk_version << std::endl; } - EXPECT_EQ (T8PROJECT_VTK_MAJOR_VERSION, VTK_MAJOR_VERSION); - EXPECT_EQ (T8PROJECT_VTK_MINOR_VERSION, VTK_MINOR_VERSION); #endif } -/* Check whether T8PROJECT_VTK_VERSION_USED equals VTK_MAJOR_VERSION.VTK_MINOR_VERSION */ -TEST (t8project_gtest_vtk_linkage, t8code_compatibility) +/* Check whether T8PROJECT_VTK_VERSION_USED equals T8_VTK_VERSION_USED */ +TEST (t8project_gtest_vtk_linkage, t8project_and_t8code_use_same_vtk_version) { -#if T8_ENABLE_VTK - EXPECT_EQ (T8PROJECT_VTK_MAJOR_VERSION, T8_MAJOR_VERSION) << "VTK version mismatch. t8code uses a different VTK major version.\n"; - EXPECT_EQ (T8PROJECT_VTK_MINOR_VERSION, T8_MINOR_VERSION) << "VTK version mismatch. t8code uses a different VTK major version.\n"; +#if T8PROJECT_ENABLE_VTK + EXPECT_FALSE (strcmp (T8PROJECT_VTK_VERSION_USED, T8_VTK_VERSION_USED)) + << "linked vtk version (" << T8PROJECT_VTK_VERSION_USED << ") does not equal the version t8code was configured with (" + << T8_VTK_VERSION_USED << ").\n"; #endif } +TEST (t8project_gtest_vtk_linkage, t8project_uses_vtk_if_t8code_does) +{ + #if T8_ENABLE_VTK + #if !T8PROJECT_ENABLE_VTK + ASSERT_FALSE (true) << "t8code was linked against VTK but t8project was not."; + #endif + #endif +} + /* Check whether we can successfully execute VTK code */ TEST (t8project_gtest_vtk_linkage, t8project_test_vtk_linkage) {