c4b970332e21241cde7dc1e170238f0cad577c19
[alexxy/gromacs.git] / cmake / gmxManageMuparser.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2021, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 set(GMX_MUPARSER_REQUIRED_VERSION "2.3")
36
37 include(gmxOptionUtilities)
38
39 # Make a three-state enumeration, defaulting to 
40 gmx_option_multichoice(GMX_USE_MUPARSER
41     "How to handle the muparser dependency of GROMACS"
42     INTERNAL
43     INTERNAL EXTERNAL NONE)
44 mark_as_advanced(GMX_USE_MUPARSER)
45
46 # Make a fully functional muparser library target that libgromacs can
47 # depend on regardless of how the user directed muparser support and/or
48 # linking to work.
49 function(gmx_manage_muparser)
50     if(GMX_USE_MUPARSER STREQUAL "INTERNAL")
51         # Create an object library for the muparser sources
52         set(BUNDLED_MUPARSER_DIR "${CMAKE_SOURCE_DIR}/src/external/muparser")
53         file(GLOB MUPARSER_SOURCES ${BUNDLED_MUPARSER_DIR}/*.cpp)
54         add_library(muparser_objlib OBJECT ${MUPARSER_SOURCES})
55         # Ensure that the objects can be used in both STATIC and SHARED
56         # libraries.
57         set_target_properties(muparser_objlib PROPERTIES POSITION_INDEPENDENT_CODE ON)
58         if (WIN32)
59             # Avoid muParser assuming DLL export attributes should be added
60             target_compile_definitions(muparser_objlib PRIVATE MUPARSER_STATIC)
61         endif()
62
63         # Create an INTERFACE (ie. fake) library for muparser, that
64         # libgromacs can depend on. The generator expression for the
65         # target_sources expands to nothing when cmake builds the
66         # export for libgromacs, so that it understands that we don't
67         # install anything for this library - using plain source files
68         # would not convey the right information.
69         add_library(muparser INTERFACE)
70         target_sources(muparser INTERFACE $<TARGET_OBJECTS:muparser_objlib>)
71         target_include_directories(muparser SYSTEM INTERFACE $<BUILD_INTERFACE:${BUNDLED_MUPARSER_DIR}>)
72         # Add the muparser interface library to the libgromacs Export name, even though
73         # we will not be installing any content.
74         install(TARGETS muparser EXPORT libgromacs)
75
76         set(HAVE_MUPARSER 1 CACHE INTERNAL "Is muparser found?")
77     elseif(GMX_USE_MUPARSER STREQUAL "EXTERNAL")
78         # Find an external muparser library.
79         find_package(muparser ${GMX_MUPARSER_REQUIRED_VERSION})
80         if(NOT MUPARSER_FOUND OR MUPARSER_VERSION VERSION_LESS GMX_MUPARSER_REQUIRED_VERSION)
81             message(FATAL_ERROR "External muparser >= ${GMX_MUPARSER_REQUIRED_VERSION} could not be found, please adjust your pkg-config path to include the muparser.pc file")
82         endif()
83
84         set(HAVE_MUPARSER 1 CACHE INTERNAL "Is muparser found?")
85     else()
86         # Create a dummy link target so the calling code doesn't need to know
87         # whether muparser support is being compiled.
88         add_library(muparser INTERFACE)
89         # Add the muparser interface library to the libgromacs Export name, even though
90         # we will not be installing any content.
91         install(TARGETS muparser EXPORT libgromacs)
92
93         set(HAVE_MUPARSER 0 CACHE INTERNAL "Is muparser found?")
94     endif()
95     mark_as_advanced(HAVE_MUPARSER)
96 endfunction()