1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: it can now be used for _platform.v files too, no matter the host os

This commit is contained in:
Delyan Angelov 2019-12-27 18:59:04 +02:00 committed by Alexander Medvednikov
parent 84fbd5b3d0
commit 6c16bac908
12 changed files with 253 additions and 175 deletions

View File

@ -15,7 +15,7 @@ jobs:
- name: Build a production tools/vfmt - name: Build a production tools/vfmt
run: ./v -prod -d vfmt tools/vfmt.v run: ./v -prod -d vfmt tools/vfmt.v
- name: fmt only the changed files (from master) - name: fmt only the changed files (from master)
run: git diff --name-status origin/master HEAD -- '*.v' |grep -v '^D'|cut -f2- | xargs ./v fmt -diff run: git diff --name-status origin/master HEAD -- '*.v' |grep -v '^D'|rev|cut -f1|rev| xargs ./v fmt -diff
- name: Run v test-fmt - name: Run v test-fmt
run: ./v test-fmt run: ./v test-fmt
@ -24,7 +24,7 @@ jobs:
env: env:
VFLAGS: -cc tcc VFLAGS: -cc tcc
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- name: Install dependencies - name: Install dependencies
run: sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update; sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev run: sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update; sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Build v - name: Build v
@ -44,7 +44,7 @@ jobs:
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v1 uses: actions/checkout@v2
- name: Build V - name: Build V
uses: spytheman/docker_alpine_v@v6.0 uses: spytheman/docker_alpine_v@v6.0
@ -62,7 +62,7 @@ jobs:
matrix: matrix:
os: [macOS-10.14, macOS-latest] os: [macOS-10.14, macOS-latest]
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- uses: actions/setup-node@v1 - uses: actions/setup-node@v1
with: with:
node-version: 12.x node-version: 12.x
@ -101,7 +101,7 @@ jobs:
ubuntu: ubuntu:
runs-on: ubuntu-18.04 runs-on: ubuntu-18.04
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- uses: actions/setup-node@v1 - uses: actions/setup-node@v1
with: with:
node-version: 12.x node-version: 12.x
@ -162,7 +162,7 @@ jobs:
env: env:
VFLAGS: -cc musl-gcc VFLAGS: -cc musl-gcc
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- uses: actions/setup-node@v1 - uses: actions/setup-node@v1
with: with:
node-version: 12.x node-version: 12.x
@ -187,7 +187,7 @@ jobs:
env: env:
VFLAGS: -cc gcc VFLAGS: -cc gcc
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
#- uses: actions/setup-node@v1 #- uses: actions/setup-node@v1
# with: # with:
# node-version: 12.x # node-version: 12.x
@ -210,7 +210,7 @@ jobs:
env: env:
VFLAGS: -cc msvc VFLAGS: -cc msvc
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
#- uses: actions/setup-node@v1 #- uses: actions/setup-node@v1
# with: # with:
# node-version: 12.x # node-version: 12.x

View File

