Minor CMake cleanup & modernization
[alexxy/gromacs.git] / src / gromacs / CMakeLists.txt
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2010,2011,2012,2013,2014,2015,2016, 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(LIBGROMACS_SOURCES)
36
37 set_property(GLOBAL PROPERTY GMX_LIBGROMACS_SOURCES)
38 set_property(GLOBAL PROPERTY GMX_INSTALLED_HEADERS)
39
40 function (_gmx_add_files_to_property PROPERTY)
41     foreach (_file ${ARGN})
42         if (IS_ABSOLUTE "${_file}")
43             set_property(GLOBAL APPEND PROPERTY ${PROPERTY} ${_file})
44         else()
45             set_property(GLOBAL APPEND PROPERTY ${PROPERTY}
46                          ${CMAKE_CURRENT_LIST_DIR}/${_file})
47         endif()
48     endforeach()
49 endfunction ()
50
51 function (gmx_add_libgromacs_sources)
52     _gmx_add_files_to_property(GMX_LIBGROMACS_SOURCES ${ARGN})
53 endfunction ()
54
55 function (gmx_install_headers)
56     if (NOT GMX_BUILD_MDRUN_ONLY)
57         file(RELATIVE_PATH _dest ${PROJECT_SOURCE_DIR}/src ${CMAKE_CURRENT_LIST_DIR})
58         install(FILES       ${ARGN}
59                 DESTINATION "${INCL_INSTALL_DIR}/${_dest}"
60                 COMPONENT   development)
61     endif()
62     _gmx_add_files_to_property(GMX_INSTALLED_HEADERS ${ARGN})
63 endfunction ()
64
65 function (gmx_write_installed_header_list)
66     get_property(_list GLOBAL PROPERTY GMX_INSTALLED_HEADERS)
67     string(REPLACE ";" "\n" _list "${_list}")
68     # TODO: Make this only update the file timestamp if the contents actually change.
69     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/installed-headers.txt "${_list}")
70 endfunction()
71
72 add_subdirectory(gmxlib)
73 add_subdirectory(mdlib)
74 add_subdirectory(applied-forces)
75 add_subdirectory(listed-forces)
76 add_subdirectory(commandline)
77 add_subdirectory(domdec)
78 add_subdirectory(ewald)
79 add_subdirectory(fft)
80 add_subdirectory(gpu_utils)
81 add_subdirectory(hardware)
82 add_subdirectory(linearalgebra)
83 add_subdirectory(math)
84 add_subdirectory(mdrunutility)
85 add_subdirectory(mdtypes)
86 add_subdirectory(onlinehelp)
87 add_subdirectory(options)
88 add_subdirectory(pbcutil)
89 add_subdirectory(random)
90 add_subdirectory(tables)
91 add_subdirectory(timing)
92 add_subdirectory(topology)
93 add_subdirectory(trajectory)
94 add_subdirectory(utility)
95 add_subdirectory(fileio)
96 add_subdirectory(swap)
97 add_subdirectory(essentialdynamics)
98 add_subdirectory(pulling)
99 add_subdirectory(simd)
100 add_subdirectory(imd)
101 if (NOT GMX_BUILD_MDRUN_ONLY)
102     add_subdirectory(gmxana)
103     add_subdirectory(gmxpreprocess)
104     add_subdirectory(correlationfunctions)
105     add_subdirectory(statistics)
106     add_subdirectory(analysisdata)
107     add_subdirectory(selection)
108     add_subdirectory(trajectoryanalysis)
109     add_subdirectory(tools)
110 endif()
111
112 get_property(PROPERTY_SOURCES GLOBAL PROPERTY GMX_LIBGROMACS_SOURCES)
113 list(APPEND LIBGROMACS_SOURCES ${GMXLIB_SOURCES} ${MDLIB_SOURCES} ${PROPERTY_SOURCES})
114
115 # This would be the standard way to include thread_mpi, but
116 # we want libgromacs to link the functions directly
117 #if(GMX_THREAD_MPI)
118 #    add_subdirectory(thread_mpi)
119 #endif()
120 #target_link_libraries(gmx ${GMX_EXTRA_LIBRARIES} ${THREAD_MPI_LIB})
121
122 tmpi_get_source_list(THREAD_MPI_SOURCES ${CMAKE_SOURCE_DIR}/src/external/thread_mpi/src)
123 list(APPEND LIBGROMACS_SOURCES ${THREAD_MPI_SOURCES})
124
125 get_lmfit_properties(LMFIT_SOURCES LMFIT_LIBRARIES_TO_LINK LMFIT_INCLUDE_DIRECTORY LMFIT_INCLUDE_DIR_ORDER)
126 include_directories(${LMFIT_INCLUDE_DIR_ORDER} SYSTEM "${LMFIT_INCLUDE_DIRECTORY}")
127 list(APPEND LIBGROMACS_SOURCES ${LMFIT_SOURCES})
128
129 configure_file(version.h.cmakein version.h)
130 gmx_install_headers(
131     analysisdata.h
132     commandline.h
133     options.h
134     random.h
135     selection.h
136     trajectoryanalysis.h
137     utility.h
138     ${CMAKE_CURRENT_BINARY_DIR}/version.h
139     )
140
141 # This code is here instead of utility/CMakeLists.txt, because CMake
142 # custom commands and source file properties can only be set in the directory
143 # that contains the target that uses them.
144 # TODO: Generate a header instead that can be included from baseversion.c.
145 # That probably simplifies things somewhat.
146 set(GENERATED_VERSION_FILE utility/baseversion-gen.c)
147 gmx_configure_version_file(
148     utility/baseversion-gen.c.cmakein ${GENERATED_VERSION_FILE}
149     REMOTE_HASH)
150 list(APPEND LIBGROMACS_SOURCES ${GENERATED_VERSION_FILE})
151
152 if (GMX_USE_CUDA)
153     # Work around FindCUDA that prevents using target_link_libraries()
154     # with keywords otherwise...
155     set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
156     cuda_add_library(libgromacs ${LIBGROMACS_SOURCES})
157 else()
158     add_library(libgromacs ${LIBGROMACS_SOURCES})
159 endif()
160
161 # Recent versions of gcc and clang give warnings on scanner.cpp, which
162 # is a generated source file. These are awkward to suppress inline, so
163 # we do it in the compilation command (after testing that the compiler
164 # supports the suppressions).
165 include(CheckCXXCompilerFlag)
166 check_cxx_compiler_flag(-Wno-unused-parameter HAS_NO_UNUSED_PARAMETER)
167 if (HAS_NO_UNUSED_PARAMETER)
168     set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-unused-parameter")
169 endif()
170 check_cxx_compiler_flag(-Wno-deprecated-register HAS_NO_DEPRECATED_REGISTER)
171 if (HAS_NO_DEPRECATED_REGISTER)
172     set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-deprecated-register")
173 else()
174     check_cxx_compiler_flag(-Wno-deprecated HAS_NO_DEPRECATED)
175     if (HAS_NO_DEPRECATED)
176         set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-deprecated")
177     endif()
178 endif()
179 set_source_files_properties(selection/scanner.cpp PROPERTIES COMPILE_FLAGS "${_scanner_cpp_compiler_flags}")
180
181 gmx_setup_tng_for_libgromacs()
182
183 target_link_libraries(libgromacs
184                       PRIVATE
185                       ${EXTRAE_LIBRARIES}
186                       ${GMX_EXTRA_LIBRARIES}
187                       ${GMX_COMMON_LIBRARIES}
188                       ${FFT_LIBRARIES} ${LINEAR_ALGEBRA_LIBRARIES}
189                       ${LMFIT_LIBRARIES_TO_LINK}
190                       ${THREAD_LIB} ${GMX_SHARED_LINKER_FLAGS} ${OPENCL_LIBRARIES}
191                       ${GMX_STDLIB_LIBRARIES}
192                       PUBLIC
193                       ${GMX_PUBLIC_LIBRARIES}
194                       )
195 set_target_properties(libgromacs PROPERTIES
196                       OUTPUT_NAME "gromacs${GMX_LIBS_SUFFIX}"
197                       SOVERSION ${LIBRARY_SOVERSION_MAJOR}
198                       VERSION ${LIBRARY_VERSION}
199                       COMPILE_FLAGS "${OpenMP_C_FLAGS}")
200
201 gmx_write_installed_header_list()
202
203 # Only install the library in mdrun-only mode if it is actually necessary
204 # for the binary
205 if (NOT GMX_BUILD_MDRUN_ONLY OR BUILD_SHARED_LIBS)
206     install(TARGETS libgromacs
207             EXPORT libgromacs
208             LIBRARY DESTINATION ${LIB_INSTALL_DIR}
209             RUNTIME DESTINATION ${BIN_INSTALL_DIR}
210             ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
211             COMPONENT libraries)
212 endif()
213
214 if (NOT GMX_BUILD_MDRUN_ONLY)
215     include(InstallLibInfo.cmake)
216 endif()
217
218 # Technically, the user could want to do this for an OpenCL build
219 # using the CUDA runtime, but currently there's no reason to want to
220 # do that.
221 if (INSTALL_CUDART_LIB) #can be set manual by user
222     if (GMX_USE_CUDA)
223         foreach(CUDA_LIB ${CUDA_LIBRARIES})
224             string(REGEX MATCH "cudart" IS_CUDART ${CUDA_LIB})
225             if(IS_CUDART) #libcuda should not be installed
226                 #install also name-links (linker uses those)
227                 file(GLOB CUDA_LIBS ${CUDA_LIB}*)
228                 install(FILES ${CUDA_LIBS} DESTINATION
229                     ${LIB_INSTALL_DIR} COMPONENT libraries)
230             endif()
231         endforeach()
232     else()
233         message(WARNING "INSTALL_CUDART_LIB only makes sense when configuring for CUDA support")
234     endif()
235 endif()
236
237 if(GMX_USE_OPENCL)
238     set(OPENCL_KERNELS ${MDLIB_OPENCL_KERNELS})
239
240     install(FILES ${OPENCL_KERNELS} DESTINATION
241         ${OCL_INSTALL_DIR} COMPONENT libraries)
242 endif()