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
测试 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