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