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

android: fix os.create and builtin

This commit is contained in:
Delyan Angelov 2020-01-05 21:13:35 +02:00 committed by Alexander Medvednikov
parent c24a1b3786
commit 8053175ead
5 changed files with 100 additions and 88 deletions

View File

@ -1,4 +1,6 @@
CC ?= cc CC ?= cc
CFLAGS ?=
LDFLAGS ?=
TMPDIR ?= /tmp TMPDIR ?= /tmp
VCFILE := v.c VCFILE := v.c
@ -39,12 +41,12 @@ endif
all: latest_vc latest_tcc all: latest_vc latest_tcc
ifdef WIN32 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 ./v2.exe -o v3.exe v.v
./v3.exe -o v.exe -prod v.v ./v3.exe -o v.exe -prod v.v
rm -f v2.exe v3.exe rm -f v2.exe v3.exe
else else
$(CC) -std=gnu11 -w -o v $(TMPVC)/$(VCFILE) $(LDFLAGS) -lm $(CC) $(CFLAGS) -std=gnu11 -w -o v $(TMPVC)/$(VCFILE) $(LDFLAGS) -lm
ifdef ANDROID ifdef ANDROID
chmod 755 v chmod 755 v
endif endif

View File

@ -22,15 +22,14 @@ fn C.puts(charptr)
*/ */
pub fn println(s string) { pub fn println(s string) {
/*
$if linux { $if linux {
$if !android {
snl := s + '\n' snl := s + '\n'
C.syscall(sys_write, stdout_value, snl.str, s.len+1) 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 { fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {

View File

@ -29,11 +29,15 @@ fn (p mut Parser) comp_time() {
} }
if not { if not {
if name == 'linux_or_macos' {
p.genln('#if !defined(__linux__) && !defined(__APPLE__)')
} else {
p.genln('#ifndef $ifdef_name') p.genln('#ifndef $ifdef_name')
} }
}
else { else {
if name == 'linux_or_macos' { if name == 'linux_or_macos' {
p.genln('#if defined(__linux) || defined(__APPLE__)') p.genln('#if defined(__linux__) || defined(__APPLE__)')
} else { } else {
p.genln('#ifdef $ifdef_name') p.genln('#ifdef $ifdef_name')
} }

View File

@ -789,7 +789,7 @@ pub fn executable() string {
$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 := C.readlink('/proc/curproc/exe', result, MAX_PATH)
if count < 0 { if count < 0 {
eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path') eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path')
return os.args[0] return os.args[0]
@ -798,7 +798,7 @@ pub fn executable() string {
} }
$if dragonfly { $if dragonfly {
mut result := calloc(MAX_PATH) 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 { if count < 0 {
eprintln('os.executable() failed at reading /proc/curproc/file to get exe path') eprintln('os.executable() failed at reading /proc/curproc/file to get exe path')
return os.args[0] return os.args[0]

View File

@ -14,6 +14,7 @@ const (
fn C.symlink(charptr, charptr) int fn C.symlink(charptr, charptr) int
pub fn init_os_args(argc int, argv &byteptr) []string { pub fn init_os_args(argc int, argv &byteptr) []string {
mut args := []string mut args := []string
for i in 0 .. argc { for i in 0 .. argc {
@ -67,9 +68,11 @@ pub fn is_dir(path string) bool {
} }
*/ */
pub fn open(path string) ?File { pub fn open(path string) ?File {
$if linux { mut file := File{}
//$if linux_or_macos { $if linux_or_macos {
$if !android {
fd := C.syscall(sys_open, path.str, 511) fd := C.syscall(sys_open, path.str, 511)
if fd == -1 { if fd == -1 {
return error('failed to open file "$path"') return error('failed to open file "$path"')
@ -79,9 +82,9 @@ pub fn open(path string) ?File {
opened: true opened: true
} }
} }
$else { }
cpath := path.str cpath := path.str
file := File{ file = File{
cfile: C.fopen(charptr(cpath), 'rb') cfile: C.fopen(charptr(cpath), 'rb')
opened: true opened: true
} }
@ -89,35 +92,34 @@ pub fn open(path string) ?File {
return error('failed to open file "$path"') return error('failed to open file "$path"')
} }
return file return 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 {
$if linux {
//$if linux_or_macos {
mut fd := 0 mut fd := 0
//println('creat SYS') 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 { $if macos {
fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6) fd = C.syscall(398, path.str, 0x601, 0x1b6)
} }
$else { $if linux {
*/
fd = C.syscall(sys_creat, path.str, 511) fd = C.syscall(sys_creat, path.str, 511)
//} }
//println('fd=$fd')
if fd == -1 { if fd == -1 {
return error('failed to create file "$path"') return error('failed to create file "$path"')
} }
return File{ file = File{
fd: fd fd: fd
opened: true opened: true
} }
return file
} }
mut file := File{ }
file = File{
cfile: C.fopen(charptr(path.str), 'wb') cfile: C.fopen(charptr(path.str), 'wb')
opened: true opened: true
} }
@ -132,16 +134,17 @@ pub fn (f mut File) fseek(pos, mode int) {
} }
*/ */
pub fn (f mut File) write(s string) { pub fn (f mut File) write(s string) {
if !f.opened { if !f.opened {
return return
} }
$if linux { $if linux_or_macos {
//$if linux_or_macos { $if !android {
C.syscall(sys_write, f.fd, s.str, s.len) C.syscall(sys_write, f.fd, s.str, s.len)
return return
} }
}
C.fputs(s.str, f.cfile) C.fputs(s.str, f.cfile)
// C.fwrite(s.str, 1, s.len, 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 { if !f.opened {
return return
} }
//$if linux_or_macos { $if linux_or_macos {
$if linux { $if !android {
snl := s + '\n' snl := s + '\n'
C.syscall(sys_write, f.fd, snl.str, snl.len) C.syscall(sys_write, f.fd, snl.str, snl.len)
return return
} }
}
// C.fwrite(s.str, 1, s.len, f.cfile) // C.fwrite(s.str, 1, s.len, f.cfile)
// ss := s.clone() // ss := s.clone()
// TODO perf // TODO perf
@ -166,20 +168,21 @@ pub fn (f mut File) writeln(s string) {
C.fputs('\n', f.cfile) C.fputs('\n', f.cfile)
} }
// mkdir creates a new directory with the specified path. // mkdir creates a new directory with the specified path.
pub fn mkdir(path string) ?bool { pub fn mkdir(path string) ?bool {
if path == '.' { if path == '.' {
return true return true
} }
apath := os.realpath(path) apath := os.realpath(path)
$if linux { $if linux_or_macos {
$if !android {
ret := C.syscall(sys_mkdir, apath.str, 511) ret := C.syscall(sys_mkdir, apath.str, 511)
if ret == -1 { if ret == -1 {
return error(get_error_msg(C.errno)) return error(get_error_msg(C.errno))
} }
return true return true
} }
}
r := C.mkdir(apath.str, 511) r := C.mkdir(apath.str, 511)
if r == -1 { if r == -1 {
return error(get_error_msg(C.errno)) return error(get_error_msg(C.errno))
@ -215,7 +218,9 @@ pub fn exec(cmd string) ?Result {
pub fn symlink(origin, target string) ?bool { pub fn symlink(origin, target string) ?bool {
res := C.symlink(origin.str, target.str) res := C.symlink(origin.str, target.str)
if res == 0 { return true } if res == 0 {
return true
}
return error(get_error_msg(C.errno)) 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 // for example if we have write(7, 4), "07 00 00 00" gets written
// write(0x1234, 2) => "34 12" // write(0x1234, 2) => "34 12"
pub fn (f mut File) write_bytes(data voidptr, size int) { pub fn (f mut File) write_bytes(data voidptr, size int) {
$if linux { $if linux_or_macos {
$if !android {
C.syscall(sys_write, f.fd, data, 1) C.syscall(sys_write, f.fd, data, 1)
} $else { return
C.fwrite(data, 1, size, f.cfile)
} }
}
C.fwrite(data, 1, size, f.cfile)
} }
pub fn (f mut File) close() { pub fn (f mut File) close() {
@ -235,12 +242,12 @@ pub fn (f mut File) close() {
return return
} }
f.opened = false f.opened = false
$if linux { $if linux_or_macos {
//$if linux_or_macos { $if !android {
C.syscall(sys_close, f.fd) C.syscall(sys_close, f.fd)
return return
} }
}
C.fflush(f.cfile) C.fflush(f.cfile)
C.fclose(f.cfile) C.fclose(f.cfile)
} }