使用Ninja取代makefile,和ccache配合加速编译过程

cmake+makefile好像已经成为了开发默认的搭配,但是实际产品开发中,编译速度依赖服务器性能,通常公司不会为了加快编译速度购买更多服务器,所以需要用一些简单的编译加速工具。 ninja ninja是一个很神奇东西,简单又好用,chrome好像也是用这个神奇的工具编译的 介绍参见[ninja介

cmake+makefile好像已经成为了开发默认的搭配,但是实际产品开发中,编译速度依赖服务器性能,通常公司不会为了加快编译速度购买更多服务器,所以需要用一些简单的编译加速工具。

  • ninja

ninja是一个很神奇东西,简单又好用,chrome好像也是用这个神奇的工具编译的

介绍参见[ninja介绍](https://cloud.tencent.com/developer/article/1837571)

官方地址 [github主页](https://github.com/ninja-build/ninja)

安装和使用参见[ninja安装和使用](https://zhuanlan.zhihu.com/p/321882707)

使用方法简单的来讲就是将-G Makefile ······ 更改为-G Ninja ······

  • cache

ccache有很多有意思的地方,在我自己的开发环境(ubuntu18.04)中,我将如下代码加入到~/.bashrc

export USE_CCACHE=1

export CCACHE_SLOPPINESS=file_macro,include_file_mtime,time_macros

export CCACHE_UMASK=002

export CC="ccache gcc"

export CPP="ccache cpp"

export CXX="ccache g++"

cmake便会自动使用ccache。

但在产品开发环境中(centos 7)实测不生效。

要在Cmakelist中添加如下代码

find_program(CCACHE_PROGRAM ccache)

if(CCACHE_PROGRAM)

message(STATUS "Set up ccache ...")

set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)

set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)

endif()

具体什么原因懒得研究。但是能使用就行了~~~~~~

安装完成后 使用 ccache - M 5 设置缓存大小为5GB

编译几次以后,使用ccache -s 查看效果

root@Piper:~# ccache -s

cache directory /root/.ccache

primary config /root/.ccache/ccache.conf

secondary config (readonly) /etc/ccache.conf

stats updated Fri May 27 13:22:26 2022

cache hit (direct) 3124

cache hit (preprocessed) 2

cache miss 1128

cache hit rate 73.48 %

called for link 265

compile failed 11

preprocessor error 14

cleanups performed 48

files in cache 681

cache size 14.1 MB

max cache size 5.0 GB

可以看到,编译几次以后命中率为73.48,效率相当高。

在实际开发中,如果更改在.cpp这些编译底层文件中,再次编译的效率会相当高。

但是如果对.h文件有大量的修改,那么数据可能不会太好看。

使用 ccache -C可以清除缓存空间。

打完收工