24ba0d1a6166491f22e3eb606a092cefaf3696c3
[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,2017,2018, 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 if (GMX_CLANG_CUDA)
38     include(gmxClangCudaUtils)
39 endif()
40
41 set_property(GLOBAL PROPERTY GMX_LIBGROMACS_SOURCES)
42 set_property(GLOBAL PROPERTY GMX_LIBGROMACS_GPU_IMPL_SOURCES)
43 set_property(GLOBAL PROPERTY GMX_INSTALLED_HEADERS)
44 set_property(GLOBAL PROPERTY GMX_AVX_512_SOURCE)
45
46 add_library(libgromacs_external OBJECT "")
47 add_library(libgromacs_generated OBJECT "")
48 if (BUILD_SHARED_LIBS)
49     set_target_properties(libgromacs_external PROPERTIES POSITION_INDEPENDENT_CODE true)
50     set_target_properties(libgromacs_generated PROPERTIES POSITION_INDEPENDENT_CODE true)
51 endif()
52
53 function (_gmx_add_files_to_property PROPERTY)
54     foreach (_file ${ARGN})
55         if (IS_ABSOLUTE "${_file}")
56             set_property(GLOBAL APPEND PROPERTY ${PROPERTY} ${_file})
57         else()
58             set_property(GLOBAL APPEND PROPERTY ${PROPERTY}
59                          ${CMAKE_CURRENT_LIST_DIR}/${_file})
60         endif()
61     endforeach()
62 endfunction ()
63
64 function (gmx_add_libgromacs_sources)
65     _gmx_add_files_to_property(GMX_LIBGROMACS_SOURCES ${ARGN})
66 endfunction ()
67
68 # TODO Reconsider this, as the CUDA driver API is probably a simpler
69 # approach, at least for the build system. See Redmine #2530
70 function (gmx_compile_cpp_as_cuda)
71     _gmx_add_files_to_property(GMX_LIBGROMACS_GPU_IMPL_SOURCES ${ARGN})
72 endfunction ()
73
74 function (gmx_install_headers)
75     if (NOT GMX_BUILD_MDRUN_ONLY)
76         file(RELATIVE_PATH _dest ${PROJECT_SOURCE_DIR}/src ${CMAKE_CURRENT_LIST_DIR})
77         install(FILES       ${ARGN}
78                 DESTINATION "${INCL_INSTALL_DIR}/${_dest}"
79                 COMPONENT   development)
80     endif()
81     _gmx_add_files_to_property(GMX_INSTALLED_HEADERS ${ARGN})
82 endfunction ()
83
84 function (gmx_write_installed_header_list)
85     get_property(_list GLOBAL PROPERTY GMX_INSTALLED_HEADERS)
86     string(REPLACE ";" "\n" _list "${_list}")
87     # TODO: Make this only update the file timestamp if the contents actually change.
88     file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/installed-headers.txt "${_list}")
89 endfunction()
90
91 add_subdirectory(gmxlib)
92 add_subdirectory(mdlib)
93 add_subdirectory(applied-forces)
94 add_subdirectory(listed-forces)
95 add_subdirectory(commandline)
96 add_subdirectory(domdec)
97 add_subdirectory(ewald)
98 add_subdirectory(fft)
99 add_subdirectory(gpu_utils)
100 add_subdirectory(hardware)
101 add_subdirectory(linearalgebra)
102 add_subdirectory(math)
103 add_subdirectory(mdrun)
104 add_subdirectory(mdrunutility)
105 add_subdirectory(mdtypes)
106 add_subdirectory(onlinehelp)
107 add_subdirectory(options)
108 add_subdirectory(pbcutil)
109 add_subdirectory(random)
110 add_subdirectory(tables)
111 add_subdirectory(taskassignment)
112 add_subdirectory(timing)
113 add_subdirectory(topology)
114 add_subdirectory(trajectory)
115 add_subdirectory(utility)
116 add_subdirectory(fileio)
117 add_subdirectory(swap)
118 add_subdirectory(essentialdynamics)
119 add_subdirectory(pulling)
120 add_subdirectory(awh)
121 add_subdirectory(simd)
122 add_subdirectory(imd)
123 if (NOT GMX_BUILD_MDRUN_ONLY)
124     add_subdirectory(gmxana)
125     add_subdirectory(gmxpreprocess)
126     add_subdirectory(correlationfunctions)
127     add_subdirectory(statistics)
128     add_subdirectory(analysisdata)
129     add_subdirectory(selection)
130     add_subdirectory(trajectoryanalysis)
131     add_subdirectory(energyanalysis)
132     add_subdirectory(tools)
133 endif()
134
135 get_property(PROPERTY_SOURCES GLOBAL PROPERTY GMX_LIBGROMACS_SOURCES)
136 list(APPEND LIBGROMACS_SOURCES ${GMXLIB_SOURCES} ${MDLIB_SOURCES} ${PROPERTY_SOURCES})
137
138 # This would be the standard way to include thread_mpi, but
139 # we want libgromacs to link the functions directly
140 #if(GMX_THREAD_MPI)
141 #    add_subdirectory(thread_mpi)
142 #endif()
143 #target_link_libraries(gmx ${GMX_EXTRA_LIBRARIES} ${THREAD_MPI_LIB})
144
145 tmpi_get_source_list(THREAD_MPI_SOURCES ${CMAKE_SOURCE_DIR}/src/external/thread_mpi/src)
146 target_sources(libgromacs_external PRIVATE ${THREAD_MPI_SOURCES})
147
148 configure_file(version.h.cmakein version.h)
149 gmx_install_headers(
150     analysisdata.h
151     commandline.h
152     options.h
153     random.h
154     selection.h
155     trajectoryanalysis.h
156     utility.h
157     ${CMAKE_CURRENT_BINARY_DIR}/version.h
158     )
159
160 # This code is here instead of utility/CMakeLists.txt, because CMake
161 # custom commands and source file properties can only be set in the directory
162 # that contains the target that uses them.
163 # TODO: Generate a header instead that can be included from baseversion.c.
164 # That probably simplifies things somewhat.
165 set(GENERATED_VERSION_FILE utility/baseversion-gen.c)
166 gmx_configure_version_file(
167     utility/baseversion-gen.c.cmakein ${GENERATED_VERSION_FILE}
168     REMOTE_HASH)
169 list(APPEND LIBGROMACS_SOURCES ${GENERATED_VERSION_FILE}
170      $<TARGET_OBJECTS:libgromacs_external>
171      $<TARGET_OBJECTS:libgromacs_generated>)
172
173 # Mark some shared GPU implementation files to compile with CUDA if needed
174 if (GMX_USE_CUDA)
175     get_property(LIBGROMACS_GPU_IMPL_SOURCES GLOBAL PROPERTY GMX_LIBGROMACS_GPU_IMPL_SOURCES)
176     set_source_files_properties(${LIBGROMACS_GPU_IMPL_SOURCES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
177 endif()
178
179 # set up CUDA compilation with clang
180 if (GMX_CLANG_CUDA)
181     foreach (_file ${LIBGROMACS_SOURCES})
182         get_filename_component(_ext ${_file} EXT)
183         get_source_file_property(_cuda_source_format ${_file} CUDA_SOURCE_PROPERTY_FORMAT)
184         if (${_ext} STREQUAL ".cu" OR _cuda_source_format)
185             gmx_compile_cuda_file_with_clang(${_file})
186         endif()
187     endforeach()
188 endif()
189
190 if (GMX_USE_CUDA)
191     # Work around FindCUDA that prevents using target_link_libraries()
192     # with keywords otherwise...
193     set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
194     if (NOT GMX_CLANG_CUDA)
195         cuda_add_library(libgromacs ${LIBGROMACS_SOURCES})
196     else()
197         add_library(libgromacs ${LIBGROMACS_SOURCES})
198     endif()
199     target_link_libraries(libgromacs PRIVATE ${CUDA_CUFFT_LIBRARIES})
200 else()
201     add_library(libgromacs ${LIBGROMACS_SOURCES})
202 endif()
203
204 if (GMX_USE_OPENCL)
205     option(GMX_EXTERNAL_CLFFT "True if an external clFFT is required to be used" FALSE)
206     mark_as_advanced(GMX_EXTERNAL_CLFFT)
207
208     # Default to using clFFT found on the system
209     find_package(clFFT)
210     if (NOT clFFT_FOUND)
211         if (GMX_EXTERNAL_CLFFT)
212             message(FATAL_ERROR "Did not find required external clFFT library, consider setting clFFT_ROOT_DIR")
213         endif()
214
215         # Fall back on the internal version
216         set (_clFFT_dir ../external/clFFT/src)
217         add_subdirectory(${_clFFT_dir} clFFT-build)
218         target_sources(libgromacs PRIVATE
219             $<TARGET_OBJECTS:clFFT>
220         )
221         target_include_directories(libgromacs PRIVATE ${_clFFT_dir}/include)
222         # Use the magic variable for how to link any library needed for
223         # dlopen, etc.  which is -ldl where needed, and empty otherwise
224         # (e.g. Windows, BSD, Mac).
225         target_link_libraries(libgromacs PRIVATE "${CMAKE_DL_LIBS}")
226     else()
227         target_link_libraries(libgromacs PRIVATE clFFT)
228     endif()
229 endif()
230
231 # Recent versions of gcc and clang give warnings on scanner.cpp, which
232 # is a generated source file. These are awkward to suppress inline, so
233 # we do it in the compilation command (after testing that the compiler
234 # supports the suppressions).
235 include(CheckCXXCompilerFlag)
236 check_cxx_compiler_flag(-Wno-unused HAS_NO_UNUSED)
237 if (HAS_NO_UNUSED)
238     set(_scanner_cpp_compiler_flags "${_scanner_cpp_compiler_flags} -Wno-unused-parameter -Wno-unused-function")
239 endif()
240 set_source_files_properties(selection/scanner.cpp PROPERTIES COMPILE_FLAGS "${_scanner_cpp_compiler_flags}")
241
242 if(SIMD_AVX_512_CXX_SUPPORTED)
243     # Since we might be overriding -march=core-avx2, add a flag so we don't warn for this specific file
244     set_source_files_properties(hardware/identifyavx512fmaunits.cpp PROPERTIES COMPILE_FLAGS "${SIMD_AVX_512_CXX_FLAGS} ${CXX_NO_UNUSED_OPTION_WARNING_FLAGS}")
245 endif()
246
247 gmx_setup_tng_for_libgromacs()
248
249 target_link_libraries(libgromacs
250                       PRIVATE
251                       ${EXTRAE_LIBRARIES}
252                       ${GMX_EXTRA_LIBRARIES}
253                       ${GMX_COMMON_LIBRARIES}
254                       ${FFT_LIBRARIES} ${LINEAR_ALGEBRA_LIBRARIES}
255                       ${THREAD_LIB} ${GMX_SHARED_LINKER_FLAGS}
256                       ${OpenCL_LIBRARIES}
257                       ${GMX_STDLIB_LIBRARIES}
258                       PUBLIC
259                       ${GMX_PUBLIC_LIBRARIES}
260                       )
261 set_target_properties(libgromacs PROPERTIES
262                       OUTPUT_NAME "gromacs${GMX_LIBS_SUFFIX}"
263                       SOVERSION ${LIBRARY_SOVERSION_MAJOR}
264                       VERSION ${LIBRARY_VERSION}
265                       COMPILE_FLAGS "${OpenMP_C_FLAGS}")
266
267 gmx_manage_lmfit()
268 target_link_libraries(libgromacs PRIVATE lmfit)
269
270 if (GMX_CLANG_TIDY)
271    set(CLANG_TIDY "clang-tidy" CACHE STRING "Name of clang-tidy executable")
272    find_program(CLANG_TIDY_EXE NAMES "${CLANG_TIDY}"
273        DOC "Path to clang-tidy executable")
274    if(NOT CLANG_TIDY_EXE)
275        message(ERROR "clang-tidy not found.")
276    endif()
277    if(CMAKE_VERSION VERSION_LESS "3.6")
278        message(ERROR "clang-tidy support requires cmake 3.6.")
279    endif()
280    set_target_properties(libgromacs PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};--checks=\
281        -clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-core.NullDereference,\
282        bugprone-*,misc-*,readability-*,performance-*,mpi-*,\
283        -misc-misplaced-widening-cast,-readability-named-parameter,\
284        -misc-misplaced-const,\
285        -misc-incorrect-roundings,-misc-macro-parentheses,-readability-function-size,-readability-else-after-return,\
286        -readability-inconsistent-declaration-parameter-name,-misc-throw-by-value-catch-by-reference,\
287        -readability-non-const-parameter,-readability-implicit-bool-conversion,\
288        modernize-use-nullptr,modernize-use-emplace;-warnings-as-errors=*;-fix")
289 endif()
290
291 gmx_write_installed_header_list()
292
293 # Only install the library in mdrun-only mode if it is actually necessary
294 # for the binary
295 if (NOT GMX_BUILD_MDRUN_ONLY OR BUILD_SHARED_LIBS)
296     install(TARGETS libgromacs
297             EXPORT libgromacs
298             LIBRARY DESTINATION ${LIB_INSTALL_DIR}
299             RUNTIME DESTINATION ${BIN_INSTALL_DIR}
300             ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
301             COMPONENT libraries)
302 endif()
303
304 if (NOT GMX_BUILD_MDRUN_ONLY)
305     include(InstallLibInfo.cmake)
306 endif()
307
308 # Technically, the user could want to do this for an OpenCL build
309 # using the CUDA runtime, but currently there's no reason to want to
310 # do that.
311 if (INSTALL_CUDART_LIB) #can be set manual by user
312     if (GMX_USE_CUDA)
313         foreach(CUDA_LIB ${CUDA_LIBRARIES})
314             string(REGEX MATCH "cudart" IS_CUDART ${CUDA_LIB})
315             if(IS_CUDART) #libcuda should not be installed
316                 #install also name-links (linker uses those)
317                 file(GLOB CUDA_LIBS ${CUDA_LIB}*)
318                 install(FILES ${CUDA_LIBS} DESTINATION
319                     ${LIB_INSTALL_DIR} COMPONENT libraries)
320             endif()
321         endforeach()
322     else()
323         message(WARNING "INSTALL_CUDART_LIB only makes sense when configuring for CUDA support")
324     endif()
325 endif()
326
327 if(GMX_USE_OPENCL)
328     set(OPENCL_KERNELS ${MDLIB_OPENCL_KERNELS})
329
330     install(FILES ${OPENCL_KERNELS} DESTINATION
331         ${OCL_INSTALL_DIR} COMPONENT libraries)
332 endif()
333
334 if (BUILD_TESTING)
335     add_subdirectory(compat/tests)
336 endif()