Skip to content

Commit

Permalink
CMake: automatically use ccache or sccache if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Mar 10, 2022
1 parent eb3b47c commit 0731fa6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ project(cxx_qt)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(CompilerCaching)

# Enable extra Qt definitions for all projects
add_compile_definitions(
QT_NO_CAST_FROM_ASCII
Expand Down
7 changes: 7 additions & 0 deletions LICENSES/BSD-3-Clause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 changes: 65 additions & 0 deletions cmake/CompilerCaching.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# SPDX-FileCopyrightText: 2021 Tenacity Audio Editor contributors
# SPDX-FileContributor: Be <[email protected]>
# SPDX-FileContributor: Emily Mabrey <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause
#[=======================================================================[.rst:
CompilerCaching
---------------

Search for sccache and ccache and use them for compiler caching for C & C++.
ccache is preferred if both are found, but the user can override this by
explicitly setting CCACHE=OFF to use sccache when both are installed.
#]=======================================================================]

# ccache does not support MSVC
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
find_program(CCACHE_PROGRAM ccache)
mark_as_advanced(FORCE CCACHE_PROGRAM)
if("${CCACHE_PROGRAM}" STREQUAL "CCACHE_PROGRAM-NOTFOUND")
message(STATUS "Could NOT find ccache")
else()
message(STATUS "Found ccache: ${CCACHE_PROGRAM}")
option(CCACHE "Use ccache for compiler caching to speed up rebuilds." ON)
endif()
endif()

find_program(SCCACHE_PROGRAM sccache)
mark_as_advanced(FORCE SCCACHE_PROGRAM)
if("${SCCACHE_PROGRAM}" STREQUAL "SCCACHE_PROGRAM-NOTFOUND")
message(STATUS "Could NOT find sccache")
else()
message(STATUS "Found sccache: ${SCCACHE_PROGRAM}")
option(SCCACHE "Use sccache for compiler caching to speed up rebuilds." ON)
endif()

if(CCACHE)
message(STATUS "Using ccache for compiler caching to speed up rebuilds")
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
elseif(SCCACHE)
message(STATUS "Using sccache for compiler caching to speed up rebuilds")
set(CMAKE_C_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")
set(CMAKE_CXX_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")

# Instruct MSVC to generate symbolic debug information within object files for sccache
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
if(IS_MULTI_CONFIG)
foreach(CONFIG ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${CONFIG}" CONFIG)
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_${CONFIG} "${CMAKE_CXX_FLAGS_${CONFIG}}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_${CONFIG} "${CMAKE_C_FLAGS_${CONFIG}}")
endforeach()
else()
string(TOUPPER "${CMAKE_BUILD_TYPE}" CONFIG)
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_${CONFIG} "${CMAKE_CXX_FLAGS_${CONFIG}}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_${CONFIG} "${CMAKE_C_FLAGS_${CONFIG}}")
endif()
endif()
else()
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message(STATUS "No compiler caching enabled. Install sccache to speed up rebuilds.")
else()
message(STATUS "No compiler caching enabled. Install ccache or sccache to speed up rebuilds.")
endif()
endif()

0 comments on commit 0731fa6

Please sign in to comment.