Skip to content

Commit

Permalink
tools: update check_os_api_parity.v (#21367)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 28, 2024
1 parent a779412 commit 3117126
Showing 1 changed file with 33 additions and 44 deletions.
77 changes: 33 additions & 44 deletions cmd/tools/check_os_api_parity.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const skip_modules = [
'builtin.bare',
'builtin.linux_bare.old',
'builtin.js',
'builtin.wasm',
'strconv',
'strconv.ftoa',
'hash',
Expand All @@ -26,43 +27,44 @@ const skip_modules = [
'szip',
'v.eval',
]

struct App {
diff_cmd string
is_verbose bool
modules []string
mut:
api_differences map[string]int
}
const is_verbose = os.getenv('VERBOSE') != ''
const diff_cmd = diff.find_working_diff_command() or { '' }

fn main() {
vexe := os.real_path(os.getenv_opt('VEXE') or { @VEXE })
vroot := os.dir(vexe)
util.set_vroot_folder(vroot)
os.chdir(vroot)!
cmd := diff.find_working_diff_command() or { '' }
mut app := App{
diff_cmd: cmd
is_verbose: os.getenv('VERBOSE').len > 0
modules: if os.args.len > 1 { os.args[1..] } else { all_vlib_modules() }
}
for mname in app.modules {
if !app.is_verbose {
eprintln('Checking module: ${mname} ...')
modules := if os.args.len > 1 { os.args[1..] } else { all_vlib_modules() }
mut diff_modules := map[string]bool{}
for m in modules {
if !is_verbose {
eprintln('Checking module: ${m} ...')
}
api_base := app.gen_api_for_module_in_os(mname, base_os)
api_base := gen_api_for_module_in_os(m, base_os)
for oname in os_names {
if oname == base_os {
continue
}
api_os := app.gen_api_for_module_in_os(mname, oname)
app.compare_api(api_base, api_os, mname, base_os, oname)
api_os := gen_api_for_module_in_os(m, oname)
if api_base == api_os {
continue
}
diff_modules[m] = true
summary := 'Different APIs found for module: `${m}`, between OS base: `${base_os}` and OS: `${api_os}`'
eprintln(term.header(summary, '-'))
if diff_cmd == '' {
continue
}
diff_ := diff.color_compare_strings(diff_cmd, rand.ulid(), api_base, api_os)
println(diff_)
eprintln(term.h_divider('-'))
}
}
howmany := app.api_differences.len
if howmany > 0 {
eprintln(term.header('Found ${howmany} modules with different APIs', '='))
for m in app.api_differences.keys() {
if diff_modules.len > 0 {
eprintln(term.header('Found ${diff_modules.len} modules with different APIs',
'='))
for m in diff_modules.keys() {
eprintln('Module: ${m}')
}
exit(1)
Expand All @@ -87,8 +89,8 @@ fn all_vlib_modules() []string {
return modules
}

fn (app App) gen_api_for_module_in_os(mod_name string, os_name string) string {
if app.is_verbose {
fn gen_api_for_module_in_os(mod_name string, os_name string) string {
if is_verbose {
eprintln('Checking module: ${mod_name:-30} for OS: ${os_name:-10} ...')
}
mpath := os.join_path('vlib', mod_name.replace('.', '/'))
Expand All @@ -99,29 +101,16 @@ fn (app App) gen_api_for_module_in_os(mod_name string, os_name string) string {
mut res := []string{}
for f in b.parsed_files {
for s in f.stmts {
if s is ast.FnDecl {
if s.is_pub {
if s is ast.FnDecl && s.is_pub {
fn_mod := s.modname()
if fn_mod == mod_name {
fn_signature := b.table.stringify_fn_decl(&s, mod_name, map[string]string{})
fn_mod := s.modname()
if fn_mod == mod_name {
fline := '${fn_mod}: ${fn_signature}'
res << fline
}
fline := '${fn_mod}: ${fn_signature}'
res << fline
}
}
}
}
res.sort()
return res.join('\n')
}

fn (mut app App) compare_api(api_base string, api_os string, mod_name string, os_base string, os_target string) {
res := diff.color_compare_strings(app.diff_cmd, rand.ulid(), api_base, api_os)
if res.len > 0 {
summary := 'Different APIs found for module: `${mod_name}`, between OS base: `${os_base}` and OS: `${os_target}`'
eprintln(term.header(summary, '-'))
eprintln(res)
eprintln(term.h_divider('-'))
app.api_differences[mod_name] = 1
}
}

0 comments on commit 3117126

Please sign in to comment.