如何在Make中使用ccache?

9
我有一个源代码目录,使用makefile编译代码。这个makefile/configure文件不支持ccache。因此我想使用ccache。我在.bashrc中创建了别名为alias gcc='ccache gcc',但是Makefile仍然没有考虑到这个gcc的定义。所以有没有什么方法可以在不修改Makefile/configure文件的情况下,让它使用ccache gcc而不是gccCC='ccache gcc' ./configure也不是一个选项,因为它不会询问CC。
如果我自己写Makefile,那么我可以提供${gcc},但这不是一个选项,因为我不写Makefile。有没有办法在不改动源文件的情况下,仍然能够启用ccache编译?

那么,Makefile使用什么来编译呢?它是硬编码使用gcc而不是$(CC)吗? - Oliver Charlesworth
Makefile 是由 configure 创建的,而 configure 又是由 autoconf 创建的。尽管 makefile 解析 configure.in 文件,但更改该文件会导致“configure 文件已更改”的另一个问题。因此,我正在寻找一种方法,在不更改源文件(makefile/configure)的情况下完成操作。 - peeyush
3个回答

10

别名只在创建它们的shell中有效,与环境变量不同,别名不会传递给任何由shell调用的程序(包括make)。Make调用/bin/sh而不是/bin/bash,而/bin/sh不会读取您的~/.bashrc等文件,因此在那里定义的别名对您没有帮助。

我不确定为什么您要对自己施加某些限制:这些功能很好用,并且我不明白您避免使用它们的原因。例如,使用configure提供一个不同的CC将起作用,如果autoconf版本不是非常古老的话,你可以这样做:

./configure CC='ccache gcc'
例如,这将在您的makefile中将CC的默认值设置为ccache gcc。我不知道您所说的“它不要求CC”是什么意思。
如果您愿意,您也可以通过以下方式覆盖make命令行上的CC设置:
make CC='ccache gcc'

这也可以很好地工作。


我已经提到了CC选项不是配置。gcc在那里硬编码。换句话说,configure脚本不会查找CC变量。 - peeyush

9
按照细致的手册所述,需要在路径中先于真实gcc所在的目录中创建一个名为" gcc"的符号链接。这将使ccache被透明地使用,无需对makefile进行任何更改。

5

将打包的ccache添加到PATH

PATH="/usr/lib/ccache:${PATH}" make

这是一种多功能的方法,它:

  • 适用于所有编译器:C、C++等
  • 不像CC那样依赖于实际的Makefile设置
  • 如果您正在自动化某些内容并且目标用户没有安装ccache,则不会破坏人们的构建

man ccache中提到:

要在Debian系统上使用第二种方法,最简单的方法就是将/usr/lib/ccache添加到您的PATH中。/usr/lib/ccache包含当前安装为Debian软件包的所有编译器的符号链接。

您可以通过以下方式进行确认:

ls -l /usr/lib/ccache

其中包含大量可能的GCC名称,包括已安装的交叉编译器:

total 0
lrwxrwxrwx 1 root root 16 May  6 13:51 aarch64-linux-gnu-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 aarch64-linux-gnu-gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Jun 23 18:25 arm-linux-gnueabi-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Jun 23 18:25 arm-linux-gnueabi-g++-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-linux-gnueabi-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-linux-gnueabi-gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May 28 22:11 arm-linux-gnueabihf-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May 28 22:11 arm-linux-gnueabihf-gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-gcc-6.3.1 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 c++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 c89-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 c99-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 cc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 clang -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 clang++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 g++-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-5 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-6 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-7 -> ../../bin/ccache

在Ubuntu 16.04上进行了测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接