dmalhotra / pvfmm

A parallel kernel-independent FMM library for particle and volume potentials
http://pvfmm.org
GNU Lesser General Public License v3.0
51 stars 28 forks source link

pvfmm_config.h breaks add_subdirectory() compilation with PVFMM as a third-party library #12

Closed mmorse1217 closed 3 years ago

mmorse1217 commented 3 years ago

It seems that when one tries to auto-download PVFMM with FetchContent_Declare() in CMake, the resulting call to make is broken because pvfmm_config.h is not found. It seems that ${CMAKE_CURRENT_SOURCE_DIR} is not added through target_include_directories(), but the root level CMakeLists.txt writes pvfmm_config.h to the root directory (seen here ).

It seems that there are two possible fixes for this:

  1. Since it seems that having pvfmm_config.h in the project root is relevant to other parts of the build, simply adding:
    configure_file(pvfmm_config.h.in include/pvfmm_config.h @ONLY)

    below line 70 seems to address the problem in a non-elegant but sufficient way. This is probably the least effort solution and makes some sense. It seems that files in PVFMM explicitly depend on pvfmm_config.h, so maybe it should live in include/.

  2. Explicitly adding something like ${PROJECT_SOURCE_DIR} to the target_include_directories() calls also works. But this could have problematic effects with the calls to install() if it is written without the proper generator expression to switch locations for the install and build interfaces.

A minimal example to reproduce the error: root level CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 14) 
project(test) 
include(cmake/PVFMM.cmake)

In cmake/PVFMM.cmake:

if (NOT TARGET PVFMM::PVFMM)                                                                                                                                                                                                                              
    include(FetchContent)                                                                                                                                                                                                                                 
    FetchContent_Declare(                                                                                                                                                                                                                                 
        PVFMM                                                                                                                                                                                                                                             
        SOURCE_DIR ${CMAKE_BINARY_DIR}/_deps/PVFMM                                                                                                                                                                                                        
        BINARY_DIR ${CMAKE_BINARY_DIR}/_deps/PVFMM                                                                                                                                                                                                        
        GIT_REPOSITORY https://github.com/dmalhotra/pvfmm.git                                                                                                                                                                                             
        GIT_TAG        ffec8376dac7e2df134e56c1a37f22051ec483bb                                                                                                                                                                                           
        GIT_SHALLOW TRUE                                                                                                                                                                                                                                  
    )                                                                                                                                                                                                                                                     

    set(CMAKE_BUILD_TYPE Release CACHE INTERNAL  "Release or debug mode")                                                                                                                                                                                 

    FetchContent_GetProperties(PVFMM)                                                                                                                                                                                                                     
    if(NOT pvfmm_POPULATED)                                                                                                                                                                                                                               
        FetchContent_Populate(PVFMM)                                                                                                                                                                                                                      
        message("pvfmm_SOURCE_DIR " ${pvfmm_SOURCE_DIR} " " ${pvfmm_BINARY_DIR})                                                                                                                                                                          
        set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)                                                                                                                                                                     

        add_subdirectory(${pvfmm_SOURCE_DIR} ${pvfmm_BINARY_DIR})                                                                                                                                                                                         
        add_library(PVFMM::PVFMM INTERFACE IMPORTED)                                                                                                                                                                                                      
        target_include_directories(PVFMM::PVFMM SYSTEM INTERFACE                                                                                                                                                                                          
            ${pvfmm_SOURCE_DIR}/include)                                                                                                                                                                                                                  
        target_link_libraries(PVFMM::PVFMM INTERFACE                                                                                                                                                                                                      
            ${pvfmm_BINARY_DIR}/lib/libpvfmm.a)                                                                                                                                                                                                           
    endif()                                                                                                                                                                                                                                               
endif() 

in the root level run:

mkdir build
cd build
cmake ..
make

Thanks!!

dmalhotra commented 3 years ago

Thanks @mmorse1217 ! Fixed with the first option for now.