From 8053175eadea8627a58bfe4de8f4d49a83419514 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 5 Jan 2020 21:13:35 +0200 Subject: [PATCH] android: fix os.create and builtin --- Makefile | 6 +- vlib/builtin/builtin_nix.v | 15 ++-- vlib/compiler/comptime.v | 8 +- vlib/os/os.v | 4 +- vlib/os/os_nix.v | 155 +++++++++++++++++++------------------ 5 files changed, 100 insertions(+), 88 deletions(-) diff --git a/Makefile b/Makefile index 25b60bc97a..a7c5fddab8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ CC ?= cc +CFLAGS ?= +LDFLAGS ?= TMPDIR ?= /tmp VCFILE := v.c @@ -39,12 +41,12 @@ endif all: latest_vc latest_tcc ifdef WIN32 - $(CC) -std=c99 -municode -w -o v2.exe $(TMPVC)/$(VCFILE) $(LDFLAGS) + $(CC) $(CFLAGS) -std=c99 -municode -w -o v2.exe $(TMPVC)/$(VCFILE) $(LDFLAGS) ./v2.exe -o v3.exe v.v ./v3.exe -o v.exe -prod v.v rm -f v2.exe v3.exe else - $(CC) -std=gnu11 -w -o v $(TMPVC)/$(VCFILE) $(LDFLAGS) -lm + $(CC) $(CFLAGS) -std=gnu11 -w -o v $(TMPVC)/$(VCFILE) $(LDFLAGS) -lm ifdef ANDROID chmod 755 v endif diff --git a/vlib/builtin/builtin_nix.v b/vlib/builtin/builtin_nix.v index 883108a4b1..3962bbe841 100644 --- a/vlib/builtin/builtin_nix.v +++ b/vlib/builtin/builtin_nix.v @@ -21,16 +21,15 @@ const ( fn C.puts(charptr) */ -pub fn println(s string) { - /* +pub fn println(s string) { $if linux { - snl := s + '\n' - C.syscall(sys_write, stdout_value, snl.str, s.len+1) + $if !android { + snl := s + '\n' + C.syscall(/* sys_write */ 1, /* stdout_value */ 1, snl.str, s.len+1) + return + } } - $else { - */ - C.printf('%.*s\n', s.len, s.str) - //} + C.printf('%.*s\n', s.len, s.str) } fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool { diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index fbcd9fb659..077bbbbd60 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -29,11 +29,15 @@ fn (p mut Parser) comp_time() { } if not { - p.genln('#ifndef $ifdef_name') + if name == 'linux_or_macos' { + p.genln('#if !defined(__linux__) && !defined(__APPLE__)') + } else { + p.genln('#ifndef $ifdef_name') + } } else { if name == 'linux_or_macos' { - p.genln('#if defined(__linux) || defined(__APPLE__)') + p.genln('#if defined(__linux__) || defined(__APPLE__)') } else { p.genln('#ifdef $ifdef_name') } diff --git a/vlib/os/os.v b/vlib/os/os.v index 084be6b62b..bebb2543bf 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -789,7 +789,7 @@ pub fn executable() string { $if haiku {} $if netbsd { mut result := calloc(MAX_PATH) - count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH)) + count := C.readlink('/proc/curproc/exe', result, MAX_PATH) if count < 0 { eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path') return os.args[0] @@ -798,7 +798,7 @@ pub fn executable() string { } $if dragonfly { mut result := calloc(MAX_PATH) - count := int(C.readlink('/proc/curproc/file', result, MAX_PATH)) + count := C.readlink('/proc/curproc/file', result, MAX_PATH) if count < 0 { eprintln('os.executable() failed at reading /proc/curproc/file to get exe path') return os.args[0] diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 91c55cb4be..a4ae13f97a 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -9,11 +9,12 @@ pub const ( const ( stdin_value = 0 stdout_value = 1 - stderr_value = 2 + stderr_value = 2 ) fn C.symlink(charptr, charptr) int + pub fn init_os_args(argc int, argv &byteptr) []string { mut args := []string for i in 0 .. argc { @@ -67,57 +68,58 @@ pub fn is_dir(path string) bool { } */ -pub fn open(path string) ?File { - $if linux { - //$if linux_or_macos { - fd := C.syscall(sys_open, path.str, 511) - if fd == -1 { - return error('failed to open file "$path"') - } - return File{ - fd: fd - opened: true - } - } - $else { - cpath := path.str - file := File{ - cfile: C.fopen(charptr(cpath), 'rb') - opened: true - } - if isnil(file.cfile) { - return error('failed to open file "$path"') - } - return file - } -} +pub fn open(path string) ?File { + mut file := File{} + $if linux_or_macos { + $if !android { + fd := C.syscall(sys_open, path.str, 511) + if fd == -1 { + return error('failed to open file "$path"') + } + return File{ + fd: fd + opened: true + } + } + } + cpath := path.str + file = File{ + cfile: C.fopen(charptr(cpath), 'rb') + opened: true + } + if isnil(file.cfile) { + return error('failed to open file "$path"') + } + return file +} // create creates a file at a specified location and returns a writable `File` object. pub fn create(path string) ?File { - $if linux { - //$if linux_or_macos { - mut fd := 0 - //println('creat SYS') - /* - $if macos { - fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6) + mut fd := 0 + mut file := File{} + // NB: android/termux/bionic is also a kind of linux, + // but linux syscalls there sometimes fail, + // while the libc version should work. + $if linux_or_macos { + $if !android { + $if macos { + fd = C.syscall(398, path.str, 0x601, 0x1b6) + } + $if linux { + fd = C.syscall(sys_creat, path.str, 511) + } + if fd == -1 { + return error('failed to create file "$path"') + } + file = File{ + fd: fd + opened: true + } + return file } - $else { - */ - fd = C.syscall(sys_creat, path.str, 511) - //} - //println('fd=$fd') - if fd == -1 { - return error('failed to create file "$path"') - } - return File{ - fd: fd - opened: true - } - } - mut file := File{ + file = File{ cfile: C.fopen(charptr(path.str), 'wb') opened: true } @@ -132,16 +134,17 @@ pub fn (f mut File) fseek(pos, mode int) { } */ + pub fn (f mut File) write(s string) { if !f.opened { return } - $if linux { - //$if linux_or_macos { - C.syscall(sys_write, f.fd, s.str, s.len) - return + $if linux_or_macos { + $if !android { + C.syscall(sys_write, f.fd, s.str, s.len) + return + } } - C.fputs(s.str, f.cfile) // C.fwrite(s.str, 1, s.len, f.cfile) } @@ -150,14 +153,13 @@ pub fn (f mut File) writeln(s string) { if !f.opened { return } - //$if linux_or_macos { - $if linux { - snl := s + '\n' - C.syscall(sys_write, f.fd, snl.str, snl.len) - return + $if linux_or_macos { + $if !android { + snl := s + '\n' + C.syscall(sys_write, f.fd, snl.str, snl.len) + return + } } - - // C.fwrite(s.str, 1, s.len, f.cfile) // ss := s.clone() // TODO perf @@ -166,19 +168,20 @@ pub fn (f mut File) writeln(s string) { C.fputs('\n', f.cfile) } - // mkdir creates a new directory with the specified path. pub fn mkdir(path string) ?bool { if path == '.' { return true } apath := os.realpath(path) - $if linux { - ret := C.syscall(sys_mkdir, apath.str, 511) - if ret == -1 { - return error(get_error_msg(C.errno)) + $if linux_or_macos { + $if !android { + ret := C.syscall(sys_mkdir, apath.str, 511) + if ret == -1 { + return error(get_error_msg(C.errno)) + } + return true } - return true } r := C.mkdir(apath.str, 511) if r == -1 { @@ -215,7 +218,9 @@ pub fn exec(cmd string) ?Result { pub fn symlink(origin, target string) ?bool { res := C.symlink(origin.str, target.str) - if res == 0 { return true } + if res == 0 { + return true + } return error(get_error_msg(C.errno)) } @@ -223,11 +228,13 @@ pub fn symlink(origin, target string) ?bool { // for example if we have write(7, 4), "07 00 00 00" gets written // write(0x1234, 2) => "34 12" pub fn (f mut File) write_bytes(data voidptr, size int) { - $if linux { - C.syscall(sys_write, f.fd, data, 1) - } $else { - C.fwrite(data, 1, size, f.cfile) + $if linux_or_macos { + $if !android { + C.syscall(sys_write, f.fd, data, 1) + return + } } + C.fwrite(data, 1, size, f.cfile) } pub fn (f mut File) close() { @@ -235,12 +242,12 @@ pub fn (f mut File) close() { return } f.opened = false - $if linux { - //$if linux_or_macos { - C.syscall(sys_close, f.fd) - return + $if linux_or_macos { + $if !android { + C.syscall(sys_close, f.fd) + return + } } C.fflush(f.cfile) C.fclose(f.cfile) } -