Skip to content

Commit

Permalink
tests passing on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 27, 2019
1 parent 2b42e91 commit 0c6ab61
Show file tree
Hide file tree
Showing 38 changed files with 348 additions and 298 deletions.
6 changes: 2 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
}

fn fileExists(filename: []const u8) !bool {
fs.File.exists(filename) catch |err| switch (err) {
error.PermissionDenied,
error.FileNotFound,
=> return false,
fs.File.access(filename) catch |err| switch (err) {
error.FileNotFound => return false,
else => return err,
};
return true;
Expand Down
4 changes: 2 additions & 2 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,8 @@ const assert = std.debug.assert;
threadlocal var x: i32 = 1234;

test "thread local storage" {
const thread1 = try std.os.spawnThread({}, testTls);
const thread2 = try std.os.spawnThread({}, testTls);
const thread1 = try std.Thread.spawn({}, testTls);
const thread2 = try std.Thread.spawn({}, testTls);
testTls({});
thread1.wait();
thread2.wait();
Expand Down
9 changes: 5 additions & 4 deletions example/cat/main.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const std = @import("std");
const io = std.io;
const process = std.process;
const File = std.fs.File;
const mem = std.mem;
const os = std.os;
const warn = std.debug.warn;
const allocator = std.debug.global_allocator;

pub fn main() !void {
var args_it = os.args();
var args_it = process.args();
const exe = try unwrapArg(args_it.next(allocator).?);
var catted_anything = false;
var stdout_file = try io.getStdOut();
Expand All @@ -20,7 +21,7 @@ pub fn main() !void {
} else if (arg[0] == '-') {
return usage(exe);
} else {
var file = os.File.openRead(arg) catch |err| {
var file = File.openRead(arg) catch |err| {
warn("Unable to open file: {}\n", @errorName(err));
return err;
};
Expand All @@ -41,7 +42,7 @@ fn usage(exe: []const u8) !void {
return error.Invalid;
}

fn cat_file(stdout: *os.File, file: *os.File) !void {
fn cat_file(stdout: *File, file: *File) !void {
var buf: [1024 * 4]u8 = undefined;

while (true) {
Expand Down
3 changes: 1 addition & 2 deletions example/guess_number/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const builtin = @import("builtin");
const std = @import("std");
const io = std.io;
const fmt = std.fmt;
const os = std.os;

pub fn main() !void {
var stdout_file = try io.getStdOut();
Expand All @@ -11,7 +10,7 @@ pub fn main() !void {
try stdout.print("Welcome to the Guess Number Game in Zig.\n");

var seed_bytes: [@sizeOf(u64)]u8 = undefined;
os.getRandomBytes(seed_bytes[0..]) catch |err| {
std.crypto.randomBytes(seed_bytes[0..]) catch |err| {
std.debug.warn("unable to seed random number generator: {}", err);
return err;
};
Expand Down
2 changes: 1 addition & 1 deletion example/hello_world/hello_libc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const c = @cImport({
});

export fn main(argc: c_int, argv: [*]?[*]u8) c_int {
c.fprintf(c.stderr, c"Hello, world!\n");
_ = c.fprintf(c.stderr, c"Hello, world!\n");
return 0;
}
1 change: 1 addition & 0 deletions src-self-hosted/compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ pub const Compilation = struct {
InvalidUtf8,
BadPathName,
DeviceBusy,
CurrentWorkingDirectoryUnlinked,
};

pub const Event = union(enum) {
Expand Down
6 changes: 3 additions & 3 deletions src-self-hosted/libc_installation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub const LibCInstallation = struct {
}

async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void {
const cc_exe = std.process.getEnvPosix("CC") orelse "cc";
const cc_exe = std.os.getenv("CC") orelse "cc";
const argv = []const []const u8{
cc_exe,
"-E",
Expand Down Expand Up @@ -392,7 +392,7 @@ pub const LibCInstallation = struct {

/// caller owns returned memory
async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 {
const cc_exe = std.process.getEnvPosix("CC") orelse "cc";
const cc_exe = std.os.getenv("CC") orelse "cc";
const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file);
defer loop.allocator.free(arg1);
const argv = []const []const u8{ cc_exe, arg1 };
Expand Down Expand Up @@ -463,7 +463,7 @@ fn fileExists(path: []const u8) !bool {
if (fs.File.access(path)) |_| {
return true;
} else |err| switch (err) {
error.FileNotFound, error.PermissionDenied => return false,
error.FileNotFound => return false,
else => return error.FileSystem,
}
}
19 changes: 10 additions & 9 deletions src-self-hosted/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ const FmtError = error{
ReadOnlyFileSystem,
LinkQuotaExceeded,
FileBusy,
CurrentWorkingDirectoryUnlinked,
} || fs.File.OpenError;

async fn asyncFmtMain(
Expand Down Expand Up @@ -851,7 +852,7 @@ fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void {
}

fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING));
try stdout.print("{}\n", std.mem.toSliceConst(u8, c.ZIG_VERSION_STRING));
}

const args_test_spec = []Flag{Flag.Bool("--help")};
Expand Down Expand Up @@ -924,14 +925,14 @@ fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {
\\ZIG_DIA_GUIDS_LIB {}
\\
,
std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR),
std.cstr.toSliceConst(c.ZIG_CXX_COMPILER),
std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE),
std.cstr.toSliceConst(c.ZIG_LLD_INCLUDE_PATH),
std.cstr.toSliceConst(c.ZIG_LLD_LIBRARIES),
std.cstr.toSliceConst(c.ZIG_STD_FILES),
std.cstr.toSliceConst(c.ZIG_C_HEADER_FILES),
std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB),
std.mem.toSliceConst(u8, c.ZIG_CMAKE_BINARY_DIR),
std.mem.toSliceConst(u8, c.ZIG_CXX_COMPILER),
std.mem.toSliceConst(u8, c.ZIG_LLVM_CONFIG_EXE),
std.mem.toSliceConst(u8, c.ZIG_LLD_INCLUDE_PATH),
std.mem.toSliceConst(u8, c.ZIG_LLD_LIBRARIES),
std.mem.toSliceConst(u8, c.ZIG_STD_FILES),
std.mem.toSliceConst(u8, c.ZIG_C_HEADER_FILES),
std.mem.toSliceConst(u8, c.ZIG_DIA_GUIDS_LIB),
);
}

Expand Down
13 changes: 9 additions & 4 deletions std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub extern "c" fn open(path: [*]const u8, oflag: c_uint, ...) c_int;
pub extern "c" fn raise(sig: c_int) c_int;
pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize;
pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize;
pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize;
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize;
pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: usize) isize;
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: usize) isize;
pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int;
pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize;
pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize;
Expand All @@ -49,7 +49,7 @@ pub extern "c" fn munmap(addr: *align(page_size) c_void, len: usize) c_int;
pub extern "c" fn mprotect(addr: *align(page_size) c_void, len: usize, prot: c_uint) c_int;
pub extern "c" fn unlink(path: [*]const u8) c_int;
pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8;
pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int;
pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_uint, options: c_uint) c_int;
pub extern "c" fn fork() c_int;
pub extern "c" fn access(path: [*]const u8, mode: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
Expand All @@ -76,7 +76,12 @@ pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usi
pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;

pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
pub extern "c" fn socket(domain: c_int, sock_type: c_int, protocol: c_int) c_int;
pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;
pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int;
pub extern "c" fn connect(sockfd: fd_t, sock_addr: *const sockaddr, addrlen: socklen_t) c_int;
pub extern "c" fn accept4(sockfd: fd_t, addr: *sockaddr, addrlen: *socklen_t, flags: c_uint) c_int;
pub extern "c" fn getsockopt(sockfd: fd_t, level: c_int, optname: c_int, optval: *c_void, optlen: *socklen_t) c_int;
pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int;
pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int;
Expand Down
22 changes: 18 additions & 4 deletions std/c/linux.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const std = @import("../std.zig");
use std.c;

pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
extern "c" fn __errno_location() *c_int;
pub const _errno = __errno_location;

pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;
pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: *epoll_event) c_int;
pub extern "c" fn epoll_create1(flags: c_uint) c_int;
pub extern "c" fn epoll_wait(epfd: fd_t, events: [*]epoll_event, maxevents: c_uint, timeout: c_int) c_int;
pub extern "c" fn epoll_pwait(
epfd: fd_t,
events: [*]epoll_event,
maxevents: c_int,
timeout: c_int,
sigmask: *const sigset_t,
) c_int;
pub extern "c" fn inotify_init1(flags: c_uint) c_int;
pub extern "c" fn inotify_add_watch(fd: fd_t, pathname: [*]const u8, mask: u32) c_int;

/// See std.elf for constants for this
pub extern fn getauxval(__type: c_ulong) c_ulong;
pub extern "c" fn getauxval(__type: c_ulong) c_ulong;

pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int;
pub extern fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
18 changes: 9 additions & 9 deletions std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ pub const ChildProcess = struct {
os.ChangeCurDirError || windows.CreateProcessError;

pub const Term = union(enum) {
Exited: i32,
Signal: i32,
Stopped: i32,
Unknown: i32,
Exited: u32,
Signal: u32,
Stopped: u32,
Unknown: u32,
};

pub const StdIo = enum {
Expand Down Expand Up @@ -155,7 +155,7 @@ pub const ChildProcess = struct {
}

pub const ExecResult = struct {
term: os.ChildProcess.Term,
term: Term,
stdout: []u8,
stderr: []u8,
};
Expand Down Expand Up @@ -224,7 +224,7 @@ pub const ChildProcess = struct {
if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
break :x Term{ .Unknown = 0 };
} else {
break :x Term{ .Exited = @bitCast(i32, exit_code) };
break :x Term{ .Exited = exit_code };
}
});

Expand All @@ -240,7 +240,7 @@ pub const ChildProcess = struct {
self.handleWaitResult(status);
}

fn handleWaitResult(self: *ChildProcess, status: i32) void {
fn handleWaitResult(self: *ChildProcess, status: u32) void {
self.term = self.cleanupAfterWait(status);
}

Expand All @@ -259,7 +259,7 @@ pub const ChildProcess = struct {
}
}

fn cleanupAfterWait(self: *ChildProcess, status: i32) !Term {
fn cleanupAfterWait(self: *ChildProcess, status: u32) !Term {
defer {
os.close(self.err_pipe[0]);
os.close(self.err_pipe[1]);
Expand All @@ -281,7 +281,7 @@ pub const ChildProcess = struct {
return statusToTerm(status);
}

fn statusToTerm(status: i32) Term {
fn statusToTerm(status: u32) Term {
return if (os.WIFEXITED(status))
Term{ .Exited = os.WEXITSTATUS(status) }
else if (os.WIFSIGNALED(status))
Expand Down
2 changes: 1 addition & 1 deletion std/cstr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test "cstr fns" {

fn testCStrFnsImpl() void {
testing.expect(cmp(c"aoeu", c"aoez") == -1);
testing.expect(len(c"123456789") == 9);
testing.expect(mem.len(u8, c"123456789") == 9);
}

/// Returns a mutable slice with 1 more byte of length which is a null byte.
Expand Down
38 changes: 13 additions & 25 deletions std/dynamic_library.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const os = std.os;
const assert = std.debug.assert;
const testing = std.testing;
const elf = std.elf;
const windows = os.windows;
const win_util = @import("os/windows/util.zig");
const windows = std.os.windows;
const maxInt = std.math.maxInt;

pub const DynLib = switch (builtin.os) {
Expand Down Expand Up @@ -102,39 +101,35 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
pub const LinuxDynLib = struct {
elf_lib: ElfLib,
fd: i32,
map_addr: usize,
map_size: usize,
memory: []align(mem.page_size) u8,

/// Trusts the file
pub fn open(allocator: *mem.Allocator, path: []const u8) !DynLib {
const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC);
errdefer std.os.close(fd);
errdefer os.close(fd);

const size = @intCast(usize, (try std.os.posixFStat(fd)).size);
const size = @intCast(usize, (try os.fstat(fd)).size);

const addr = os.mmap(
const bytes = try os.mmap(
null,
size,
os.PROT_READ | os.PROT_EXEC,
os.MAP_PRIVATE | os.MAP_LOCKED,
fd,
0,
);
errdefer os.munmap(addr, size);

const bytes = @intToPtr([*]align(mem.page_size) u8, addr)[0..size];
errdefer os.munmap(bytes);

return DynLib{
.elf_lib = try ElfLib.init(bytes),
.fd = fd,
.map_addr = addr,
.map_size = size,
.memory = bytes,
};
}

pub fn close(self: *DynLib) void {
os.munmap(self.map_addr, self.map_size);
std.os.close(self.fd);
os.munmap(self.memory);
os.close(self.fd);
self.* = undefined;
}

Expand Down Expand Up @@ -253,28 +248,21 @@ pub const WindowsDynLib = struct {
dll: windows.HMODULE,

pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib {
const wpath = try win_util.sliceToPrefixedFileW(path);
const wpath = try windows.sliceToPrefixedFileW(path);

return WindowsDynLib{
.allocator = allocator,
.dll = windows.LoadLibraryW(&wpath) orelse {
switch (windows.GetLastError()) {
windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound,
else => |err| return windows.unexpectedError(err),
}
},
.dll = try windows.LoadLibraryW(&wpath),
};
}

pub fn close(self: *WindowsDynLib) void {
assert(windows.FreeLibrary(self.dll) != 0);
windows.FreeLibrary(self.dll);
self.* = undefined;
}

pub fn lookup(self: *WindowsDynLib, name: []const u8) ?usize {
return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr));
return @ptrToInt(windows.kernel32.GetProcAddress(self.dll, name.ptr));
}
};

Expand Down
Loading

0 comments on commit 0c6ab61

Please sign in to comment.