From b5d0739de07c29daa01e108dc162e22f8603d1b4 Mon Sep 17 00:00:00 2001 From: Boris Barbulovski Date: Thu, 10 Apr 2025 03:22:32 +0200 Subject: [PATCH] Add env::ANDROID to std.* --- lib/std/core/builtin.c3 | 2 +- lib/std/io/os/fileinfo.c3 | 8 +-- lib/std/libc/libc.c3 | 2 +- lib/std/net/os/android.c3 | 92 +++++++++++++++++++++++++++++++++ lib/std/net/os/common.c3 | 6 +-- test/unit/stdlib/io/fileinfo.c3 | 4 +- 6 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 lib/std/net/os/android.c3 diff --git a/lib/std/core/builtin.c3 b/lib/std/core/builtin.c3 index 98db25e27..e7171dc59 100644 --- a/lib/std/core/builtin.c3 +++ b/lib/std/core/builtin.c3 @@ -772,7 +772,7 @@ macro void* get_returnaddress(int n) } } -module std::core::builtin @if((env::LINUX || env::DARWIN) && env::COMPILER_SAFE_MODE && env::DEBUG_SYMBOLS); +module std::core::builtin @if((env::LINUX || env::ANDROID || env::DARWIN) && env::COMPILER_SAFE_MODE && env::DEBUG_SYMBOLS); import libc, std::io; fn void sig_panic(String message) diff --git a/lib/std/io/os/fileinfo.c3 b/lib/std/io/os/fileinfo.c3 index 751f3415d..87f30f980 100644 --- a/lib/std/io/os/fileinfo.c3 +++ b/lib/std/io/os/fileinfo.c3 @@ -1,9 +1,9 @@ module std::io::os; import libc, std::os, std::io; -fn void? native_stat(Stat* stat, String path) @if(env::DARWIN || env::LINUX || env::BSD_FAMILY) => @pool() +fn void? native_stat(Stat* stat, String path) @if(env::DARWIN || env::LINUX || env::ANDROID || env::BSD_FAMILY) => @pool() { - $if env::DARWIN || env::LINUX || env::BSD_FAMILY: + $if env::DARWIN || env::LINUX || env::ANDROID || env::BSD_FAMILY: int res = libc::stat(path.zstr_tcopy(), stat); $else unreachable("Stat unimplemented"); @@ -69,6 +69,7 @@ fn bool native_file_or_dir_exists(String path) $case env::NETBSD: $case env::OPENBSD: $case env::LINUX: + $case env::ANDROID: Stat stat; return @ok(native_stat(&stat, path)); $case env::WIN32: @@ -94,6 +95,7 @@ fn bool native_is_file(String path) $case env::NETBSD: $case env::OPENBSD: $case env::LINUX: + $case env::ANDROID: Stat stat; return @ok(native_stat(&stat, path)) && libc_S_ISTYPE(stat.st_mode, libc::S_IFREG); $default: @@ -105,7 +107,7 @@ fn bool native_is_file(String path) fn bool native_is_dir(String path) { - $if env::DARWIN || env::LINUX || env::BSD_FAMILY: + $if env::DARWIN || env::LINUX || env::ANDROID || env::BSD_FAMILY: Stat stat; return @ok(native_stat(&stat, path)) && libc_S_ISTYPE(stat.st_mode, libc::S_IFDIR); $else diff --git a/lib/std/libc/libc.c3 b/lib/std/libc/libc.c3 index f21c6df5e..bd3e6e9eb 100644 --- a/lib/std/libc/libc.c3 +++ b/lib/std/libc/libc.c3 @@ -373,7 +373,7 @@ module libc; alias CFile = void*; -const HAS_MALLOC_SIZE = env::LINUX || env::WIN32 || env::DARWIN; +const HAS_MALLOC_SIZE = env::LINUX || env::ANDROID || env::WIN32 || env::DARWIN; // The following needs to be set per arch+os // For now I have simply pulled the defaults from MacOS diff --git a/lib/std/net/os/android.c3 b/lib/std/net/os/android.c3 new file mode 100644 index 000000000..10fecb6b4 --- /dev/null +++ b/lib/std/net/os/android.c3 @@ -0,0 +1,92 @@ +module std::net::os @if(env::ANDROID); +import libc; + +const AIFamily PLATFORM_AF_AX25 = 3; +const AIFamily PLATFORM_AF_IPX = 4; +const AIFamily PLATFORM_AF_APPLETALK = 5; +const AIFamily PLATFORM_AF_NETROM = 6; +const AIFamily PLATFORM_AF_BRIDGE = 7; +const AIFamily PLATFORM_AF_AAL5 = 8; +const AIFamily PLATFORM_AF_X25 = 9; +const AIFamily PLATFORM_AF_INET6 = 10; + +const PLATFORM_O_NONBLOCK = 0o4000; + +// https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/socket.h +const int SOL_SOCKET = 1; + +const int SO_DEBUG = 1; // turn on debugging info recording +const int SO_REUSEADDR = 2; // allow local address reuse +const int SO_TYPE = 3; +const int SO_ERROR = 4; +const int SO_DONTROUTE = 5; // just use interface addresses +const int SO_BROADCAST = 6; // permit sending of broadcast msgs +const int SO_SNDBUF = 7; // Send buffer size +const int SO_RCVBUF = 8; // Receive buffer size +const int SO_KEEPALIVE = 9; // keep connections alive +const int SO_OOBINLINE = 10; // leave received OOB data in line +const int SO_NO_CHECK = 11; +const int SO_PRIORITY = 12; +const int SO_LINGER = 13; // linger on close if data present (in ticks) +const int SO_BSDCOMPAT = 14; +const int SO_REUSEPORT = 15; // allow local address & port reuse +const int SO_RCVLOWAT = 18; +const int SO_SNDLOWAT = 19; +const int SO_RCVTIMEO = 20; // IMPORTANT Verify before use +const int SO_SNDTIMEO = 21; // IMPORTANT Verify before use +const int SO_BINDTODEVICE = 25; +const int SO_ATTACH_FILTER = 26; +const int SO_DETACH_FILTER = 27; +const int SO_PEERNAME = 28; +const int SO_TIMESTAMP = 29; // IMPORTANT Verify before use timestamp received dgram traffic +const int SO_ACCEPTCONN = 30; +const int SO_PEERSEC = 31; +const int SO_SNDBUFFORCE = 32; +const int SO_RCVBUFFORCE = 33; +const int SO_PASSSEC = 34; +const int SO_MARK = 36; +const int SO_PROTOCOL = 38; +const int SO_DOMAIN = 39; +const int SO_RXQ_OVFL = 40; +const int SO_WIFI_STATUS = 41; +const int SO_PEEK_OFF = 42; +const int SO_NOFCS = 43; +const int SO_LOCK_FILTER = 44; +const int SO_SELECT_ERR_QUEUE = 45; +const int SO_BUSY_POLL = 46; +const int SO_MAX_PACING_RATE = 47; +const int SO_BPF_EXTENSIONS = 48; +const int SO_INCOMING_CPU = 49; +const int SO_ATTACH_BPF = 50; +const int SO_ATTACH_REUSEPORT_CBPF = 51; +const int SO_ATTACH_REUSEPORT_EBPF = 52; +const int SO_CNX_ADVICE = 53; +const int SO_MEMINFO = 55; +const int SO_INCOMING_NAPI_ID = 56; +const int SO_COOKIE = 57; +const int SO_PEERGROUPS = 59; +const int SO_ZEROCOPY = 60; +const int SO_TXTIME = 61; +const int SO_BINDTOIFINDEX = 62; +const int SO_DETACH_REUSEPORT_BPF = 68; +const int SO_PREFER_BUSY_POLL = 69; +const int SO_BUSY_POLL_BUDGET = 70; +const int SO_NETNS_COOKIE = 71; +const int SO_BUF_LOCK = 72; +const int SO_RESERVE_MEM = 73; +const int SO_TXREHASH = 74; +const int SO_RCVMARK = 75; +const int SO_PASSPIDFD = 76; +const int SO_PEERPIDFD = 77; + +const CUShort POLLRDNORM = 0x0040; +const CUShort POLLRDBAND = 0x0080; +const CUShort POLLWRNORM = 0x0100; +const CUShort POLLWRBAND = 0x0200; +const CUShort POLLMSG = 0x0400; +const CUShort POLLREMOVE = 0x1000; +const CUShort POLLRDHUP = 0x2000; +const CUShort POLLFREE = 0x4000; +const CUShort POLL_BUSY_LOOP = 0x8000; + +const CInt MSG_PEEK = 0x0002; diff --git a/lib/std/net/os/common.c3 b/lib/std/net/os/common.c3 index 525ab014b..eaef8974f 100644 --- a/lib/std/net/os/common.c3 +++ b/lib/std/net/os/common.c3 @@ -1,5 +1,5 @@ module std::net::os; -const bool SUPPORTS_INET = env::LIBC && (env::WIN32 || env::DARWIN || env::LINUX); +const bool SUPPORTS_INET = env::LIBC && (env::WIN32 || env::DARWIN || env::LINUX || env::ANDROID); typedef AIFamily = CInt; typedef AIProtocol = CInt; @@ -18,7 +18,7 @@ struct AddrInfo AISockType ai_socktype; AIProtocol ai_protocol; Socklen_t ai_addrlen; - struct @if(env::WIN32 || env::DARWIN) + struct @if(env::WIN32 || env::DARWIN || env::ANDROID) { ZString ai_canonname; SockAddrPtr ai_addr; @@ -58,7 +58,7 @@ extern fn void freeaddrinfo(AddrInfo* res) @if(SUPPORTS_INET); extern fn CInt setsockopt(NativeSocket socket, CInt level, CInt optname, void* optval, Socklen_t optlen) @if(SUPPORTS_INET); extern fn CInt getsockopt(NativeSocket socket, CInt level, CInt optname, void* optval, Socklen_t optlen) @if(SUPPORTS_INET); -module std::net::os @if(!env::LIBC || !(env::WIN32 || env::DARWIN || env::LINUX)); +module std::net::os @if(!env::LIBC || !(env::WIN32 || env::DARWIN || env::LINUX || env::ANDROID)); const AIFamily PLATFORM_AF_INET6 = 0; const AIFamily PLATFORM_AF_IPX = 0; diff --git a/test/unit/stdlib/io/fileinfo.c3 b/test/unit/stdlib/io/fileinfo.c3 index b682f6ab6..3b78d295f 100644 --- a/test/unit/stdlib/io/fileinfo.c3 +++ b/test/unit/stdlib/io/fileinfo.c3 @@ -1,13 +1,13 @@ module std::io::fileinfo @test; import std::io::os; -fn void test_native_is_file() @if(env::LINUX || env::DARWIN) +fn void test_native_is_file() @if(env::LINUX || env::DARWIN || env::ANDROID) { assert(!os::native_is_file("/dev/loop0")); assert(!os::native_is_file("/dev/null")); } -fn void test_native_is_dir() @if(env::LINUX || env::DARWIN) +fn void test_native_is_dir() @if(env::LINUX || env::DARWIN || env::ANDROID) { assert(os::native_is_dir("/")); assert(!os::native_is_file("/"));