Skip to content

feat: hashing sources for caching #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ if %ERRORLEVEL% neq 0 (
exit %ERRORLEVEL%
)

echo INFO: compiling xxh3 module...
pushd %modules_dir%
cl /nologo ^
/std:c++latest /W4 /MDd /EHsc ^
/reference "%modules_dir%\std.ifc" ^
/reference "%modules_dir%\std.compat.ifc" ^
/c /interface /TP "%root_dir%src\xxh3.cppm" > NUL
popd

if %ERRORLEVEL% neq 0 (
echo ERROR: failed to compile xxh3 module
exit %ERRORLEVEL%
)

echo INFO: compiling nlohmann.json module...
pushd %modules_dir%
cl /nologo ^
Expand Down Expand Up @@ -140,6 +154,7 @@ cl /nologo "%root_dir%.cache/cpp2/source/src/main.cpp" ^
/reference "%modules_dir%\std.compat.ifc" "%modules_dir%\std.compat.obj" ^
/reference "%modules_dir%\dylib.ifc" "%modules_dir%\dylib.obj" ^
/reference "%modules_dir%\nlohmann.json.ifc" "%modules_dir%\nlohmann.json.obj" ^
/reference "%modules_dir%\xxh3.ifc" "%modules_dir%\xxh3.obj" ^
/reference "%modules_dir%\cpp2b.ifc" "%modules_dir%\cpp2b.obj" ^
/std:c++latest /W4 /MDd /EHsc ^
/DEBUG:FULL /Zi /FC ^
Expand Down
15 changes: 15 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ if ! [ -f $MODULES_DIR/dylib.pcm ]; then
cd $ROOT_DIR
fi

if ! [ -f $MODULES_DIR/xxh3.pcm ]; then
log_info "compiling xxh3 module..."

$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LIBCXX_INCLUDE_DIR/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/src/xxh3.cppm" \
--precompile -o $MODULES_DIR/xxh3.pcm

cd $ROOT_DIR
fi

if ! [ -f $MODULES_DIR/nlohmann.json.pcm ]; then
log_info "compiling nlohmann.json module..."

Expand Down
88 changes: 84 additions & 4 deletions src/main.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import std.compat;
import cpp2b;
import dylib;
import nlohmann.json;
import xxh3;

