LoginSignup
9
3

More than 5 years have passed since last update.

cmakeでccacheを使う

Last updated at Posted at 2018-01-25

ccacheを使うとコンパイルの中間ファイルがキャッシュされ、2度め以降のコンパイルが速く終わります。

cmakeでのビルドで、ccacheを活用するにはどうしたらよいでしょうか。
よくあるのはexport CC='ccache gcc'とかでコンパイラ自体をccacheに置き換える方法です。

cmakeにはコンパイル時にラッパを噛ませるための、RULE_LAUNCH_COMPILEプロパティがあります。
このプロパティを使い、かつcmakeのオプションに対応したのが以下です。ccmake等で有効/無効を調整できるので、少し便利です。

EnableCcache.cmake
option(CCACHE_ENABLE
  "If the command ccache is avilable, use it for compile."
  ON)
find_program(CCACHE_EXE ccache)
if(CCACHE_EXE)
  if(CCACHE_ENABLE)
    message(STATUS "Enable ccache")
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_EXE}")
  endif()
endif()

cmake 3.4以降ならCMAKE_<LANG>_COMPILER_LAUNCHER変数を使ったほうがよさそうです。
Specify multiple RULE_LAUNCH_COMPILE will result in command line with semicolon (#17273) · Issues · CMake / CMake · GitLab

EnableCcache.cmake(3.4以降)
option(CCACHE_ENABLE
  "If the command ccache is avilable, use it for compile."
  ON)
find_program(CCACHE_EXE ccache)
if(CCACHE_EXE)
  if(CCACHE_ENABLE)
    message(STATUS "Enable ccache")
    if(CMAKE_C_COMPILER_LAUNCHER)
      set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_C_COMPILER_LAUNCHER}" "${CCACHE_EXE}")
    else()
      set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXE}")
    endif()
    if(CMAKE_CXX_COMPILER_LAUNCHER)
      set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_CXX_COMPILER_LAUNCHER}" "${CCACHE_EXE}")
    else()
      set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXE}")
    endif()
  endif()
endif()
9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3