From 0b41ff0c6a979f933eb09e53f6d9603e740244bb Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 21 Aug 2022 08:55:21 +0300 Subject: [PATCH] v.vcache, v.builder: use the module name inside the usecache paths (for more readable cache entries at a glance) (#15476) --- vlib/v/builder/cc.v | 17 ++++++++-------- vlib/v/builder/cflags.v | 3 ++- vlib/v/builder/msvc_windows.v | 2 +- vlib/v/builder/rebuilding.v | 4 ++-- vlib/v/vcache/vcache.v | 37 +++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 714b49bb9..af7f776bd 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -456,11 +456,12 @@ fn (mut v Builder) setup_output_name() { } } if v.pref.build_mode == .build_module { - v.pref.out_name = v.pref.cache_manager.postfix_with_key2cpath('.o', v.pref.path) // v.out_name + v.pref.out_name = v.pref.cache_manager.mod_postfix_with_key2cpath(v.pref.path, + '.o', v.pref.path) // v.out_name if v.pref.is_verbose { println('Building $v.pref.path to $v.pref.out_name ...') } - v.pref.cache_manager.save('.description.txt', v.pref.path, '${v.pref.path:-30} @ $v.pref.cache_manager.vopts\n') or { + v.pref.cache_manager.mod_save(v.pref.path, '.description.txt', v.pref.path, '${v.pref.path:-30} @ $v.pref.cache_manager.vopts\n') or { panic(err) } // println('v.ast.imports:') @@ -889,19 +890,19 @@ fn (mut b Builder) build_thirdparty_obj_files() { rest_of_module_flags := b.get_rest_of_module_cflags(flag) $if windows { if b.pref.ccompiler == 'msvc' { - b.build_thirdparty_obj_file_with_msvc(flag.value, rest_of_module_flags) + b.build_thirdparty_obj_file_with_msvc(flag.mod, flag.value, rest_of_module_flags) + continue } - continue } - b.build_thirdparty_obj_file(flag.value, rest_of_module_flags) + b.build_thirdparty_obj_file(flag.mod, flag.value, rest_of_module_flags) } } } -fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CFlag) { +fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflags []cflag.CFlag) { obj_path := os.real_path(path) cfile := '${obj_path[..obj_path.len - 2]}.c' - opath := v.pref.cache_manager.postfix_with_key2cpath('.o', obj_path) + opath := v.pref.cache_manager.mod_postfix_with_key2cpath(mod, '.o', obj_path) mut rebuild_reason_message := '$obj_path not found, building it in $opath ...' if os.exists(opath) { if os.exists(cfile) && os.file_last_mod_unix(opath) < os.file_last_mod_unix(cfile) { @@ -943,7 +944,7 @@ fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CF verror(res.output) return } - v.pref.cache_manager.save('.description.txt', obj_path, '${obj_path:-30} @ $cmd\n') or { + v.pref.cache_manager.mod_save(mod, '.description.txt', obj_path, '${obj_path:-30} @ $cmd\n') or { panic(err) } if res.output != '' { diff --git a/vlib/v/builder/cflags.v b/vlib/v/builder/cflags.v index 8231ed17c..4cfcbe572 100644 --- a/vlib/v/builder/cflags.v +++ b/vlib/v/builder/cflags.v @@ -13,7 +13,8 @@ fn (mut v Builder) get_os_cflags() []cflag.CFlag { } for mut flag in v.table.cflags { if flag.value.ends_with('.o') { - flag.cached = v.pref.cache_manager.postfix_with_key2cpath('.o', os.real_path(flag.value)) + flag.cached = v.pref.cache_manager.mod_postfix_with_key2cpath(flag.mod, '.o', + os.real_path(flag.value)) } if flag.os == '' || flag.os in ctimedefines { flags << flag diff --git a/vlib/v/builder/msvc_windows.v b/vlib/v/builder/msvc_windows.v index 53c854060..a76b412c7 100644 --- a/vlib/v/builder/msvc_windows.v +++ b/vlib/v/builder/msvc_windows.v @@ -388,7 +388,7 @@ pub fn (mut v Builder) cc_msvc() { os.rm(out_name_obj) or {} } -fn (mut v Builder) build_thirdparty_obj_file_with_msvc(path string, moduleflags []cflag.CFlag) { +fn (mut v Builder) build_thirdparty_obj_file_with_msvc(mod string, path string, moduleflags []cflag.CFlag) { msvc := v.cached_msvc if msvc.valid == false { verror('Cannot find MSVC on this OS') diff --git a/vlib/v/builder/rebuilding.v b/vlib/v/builder/rebuilding.v index 076b958e3..addbb4313 100644 --- a/vlib/v/builder/rebuilding.v +++ b/vlib/v/builder/rebuilding.v @@ -193,12 +193,12 @@ fn (mut b Builder) v_build_module(vexe string, imp_path string) { } fn (mut b Builder) rebuild_cached_module(vexe string, imp_path string) string { - res := b.pref.cache_manager.exists('.o', imp_path) or { + res := b.pref.cache_manager.mod_exists(imp_path, '.o', imp_path) or { if b.pref.is_verbose { println('Cached $imp_path .o file not found... Building .o file for $imp_path') } b.v_build_module(vexe, imp_path) - rebuilded_o := b.pref.cache_manager.exists('.o', imp_path) or { + rebuilded_o := b.pref.cache_manager.mod_exists(imp_path, '.o', imp_path) or { panic('could not rebuild cache module for $imp_path, error: $err.msg()') } return rebuilded_o diff --git a/vlib/v/vcache/vcache.v b/vlib/v/vcache/vcache.v index 0eca4e59b..abcbe9671 100644 --- a/vlib/v/vcache/vcache.v +++ b/vlib/v/vcache/vcache.v @@ -107,6 +107,16 @@ pub fn (mut cm CacheManager) postfix_with_key2cpath(postfix string, key string) return res } +fn normalise_mod(mod string) string { + return mod.replace('/', '.').replace('\\', '.').replace('vlib.', '').trim('.') +} + +pub fn (mut cm CacheManager) mod_postfix_with_key2cpath(mod string, postfix string, key string) string { + prefix := cm.key2cpath(key) + res := '${prefix}.module.${normalise_mod(mod)}$postfix' + return res +} + pub fn (mut cm CacheManager) exists(postfix string, key string) ?string { fpath := cm.postfix_with_key2cpath(postfix, key) dlog(@FN, 'postfix: $postfix | key: $key | fpath: $fpath') @@ -116,6 +126,17 @@ pub fn (mut cm CacheManager) exists(postfix string, key string) ?string { return fpath } +pub fn (mut cm CacheManager) mod_exists(mod string, postfix string, key string) ?string { + fpath := cm.mod_postfix_with_key2cpath(mod, postfix, key) + dlog(@FN, 'mod: $mod | postfix: $postfix | key: $key | fpath: $fpath') + if !os.exists(fpath) { + return error('does not exist yet') + } + return fpath +} + +// + pub fn (mut cm CacheManager) save(postfix string, key string, content string) ?string { fpath := cm.postfix_with_key2cpath(postfix, key) os.write_file(fpath, content)? @@ -123,6 +144,15 @@ pub fn (mut cm CacheManager) save(postfix string, key string, content string) ?s return fpath } +pub fn (mut cm CacheManager) mod_save(mod string, postfix string, key string, content string) ?string { + fpath := cm.mod_postfix_with_key2cpath(mod, postfix, key) + os.write_file(fpath, content)? + dlog(@FN, 'mod: $mod | postfix: $postfix | key: $key | fpath: $fpath') + return fpath +} + +// + pub fn (mut cm CacheManager) load(postfix string, key string) ?string { fpath := cm.exists(postfix, key)? content := os.read_file(fpath)? @@ -130,6 +160,13 @@ pub fn (mut cm CacheManager) load(postfix string, key string) ?string { return content } +pub fn (mut cm CacheManager) mod_load(mod string, postfix string, key string) ?string { + fpath := cm.mod_exists(mod, postfix, key)? + content := os.read_file(fpath)? + dlog(@FN, 'mod: $mod | postfix: $postfix | key: $key | fpath: $fpath') + return content +} + [if trace_usecache ?] pub fn dlog(fname string, s string) { xlog(fname, s) -- 2.30.2