fs: namespace == std::filesystem;
json: type == nlohmann::json;
Expand Down Expand Up @@ -701,10 +702,15 @@ unix_build_build_script_cmd: (compiler_cmd: std::string, info: cpp2b_source_buil
build_build_script: (info: cpp2b_source_build_info) -> build_binary_result = {
compiler :== cpp2b::compiler();
bin_outpath := fs::absolute(".cache/cpp2/bin") / fs::path(info.src).replace_extension(shared_library_extension());
bin_cache_file_path: fs::path = bin_outpath.generic_string() + ".cpp2bcache";
log_path := fs::absolute(".cache/cpp2/log/compile") / fs::path(info.src).replace_extension(".log");
ensure_dir(log_path.parent_path());
ensure_dir(bin_outpath.parent_path());

if fs::exists(bin_outpath) && fs::exists(bin_cache_file_path) {
bin_cache_file: std::fstream = (bin_cache_file_path, std::ios::binary);
}

d := fs::absolute(modules_dir());
cmd_str: std::string = "";
if compiler == cpp2b::compiler_type::msvc { cmd_str = cl_build_build_script_cmd(info, bin_outpath); }
Expand Down Expand Up @@ -758,7 +764,19 @@ contains_target: (targets, value: std::string) -> bool = {
return false;
}

hash_file: (p: fs::path) -> u64 = {
f: std::ifstream = (p, std::ios::binary | std::ios::ate);
size: size_t = unchecked_cast<size_t>(f.tellg());
f.seekg(0, std::ios::beg);
buffer: std::vector<char> = ();
buffer.resize(size);
f.read(buffer.data(), size);
if f.fail() { return 0; }
return XXH3_64bits(buffer.data(), buffer.size());
}

do_build: (targets: std::vector<std::string>) -> (stuff: full_build_info, exit_code: int) = {
cwd := fs::current_path();
build_cpp2_dir := fs::current_path();
stuff = ();

Expand Down Expand Up @@ -786,9 +804,9 @@ do_build: (targets: std::vector<std::string>) -> (stuff: full_build_info, exit_c
warn_if_error("remove", p, ec);
}

src_loop: for fs::recursive_directory_iterator(fs::current_path(), fs::directory_options::follow_directory_symlink) do(p: fs::path) {
src_loop: for fs::recursive_directory_iterator(cwd, fs::directory_options::follow_directory_symlink) do(p: fs::path) {
if p.extension() == ".cpp2" {
rel_path := fs::relative(p, fs::current_path());
rel_path := fs::relative(p, cwd);
for rel_path do(rel_path_comp) {
if rel_path_comp.string().starts_with(".") {
continue src_loop;
Expand Down Expand Up @@ -816,15 +834,78 @@ do_build: (targets: std::vector<std::string>) -> (stuff: full_build_info, exit_c

transpile_futures: std::vector<std::future<transpile_cpp2_result>> = ();
cpp2b_parse_futures: std::vector<std::future<cpp2b_source_info>> = ();
file_hash_futures: std::unordered_map<fs::path, std::future<u64>> = ();
file_hashes: std::unordered_map<fs::path, u64> = ();
prev_file_hashes: std::unordered_map<fs::path, u64> = ();

transpile_futures.reserve(cpp2_source_files.size());
cpp2b_parse_futures.reserve(cpp2_source_files.size());

file_hash_futures.reserve(cpp2_source_files.size());
file_hashes.reserve(cpp2_source_files.size());
prev_file_hashes.reserve(cpp2_source_files.size());

for cpp2_source_files do(src_file: fs::path) {
file_hash_futures.insert(std::make_pair(
src_file,
std::async(std::launch::async, hash_file, src_file)
));
transpile_futures.emplace_back(std::async(std::launch::async, transpile_cpp2, src_file, transpile_source_dir));
cpp2b_parse_futures.emplace_back(std::async(std::launch::async, cpp2b_parse_source, src_file));
}

for file_hash_futures do(inout entry) {
p := entry.first;
hash_fut := entry.second&;
hash := hash_fut*.get();
file_hashes.insert(std::make_pair(p, hash));
}

hash_data_file: std::fstream = (".cache/cpp2/.hash", std::ios::binary | std::ios::in);

while hash_data_file {
path_length: u16 = 0;
hash_data_file >> path_length;
if !hash_data_file { break; }

p: std::string = "";
p.resize(path_length);
hash_data_file.read(p.data(), path_length);
if !hash_data_file { break; }

path_hash: u64 = 0;
hash_data_file.read(reinterpret_cast<*char>(path_hash&), 8);
if !hash_data_file { break; }

prev_file_hashes[fs::path(p)] = path_hash;
}

for file_hashes do(entry) {
p := entry.first;
hash := entry.second;

if prev_file_hashes.contains(p) {
if prev_file_hashes.at(p) == hash {
log_info("{} no change", p.generic_string());
} else {
log_info("{} changed", p.generic_string());
}
} else {
log_info("new file {}", p.generic_string());
}
}

hash_data_file.close();
hash_data_file.open(".cache/cpp2/.hash", std::ios::binary | std::ios::out | std::ios::trunc);

for file_hashes do(inout entry) {
p := entry.first.generic_string();
hash := entry.second;

hash_data_file << unchecked_cast<u16>(p.size());
hash_data_file.write(p.data(), p.size());
hash_data_file.write(reinterpret_cast<*char>(hash&), 8);
}

for transpile_futures do(inout fut) {
info := fut.get();
if info.cppfront_exit_code != 0 {
Expand Down Expand Up @@ -863,7 +944,6 @@ do_build: (targets: std::vector<std::string>) -> (stuff: full_build_info, exit_c
std::pair("std.compat", true),
);


for cpp1_module_source_files do(src_file: fs::path) {
result := cpp2b_parse_cpp1_module_statements(std::ifstream(src_file));

Expand Down
Loading