Skip to content

Commit

Permalink
feat: increase ccache hit rate
Browse files Browse the repository at this point in the history
When using MSVC as the compiler, `cmake` automatically adds the '/Zi' flag
which is not compatible with ccache.
  • Loading branch information
abdes committed Sep 21, 2022
1 parent 94522c8 commit a22a912
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ endif()
# project hierarchy
include(LogHelpers)

# Setup our override file, in which we may cleanup cmake's default compiler
# options, based on what we are doing.
set(CMAKE_USER_MAKE_RULES_OVERRIDE "ResetInitialCompilerOptions")

# ------------------------------------------------------------------------------
# Project description and (meta) information
# ------------------------------------------------------------------------------
Expand Down
35 changes: 35 additions & 0 deletions cmake/ResetInitialCompilerOptions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ===-----------------------------------------------------------------------===#
# Distributed under the 3-Clause BSD License. See accompanying file LICENSE or
# copy at https://opensource.org/licenses/BSD-3-Clause).
# SPDX-License-Identifier: BSD-3-Clause
# ===-----------------------------------------------------------------------===#

# This module is loaded by `cmake` while enabling support for each language from
# either the project() or enable_language() commands. It is loaded after CMake's
# builtin compiler and platform information modules have been loaded but before
# the information is used. The file may set platform information variables to
# override CMake's defaults.
#
# To load this module, set the variable `CMAKE_USER_MAKE_RULES_OVERRIDE` before
# you declare the project or enable a language:
# ~~~
# set(CMAKE_USER_MAKE_RULES_OVERRIDE "ResetInitialCompilerOptions")
# ~~~

# We use this module to strip compiler options that are not really needed but
# will cause compatibility issues with `ccache`.
if(MSVC AND USE_CCACHE)
# As of ccache 4.6, /Zi option automatically added by cmake is unsupported.
# Given that we are doing ccache only in development environments (USE_CCACHE
# controls if ccache is enabled), we can just strip that option.
macro(strip_unwanted_options_from cmake_flags)
if(${cmake_flags} MATCHES "/Zi")
string(REPLACE "/Zi" "/Z7" ${cmake_flags} ${${cmake_flags}})
endif()
endmacro()
strip_unwanted_options_from(CMAKE_CXX_FLAGS_DEBUG_INIT)
strip_unwanted_options_from(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT)
strip_unwanted_options_from(CMAKE_C_FLAGS_DEBUG_INIT)
strip_unwanted_options_from(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT)
set(first_time FALSE)
endif()

0 comments on commit a22a912

Please sign in to comment.