Provide common library headers through new CMake target.
[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 by the GROMACS development team.
5 # Copyright (c) 2015,2016,2017,2018,2019,2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 set(LIBGROMACS_SOURCES)
37
38 if (GMX_CLANG_CUDA)
39     include(gmxClangCudaUtils)
40 endif()
41
42 set_property(GLOBAL PROPERTY GMX_LIBGROMACS_SOURCES)
43 set_property(GLOBAL PROPERTY CUDA_SOURCES)
44 set_property(GLOBAL PROPERTY GMX_INSTALLED_HEADERS)
45 set_property(GLOBAL PROPERTY GMX_AVX_512_SOURCE)
46
47 set(libgromacs_object_library_dependencies "")
48 function (_gmx_add_files_to_property PROPERTY)
49     foreach (_file ${ARGN})
50         if (IS_ABSOLUTE "${_file}")
51             set_property(GLOBAL APPEND PROPERTY ${PROPERTY} ${_file})
52         else()
53             set_property(GLOBAL APPEND PROPERTY ${PROPERTY}
54                          ${CMAKE_CURRENT_LIST_DIR}/${_file})
55         endif()
56     endforeach()
57 endfunction ()
58
59 function (gmx_add_libgromacs_sources)
60     _gmx_add_files_to_property(GMX_LIBGROMACS_SOURCES ${ARGN})
61 endfunction ()
62
63 # Permit the configuration to disable compiling the many nbnxm kernels
64 # and others involved in force calculations. Currently only
65 # short-ranged and bonded kernels are disabled this way, but in future
66 # others may be appropriate. Thus the cmake option is not specific to
67 # nbnxm module.
68 option(GMX_USE_SIMD_KERNELS "Whether to compile NBNXM and other SIMD kernels" ON)
69 mark_as_advanced(GMX_USE_SIMD_KERNELS)
70
71 # Add these contents first because linking their tests can take a lot
72 # of time, so we want lots of parallel work still available after
73 # linking starts.
74 add_subdirectory(utility)
75 # Add normal contents
76 add_subdirectory(gmxlib)
77 add_subdirectory(mdlib)
78 add_subdirectory(applied_forces)
79 add_subdirectory(listed_forces)
80 add_subdirectory(nbnxm)
81 add_subdirectory(commandline)
82 add_subdirectory(domdec)
83 add_subdirectory(ewald)
84 add_subdirectory(fft)
85 add_subdirectory(gpu_utils)
86 add_subdirectory(hardware)
87 add_subdirectory(linearalgebra)
88 add_subdirectory(math)
89 add_subdirectory(mdrun)
90 add_subdirectory(mdrunutility)
91 add_subdirectory(mdspan)
92 add_subdirectory(mdtypes)
93 add_subdirectory(onlinehelp)
94 add_subdirectory(options)
95 add_subdirectory(pbcutil)
96 add_subdirectory(random)
97 add_subdirectory(restraint)
98 add_subdirectory(tables)
99 add_subdirectory(taskassignment)
100 add_subdirectory(timing)
101 add_subdirectory(topology)
102 add_subdirectory(trajectory)
103 add_subdirectory(swap)
104 add_subdirectory(essentialdynamics)
105 add_subdirectory(pulling)
106 add_subdirectory(simd)
107 add_subdirectory(imd)
108 add_subdirectory(compat)
109 add_subdirectory(mimic)
110 add_subdirectory(modularsimulator)
111 if (NOT GMX_BUILD_MDRUN_ONLY)
112     add_subdirectory(gmxana)
113     add_subdirectory(gmxpreprocess)
114     add_subdirectory(correlationfunctions)
115     add_subdirectory(statistics)
116     add_subdirectory(analysisdata)
117     add_subdirectory(coordinateio)
118     add_subdirectory(trajectoryanalysis)
119     add_subdirectory(energyanalysis)
120     add_subdirectory(tools)
121 endif()
122
123 get_property(PROPERTY_SOURCES GLOBAL PROPERTY GMX_LIBGROMACS_SOURCES)
124 list(APPEND LIBGROMACS_SOURCES ${GMXLIB_SOURCES} ${MDLIB_SOURCES} ${PROPERTY_SOURCES})
125
126 # This would be the standard way to include thread_mpi, but
127 # we want libgromacs to link the functions directly
128 #if(GMX_THREAD_MPI)
129 #    add_subdirectory(thread_mpi)
130 #endif()
131 #target_link_libraries(gmx ${GMX_EXTRA_LIBRARIES} ${THREAD_MPI_LIB})
132 tmpi_get_source_list(THREAD_MPI_SOURCES ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/src)
133 add_library(thread_mpi OBJECT ${THREAD_MPI_SOURCES})
134 target_compile_definitions(thread_mpi PRIVATE HAVE_CONFIG_H)
135 if(CYGWIN)
136     # Needs POSIX-isms for strdup, not just std-isms
137     target_compile_definitions(thread_mpi PRIVATE _POSIX_C_SOURCE=200809L)
138 endif()
139 gmx_target_compile_options(thread_mpi)
140 if (WIN32)
141     gmx_target_warning_suppression(thread_mpi /wd4996 HAS_NO_MSVC_UNSAFE_FUNCTION)
142 endif()
143 list(APPEND libgromacs_object_library_dependencies thread_mpi)
144
145 if(GMX_INSTALL_LEGACY_API)
146   install(FILES
147           analysisdata.h
148           options.h
149           selection.h
150           trajectoryanalysis.h
151           DESTINATION include/gromacs)
152 endif()
153
154 # This code is here instead of utility/CMakeLists.txt, because CMake
155 # custom commands and source file properties can only be set in the directory
156 # that contains the target that uses them.
157 # TODO: Generate a header instead that can be included from baseversion.cpp.
158 # That probably simplifies things somewhat.
159 set(GENERATED_VERSION_FILE utility/baseversion-gen.cpp)
160 gmx_configure_version_file(
161     utility/baseversion-gen.cpp.cmakein ${GENERATED_VERSION_FILE}
162     REMOTE_HASH
163     EXTRA_VARS
164         GMX_SOURCE_DOI
165         GMX_RELEASE_HASH
166         GMX_SOURCE_HASH
167         )
168 list(APPEND LIBGROMACS_SOURCES ${GENERATED_VERSION_FILE})
169
170 # Mark some shared GPU implementation files to compile with CUDA if needed
171 if (GMX_GPU_CUDA)
172     get_property(CUDA_SOURCES GLOBAL PROPERTY CUDA_SOURCES)
173     set_source_files_properties(${CUDA_SOURCES} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
174 endif()
175
176 if (GMX_GPU_CUDA)
177     # Work around FindCUDA that prevents using target_link_libraries()
178     # with keywords otherwise...
179     set(CUDA_LIBRARIES PRIVATE ${CUDA_LIBRARIES})
180     if (NOT GMX_CLANG_CUDA)
181         gmx_cuda_add_library(libgromacs ${LIBGROMACS_SOURCES})
182     else()
183         add_library(libgromacs ${LIBGROMACS_SOURCES})
184     endif()
185     target_link_libraries(libgromacs PRIVATE ${CUDA_CUFFT_LIBRARIES})
186 else()
187     add_library(libgromacs ${LIBGROMACS_SOURCES})
188 endif()
189 target_link_libraries(libgromacs PRIVATE common)
190
191 # Add these contents first because linking their tests can take a lot
192 # of time, so we want lots of parallel work still available after
193 # linking starts.
194 add_subdirectory(fileio)
195 add_subdirectory(selection)
196
197 # Suppress a warning about our abuse of t_inputrec
198 gmx_source_file_warning_suppression(mdtypes/inputrec.cpp -Wno-class-memaccess HAS_NO_CLASS_MEMACCESS)
199
200 # Handle the object libraries that contain the source file
201 # dependencies that need special handling because they are generated
202 # or external code.
203 foreach(object_library ${libgromacs_object_library_dependencies})
204     if (BUILD_SHARED_LIBS)
205         set_target_properties(${object_library} PROPERTIES POSITION_INDEPENDENT_CODE true)
206     endif()
207     target_include_directories(${object_library} SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
208     target_link_libraries(${object_library} PRIVATE common)
209
210     # Add the sources from the object libraries to the main library.
211     target_sources(libgromacs PRIVATE $<TARGET_OBJECTS:${object_library}>)
212 endforeach()
213 gmx_target_compile_options(libgromacs)
214 target_compile_definitions(libgromacs PRIVATE HAVE_CONFIG_H)
215 target_include_directories(libgromacs SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
216
217 if (GMX_GPU_OPENCL)
218     option(GMX_EXTERNAL_CLFFT "True if an external clFFT is required to be used" FALSE)
219     mark_as_advanced(GMX_EXTERNAL_CLFFT)
220
221     # Default to using clFFT found on the system
222     # switch to quiet at the second run.
223     if (DEFINED clFFT_LIBRARY)
224         set (clFFT_FIND_QUIETLY TRUE)
225     endif()
226     find_package(clFFT)
227     if (NOT clFFT_FOUND)
228         if (GMX_EXTERNAL_CLFFT)
229             message(FATAL_ERROR "Did not find required external clFFT library, consider setting clFFT_ROOT_DIR")
230         endif()
231
232         if(MSVC)
233             message(FATAL_ERROR
234 "An OpenCL build was requested with Visual Studio compiler, but GROMACS
235 requires clFFT, which was not found on your system. GROMACS does bundle
236 clFFT to help with building for OpenCL, but that clFFT has not yet been
237 ported to the more recent versions of that compiler that GROMACS itself
238 requires. Thus for now, OpenCL is not available with MSVC and the internal
239 build of clFFT in GROMACS 2019. Either change compiler, try installing
240 a clFFT package, or use the latest GROMACS 2018 point release.")
241         endif()
242
243         # Fall back on the internal version
244         set (_clFFT_dir ../external/clFFT/src)
245         add_subdirectory(${_clFFT_dir} clFFT-build)
246         target_sources(libgromacs PRIVATE
247             $<TARGET_OBJECTS:clFFT>
248         )
249         target_include_directories(libgromacs SYSTEM PRIVATE ${_clFFT_dir}/include)
250         # Use the magic variable for how to link any library needed for
251         # dlopen, etc.  which is -ldl where needed, and empty otherwise
252         # (e.g. Windows, BSD, Mac).
253         target_link_libraries(libgromacs PRIVATE "${CMAKE_DL_LIBS}")
254     else()
255         target_link_libraries(libgromacs PRIVATE clFFT)
256     endif()
257 endif()
258
259 # Permit GROMACS code to include externally developed headers, such as
260 # the functionality from the nonstd project that we use for
261 # gmx::compat::optional. These are included as system headers so that
262 # no warnings are issued from them.
263 #
264 # TODO Perhaps generalize this for all headers from src/external
265 target_include_directories(libgromacs SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/src/external)
266
267 if(SIMD_AVX_512_CXX_SUPPORTED AND NOT ("${GMX_SIMD_ACTIVE}" STREQUAL "AVX_512_KNL"))
268     # Since we might be overriding -march=core-avx2, add a flag so we don't warn for this specific file.
269     # On KNL this can cause illegal instruction because the compiler might use non KNL AVX instructions
270     # with the SIMD_AVX_512_CXX_FLAGS flags.
271     set_source_files_properties(hardware/identifyavx512fmaunits.cpp PROPERTIES COMPILE_FLAGS "${SIMD_AVX_512_CXX_FLAGS} ${CXX_NO_UNUSED_OPTION_WARNING_FLAGS}")
272 endif()
273
274 # Do any special handling needed for .cpp files that use
275 # CUDA runtime headers
276 if (GMX_GPU_CUDA AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
277     # CUDA header cuda_runtime_api.h in at least CUDA 10.1 uses 0
278     # where nullptr would be preferable. GROMACS can't fix these, so
279     # must suppress them.
280     GMX_TEST_CXXFLAG(CXXFLAGS_NO_ZERO_AS_NULL_POINTER_CONSTANT "-Wno-zero-as-null-pointer-constant" NVCC_CLANG_SUPPRESSIONS_CXXFLAGS)
281
282     foreach(_compile_flag ${NVCC_CLANG_SUPPRESSIONS_CXXFLAGS})
283         set(GMX_CUDA_CLANG_FLAGS "${GMX_CUDA_CLANG_FLAGS} ${_compile_flag}")
284     endforeach()
285     if (GMX_CLANG_CUDA)
286         foreach (_file ${LIBGROMACS_SOURCES})
287             get_filename_component(_ext ${_file} EXT)
288             get_source_file_property(_cuda_source_format ${_file} CUDA_SOURCE_PROPERTY_FORMAT)
289             if ("${_ext}" STREQUAL ".cu" OR _cuda_source_format)
290                 gmx_compile_cuda_file_with_clang(${_file})
291             endif()
292         endforeach()
293     else()
294         get_property(CUDA_SOURCES GLOBAL PROPERTY CUDA_SOURCES)
295         set_source_files_properties(${CUDA_SOURCES} PROPERTIES COMPILE_FLAGS ${GMX_CUDA_CLANG_FLAGS})
296     endif()
297 endif()
298
299 # Only add the -fsycl flag to sources that really need it
300 if (GMX_GPU_SYCL)
301     get_property(SYCL_SOURCES GLOBAL PROPERTY SYCL_SOURCES)
302     set_source_files_properties(${SYCL_SOURCES} PROPERTIES COMPILE_FLAGS "${SYCL_CXX_FLAGS}")
303 endif()
304
305 gmx_setup_tng_for_libgromacs()
306
307 # We apply the SYCL flag explicitly just for libgromacs, since bugs in the beta versions of
308 # icpx/dpcpp leads to crashes if we try to link an library without any SYCL code with the
309 # -fsycl flag enabled. Once that bug is fixed, we should change it to simply add
310 # SYCL_CXX_FLAGS to GMX_SHARED_LINKER_FLAGS.
311 target_link_libraries(libgromacs
312                       PRIVATE
313                       ${EXTRAE_LIBRARIES}
314                       ${GMX_EXTRA_LIBRARIES}
315                       ${GMX_COMMON_LIBRARIES}
316                       ${FFT_LIBRARIES} ${LINEAR_ALGEBRA_LIBRARIES}
317                       ${THREAD_LIB} ${GMX_SHARED_LINKER_FLAGS}
318                       ${SYCL_CXX_FLAGS}
319                       ${OpenCL_LIBRARIES}
320                       $<$<PLATFORM_ID:SunOS>:socket>
321                       PUBLIC
322                       ${GMX_PUBLIC_LIBRARIES}
323                       )
324 target_link_libraries(libgromacs PUBLIC legacy_api)
325
326 if (GMX_OPENMP)
327     target_link_libraries(libgromacs PUBLIC OpenMP::OpenMP_CXX)
328 endif()
329 set_target_properties(libgromacs PROPERTIES
330                       OUTPUT_NAME "gromacs${GMX_LIBS_SUFFIX}"
331                       SOVERSION ${LIBRARY_SOVERSION_MAJOR}
332                       VERSION ${LIBRARY_VERSION}
333                       )
334
335 gmx_manage_lmfit()
336 target_link_libraries(libgromacs PRIVATE lmfit)
337
338 # Make sure we fix "everything" found by more recent versions of clang.
339 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7")
340    target_compile_options(libgromacs PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Weverything ${IGNORED_CLANG_ALL_WARNINGS}>)
341 endif()
342 if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
343    target_compile_options(libgromacs PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/analyze /analyze:stacksize 70000
344      #Control flow warnings are disabled because the commond line output is insufficient. There is no tool
345      #to convert the xml report to e.g. HTML and even in Visual Studio the viewer doesn't work with cmake support.
346      /wd6001  #unitialized memory
347      /wd6011  #derefencing NULL
348      /wd6053  #prior call not zero-terminate
349      /wd6054  #might not be zero-terminated
350      /wd6385  #reading invalid data
351      /wd6386  #buffer overrun
352      /wd6387  #could be '0'
353      /wd28199 #uninitialized memory
354      # For compile time constant (e.g. templates) the following warnings have flase postives
355      /wd6239  #(<non-zero> && <expr>)
356      /wd6240  #(<expr> && <non-zero>)
357      /wd6294  #Ill-defined for-loop
358      /wd6326  #comparison of constant with other constant
359      /wd28020 #expression involving paramter is not true
360      # Misc
361      /wd6330  #incorrect type to function (warns for char (instead of unsigned) for isspace/isalpha/isdigit/..))
362      /wd6993  #OpenMP ignored
363      #TODO
364      /wd6031  #return value ignored (important - mostly warnigns about sscanf)
365      /wd6244  #hides declaration (known issue - we ingore similar warnings for other compilers)
366      /wd6246  #hides declaration
367      >
368    )
369 endif()
370
371 if (GMX_CLANG_TIDY)
372    set_target_properties(libgromacs PROPERTIES CXX_CLANG_TIDY
373        "${CLANG_TIDY_EXE};-warnings-as-errors=*")
374 endif()
375
376 # clang-3.6 warns about a number of issues that are not reported by more modern compilers
377 # and we know they are not real issues. So we only check that it can compile without error
378 # but ignore all warnings.
379 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION MATCHES "^3\.6")
380     target_compile_options(libgromacs PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-w>)
381 endif()
382
383 # Only install the library in mdrun-only mode if it is actually necessary
384 # for the binary
385 # TODO: Stop installing libgromacs. Possibly allow installation during deprecation period with GMX_INSTALL_LEGACY_API.
386 if (NOT GMX_BUILD_MDRUN_ONLY OR BUILD_SHARED_LIBS)
387     install(TARGETS libgromacs
388             EXPORT libgromacs
389             LIBRARY
390                 DESTINATION ${CMAKE_INSTALL_LIBDIR}
391                 COMPONENT libraries
392             RUNTIME
393                 DESTINATION ${CMAKE_INSTALL_BINDIR}
394                 COMPONENT libraries
395             ARCHIVE
396                 DESTINATION ${CMAKE_INSTALL_LIBDIR}
397                 COMPONENT libraries
398             INCLUDES DESTINATION include)
399     target_compile_definitions(libgromacs PUBLIC $<INSTALL_INTERFACE:GMX_DOUBLE=${GMX_DOUBLE_VALUE}>)
400     # legacy headers use c++17 features, so consumer codes need to use that standard, too
401     if(GMX_INSTALL_LEGACY_API)
402         target_compile_features(libgromacs INTERFACE cxx_std_${CMAKE_CXX_STANDARD})
403     endif()
404 endif()
405 add_library(Gromacs::libgromacs ALIAS libgromacs)
406
407 if (NOT GMX_BUILD_MDRUN_ONLY)
408     include(InstallLibInfo.cmake)
409 endif()
410
411 # Technically, the user could want to do this for an OpenCL build
412 # using the CUDA runtime, but currently there's no reason to want to
413 # do that.
414 if (INSTALL_CUDART_LIB) #can be set manual by user
415     if (GMX_GPU_CUDA)
416         foreach(CUDA_LIB ${CUDA_LIBRARIES})
417             string(REGEX MATCH "cudart" IS_CUDART ${CUDA_LIB})
418             if(IS_CUDART) #libcuda should not be installed
419                 #install also name-links (linker uses those)
420                 file(GLOB CUDA_LIBS ${CUDA_LIB}*)
421                 install(FILES ${CUDA_LIBS} DESTINATION
422                     ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries)
423             endif()
424         endforeach()
425     else()
426         message(WARNING "INSTALL_CUDART_LIB only makes sense when configuring for CUDA support")
427     endif()
428 endif()
429
430 if(GMX_GPU_OPENCL)
431     # Install the utility headers
432     file(GLOB OPENCL_INSTALLED_FILES
433         gpu_utils/vectype_ops.clh
434         gpu_utils/device_utils.clh
435         )
436     install(FILES ${OPENCL_INSTALLED_FILES}
437         DESTINATION ${GMX_INSTALL_OCLDIR}/gromacs/gpu_utils
438         COMPONENT libraries)
439     file(GLOB OPENCL_INSTALLED_FILES
440         pbcutil/ishift.h
441         )
442     install(FILES ${OPENCL_INSTALLED_FILES}
443         DESTINATION ${GMX_INSTALL_OCLDIR}/gromacs/pbcutil
444         COMPONENT libraries)
445
446     # Install the NBNXM source and headers
447     file(GLOB OPENCL_INSTALLED_FILES
448         nbnxm/constants.h
449         )
450     install(FILES ${OPENCL_INSTALLED_FILES}
451         DESTINATION ${GMX_INSTALL_OCLDIR}/gromacs/nbnxm
452         COMPONENT libraries)
453     file(GLOB OPENCL_INSTALLED_FILES
454         nbnxm/opencl/nbnxm_ocl_kernels.cl
455         nbnxm/opencl/nbnxm_ocl_kernel.clh
456         nbnxm/opencl/nbnxm_ocl_kernel_pruneonly.clh
457         nbnxm/opencl/nbnxm_ocl_kernels.clh
458         nbnxm/opencl/nbnxm_ocl_kernels_fastgen.clh
459         nbnxm/opencl/nbnxm_ocl_kernels_fastgen_add_twincut.clh
460         nbnxm/opencl/nbnxm_ocl_kernel_utils.clh
461         nbnxm/opencl/nbnxm_ocl_consts.h
462         )
463     install(FILES ${OPENCL_INSTALLED_FILES}
464         DESTINATION ${GMX_INSTALL_OCLDIR}/gromacs/nbnxm/opencl
465         COMPONENT libraries)
466
467     # Install the PME source and headers
468     file(GLOB OPENCL_INSTALLED_FILES
469         ewald/pme_spread.clh
470         ewald/pme_solve.clh
471         ewald/pme_gather.clh
472         ewald/pme_gpu_calculate_splines.clh
473         ewald/pme_program.cl
474         ewald/pme_gpu_types.h
475         )
476     install(FILES ${OPENCL_INSTALLED_FILES}
477         DESTINATION ${GMX_INSTALL_OCLDIR}/gromacs/ewald
478         COMPONENT libraries)
479 endif()