Disable PME Mixed mode with FEP
[alexxy/gromacs.git] / cmake / gmxManageSharedLibraries.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2018,2020,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 # Manage the GROMACS shared library setup.
36
37 ########################################################################
38 # Shared/static library settings
39 ########################################################################
40 # Determine the defaults (this block has no effect if the variables have
41 # already been set)
42 if((APPLE OR CYGWIN OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux|.*BSD|GNU") AND NOT GMX_BUILD_MDRUN_ONLY)
43     # Maybe Solaris should be here? Patch this if you know!
44     SET(SHARED_LIBS_DEFAULT ON)
45 elseif(WIN32)
46     # Support for shared libs on native Windows is a bit new. Its
47     # default might change later if/when we sort things out. Also,
48     # Cray should go here. What variable value can detect it?
49     SET(SHARED_LIBS_DEFAULT OFF)
50 else()
51     if (NOT DEFINED BUILD_SHARED_LIBS)
52         message(STATUS "Defaulting to building static libraries")
53     endif()
54     SET(SHARED_LIBS_DEFAULT OFF)
55 endif()
56
57 if (GMX_PREFER_STATIC_LIBS)
58     if (NOT DEFINED BUILD_SHARED_LIBS AND SHARED_LIBS_DEFAULT)
59         message("Searching for static libraries requested, so the GROMACS libraries will also be static (BUILD_SHARED_LIBS=OFF)")
60     endif()
61     set(SHARED_LIBS_DEFAULT OFF)
62 endif()
63
64 if (NOT GMX_BUILD_SHARED_EXE)
65     set(GMX_PREFER_STATIC_LIBS_DEFAULT ON)
66     set(SHARED_LIBS_DEFAULT OFF)
67 endif()
68
69 # Declare the user-visible options
70 option(BUILD_SHARED_LIBS "Enable shared libraries (can be problematic e.g. with MPI, or on some HPC systems)" ${SHARED_LIBS_DEFAULT})
71
72 set(GMX_PREFER_STATIC_LIBS_DEFAULT OFF)
73 if (WIN32 AND NOT BUILD_SHARED_LIBS)
74     set(GMX_PREFER_STATIC_LIBS_DEFAULT ON)
75 endif()
76
77 if(BUILD_SHARED_LIBS AND GMX_BUILD_MDRUN_ONLY)
78     message(WARNING "Both BUILD_SHARED_LIBS and GMX_BUILD_MDRUN_ONLY are set. Generally, an mdrun-only build should prefer to use static libraries, which is the default if you make a fresh build tree. You may be re-using an old build tree, and so may wish to set BUILD_SHARED_LIBS=off yourself.")
79 endif()
80
81 if (UNIX)
82     set(GMX_PREFER_STATIC_LIBS_DESCRIPTION
83         "When finding libraries prefer static archives (it will only work if static versions of external dependencies are available and found)")
84 elseif (WIN32)
85     set(GMX_PREFER_STATIC_LIBS_DESCRIPTION
86         "When finding libraries prefer static system libraries (MT instead of MD)")
87 endif()
88 option(GMX_PREFER_STATIC_LIBS "${GMX_PREFER_STATIC_LIBS_DESCRIPTION}"
89        ${GMX_PREFER_STATIC_LIBS_DEFAULT})
90 mark_as_advanced(GMX_PREFER_STATIC_LIBS)
91
92 # Act on the set values
93 if (UNIX AND GMX_PREFER_STATIC_LIBS)
94     if(BUILD_SHARED_LIBS)
95         # Warn the user about the combination. But don't overwrite the request.
96         message(WARNING "Searching for static libraries requested, and building shared GROMACS libraries requested. This might cause problems linking later.")
97     endif()
98     # On Linux .a is the static library suffix, on Mac OS X .lib can also
99     # be used, so we'll add both to the preference list.
100     SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib;.a" ${CMAKE_FIND_LIBRARY_SUFFIXES})
101 endif()
102
103 # ==========
104 # Only things for managing shared libraries and build types on Windows follow
105
106 # Change the real CMake variables so we prefer static linking. This
107 # should be a function so we can have proper local variables while
108 # avoiding duplicating code.
109 function(gmx_manage_prefer_static_libs_flags build_type)
110     if("${build_type}" STREQUAL "")
111         set(punctuation "") # for general compiler flags (e.g.) CMAKE_CXX_FLAGS
112     else()
113         set(punctuation "_") # for build-type-specific compiler flags (e.g.) CMAKE_CXX_FLAGS_RELEASE
114     endif()
115
116     # Change the real CMake variables for the given build type in each
117     # language, in the parent scope.
118     foreach(language C CXX)
119         string(REPLACE /MD /MT CMAKE_${language}_FLAGS${punctuation}${build_type} ${CMAKE_${language}_FLAGS${punctuation}${build_type}})
120         set(CMAKE_${language}_FLAGS${punctuation}${build_type} ${CMAKE_${language}_FLAGS${punctuation}${build_type}} PARENT_SCOPE)
121     endforeach()
122 endfunction()
123
124 IF( WIN32)
125   if (NOT BUILD_SHARED_LIBS)
126       if(NOT GMX_PREFER_STATIC_LIBS)
127           message(WARNING "Shared system libraries requested, and static GROMACS libraries requested.")
128       endif()
129   else()
130       if(MINGW)
131           set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--export-all-symbols ${CMAKE_SHARED_LINKER_FLAGS}")
132       else()
133           message(FATAL_ERROR "BUILD_SHARED_LIBS=ON not yet working for Windows in the master branch")
134       endif()
135       if(GMX_PREFER_STATIC_LIBS)
136           #this combination segfaults (illegal passing of file handles)
137           message(FATAL_ERROR "Static system libraries requested, and shared GROMACS libraries requested.")
138       endif()
139       # Visibility not yet implemented
140       # add_definitions(-DUSE_VISIBILITY -DTMPI_USE_VISIBILITY)
141       # set(PKG_CFLAGS "$PKG_CFLAGS -DUSE_VISIBILITY -DTMPI_USE_VISIBILITY")
142   endif()
143
144   IF (GMX_PREFER_STATIC_LIBS)
145       foreach(build_type "" ${build_types_with_explicit_flags})
146           gmx_manage_prefer_static_libs_flags("${build_type}")
147       endforeach()
148   ENDIF()
149   IF( CMAKE_C_COMPILER_ID STREQUAL "Intel" )
150     if(BUILD_SHARED_LIBS) #not sure why incremental building with shared libs doesn't work
151         STRING(REPLACE "/INCREMENTAL:YES" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
152         set(CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} PARENT_SCOPE)
153     endif()
154   ENDIF()
155 ENDIF()