@ -9,14 +9,13 @@ import (
pub struct TestSession { pub struct TestSession {
pub mut: pub mut:
files []string files []string
vexe string vexe string
vargs string vargs string
failed bool failed bool
benchmark benchmark.Benchmark benchmark benchmark.Benchmark
ok string
ok string fail string
fail string
} }
pub fn new_test_session(vargs string) TestSession { pub fn new_test_session(vargs string) TestSession {
@ -27,55 +26,74 @@ pub fn new_test_session(vargs string) TestSession {
} }
pub fn vexe_path() string { pub fn vexe_path() string {
// NB: tools extracted from v require that the VEXE // NB: tools extracted from v require that the VEXE
// environment variable contains the path to the v executable location. // environment variable contains the path to the v executable location.
// They are usually launched by vlib/compiler/vtools.v, // They are usually launched by vlib/compiler/vtools.v,
// launch_tool/1 , which provides it. // launch_tool/1 , which provides it.
return os.getenv('VEXE') return os.getenv('VEXE')
} }
pub fn (ts mut TestSession) init() { pub fn (ts mut TestSession) init() {
ts.ok = term.ok_message('OK') ts.ok = term.ok_message('OK')
ts.fail = term.fail_message('FAIL') ts.fail = term.fail_message('FAIL')
ts.benchmark = benchmark.new_benchmark() ts.benchmark = benchmark.new_benchmark()
} }
pub fn (ts mut TestSession) test() { pub fn (ts mut TestSession) test() {
tmpd := os.tmpdir()
ts.init() ts.init()
show_stats := '-stats' in ts.vargs.split(' ') show_stats := '-stats' in ts.vargs.split(' ')
for dot_relative_file in ts.files { for dot_relative_file in ts.files {
relative_file := dot_relative_file.replace('./', '') relative_file := dot_relative_file.replace('./', '')
file := os.realpath( relative_file ) file := os.realpath(relative_file)
$if windows { $if windows {
if file.contains('sqlite') { continue } if file.contains('sqlite') {
continue
}
} }
$if !macos { $if !macos {
if file.contains('customer') { continue } if file.contains('customer') {
continue
}
} }
$if msvc { $if msvc {
if file.contains('asm') { continue } if file.contains('asm') {
continue
}
} }
$if tinyc { $if tinyc {
if file.contains('asm') { continue } if file.contains('asm') {
continue
}
} }
tmpc_filepath := file.replace('.v', '.tmp.c') // Ensure that the generated binaries will be stored in the temporary folder.
// Remove them after a test passes/fails.
cmd := '"$ts.vexe" $ts.vargs "$file"' fname := filepath.filename(file)
//eprintln('>>> v cmd: $cmd') generated_binary_fname := if os.user_os() == 'windows' { fname.replace('.v', '.exe') } else { fname.replace('.v', '') }
generated_binary_fpath := filepath.join(tmpd,generated_binary_fname)
if os.exists(generated_binary_fpath) {
os.rm(generated_binary_fpath)
}
mut cmd_options := [ts.vargs]
if !ts.vargs.contains('fmt') {
cmd_options << ' -o "$generated_binary_fpath"'
}
cmd := '"${ts.vexe}" ' + cmd_options.join(' ') + ' "${file}"'
// eprintln('>>> v cmd: $cmd')
ts.benchmark.step() ts.benchmark.step()
if show_stats { if show_stats {
eprintln('-------------------------------------------------') eprintln('-------------------------------------------------')
status := os.system(cmd) status := os.system(cmd)
if status == 0 { if status == 0 {
ts.benchmark.ok() ts.benchmark.ok()
}else{ }
else {
ts.benchmark.fail() ts.benchmark.fail()
ts.failed = true ts.failed = true
continue continue
} }
}else{ }
else {
r := os.exec(cmd) or { r := os.exec(cmd) or {
ts.benchmark.fail() ts.benchmark.fail()
ts.failed = true ts.failed = true
@ -86,20 +104,23 @@ pub fn (ts mut TestSession) test() {
ts.benchmark.fail() ts.benchmark.fail()
ts.failed = true ts.failed = true
eprintln(ts.benchmark.step_message('$relative_file ${ts.fail}\n`$file`\n (\n$r.output\n)')) eprintln(ts.benchmark.step_message('$relative_file ${ts.fail}\n`$file`\n (\n$r.output\n)'))
} else { }
else {
ts.benchmark.ok() ts.benchmark.ok()
eprintln(ts.benchmark.step_message('$relative_file ${ts.ok}')) eprintln(ts.benchmark.step_message('$relative_file ${ts.ok}'))
} }
} }
os.rm( tmpc_filepath ) if os.exists(generated_binary_fpath) {
os.rm(generated_binary_fpath)
}
} }
ts.benchmark.stop() ts.benchmark.stop()
eprintln(term.h_divider()) eprintln(term.h_divider())
} }
pub fn vlib_should_be_present( parent_dir string ) { pub fn vlib_should_be_present(parent_dir string) {
vlib_dir := filepath.join( parent_dir, 'vlib' ) vlib_dir := filepath.join(parent_dir,'vlib')
if !os.is_dir( vlib_dir ){ if !os.is_dir(vlib_dir) {
eprintln('$vlib_dir is missing, it must be next to the V executable') eprintln('$vlib_dir is missing, it must be next to the V executable')
exit(1) exit(1)
} }
@ -110,36 +131,26 @@ pub fn v_build_failing(zargs string, folder string) bool {
finish_label := 'building $folder' finish_label := 'building $folder'
vexe := vexe_path() vexe := vexe_path()
parent_dir := filepath.dir(vexe) parent_dir := filepath.dir(vexe)
vlib_should_be_present( parent_dir ) vlib_should_be_present(parent_dir)
vargs := zargs.replace(vexe, '') vargs := zargs.replace(vexe, '')
eprintln(main_label) eprintln(main_label)
eprintln(' v compiler args: "$vargs"') eprintln(' v compiler args: "$vargs"')
mut session := new_test_session(vargs)
mut session := new_test_session( vargs ) files := os.walk_ext(filepath.join(parent_dir,folder), '.v')
files := os.walk_ext(filepath.join(parent_dir, folder),'.v')
mains := files.filter(!it.contains('modules')) mains := files.filter(!it.contains('modules'))
mut rebuildable_mains := mains session.files << mains
if os.user_os() == 'windows' {
// on windows, an executable can not be rebuilt, while it is running
myself := os.executable().replace('.exe', '') + '.v'
mains_without_myself := mains.filter(!it.contains(myself))
rebuildable_mains = mains_without_myself // workaround a bug in it.contains generation
}
session.files << rebuildable_mains
session.test() session.test()
eprintln( session.benchmark.total_message( finish_label ) ) eprintln(session.benchmark.total_message(finish_label))
return session.failed return session.failed
} }
pub fn build_v_cmd_failed (cmd string) bool { pub fn build_v_cmd_failed(cmd string) bool {
res := os.exec(cmd) or { res := os.exec(cmd) or {
return true return true
} }
if res.exit_code != 0 { if res.exit_code != 0 {
eprintln('') eprintln('')
eprintln( res.output ) eprintln(res.output)
return true return true
} }
return false return false
@ -150,20 +161,17 @@ pub fn building_any_v_binaries_failed() bool {
eprintln('VFLAGS is: "' + os.getenv('VFLAGS') + '"') eprintln('VFLAGS is: "' + os.getenv('VFLAGS') + '"')
vexe := testing.vexe_path() vexe := testing.vexe_path()
parent_dir := filepath.dir(vexe) parent_dir := filepath.dir(vexe)
testing.vlib_should_be_present( parent_dir ) testing.vlib_should_be_present(parent_dir)
os.chdir( parent_dir ) os.chdir(parent_dir)
mut failed := false mut failed := false
v_build_commands := [ v_build_commands := ['$vexe -o v_g -g v.v',
'$vexe -o v_g -g v.v', '$vexe -o v_prod_g -prod -g v.v',
'$vexe -o v_prod_g -prod -g v.v', '$vexe -o v_cg -cg v.v',
'$vexe -o v_cg -cg v.v', '$vexe -o v_prod_cg -prod -cg v.v',
'$vexe -o v_prod_cg -prod -cg v.v', '$vexe -o v_prod -prod v.v',
'$vexe -o v_prod -prod v.v',
] ]
mut bmark := benchmark.new_benchmark() mut bmark := benchmark.new_benchmark()
bok := term.ok_message('OK') bok := term.ok_message('OK')
bfail := term.fail_message('FAIL') bfail := term.fail_message('FAIL')
for cmd in v_build_commands { for cmd in v_build_commands {
bmark.step() bmark.step()
@ -178,8 +186,7 @@ pub fn building_any_v_binaries_failed() bool {
eprintln(bmark.step_message('$cmd => ${bok}')) eprintln(bmark.step_message('$cmd => ${bok}'))
} }
bmark.stop() bmark.stop()
eprintln(term.h_divider()) eprintln(term.h_divider())
eprintln( bmark.total_message( 'building v binaries' ) ) eprintln(bmark.total_message('building v binaries'))
return failed return failed
} }

View File

@ -11,23 +11,42 @@ import (
) )
struct FormatOptions { struct FormatOptions {
is_l bool
is_c bool
is_w bool is_w bool
is_diff bool is_diff bool
is_verbose bool is_verbose bool
is_all bool is_all bool
is_worker bool is_worker bool
is_debug bool
} }
const (
platform_and_file_extensions = [['windows', '_win.v', '_windows.v'],
['linux', '_lin.v', '_linux.v', '_nix.v'],
['macos', '_mac.v', '_darwin.v'],
['freebsd', '_bsd.v', '_freebsd.v'],
['solaris', '_solaris.v'],
['haiku', '_haiku.v'],
]
)
fn main() { fn main() {
toolexe := os.executable() toolexe := os.executable()
compiler.set_vroot_folder(filepath.dir(filepath.dir(toolexe))) compiler.set_vroot_folder(filepath.dir(filepath.dir(toolexe)))
args := compiler.env_vflags_and_os_args() args := compiler.env_vflags_and_os_args()
foptions := FormatOptions{ foptions := FormatOptions{
is_c: '-c' in args
is_l: '-l' in args
is_w: '-w' in args is_w: '-w' in args
is_diff: '-diff' in args is_diff: '-diff' in args
is_verbose: '-verbose' in args || '--verbose' in args is_verbose: '-verbose' in args || '--verbose' in args
is_all: '-all' in args || '--all' in args is_all: '-all' in args || '--all' in args
is_worker: '-worker' in args is_worker: '-worker' in args
is_debug: '-debug' in args
}
if foptions.is_verbose {
eprintln('vfmt foptions: $foptions')
} }
if foptions.is_worker { if foptions.is_worker {
// -worker should be added by a parent vfmt process. // -worker should be added by a parent vfmt process.
@ -45,7 +64,6 @@ fn main() {
eprintln('vfmt args: ' + os.args.str()) eprintln('vfmt args: ' + os.args.str())
eprintln('vfmt env_vflags_and_os_args: ' + args.str()) eprintln('vfmt env_vflags_and_os_args: ' + args.str())
eprintln('vfmt possible_files: ' + possible_files.str()) eprintln('vfmt possible_files: ' + possible_files.str())
eprintln('vfmt foptions: $foptions')
} }
mut files := []string mut files := []string
for file in possible_files { for file in possible_files {
@ -78,7 +96,9 @@ fn main() {
} }
cmdcode := os.system(worker_cmd) cmdcode := os.system(worker_cmd)
if cmdcode != 0 { if cmdcode != 0 {
eprintln('vfmt error while formatting file: $file .') if cmdcode == 1 {
eprintln('vfmt error while formatting file: $file .')
}
errors++ errors++
} }
} }
@ -91,29 +111,23 @@ fn main() {
fn (foptions &FormatOptions) format_file(file string) { fn (foptions &FormatOptions) format_file(file string) {
tmpfolder := os.tmpdir() tmpfolder := os.tmpdir()
mut compiler_params := []string mut compiler_params := []string
target_os := file_to_target_os(file)
if target_os != '' {
compiler_params << ['-os', target_os]
}
mut cfile := file mut cfile := file
mut mod_folder_parent := tmpfolder mut mod_folder_parent := tmpfolder
fcontent := os.read_file(file) or {
return
}
is_test_file := file.ends_with('_test.v') is_test_file := file.ends_with('_test.v')
is_module_file := fcontent.contains('module ') && !fcontent.contains('module main\n') mod_name,is_module_file := file_to_mod_name_and_is_module_file(file)
use_tmp_main_program := is_module_file && !is_test_file use_tmp_main_program := is_module_file && !is_test_file
mod_folder := filepath.basedir(file) mod_folder := filepath.basedir(file)
mut mod_name := 'main'
if is_module_file {
mod_name = filepath.filename(mod_folder)
}
if use_tmp_main_program { if use_tmp_main_program {
// TODO: remove the need for this // TODO: remove the need for this
// This makes a small program that imports the module, // This makes a small program that imports the module,
// so that the module files will get processed by the // so that the module files will get processed by the
// vfmt implementation. // vfmt implementation.
mod_folder_parent = filepath.basedir(mod_folder) mod_folder_parent = filepath.basedir(mod_folder)
mut main_program_content := 'import ${mod_name} \n fn main(){}' mut main_program_content := if mod_name == 'builtin' || mod_name == 'main' { 'fn main(){}\n' } else { 'import ${mod_name}\n' + 'fn main(){}\n' }
if fcontent.contains('module builtin\n') {
main_program_content = 'fn main(){}'
}
main_program_file := filepath.join(tmpfolder,'vfmt_tmp_${mod_name}_program.v') main_program_file := filepath.join(tmpfolder,'vfmt_tmp_${mod_name}_program.v')
if os.exists(main_program_file) { if os.exists(main_program_file) {
os.rm(main_program_file) os.rm(main_program_file)
@ -137,7 +151,9 @@ fn (foptions &FormatOptions) format_file(file string) {
} }
formatted_file_path := foptions.compile_file(file, compiler_params) formatted_file_path := foptions.compile_file(file, compiler_params)
if use_tmp_main_program { if use_tmp_main_program {
os.rm(cfile) if !foptions.is_debug {
os.rm(cfile)
}
} }
if formatted_file_path.len == 0 { if formatted_file_path.len == 0 {
return return
@ -150,17 +166,42 @@ fn (foptions &FormatOptions) format_file(file string) {
os.system('$diff_cmd --minimal --text --unified=2 --show-function-line="fn " "$file" "$formatted_file_path" ') os.system('$diff_cmd --minimal --text --unified=2 --show-function-line="fn " "$file" "$formatted_file_path" ')
return return
} }
if foptions.is_w { fc := os.read_file(file) or {
os.mv_by_cp(formatted_file_path, file) or { eprintln('File $file could not be read')
panic(err) return
}
formatted_fc := os.read_file(formatted_file_path) or {
eprintln('File $formatted_file_path could not be read')
return
}
is_formatted_different := fc != formatted_fc
if foptions.is_c {
if is_formatted_different {
eprintln('File is not formatted: $file')
exit(2)
} }
eprintln('Reformatted file in place: $file .') return
}
if foptions.is_l {
if is_formatted_different {
eprintln('File needs formatting: $file')
}
return
}
if foptions.is_w {
if is_formatted_different {
os.mv_by_cp(formatted_file_path, file) or {
panic(err)
}
eprintln('Reformatted file: $file')
}
else {
eprintln('Already formatted file: $file')
}
return
} }
else { else {
content := os.read_file(formatted_file_path) or { print(formatted_fc)
panic(err)
}
print(content)
} }
} }
@ -168,7 +209,10 @@ fn usage() {
print('Usage: tools/vfmt [flags] fmt path_to_source.v [path_to_other_source.v] print('Usage: tools/vfmt [flags] fmt path_to_source.v [path_to_other_source.v]
Formats the given V source files, and prints their formatted source to stdout. Formats the given V source files, and prints their formatted source to stdout.
Options: Options:
-c check if file is already formatted.
If it is not, print filepath, and exit with code 2.
-diff display only diffs between the formatted source and the original source. -diff display only diffs between the formatted source and the original source.
-l list files whose formatting differs from vfmt.
-w write result to (source) file(s) instead of to stdout. -w write result to (source) file(s) instead of to stdout.
') ')
} }
@ -186,6 +230,10 @@ fn find_working_diff_command() ?string {
} }
fn (foptions &FormatOptions) compile_file(file string, compiler_params []string) string { fn (foptions &FormatOptions) compile_file(file string, compiler_params []string) string {
if foptions.is_verbose {
eprintln('> new_v_compiler_with_args file: ' + file)
eprintln('> new_v_compiler_with_args compiler_params: ' + compiler_params.join(' '))
}
mut v := compiler.new_v_compiler_with_args(compiler_params) mut v := compiler.new_v_compiler_with_args(compiler_params)
v.v_fmt_file = file v.v_fmt_file = file
if foptions.is_all { if foptions.is_all {
@ -196,5 +244,37 @@ fn (foptions &FormatOptions) compile_file(file string, compiler_params []string)
} }
pub fn (f FormatOptions) str() string { pub fn (f FormatOptions) str() string {
return 'FormatOptions{ ' + ' is_w: $f.is_w' + ' is_diff: $f.is_diff' + ' is_verbose: $f.is_verbose' + ' is_all: $f.is_all' + ' is_worker: $f.is_worker' + ' }' return 'FormatOptions{ ' + ' is_l: $f.is_l' + ' is_w: $f.is_w' + ' is_diff: $f.is_diff' + ' is_verbose: $f.is_verbose' + ' is_all: $f.is_all' + ' is_worker: $f.is_worker' + ' is_debug: $f.is_debug' + ' }'
}
fn file_to_target_os(file string) string {
for extensions in platform_and_file_extensions {
for ext in extensions {
if file.ends_with(ext) {
return extensions[0]
}
}
}
return ''
}
fn file_to_mod_name_and_is_module_file(file string) (string,bool) {
mut mod_name := 'main'
mut is_module_file := false
raw_fcontent := os.read_file(file) or {
return mod_name,is_module_file
}
fcontent := raw_fcontent.replace('\r\n', '\n')
flines := fcontent.split('\n')
for fline in flines {
line := fline.trim_space()
if line.starts_with('module ') {
if !line.starts_with('module main') {
is_module_file = true
mod_name = line.replace('module ', ' ').trim_space()
}
break
}
}
return mod_name,is_module_file
} }

View File

@ -3,26 +3,10 @@ module main
import ( import (
os os
testing testing
benchmark
) )
fn main() { const (
args := os.args known_failing_exceptions = ['./examples/vweb/vweb_example.v',
args_string := args[1..].join(' ')
v_test_formatting(args_string.all_before('test-fmt'))
}
fn v_test_formatting(vargs string) {
// NB: vfmt have to be build with '-d vfmt' . V itself knows about this,
// and v will rebuild tools/vfmt, if it is missing.
// Removing the binaries below is needed, since the building tools step
// rebuilds all the tools without the special option needed by vfmt
// by simply compiling each of them with `v tools/{toolname}.v`
// os.rm('tools/vfmt')
// os.rm('tools/vfmt.exe')
mut files_able_to_be_formatted := []string
all_test_files := os.walk_ext('.', '.v')
known_failing_exceptions := ['./examples/vweb/vweb_example.v',
'./tools/gen_vc.v', './tools/gen_vc.v',
'./tutorials/code/blog/article.v', './tutorials/code/blog/article.v',
'./tutorials/code/blog/blog.v', './tutorials/code/blog/blog.v',
@ -42,25 +26,40 @@ fn v_test_formatting(vargs string) {
'./vlib/crypto/aes/cypher_generic.v', './vlib/crypto/aes/cypher_generic.v',
'./vlib/crypto/rc4/rc4.v', './vlib/crypto/rc4/rc4.v',
'./vlib/eventbus/eventbus_test.v', './vlib/eventbus/eventbus_test.v',
'./vlib/flag/flag.v',
'./vlib/os/bare/bare_example_linux.v', './vlib/os/bare/bare_example_linux.v',
'./vlib/szip/szip.v', './vlib/szip/szip.v',
'./vlib/ui/examples/users_gui/users.v', './vlib/ui/examples/users_gui/users.v',
'./vlib/vweb/assets/assets.v', './vlib/vweb/assets/assets.v',
'./vlib/vweb/vweb.v', './vlib/vweb/vweb.v',
] ]
for tfile in all_test_files { )
if tfile in known_failing_exceptions {
continue fn main() {
} args := os.args
files_able_to_be_formatted << tfile args_string := args[1..].join(' ')
} v_test_formatting(args_string.all_before('test-fmt'))
}
fn v_test_formatting(vargs string) {
all_v_files := v_files()
eprintln('Run "v fmt" over all .v files') eprintln('Run "v fmt" over all .v files')
mut vfmt_test_session := testing.new_test_session('$vargs fmt') mut vfmt_test_session := testing.new_test_session('$vargs fmt -worker')
vfmt_test_session.files << files_able_to_be_formatted vfmt_test_session.files << all_v_files
vfmt_test_session.test() vfmt_test_session.test()
eprintln(vfmt_test_session.benchmark.total_message('running vfmt over V test files')) eprintln(vfmt_test_session.benchmark.total_message('running vfmt over V files'))
if vfmt_test_session.benchmark.nfail > 0 { if vfmt_test_session.benchmark.nfail > 0 {
panic('\nWARNING: v fmt failed ${vfmt_test_session.benchmark.nfail} times.\n') panic('\nWARNING: v fmt failed ${vfmt_test_session.benchmark.nfail} times.\n')
} }
} }
fn v_files() []string {
mut files_that_can_be_formatted := []string
all_test_files := os.walk_ext('.', '.v')
for tfile in all_test_files {
if tfile in known_failing_exceptions {
continue
}
files_that_can_be_formatted << tfile
}
return files_that_can_be_formatted
}

View File

@ -148,7 +148,7 @@ fn C.toupper() int
fn C.getchar() int fn C.getchar() int
fn C.strerror() charptr fn C.strerror(int) charptr
fn C.snprintf() int fn C.snprintf() int
@ -397,4 +397,3 @@ fn C.WaitForSingleObject(voidptr, int) int
fn C.ReleaseMutex(voidptr) bool fn C.ReleaseMutex(voidptr) bool

View File

@ -34,12 +34,3 @@ pub fn (cb Clipboard) check_ownership() bool {
pub fn (cb &Clipboard) is_available() bool { pub fn (cb &Clipboard) is_available() bool {
return cb.check_availability() return cb.check_availability()
} }
// create a new PRIMARY clipboard (only supported on Linux)
pub fn new_primary() &Clipboard {
$if linux {
return new_x11_clipboard(.primary)
} $else {
panic("Primary clipboard is not supported on non-Linux systems.")
}
}

View File

@ -72,3 +72,7 @@ fn (cb mut Clipboard) get_text() string {
#utf8_clip = [ns_clip UTF8String]; #utf8_clip = [ns_clip UTF8String];
return string(utf8_clip) return string(utf8_clip)
} }
pub fn new_primary() &Clipboard {
panic('Primary clipboard is not supported on non-Linux systems.')
}

View File

@ -423,3 +423,7 @@ fn new_display() &Display {
return XOpenDisplay(C.NULL) return XOpenDisplay(C.NULL)
} }
// create a new PRIMARY clipboard (only supported on Linux)
pub fn new_primary() &Clipboard {
return new_x11_clipboard(.primary)
}

View File

@ -4,6 +4,8 @@ import time
#include <windows.h> #include <windows.h>
type HANDLE voidptr
struct C.HWND struct C.HWND
struct C.WPARAM struct C.WPARAM
struct C.LPARAM struct C.LPARAM
@ -153,3 +155,7 @@ fn (cb mut Clipboard) get_text() string {
GlobalUnlock(h_data) GlobalUnlock(h_data)
return str return str
} }
pub fn new_primary() &Clipboard {
panic('Primary clipboard is not supported on non-Linux systems.')
}

View File

@ -1102,7 +1102,7 @@ pub fn new_v(args []string) &V {
build_mode: build_mode build_mode: build_mode
cflags: cflags cflags: cflags
ccompiler: find_c_compiler() ccompiler: find_c_compiler()
building_v: !is_repl && (rdir_name == 'compiler' || rdir_name == 'v.v' || dir.contains('vlib')) building_v: !is_repl && (rdir_name == 'compiler' || rdir_name == 'v.v' || rdir_name == 'vfmt.v' || dir.contains('vlib'))
// is_fmt: comptime_define == 'vfmt' // is_fmt: comptime_define == 'vfmt'
user_mod_path: user_mod_path user_mod_path: user_mod_path

View File

@ -46,8 +46,7 @@ struct C.stat {
st_mtime int st_mtime int
} }
struct C.DIR { struct C.DIR {}
}
// struct C.dirent { // struct C.dirent {
// d_name byteptr // d_name byteptr
@ -123,8 +122,7 @@ pub fn read_file(path string) ?string {
// file_size returns the size of the file located in `path`. // file_size returns the size of the file located in `path`.
pub fn file_size(path string) int { pub fn file_size(path string) int {
mut s := C.stat{ mut s := C.stat{}
}
$if windows { $if windows {
C._wstat(path.to_wide(), voidptr(&s)) C._wstat(path.to_wide(), voidptr(&s))
} $else { } $else {
@ -272,8 +270,7 @@ fn read_ulines(path string) ?[]ustring {
} }
pub fn open(path string) ?File { pub fn open(path string) ?File {
mut file := File{ mut file := File{}
}
$if windows { $if windows {
wpath := path.to_wide() wpath := path.to_wide()
mode := 'rb' mode := 'rb'
@ -295,8 +292,7 @@ pub fn open(path string) ?File {
// create creates a file at a specified location and returns a writable `File` object. // create creates a file at a specified location and returns a writable `File` object.
pub fn create(path string) ?File { pub fn create(path string) ?File {
mut file := File{ mut file := File{}
}
$if windows { $if windows {
wpath := path.replace('/', '\\').to_wide() wpath := path.replace('/', '\\').to_wide()
mode := 'wb' mode := 'wb'
@ -317,8 +313,7 @@ pub fn create(path string) ?File {
} }
pub fn open_append(path string) ?File { pub fn open_append(path string) ?File {
mut file := File{ mut file := File{}
}
$if windows { $if windows {
wpath := path.replace('/', '\\').to_wide() wpath := path.replace('/', '\\').to_wide()
mode := 'ab' mode := 'ab'
@ -493,8 +488,8 @@ pub fn sigint_to_signal_name(si int) string {
15 { 15 {
return 'SIGTERM' return 'SIGTERM'
} }
else { else {}
}} }
$if linux { $if linux {
// From `man 7 signal` on linux: // From `man 7 signal` on linux:
match si { match si {
@ -529,8 +524,8 @@ pub fn sigint_to_signal_name(si int) string {
7 { 7 {
return 'SIGBUS' return 'SIGBUS'
} }
else { else {}
}} }
} }
return 'unknown' return 'unknown'
} }
@ -608,7 +603,9 @@ pub fn rmdir(path string) {
} }
fn print_c_errno() { fn print_c_errno() {
// C.printf('errno=%d err="%s"\n', C.errno, C.strerror(C.errno)) e := C.errno
se := tos_clone(byteptr(C.strerror(C.errno)))
println('errno=$e err=$se')
} }
[deprecated] [deprecated]
@ -776,8 +773,7 @@ pub fn on_segfault(f voidptr) {
return return
} }
$if macos { $if macos {
mut sa := C.sigaction{ mut sa := C.sigaction{}
}
C.memset(&sa, 0, sizeof(sigaction)) C.memset(&sa, 0, sizeof(sigaction))
C.sigemptyset(&sa.sa_mask) C.sigemptyset(&sa.sa_mask)
sa.sa_sigaction = f sa.sa_sigaction = f
@ -833,10 +829,8 @@ pub fn executable() string {
// lol // lol
return os.args[0] return os.args[0]
} }
$if solaris { $if solaris {}
} $if haiku {}
$if haiku {
}
$if netbsd { $if netbsd {
mut result := calloc(MAX_PATH) mut result := calloc(MAX_PATH)
count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH)) count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH))
@ -876,8 +870,7 @@ pub fn is_dir(path string) bool {
} }
return false return false
} $else { } $else {
statbuf := C.stat{ statbuf := C.stat{}
}
if C.stat(path.str, &statbuf) != 0 { if C.stat(path.str, &statbuf) != 0 {
return false return false
} }
@ -891,8 +884,7 @@ pub fn is_link(path string) bool {
$if windows { $if windows {
return false // TODO return false // TODO
} $else { } $else {
statbuf := C.stat{ statbuf := C.stat{}
}
if C.lstat(path.str, &statbuf) != 0 { if C.lstat(path.str, &statbuf) != 0 {
return false return false
} }
@ -1028,8 +1020,7 @@ pub fn wait() int {
} }
pub fn file_last_mod_unix(path string) int { pub fn file_last_mod_unix(path string) int {
attr := C.stat{ attr := C.stat{}
}
// # struct stat attr; // # struct stat attr;
C.stat(path.str, &attr) C.stat(path.str, &attr)
// # stat(path.str, &attr); // # stat(path.str, &attr);
@ -1045,16 +1036,6 @@ pub fn flush_stdout() {
C.fflush(stdout) C.fflush(stdout)
} }
pub fn print_backtrace() {
/*
# void *buffer[100];
nptrs := 0
# nptrs = backtrace(buffer, 100);
# printf("%d!!\n", nptrs);
# backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
*/
}
pub fn mkdir_all(path string) { pub fn mkdir_all(path string) {
mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' } mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
for subdir in path.split(os.path_separator) { for subdir in path.split(os.path_separator) {
@ -1120,4 +1101,3 @@ pub fn chmod(path string, mode int) {
pub const ( pub const (
wd_at_startup = getwd() wd_at_startup = getwd()
) )

View File

@ -1,22 +1,31 @@
module os2 module os2
#include <fcntl.h> #include <fcntl.h>
struct File { struct File {
fd int fd int
} }
fn C.perror(charptr)
fn C.open(byteptr, int, int) int fn C.open(byteptr, int, int) int
fn C.write(voidptr, byteptr, int) int fn C.write(voidptr, byteptr, int) int
fn C.close(int) int
pub fn create(path string) ?File { pub fn create(path string) ?File {
fd := C.creat(path.str, 0644)//511) fd := C.open(path.str, C.O_CREAT | C.O_TRUNC | C.O_WRONLY, 0644) // 511
if fd == -1 { if fd == -1 {
return error('failed to create "$path":') return error('failed to create "$path":')
//os.print_c_errno() // os.print_c_errno()
} }
return File{fd} return File{
} fd}
}
pub fn (f File) writeln(s string) { pub fn (f File) writeln(s string) {
ss := s + '\n' ss := s + '\n'
@ -29,4 +38,3 @@ pub fn (f File) writeln(s string) {
pub fn (f File) close() { pub fn (f File) close() {
C.close(f.fd) C.close(f.fd)
} }