ccache#

import shutil
from pathlib import Path
import tempfile
import tvm
from tvm.contrib.cc import create_shared, create_executable, _is_linux_like, _is_windows_like
def _src_gen(text):
    return """
#include <iostream>

int main() {
    std::cout << "text";
    return 0;
}""".replace(
        "text", text
    )
def _compile(f_create, text, output):
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_dir = Path(temp_dir)
        src_path = temp_dir/"src.cpp"
        with open(src_path, "w", encoding="utf-8") as file:
            file.write(_src_gen(text))
        log_path = temp_dir/"log.txt"
        ccache_env = {
            "CCACHE_COMPILERCHECK": "content",
            "CCACHE_LOGFILE": log_path,
        }
        f_create(output, ["src.cpp"], ["-c"], cwd=temp_dir, ccache_env=ccache_env)
        with open(log_path, "r", encoding="utf-8") as file:
            log = file.read()
        return log
shutil.which("ccache") # 查看安装位置
'/usr/bin/ccache'

测试共享#

if _is_linux_like():
    _ = _compile(create_shared, "shared", "main.o")
    log = _compile(create_shared, "shared", "main.o")
    assert "Succeeded getting cached result" in log
elif _is_windows_like():
    _ = _compile(create_shared, "shared", "main.obj")
    log = _compile(create_shared, "shared", "main.obj")
    assert "Succeeded getting cached result" in log
print(log)
[2025-05-16T15:27:35.389101 995845] === CCACHE 3.7.7 STARTED =========================================
[2025-05-16T15:27:35.389101 995845] Config: (default) base_dir = 
[2025-05-16T15:27:35.389101 995845] Config: (default) cache_dir = /home/ai/.ccache
[2025-05-16T15:27:35.389101 995845] Config: (default) cache_dir_levels = 2
[2025-05-16T15:27:35.389101 995845] Config: (default) compiler = 
[2025-05-16T15:27:35.389101 995845] Config: (environment) compiler_check = content
[2025-05-16T15:27:35.389101 995845] Config: (default) compression = false
[2025-05-16T15:27:35.389101 995845] Config: (default) compression_level = 6
[2025-05-16T15:27:35.389101 995845] Config: (default) cpp_extension = 
[2025-05-16T15:27:35.389101 995845] Config: (default) debug = false
[2025-05-16T15:27:35.389101 995845] Config: (default) depend_mode = false
[2025-05-16T15:27:35.389101 995845] Config: (default) direct_mode = true
[2025-05-16T15:27:35.389101 995845] Config: (default) disable = false
[2025-05-16T15:27:35.389101 995845] Config: (default) extra_files_to_hash = 
[2025-05-16T15:27:35.389101 995845] Config: (default) hard_link = false
[2025-05-16T15:27:35.389101 995845] Config: (default) hash_dir = true
[2025-05-16T15:27:35.389101 995845] Config: (default) ignore_headers_in_manifest = 
[2025-05-16T15:27:35.389101 995845] Config: (default) keep_comments_cpp = false
[2025-05-16T15:27:35.389101 995845] Config: (default) limit_multiple = 0.8
[2025-05-16T15:27:35.389101 995845] Config: (environment) log_file = /tmp/tmpf6wi1lka/log.txt
[2025-05-16T15:27:35.389101 995845] Config: (default) max_files = 0
[2025-05-16T15:27:35.389101 995845] Config: (/home/ai/.ccache/ccache.conf) max_size = 5.0G
[2025-05-16T15:27:35.389101 995845] Config: (default) path = 
[2025-05-16T15:27:35.389101 995845] Config: (default) pch_external_checksum = false
[2025-05-16T15:27:35.389101 995845] Config: (default) prefix_command = 
[2025-05-16T15:27:35.389101 995845] Config: (default) prefix_command_cpp = 
[2025-05-16T15:27:35.389101 995845] Config: (default) read_only = false
[2025-05-16T15:27:35.389101 995845] Config: (default) read_only_direct = false
[2025-05-16T15:27:35.389101 995845] Config: (default) recache = false
[2025-05-16T15:27:35.389101 995845] Config: (default) run_second_cpp = true
[2025-05-16T15:27:35.389101 995845] Config: (default) sloppiness = 
[2025-05-16T15:27:35.389101 995845] Config: (default) stats = true
[2025-05-16T15:27:35.389101 995845] Config: (default) temporary_dir = 
[2025-05-16T15:27:35.389101 995845] Config: (default) umask = 
[2025-05-16T15:27:35.389162 995845] Command line: ccache /media/pc/data/lxw/envs/anaconda3a/envs/ai/bin/x86_64-conda-linux-gnu-c++ -shared -fPIC -o main.o src.cpp -c
[2025-05-16T15:27:35.389172 995845] Hostname: Alg
[2025-05-16T15:27:35.389189 995845] Working directory: /tmp/tmpf6wi1lka
[2025-05-16T15:27:35.389216 995845] Source file: src.cpp
[2025-05-16T15:27:35.389221 995845] Object file: main.o
[2025-05-16T15:27:35.391639 995845] Trying direct lookup
[2025-05-16T15:27:35.391668 995845] Looking for object file hash in /home/ai/.ccache/9/9/baa259ab40c58c274e7c1946edd857-1723286.manifest
[2025-05-16T15:27:35.398058 995845] Got object file hash from manifest
[2025-05-16T15:27:35.398124 995845] Copying /home/ai/.ccache/e/2/126b3df1985c57a73bd0ef5d5d9af7-2712409.o to main.o via main.o.Alg.995845.YVFGE4 (uncompressed)
[2025-05-16T15:27:35.398170 995845] Created from cache: /home/ai/.ccache/e/2/126b3df1985c57a73bd0ef5d5d9af7-2712409.o -> main.o
[2025-05-16T15:27:35.398186 995845] Succeeded getting cached result
[2025-05-16T15:27:35.398193 995845] Result: cache hit (direct)
[2025-05-16T15:27:35.398212 995845] Acquired lock /home/ai/.ccache/e/stats.lock
[2025-05-16T15:27:35.398330 995845] Releasing lock /home/ai/.ccache/e/stats.lock
[2025-05-16T15:27:35.398338 995845] Unlink /home/ai/.ccache/e/stats.lock

测试 executable#

if _is_linux_like():
    _ = _compile(create_executable, "executable", "main")
    log = _compile(create_executable, "executable", "main")
    assert "Succeeded getting cached result" in log
elif _is_windows_like():
    _ = _compile(create_executable, "executable", "main.exe")
    log = _compile(create_executable, "executable", "main.exe")
    assert "Succeeded getting cached result" in log