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

@ -21,16 +21,15 @@ const (
fn C.puts(charptr) fn C.puts(charptr)
*/ */
pub fn println(s string) { pub fn println(s string) {
/*
$if linux { $if linux {
snl := s + '\n' $if !android {
C.syscall(sys_write, stdout_value, snl.str, s.len+1) 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 { 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 {
p.genln('#ifndef $ifdef_name') if name == 'linux_or_macos' {
p.genln('#if !defined(__linux__) && !defined(__APPLE__)')
} else {
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

@ -9,11 +9,12 @@ pub const (
const ( const (
stdin_value = 0 stdin_value = 0
stdout_value = 1 stdout_value = 1
stderr_value = 2 stderr_value = 2
) )
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,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. // 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 { mut fd := 0
//$if linux_or_macos { mut file := File{}
mut fd := 0 // NB: android/termux/bionic is also a kind of linux,
//println('creat SYS') // but linux syscalls there sometimes fail,
/* // while the libc version should work.
$if macos { $if linux_or_macos {
fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6) $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') 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,19 +168,20 @@ 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 {
ret := C.syscall(sys_mkdir, apath.str, 511) $if !android {
if ret == -1 { ret := C.syscall(sys_mkdir, apath.str, 511)
return error(get_error_msg(C.errno)) if ret == -1 {
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 {
@ -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 {
C.syscall(sys_write, f.fd, data, 1) $if !android {
} $else { C.syscall(sys_write, f.fd, data, 1)
C.fwrite(data, 1, size, f.cfile) return
}
} }
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)
} }