diff --git a/example/CMakeProject/CMakeLists.txt b/example/CMakeProject/CMakeLists.txt index 3640e5738bf4a4f6e7283761d36c10df249bcfbf..843c33e2279fc206e5bdee7ca6cc4ac65e0f935e 100644 --- a/example/CMakeProject/CMakeLists.txt +++ b/example/CMakeProject/CMakeLists.txt @@ -1,6 +1,5 @@ -# REMEMBER TO SET CC AND CXX! CMake is too dumb to find compiler in your path! -# mkdcd build -# CC=gcc CXX=g++ cmake -DALL_DIR=`pwd`/../../jall_bin/lib/cmake/ALL ../ +# REMEMBER TO SET CC AND CXX! +# CC=gcc CXX=g++ cmake -S . -B build -DALL_DIR=`pwd`/../../jall_bin/lib/cmake/ALL cmake_minimum_required(VERSION 3.14) project(ALL_Staggered) @@ -11,14 +10,42 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) +# For cleaner code we should also include MPI here, since this project's source +# also needs MPI. Otherwise, we are free to leave it out, since the parts +# required by ALL are automatically included. find_package(MPI REQUIRED COMPONENTS CXX Fortran) + +# Some variants for testing VTK output and the voronoi method. These are only +# required, so the code used in .this. project has the corresponding features +# enabled and calls the respective functions from the library. This is not +# required for the library itself. Although the library needs have been built +# with the respective features enabled. +add_compile_definitions(TEST_VTK_OUTPUT) +add_compile_definitions(TEST_VORONOI) +# For C++ projects including ALL the following macros must also be defined at +# the moment, if the respective features are used. Otherwise, they are not +# visible in the header files. +add_compile_definitions(ALL_VTK_OUTPUT) +add_compile_definitions(ALL_VORONOI_ACTIVE) + find_package(ALL 0.9.1) add_executable(ALL_Staggered ALL_Staggered.cpp) -target_link_libraries(ALL_Staggered PRIVATE ALL::ALL) +target_link_libraries(ALL_Staggered PUBLIC ALL::ALL) +# This is in this case not .necessary., since it is already inherited from ALL, +# however, for readability and clear intent, we should still link against MPI +# ourselves, since our code also uses MPI directly. +target_link_libraries(ALL_Staggered PUBLIC ${MPI_CXX_LINK_FLAGS} ${MPI_CXX_LIBRARIES}) +target_include_directories(ALL_Staggered PRIVATE ${MPI_CXX_INCLUDE_PATH}) add_executable(ALL_Staggered_f ALL_Staggered_f.F90) -target_link_libraries(ALL_Staggered_f LINK_PRIVATE ALL::ALL) -target_link_libraries(ALL_Staggered_f LINK_PRIVATE ALL::ALL_fortran) set_property(TARGET ALL_Staggered_f PROPERTY LINKER_LANGUAGE Fortran) -#todo(s.schulz): link against MPI +target_link_libraries(ALL_Staggered_f PUBLIC ALL::ALL_fortran) +target_link_libraries(ALL_Staggered_f PUBLIC ${MPI_Fortran_LINK_FLAGS} ${MPI_Fortran_LIBRARIES}) +target_include_directories(ALL_Staggered_f PRIVATE ${MPI_Fortran_INCLUDE_PATH}) + +add_executable(ALL_test ALL_test.cpp) +target_link_libraries(ALL_test PUBLIC ALL::ALL) +target_link_libraries(ALL_test PUBLIC ${MPI_CXX_LINK_FLAGS} ${MPI_CXX_LIBRARIES}) +target_include_directories(ALL_test PRIVATE ${MPI_CXX_INCLUDE_PATH}) +# should also explicitly link against VTK diff --git a/example/CMakeProject/build_all.sh b/example/CMakeProject/build_all.sh index e059c34dbdfbb41b864c211022667654703c95f3..1acd12fe93dfd6850eb21117df8ba1cfa299f380 100755 --- a/example/CMakeProject/build_all.sh +++ b/example/CMakeProject/build_all.sh @@ -9,9 +9,16 @@ set -x # $ALL_INSTALL_DIR must be an absolute path! ALL_ROOT_DIR=../.. ALL_BUILD_DIR=all_build -ALL_INSTALL_DIR=`pwd`/all_bin +ALL_INSTALL_DIR=`pwd`/all_bin #where ALL installs itself to +ALL_PACKAGE=`pwd`/all_package #where our project expects the installed ALL +VTK_DIR=`pwd`/../../../vtk_bin BUILD_DIR=build +# We only move ALL after installation from $ALL_INSTALL_DIR to $ALL_PACKAGE to +# test for errors in relocatability of the library. A typical user does not +# need to do that and can just set $ALL_PACKAGE to the directory ALL installs +# itself to. + export CC=gcc export CXX=g++ @@ -21,21 +28,38 @@ build_all () { cmake -S "$ALL_ROOT_DIR" -B "$ALL_BUILD_DIR"\ -DCMAKE_INSTALL_PREFIX="$ALL_INSTALL_DIR"\ -DCM_ALL_FORTRAN=ON\ - -DCMAKE_BUILD_TYPE=Release + -DCM_ALL_VTK_OUTPUT=ON\ + -DCM_ALL_VORONOI=ON\ + -DCMAKE_BUILD_TYPE=Release\ + -DVTK_DIR="$VTK_DIR"/lib/cmake/vtk-7.1 cmake --build "$ALL_BUILD_DIR" rm -rf "ALL_INSTALL_DIR" cmake --install "$ALL_BUILD_DIR" + if [[ $ALL_INSTALL_DIR != $ALL_PACKAGE ]] + then + mv "$ALL_INSTALL_DIR" "$ALL_PACKAGE" + fi } build_self () { rm -rf "$BUILD_DIR" mkdir "$BUILD_DIR" cmake -S . -B "$BUILD_DIR"\ - -DALL_DIR="$ALL_INSTALL_DIR"/lib/cmake/ALL + -DALL_DIR="$ALL_PACKAGE"/lib/cmake/ALL\ + -DVTK_DIR="$VTK_DIR"/lib/cmake/vtk-7.1 + + VERBOSE=1 cmake --build "$BUILD_DIR" +} - cmake --build "$BUILD_DIR" +prepare_example () { + cat $1 | sed \ + -e 's/ALL_VTK_OUTPUT/TEST_VTK_OUTPUT/'\ + -e 's/ALL_VORONOI_ACTIVE/TEST_VORONOI/'\ + > $2 } +prepare_example ../ALL_test.cpp ALL_test.cpp + build_all build_self diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2584174ca64b13f65d988030f33acc8339dbf55a..bc2a42d257bfe720eabf1ecfff9a7d4ea7ec1369 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,9 +12,6 @@ set(ALL_HEADER_FILES ${ALL_INCLUDE_DIR}/ALL_Tensor.hpp ${ALL_INCLUDE_DIR}/ALL_Voronoi.hpp) -# todo(s.schulz): Should these be installed explicitly? -# install(FILES ${ALL_HEADER_FILES} DESTINATION include) - add_library (ALL ALL.cpp ALL_fortran.cpp ${ALL_HEADER_FILES}) if(CM_ALL_VTK_OUTPUT) @@ -25,14 +22,18 @@ if(CM_ALL_VORONOI) target_link_libraries(ALL PUBLIC voro) endif(CM_ALL_VORONOI) -target_link_libraries(ALL INTERFACE stdc++) - target_include_directories(ALL PUBLIC ${MPI_CXX_INCLUDE_PATH}) +target_link_libraries(ALL PUBLIC ${MPI_CXX_LINK_FLAGS} ${MPI_CXX_LIBRARIES}) target_include_directories (ALL PUBLIC "$<BUILD_INTERFACE:${ALL_INCLUDE_DIR}>" "$<INSTALL_INTERFACE:include>") -target_link_libraries(ALL PUBLIC ${MPI_CXX_LINK_FLAGS} ${MPI_CXX_LIBRARIES}) + +# For inclusion in Fortran code, explicit linking against the C++ standard +# library is necessary. +target_link_libraries(ALL INTERFACE stdc++) + + install(TARGETS ALL EXPORT ALLTargets @@ -49,17 +50,13 @@ if (CM_ALL_FORTRAN) add_library (ALL_fortran ALL_module.F90) set_property(TARGET ALL_fortran PROPERTY LINKER_LANGUAGE Fortran) - if(CM_ALL_VTK_OUTPUT) - target_include_directories(ALL_fortran PUBLIC ${VTK_INCLUDE_DIRS}) - target_link_libraries(ALL_fortran PUBLIC ${VTK_LIBRARIES}) - endif(CM_ALL_VTK_OUTPUT) - target_include_directories(ALL_fortran PUBLIC ${MPI_Fortran_INCLUDE_PATH}) target_include_directories(ALL_fortran INTERFACE "$<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY}>" "$<INSTALL_INTERFACE:include/modules>") - target_link_libraries(ALL_fortran PUBLIC ${MPI_Fortran_LINK_FLAGS} ${MPI_Fortran_LIBRARIES} ALL) + target_link_libraries(ALL_fortran PUBLIC ${MPI_Fortran_LINK_FLAGS} ${MPI_Fortran_LIBRARIES}) + target_link_libraries(ALL_fortran PUBLIC ALL) install(TARGETS ALL_fortran EXPORT ALLTargets