Use CMake to propagate versions and hashes to gitlab jobs
[alexxy/gromacs.git] / cmake / gmxManageCcache.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 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 # Permit the use of ccache (when available in the system path or
36 # CMAKE_PREFIX_PATH), which wraps the CMAKE_C_COMPILER and
37 # CMAKE_CXX_COMPILER to speed up build times. Reference
38 # https://ccache.samba.org and
39 # https://crascit.com/2016/04/09/using-ccache-with-cmake/
40
41 option(GMX_ENABLE_CCACHE "Allow CMake to use ccache compiler wrappers if available." OFF)
42
43 if(NOT GMX_ENABLE_CCACHE)
44     return()
45 endif()
46
47 if(GMX_CLANG_TIDY)
48     message(FATAL_ERROR "ccache does not work with the wrapper script used for "
49         "clang-tidy builds. Use -DGMX_ENABLE_CCACHE=off.")
50 endif()
51 if(GMX_CLANG_ANALYZER)
52     message(FATAL_ERROR "ccache does not work with the wrapper script used for "
53         "clang-analyzer builds. Use -DGMX_ENABLE_CCACHE=off.")
54 endif()
55
56 # Here we try to make sure that ccache is invoked as `/.../ccache compiler args` to best handle more than one local
57 # compiler or a compiler wrapper, whereas it is otherwise common to replace the default compilers with symbolic links
58 # to the ccache binary.
59 #
60 # ccache only works for gcc compatible compilers. We should test with anything other than CMAKE_<LANG>_COMPILER_ID==GNU
61 # Clang is reported to work with some caveats. See https://pspdfkit.com/blog/2015/ccache-for-fun-and-profit/
62 find_program(CCACHE_PROGRAM ccache)
63 if(CCACHE_PROGRAM)
64     # Check whether C compiler wrapper has been set up.
65     if(NOT DEFINED GMX_CCACHE_C_COMPILER)
66         # Determine whether we have a cacheable compiler.
67         set(_cacheable OFF)
68         if (CMAKE_C_COMPILER_ID MATCHES "GNU"
69             OR CMAKE_C_COMPILER_ID MATCHES "AppleClang"
70             OR CMAKE_C_COMPILER_ID MATCHES "Clang")
71             message(STATUS "Setting up ccache wrapper for ${CMAKE_C_COMPILER_ID} C compiler ${CMAKE_C_COMPILER}")
72             configure_file(${CMAKE_CURRENT_SOURCE_DIR}/admin/ccache-wrapper-c.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ccache-wrapper-c)
73             file(COPY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ccache-wrapper-c
74                  DESTINATION ${CMAKE_BINARY_DIR}
75                  FILE_PERMISSIONS
76                  OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
77                  )
78             set(_cacheable ON)
79         else()
80             message(FATAL_ERROR "Cannot set up ccache, as it is not confirmed to "
81                 "work with compiler ID ${CMAKE_C_COMPILER_ID}.")
82         endif()
83         set(GMX_CCACHE_C_COMPILER ${_cacheable} CACHE INTERNAL "Whether the C compiler will be wrapped for caching.")
84         unset(_cacheable)
85     endif() # defined
86     # Check whether we should use the wrapper. If so, set CMAKE variables.
87     if(GMX_CCACHE_C_COMPILER)
88         if(CMAKE_GENERATOR STREQUAL "Xcode")
89             # Set Xcode project attributes to route compilation and linking
90             # through our scripts
91             set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/ccache-wrapper-c")
92             set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/ccache-wrapper-c")
93         else()
94             # Support Unix Makefiles and Ninja
95             set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/ccache-wrapper-c")
96         endif()
97     endif()
98
99     # Check whether CXX compiler wrapper has been set up
100     if(NOT DEFINED GMX_CCACHE_CXX_COMPILER)
101         # Determine whether we have a cacheable compiler.
102         set(_cacheable OFF)
103         if (CMAKE_CXX_COMPILER_ID MATCHES "GNU"
104             OR CMAKE_CXX_COMPILER_ID MATCHES "AppleClang"
105             OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
106             message(STATUS "Setting up ccache wrapper for ${CMAKE_CXX_COMPILER_ID} CXX compiler ${CMAKE_CXX_COMPILER}")
107             configure_file(${CMAKE_CURRENT_SOURCE_DIR}/admin/ccache-wrapper-cxx.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ccache-wrapper-cxx)
108             file(COPY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/ccache-wrapper-cxx
109                  DESTINATION ${CMAKE_BINARY_DIR}
110                  FILE_PERMISSIONS
111                  OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
112                 )
113             set(_cacheable ON)
114         else()
115             message(FATAL_ERROR "Cannot set up ccache, as it is not confirmed to "
116                 "work with compiler ID ${CMAKE_CXX_COMPILER_ID}.")
117         endif()
118         set(GMX_CCACHE_CXX_COMPILER ${_cacheable} CACHE INTERNAL "Whether the C++ compiler will be wrapped for caching.")
119         unset(_cacheable)
120     endif() # defined
121     # Check whether we should use the wrapper. If so, set CMAKE variables.
122     if(GMX_CCACHE_CXX_COMPILER)
123         if(CMAKE_GENERATOR STREQUAL "Xcode")
124             # Set Xcode project attributes to route compilation and linking
125             # through our scripts
126             set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/ccache-wrapper-cxx")
127             set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/ccache-wrapper-cxx")
128         else()
129             # Support Unix Makefiles and Ninja
130             set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/ccache-wrapper-cxx")
131         endif()
132     endif()
133 else()
134     message(FATAL_ERROR "GMX_ENABLE_CCACHE requires that the ccache executable "
135         "can be found. Add its path to the CMAKE_PREFIX_PATH path")
136 endif()