PCMSolver / pcmsolver

An API for the Polarizable Continuum Model
http://pcmsolver.readthedocs.io/
GNU Lesser General Public License v3.0
32 stars 21 forks source link

Fix CMake to work with FetchContent #196

Open awild82 opened 2 years ago

awild82 commented 2 years ago

Description

This pull request makes some trivial changes to the CMake structure in order to enable inclusion of PCMSolver in other projects via FetchContent. In particular, it adds the api directory as an interface include directory on both the shared and static targets, and it makes the external/Catch directory relative to the PCMSolver root, not the total build root.

Motivation and Context

This PR enables inclusion of PCMSolver through FetchContent.

How Has This Been Tested?

This PR was primarily tested through:

  1. Ensuring that changes did not affect the build process of PCMSolver on its own (and ensuring that the tests still pass)
  2. Including the PCMSolver build through FetchContent in a dummy CMake build. This was tested with the standard build, SHARED_LIBRARY_ONLY=ON, and STATIC_LIBRARY_ONLY=ON. I only tested this on a CentOS 7 server, but none of the code I changed should affect builds on Windows or Mac, so I don't expect issues from those installations either.

FetchContent CMake build details

The structure of the dummy CMake project is a single CMakeLists.txt and a single source file, test.cxx.

The CMakeLists.txt file had the following content:

cmake_minimum_required(VERSION 3.14)
project(Testing LANGUAGES C CXX Fortran)

include(FetchContent)

FetchContent_Declare(pcmsolver
  GIT_REPOSITORY https://github.com/awild82/pcmsolver.git
  GIT_TAG c0fe869a6245f28186af0329f6308f47e8ca75ea
)
FetchContent_MakeAvailable(pcmsolver)

add_executable(test_fetchcontent test.cxx)
target_link_libraries(test_fetchcontent PCMSolver)

The only non-standard thing here is that this project must be declared having a Fortran language dependency when linking against the PCMSolver static library in order for the Fortran symbols from pedra to be resolved.

The test.cxx file can be pretty much whatever you like to test inclusion and linking. I used some code from the C host example:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pcmsolver.h>

#define NR_NUCLEI 6

void host_writer(const char * message) { printf("%s\n", message); }

struct PCMInput pcmsolver_input() {
  struct PCMInput host_input;

  strcpy(host_input.cavity_type, "gepol");
  host_input.patch_level = 2;
  host_input.coarsity = 0.5;
  host_input.area = 0.2;
  host_input.min_distance = 0.1;
  host_input.der_order = 4;
  host_input.scaling = true;
  strcpy(host_input.radii_set, "bondi");
  strcpy(host_input.restart_name, "cavity.npz");
  host_input.min_radius = 100.0;

  strcpy(host_input.solver_type, "iefpcm");
  strcpy(host_input.solvent, "water");
  strcpy(host_input.equation_type, "secondkind");
  host_input.correction = 0.0;
  host_input.probe_radius = 1.0;

  strcpy(host_input.inside_type, "vacuum");
  host_input.outside_epsilon = 1.0;
  strcpy(host_input.outside_type, "uniformdielectric");

  return host_input;
}

int main() {
  // Setup stolen from C-Host example
  double charges[NR_NUCLEI] = {6.0, 1.0, 1.0, 6.0, 1.0, 1.0};
  double coordinates[3 * NR_NUCLEI] = {0.0,
                                       0.000000,
                                       1.257892,
                                       0.0,
                                       1.745462,
                                       2.342716,
                                       0.0,
                                       -1.745462,
                                       2.342716,
                                       0.0,
                                       0.000000,
                                       -1.257892,
                                       0.0,
                                       1.745462,
                                       -2.342716,
                                       0.0,
                                       -1.745462,
                                       -2.342716};
  int symmetry_info[4] = {3, 4, 2, 1};
  struct PCMInput host_input = pcmsolver_input();

  pcmsolver_context_t * pcm_context = pcmsolver_new(PCMSOLVER_READER_HOST,
                                                    NR_NUCLEI,
                                                    charges,
                                                    coordinates,
                                                    symmetry_info,
                                                    &host_input,
                                                    host_writer);

  pcmsolver_print(pcm_context);

  return 0;
}

The final executable was executed and gave the expected print to stdout.

Types of changes

Status