Adjust loader path resolution on Mac OS X.
[gromacs.git] / cmake / gmxCcache.cmake
blob7ffe32ae07b8f3ae19c99b47d61170b36cdd1350
2 # This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 # Reference https://crascit.com/2016/04/09/using-ccache-with-cmake/
37 # Here we try to make sure that ccache is invoked as `/.../ccache compiler args` to best handle more than one local
38 # compiler or a compiler wrapper, whereas it is otherwise common to replace the default compilers with symbolic links
39 # to the ccache binary.
41 # ccache only works for gcc compatible compilers. We should test with anything other than CMAKE_<LANG>_COMPILER_ID==GNU
42 # Clang is reported to work with some caveats. See https://pspdfkit.com/blog/2015/ccache-for-fun-and-profit/
43 find_program(CCACHE_PROGRAM ccache)
44 if(CCACHE_PROGRAM)
45     # Check whether C compiler wrapper has been set up.
46     if(NOT DEFINED GMX_CACHE_C_COMPILER)
47         # Determine whether we have a cacheable compiler.
48         set(_cacheable OFF)
49         if (CMAKE_C_COMPILER_ID MATCHES "GNU"
50             OR CMAKE_C_COMPILER_ID MATCHES "AppleClang"
51             OR CMAKE_C_COMPILER_ID MATCHES "Clang")
52             message(STATUS "Setting up ccache wrapper for ${CMAKE_C_COMPILER_ID} C compiler ${CMAKE_C_COMPILER}")
53             set(_c_launcher "${CCACHE_PROGRAM}")
54             configure_file(launch-c.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/launch-c)
55             unset(_c_launcher)
56             file(COPY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/launch-c
57                  DESTINATION ${CMAKE_BINARY_DIR}
58                  FILE_PERMISSIONS
59                  OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
60                  )
61             set(_cacheable ON)
62         else()
63             message(STATUS "Disabling ccache set up. Not confirmed to work with compiler ID ${CMAKE_C_COMPILER_ID}.")
64         endif() # GNU C compiler
65         set(GMX_CACHE_C_COMPILER ${_cacheable} CACHE INTERNAL "Whether the C compiler will be wrapped for caching.")
66         unset(_cacheable)
67     endif() # defined
68     # Check whether we should use the wrapper. If so, set CMAKE variables.
69     if(GMX_CACHE_C_COMPILER)
70         if(CMAKE_GENERATOR STREQUAL "Xcode")
71             # Set Xcode project attributes to route compilation and linking
72             # through our scripts
73             set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/launch-c")
74             set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/launch-c")
75         else()
76             # Support Unix Makefiles and Ninja
77             set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-c")
78         endif()
79     endif(GMX_CACHE_C_COMPILER)
81     # Check whether CXX compiler wrapper has been set up
82     if(NOT DEFINED GMX_CACHE_CXX_COMPILER)
83         # Determine whether we have a cacheable compiler.
84         set(_cacheable OFF)
85         if (CMAKE_CXX_COMPILER_ID MATCHES "GNU"
86             OR CMAKE_CXX_COMPILER_ID MATCHES "AppleClang"
87             OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
88             message(STATUS "Setting up ccache wrapper for ${CMAKE_CXX_COMPILER_ID} CXX compiler ${CMAKE_CXX_COMPILER}")
89             set(_cxx_launcher "${CCACHE_PROGRAM}")
90             configure_file(launch-cxx.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/launch-cxx)
91             unset(_cxx_launcher)
92             file(COPY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/launch-cxx
93                  DESTINATION ${CMAKE_BINARY_DIR}
94                  FILE_PERMISSIONS
95                  OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
96                 )
97             set(_cacheable ON)
98         else()
99             message(STATUS "Skipping ccache set up. Not confirmed to work with compiler ID ${CMAKE_CXX_COMPILER_ID}.")
100         endif() # GNU C++ compiler
101         set(GMX_CACHE_CXX_COMPILER ${_cacheable} CACHE INTERNAL "Whether the C++ compiler will be wrapped for caching.")
102         unset(_cacheable)
103     endif() # defined
104     # Check whether we should use the wrapper. If so, set CMAKE variables.
105     if(GMX_CACHE_CXX_COMPILER)
106         if(CMAKE_GENERATOR STREQUAL "Xcode")
107             # Set Xcode project attributes to route compilation and linking
108             # through our scripts
109             set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/launch-cxx")
110             set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/launch-cxx")
111         else()
112             # Support Unix Makefiles and Ninja
113             set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_BINARY_DIR}/launch-cxx")
114         endif()
115     endif(GMX_CACHE_CXX_COMPILER)
116 endif(CCACHE_PROGRAM) # ccache program