From 9a541c12140e8768fabbbd8834cd0e1570df344b Mon Sep 17 00:00:00 2001 From: Greg V Date: Wed, 17 Oct 2018 16:48:17 +0300 Subject: Add FreeBSD support to os.cpp --- src/os.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index 6df463d8a5..86bef6d5e8 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -50,10 +50,13 @@ typedef SSIZE_T ssize_t; #endif -#if defined(ZIG_OS_LINUX) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) #include #endif +#if defined(ZIG_OS_FREEBSD) +#include +#endif #if defined(__MACH__) #include @@ -75,7 +78,9 @@ static clock_serv_t cclock; #if defined(__APPLE__) && !defined(environ) #include #define environ (*_NSGetEnviron()) -#endif +#elif defined(ZIG_OS_FREEBSD) +extern char **environ; +#endif #if defined(ZIG_OS_POSIX) static void populate_termination(Termination *term, int status) { @@ -1442,6 +1447,15 @@ Error os_self_exe_path(Buf *out_path) { } buf_resize(out_path, amt); return ErrorNone; +#elif defined(ZIG_OS_FREEBSD) + buf_resize(out_path, PATH_MAX); + int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + size_t cb = PATH_MAX; + if (sysctl(mib, 4, buf_ptr(out_path), &cb, nullptr, 0) != 0) { + return ErrorUnexpected; + } + buf_resize(out_path, cb); + return ErrorNone; #endif return ErrorFileNotFound; } @@ -1747,7 +1761,7 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) { buf_resize(out_path, 0); buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname); return ErrorNone; -#elif defined(ZIG_OS_LINUX) +#elif defined(ZIG_OS_POSIX) const char *home_dir = getenv("HOME"); if (home_dir == nullptr) { // TODO use /etc/passwd @@ -1760,7 +1774,7 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) { } -#if defined(ZIG_OS_LINUX) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) { ZigList *libs = reinterpret_cast< ZigList *>(data); if (info->dlpi_name[0] == '/') { @@ -1771,7 +1785,7 @@ static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, #endif Error os_self_exe_shared_libs(ZigList &paths) { -#if defined(ZIG_OS_LINUX) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) paths.resize(0); dl_iterate_phdr(self_exe_shared_libs_callback, &paths); return ErrorNone; @@ -1940,7 +1954,7 @@ Error os_file_mtime(OsFile file, OsTimeStamp *mtime) { mtime->sec = (((ULONGLONG) last_write_time.dwHighDateTime) << 32) + last_write_time.dwLowDateTime; mtime->nsec = 0; return ErrorNone; -#elif defined(ZIG_OS_LINUX) +#elif defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) struct stat statbuf; if (fstat(file, &statbuf) == -1) return ErrorFileSystem; -- cgit v1.2.3 From 947cdafd917f30cfd3d28b85e16a6ca980162dc9 Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sun, 18 Nov 2018 15:15:17 +0900 Subject: src/os.cpp: os_file_read: return ErrorIsDir on case EISDIR; --- src/os.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index 5d43e73d8a..9d16d763ec 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1976,7 +1976,7 @@ Error os_file_read(OsFile file, void *ptr, size_t *len) { case EFAULT: zig_unreachable(); case EISDIR: - zig_unreachable(); + return ErrorIsDir; default: return ErrorFileSystem; } -- cgit v1.2.3 From 67a39a4c99106714588676db0168fef52e0ecd9c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 26 Nov 2018 20:04:35 -0500 Subject: stage1: better file path handling * better message printed when cache hash fails * better handling of '/' as root source file * os_path_split parses '/' and '/a' correctly closes #1693 closes #1746 --- src/cache_hash.cpp | 6 ++++-- src/codegen.cpp | 11 ++++++++++- src/error.cpp | 1 + src/error.hpp | 1 + src/os.cpp | 10 ++++++++-- 5 files changed, 24 insertions(+), 5 deletions(-) (limited to 'src/os.cpp') diff --git a/src/cache_hash.cpp b/src/cache_hash.cpp index 7a3c08bc27..5e6c3b9a9d 100644 --- a/src/cache_hash.cpp +++ b/src/cache_hash.cpp @@ -352,8 +352,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { // if the mtime matches we can trust the digest OsFile this_file; if ((err = os_file_open_r(chf->path, &this_file))) { + fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err)); os_file_close(ch->manifest_file); - return err; + return ErrorCacheUnavailable; } OsTimeStamp actual_mtime; if ((err = os_file_mtime(this_file, &actual_mtime))) { @@ -392,8 +393,9 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { for (; file_i < input_file_count; file_i += 1) { CacheHashFile *chf = &ch->files.at(file_i); if ((err = populate_file_hash(ch, chf, nullptr))) { + fprintf(stderr, "Unable to hash %s: %s\n", buf_ptr(chf->path), err_str(err)); os_file_close(ch->manifest_file); - return err; + return ErrorCacheUnavailable; } } return ErrorNone; diff --git a/src/codegen.cpp b/src/codegen.cpp index 37e0424961..1033ed8120 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -129,6 +129,11 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out Buf *src_dir = buf_alloc(); os_path_split(root_src_path, src_dir, src_basename); + if (buf_len(src_basename) == 0) { + fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path)); + exit(1); + } + g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename)); g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig"); g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); @@ -8178,7 +8183,11 @@ void codegen_build_and_link(CodeGen *g) { os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir); if ((err = check_cache(g, manifest_dir, &digest))) { - fprintf(stderr, "Unable to check cache: %s\n", err_str(err)); + if (err == ErrorCacheUnavailable) { + // message already printed + } else { + fprintf(stderr, "Unable to check cache: %s\n", err_str(err)); + } exit(1); } diff --git a/src/error.cpp b/src/error.cpp index d0575a8494..10186fbde5 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -33,6 +33,7 @@ const char *err_str(Error err) { case ErrorSharingViolation: return "sharing violation"; case ErrorPipeBusy: return "pipe busy"; case ErrorPrimitiveTypeNotFound: return "primitive type not found"; + case ErrorCacheUnavailable: return "cache unavailable"; } return "(invalid error)"; } diff --git a/src/error.hpp b/src/error.hpp index 8b8fa5ce17..b60cb8517e 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -35,6 +35,7 @@ enum Error { ErrorSharingViolation, ErrorPipeBusy, ErrorPrimitiveTypeNotFound, + ErrorCacheUnavailable, }; const char *err_str(Error err); diff --git a/src/os.cpp b/src/os.cpp index 9d16d763ec..f739ee44e7 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -188,14 +188,20 @@ void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { size_t len = buf_len(full_path); if (len != 0) { size_t last_index = len - 1; - if (os_is_sep(buf_ptr(full_path)[last_index])) { + char last_char = buf_ptr(full_path)[last_index]; + if (os_is_sep(last_char)) { + if (last_index == 0) { + if (out_dirname) buf_init_from_mem(out_dirname, &last_char, 1); + if (out_basename) buf_init_from_str(out_basename, ""); + return; + } last_index -= 1; } for (size_t i = last_index;;) { uint8_t c = buf_ptr(full_path)[i]; if (os_is_sep(c)) { if (out_dirname) { - buf_init_from_mem(out_dirname, buf_ptr(full_path), i); + buf_init_from_mem(out_dirname, buf_ptr(full_path), (i == 0) ? 1 : i); } if (out_basename) { buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); -- cgit v1.2.3 From d770333827549e5dcbb727c7f434eaac9dd433d2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 12 Dec 2018 22:28:15 -0500 Subject: freebsd: fix os_self_exe_path function and update std lib --- src/os.cpp | 2 +- std/os/freebsd/index.zig | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index eba95b9f2f..2f0379c09b 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1456,7 +1456,7 @@ Error os_self_exe_path(Buf *out_path) { if (sysctl(mib, 4, buf_ptr(out_path), &cb, nullptr, 0) != 0) { return ErrorUnexpected; } - buf_resize(out_path, cb); + buf_resize(out_path, cb - 1); return ErrorNone; #endif return ErrorFileNotFound; diff --git a/std/os/freebsd/index.zig b/std/os/freebsd/index.zig index 75389fc403..9fbeaf1dc1 100644 --- a/std/os/freebsd/index.zig +++ b/std/os/freebsd/index.zig @@ -9,6 +9,7 @@ pub use @import("errno.zig"); const std = @import("../../index.zig"); const c = std.c; +const maxInt = std.math.maxInt; pub const Kevent = c.Kevent; pub const PATH_MAX = 1024; @@ -22,7 +23,7 @@ pub const PROT_READ = 1; pub const PROT_WRITE = 2; pub const PROT_EXEC = 4; -pub const MAP_FAILED = @maxValue(usize); +pub const MAP_FAILED = maxInt(usize); pub const MAP_SHARED = 0x0001; pub const MAP_PRIVATE = 0x0002; pub const MAP_FIXED = 0x0010; @@ -700,7 +701,7 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti const NSIG = 65; const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []usize{@maxValue(usize)}; +const all_mask = []usize{maxInt(usize)}; const app_mask = []usize{0xfffffffc7fffffff}; /// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. @@ -711,7 +712,7 @@ pub const Sigaction = struct { flags: u32, }; -pub const SIG_ERR = @intToPtr(extern fn (i32) void, @maxValue(usize)); +pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize)); pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0); pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1); pub const empty_sigset = []usize{0} ** sigset_t.len; -- cgit v1.2.3