Browse Source

Remove `core:os` dependency completely from `base:runtime`

gingerBill 1 year ago
parent
commit
535b8a9483

+ 0 - 18
base/runtime/os_specific_any.odin

@@ -1,18 +0,0 @@
-//+build !darwin
-//+build !linux
-//+build !freestanding
-//+build !js
-//+build !wasi
-//+build !windows
-//+private
-package runtime
-
-import "core:os"
-
-// TODO(bill): reimplement `os.write` so that it does not rely on package os
-// NOTE: Use os_specific_linux.odin, os_specific_darwin.odin, etc
-_os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
-	context = default_context()
-	n, err := os.write(os.stderr, data)
-	return int(n), _OS_Errno(err)
-}

+ 21 - 0
base/runtime/os_specific_bsd.odin

@@ -0,0 +1,21 @@
+//+build freebsd, openbsd
+//+private
+package runtime
+
+foreign import libc "system:c"
+
+foreign libc {
+	@(link_name="write")
+	_unix_write :: proc(fd: uintptr, buf: rawptr, size: int) -> int ---
+
+	__error :: proc() -> ^i32 ---
+}
+
+_os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
+	ret := _unix_write(2, raw_data(data), len(data))
+	if ret < len(data) {
+		err := __error()
+		return int(ret), _OS_Errno(err^ if err != nil else 0)
+	}
+	return int(ret), 0
+}

+ 1 - 1
core/os/os_freebsd.odin

@@ -255,7 +255,7 @@ W_OK :: 2 // Test for write permission
 R_OK :: 4 // Test for read permission
 
 foreign libc {
-	@(link_name="__error")		__errno_location :: proc() -> ^int ---
+	@(link_name="__error")		__errno_location :: proc() -> ^c.int ---
 
 	@(link_name="open")             _unix_open          :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
 	@(link_name="close")            _unix_close         :: proc(fd: Handle) -> c.int ---

+ 2 - 2
core/os/os_openbsd.odin

@@ -246,7 +246,7 @@ AT_REMOVEDIR        :: 0x08
 
 @(default_calling_convention="c")
 foreign libc {
-	@(link_name="__errno")	__errno		:: proc() -> ^int ---
+	@(link_name="__error")	__error		:: proc() -> ^c.int ---
 
 	@(link_name="fork")	_unix_fork	:: proc() -> pid_t ---
 	@(link_name="getthrid")	_unix_getthrid	:: proc() -> int ---
@@ -296,7 +296,7 @@ is_path_separator :: proc(r: rune) -> bool {
 }
 
 get_last_error :: proc "contextless" () -> int {
-	return __errno()^
+	return __error()^
 }
 
 fork :: proc() -> (Pid, Errno) {