zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 5bc9feb5cb98fc13db62d01b2b9fec15677310a7 (tree)
parent 7ce753a16b0c16b4c6494467f42f2d5fe9a235e6
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sun, 26 Mar 2017 06:39:28 -0400

organize std and make import relative to current file

closes #216

Diffstat:
MCMakeLists.txt | 28++++++++++++++--------------
Msrc/all_types.hpp | 1+
Msrc/analyze.cpp | 9+++------
Msrc/codegen.cpp | 12+++++++-----
Msrc/ir.cpp | 5++++-
Msrc/link.cpp | 8++++----
Dstd/bootstrap.zig | 63---------------------------------------------------------------
Mstd/c/index.zig | 10+++++-----
Mstd/debug.zig | 2+-
Mstd/index.zig | 15++++-----------
Mstd/io.zig | 8++++----
Mstd/mem.zig | 2+-
Mstd/net.zig | 4++--
Dstd/os.zig | 74--------------------------------------------------------------------------
Rstd/darwin.zig -> std/os/darwin.zig | 0
Rstd/darwin_x86_64.zig -> std/os/darwin_x86_64.zig | 0
Rstd/errno.zig -> std/os/errno.zig | 0
Astd/os/index.zig | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rstd/linux.zig -> std/os/linux.zig | 0
Rstd/linux_i386.zig -> std/os/linux_i386.zig | 0
Rstd/linux_x86_64.zig -> std/os/linux_x86_64.zig | 0
Rstd/windows.zig -> std/os/windows.zig | 0
Astd/special/bootstrap.zig | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rstd/builtin.zig -> std/special/builtin.zig | 0
Rstd/compiler_rt.zig -> std/special/compiler_rt.zig | 0
Rstd/panic.zig -> std/special/panic.zig | 0
Rstd/test_runner.zig -> std/special/test_runner.zig | 0
Rstd/build.zig -> std/target.zig | 0
Mtest/cases/import.zig | 2+-
Mtest/cases/namespace_depends_on_compile_var/index.zig | 4++--
Mtest/cases/pub_enum/index.zig | 2+-
Mtest/run_tests.cpp | 3+--
32 files changed, 192 insertions(+), 197 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -201,41 +201,41 @@ install(TARGETS zig DESTINATION bin) install(FILES ${C_HEADERS} DESTINATION ${C_HEADERS_DEST}) -install(FILES "${CMAKE_SOURCE_DIR}/std/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/build.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/builtin.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/c/darwin.zig" DESTINATION "${ZIG_STD_DEST}/c") install(FILES "${CMAKE_SOURCE_DIR}/std/c/index.zig" DESTINATION "${ZIG_STD_DEST}/c") install(FILES "${CMAKE_SOURCE_DIR}/std/c/linux.zig" DESTINATION "${ZIG_STD_DEST}/c") install(FILES "${CMAKE_SOURCE_DIR}/std/c/windows.zig" DESTINATION "${ZIG_STD_DEST}/c") -install(FILES "${CMAKE_SOURCE_DIR}/std/compiler_rt.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/darwin.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/darwin_x86_64.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/dwarf.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/elf.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/endian.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/fmt.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/panic.zig" DESTINATION "${ZIG_STD_DEST}") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/darwin.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/darwin_x86_64.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/errno.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/index.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/linux.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/windows.zig" DESTINATION "${ZIG_STD_DEST}/os") install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/windows.zig" DESTINATION "${ZIG_STD_DEST}") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/builtin.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/panic.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/test_runner.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/target.zig" DESTINATION "${ZIG_STD_DEST}") add_executable(run_tests ${TEST_SOURCES}) target_link_libraries(run_tests) diff --git a/src/all_types.hpp b/src/all_types.hpp @@ -1362,6 +1362,7 @@ struct CodeGen { Buf *libc_static_lib_dir; Buf *libc_include_dir; Buf *zig_std_dir; + Buf *zig_std_special_dir; Buf *dynamic_linker; Buf *ar_path; Buf triple_str; diff --git a/src/analyze.cpp b/src/analyze.cpp @@ -2874,11 +2874,8 @@ void preview_use_decl(CodeGen *g, AstNode *node) { ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *src_dirname, Buf *src_basename, Buf *source_code) { - Buf *full_path = buf_alloc(); - os_path_join(src_dirname, src_basename, full_path); - if (g->verbose) { - fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(full_path)); + fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(abs_full_path)); fprintf(stderr, "----------------\n"); fprintf(stderr, "%s\n", buf_ptr(source_code)); @@ -2890,7 +2887,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, tokenize(source_code, &tokenization); if (tokenization.err) { - ErrorMsg *err = err_msg_create_with_line(full_path, tokenization.err_line, tokenization.err_column, + ErrorMsg *err = err_msg_create_with_line(abs_full_path, tokenization.err_line, tokenization.err_column, source_code, tokenization.line_offsets, tokenization.err); print_err_msg(err, g->err_color); @@ -2908,7 +2905,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, import_entry->package = package; import_entry->source_code = source_code; import_entry->line_offsets = tokenization.line_offsets; - import_entry->path = full_path; + import_entry->path = abs_full_path; import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color, &g->next_node_index); diff --git a/src/codegen.cpp b/src/codegen.cpp @@ -80,6 +80,9 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) { g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); g->zig_std_dir = buf_create_from_str(ZIG_STD_DIR); + g->zig_std_special_dir = buf_alloc(); + os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir); + if (target) { // cross compiling, so we can't rely on all the configured stuff since @@ -4237,10 +4240,9 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source } static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) { - Buf *std_dir = g->zig_std_dir; Buf *code_basename = buf_create_from_str(basename); Buf path_to_code_src = BUF_INIT; - os_path_join(std_dir, code_basename, &path_to_code_src); + os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src); Buf *abs_full_path = buf_alloc(); int err; if ((err = os_path_real(&path_to_code_src, abs_full_path))) { @@ -4251,18 +4253,18 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err)); } - return add_source_file(g, package, abs_full_path, std_dir, code_basename, import_code); + return add_source_file(g, package, abs_full_path, g->zig_std_special_dir, code_basename, import_code); } static PackageTableEntry *create_bootstrap_pkg(CodeGen *g) { - PackageTableEntry *package = new_package(buf_ptr(g->zig_std_dir), ""); + PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), ""); package->package_table.put(buf_create_from_str("std"), g->std_package); package->package_table.put(buf_create_from_str("@root"), g->root_package); return package; } static PackageTableEntry *create_panic_pkg(CodeGen *g) { - PackageTableEntry *package = new_package(buf_ptr(g->zig_std_dir), ""); + PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), ""); package->package_table.put(buf_create_from_str("std"), g->std_package); return package; } diff --git a/src/ir.cpp b/src/ir.cpp @@ -10385,7 +10385,10 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi // try it as a filename target_package = import->package; import_target_path = import_target_str; - search_dir = &import->package->root_src_dir; + + // search relative to importing file + search_dir = buf_alloc(); + os_path_dirname(import->path, search_dir); } Buf full_path = BUF_INIT; diff --git a/src/link.cpp b/src/link.cpp @@ -42,10 +42,9 @@ static const char *get_o_file_extension(CodeGen *g) { static Buf *build_o(CodeGen *parent_gen, const char *oname) { Buf *source_basename = buf_sprintf("%s.zig", oname); - Buf *std_dir_path = parent_gen->zig_std_dir; ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target; - CodeGen *child_gen = codegen_create(std_dir_path, child_target); + CodeGen *child_gen = codegen_create(parent_gen->zig_std_special_dir, child_target); child_gen->link_libc = parent_gen->link_libc; child_gen->link_libs.resize(parent_gen->link_libs.length); @@ -70,13 +69,14 @@ static Buf *build_o(CodeGen *parent_gen, const char *oname) { codegen_set_mios_version_min(child_gen, parent_gen->mios_version_min); Buf *full_path = buf_alloc(); - os_path_join(std_dir_path, source_basename, full_path); + os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path); + Buf source_code = BUF_INIT; if (os_fetch_file_path(full_path, &source_code)) { zig_panic("unable to fetch file: %s\n", buf_ptr(full_path)); } - codegen_add_root_code(child_gen, std_dir_path, source_basename, &source_code); + codegen_add_root_code(child_gen, parent_gen->zig_std_special_dir, source_basename, &source_code); const char *o_ext = get_o_file_extension(child_gen); Buf *o_out = buf_sprintf("%s%s", oname, o_ext); codegen_link(child_gen, buf_ptr(o_out)); diff --git a/std/bootstrap.zig b/std/bootstrap.zig @@ -1,63 +0,0 @@ -// This file is in a package which has the root source file exposed as "@root". -// It is included in the compilation unit when exporting an executable. - -const root = @import("@root"); -const std = @import("std"); - -const want_main_symbol = std.build.linking_libc; -const want_start_symbol = !want_main_symbol; - -const exit = switch(@compileVar("os")) { - Os.linux => std.linux.exit, - Os.darwin => std.darwin.exit, - else => @compileError("Unsupported OS"), -}; - -var argc: usize = undefined; -var argv: &&u8 = undefined; - -export nakedcc fn _start() -> noreturn { - @setFnVisible(this, want_start_symbol); - if (!want_start_symbol) { - unreachable; - } - - switch (@compileVar("arch")) { - Arch.x86_64 => { - argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize)); - argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8)); - }, - Arch.i386 => { - argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize)); - argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8)); - }, - else => @compileError("unsupported arch"), - } - callMainAndExit() -} - -fn callMain() -> %void { - const args = @alloca([]u8, argc); - for (args) |_, i| { - const ptr = argv[i]; - args[i] = ptr[0...std.cstr.len(ptr)]; - } - return root.main(args); -} - -fn callMainAndExit() -> noreturn { - callMain() %% exit(1); - exit(0); -} - -export fn main(c_argc: i32, c_argv: &&u8) -> i32 { - @setFnVisible(this, want_main_symbol); - if (!want_main_symbol) { - unreachable; - } - - argc = usize(c_argc); - argv = c_argv; - callMain() %% return 1; - return 0; -} diff --git a/std/c/index.zig b/std/c/index.zig @@ -1,13 +1,13 @@ -pub use @import("errno.zig"); +pub use @import("../os/errno.zig"); pub use switch(@compileVar("os")) { - Os.linux => @import("c/linux.zig"), - Os.windows => @import("c/windows.zig"), - Os.darwin, Os.macosx, Os.ios => @import("c/darwin.zig"), + Os.linux => @import("linux.zig"), + Os.windows => @import("windows.zig"), + Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"), else => empty_import, }; pub extern fn abort() -> noreturn; -const empty_import = @import("empty.zig"); +const empty_import = @import("../empty.zig"); diff --git a/std/debug.zig b/std/debug.zig @@ -1,6 +1,6 @@ const mem = @import("mem.zig"); const io = @import("io.zig"); -const os = @import("os.zig"); +const os = @import("os/index.zig"); const elf = @import("elf.zig"); const DW = @import("dwarf.zig"); const List = @import("list.zig").List; diff --git a/std/index.zig b/std/index.zig @@ -1,6 +1,7 @@ -pub const build = @import("build.zig"); +pub const c = @import("c/index.zig"); pub const cstr = @import("cstr.zig"); pub const debug = @import("debug.zig"); +pub const empty_import = @import("empty.zig"); pub const fmt = @import("fmt.zig"); pub const hash_map = @import("hash_map.zig"); pub const io = @import("io.zig"); @@ -8,15 +9,7 @@ pub const list = @import("list.zig"); pub const math = @import("math.zig"); pub const mem = @import("mem.zig"); pub const net = @import("net.zig"); -pub const os = @import("os.zig"); +pub const os = @import("os/index.zig"); pub const rand = @import("rand.zig"); pub const sort = @import("sort.zig"); -pub const linux = switch(@compileVar("os")) { - Os.linux => @import("linux.zig"), - else => empty_import, -}; -pub const darwin = switch(@compileVar("os")) { - Os.darwin => @import("darwin.zig"), - else => empty_import, -}; -pub const empty_import = @import("empty.zig"); +pub const target = @import("target.zig"); diff --git a/std/io.zig b/std/io.zig @@ -1,14 +1,14 @@ const system = switch(@compileVar("os")) { - Os.linux => @import("linux.zig"), - Os.darwin => @import("darwin.zig"), + Os.linux => @import("os/linux.zig"), + Os.darwin => @import("os/darwin.zig"), else => @compileError("Unsupported OS"), }; -const errno = @import("errno.zig"); +const errno = @import("os/errno.zig"); const math = @import("math.zig"); const debug = @import("debug.zig"); const assert = debug.assert; -const os = @import("os.zig"); +const os = @import("os/index.zig"); const mem = @import("mem.zig"); const Buffer0 = @import("cstr.zig").Buffer0; const fmt = @import("fmt.zig"); diff --git a/std/mem.zig b/std/mem.zig @@ -1,6 +1,6 @@ const assert = @import("debug.zig").assert; const math = @import("math.zig"); -const os = @import("os.zig"); +const os = @import("os/index.zig"); const io = @import("io.zig"); pub const Cmp = math.Cmp; diff --git a/std/net.zig b/std/net.zig @@ -1,5 +1,5 @@ -const linux = @import("linux.zig"); -const errno = @import("errno.zig"); +const linux = @import("os/linux.zig"); +const errno = @import("os/errno.zig"); const assert = @import("debug.zig").assert; const endian = @import("endian.zig"); diff --git a/std/os.zig b/std/os.zig @@ -1,74 +0,0 @@ -const posix = switch(@compileVar("os")) { - Os.linux => @import("linux.zig"), - Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"), - else => @compileError("Unsupported OS"), -}; -const windows = @import("windows.zig"); -const errno = @import("errno.zig"); -const linking_libc = @import("build.zig").linking_libc; -const c = @import("c/index.zig"); - -error Unexpected; - -/// Fills `buf` with random bytes. If linking against libc, this calls the -/// appropriate OS-specific library call. Otherwise it uses the zig standard -/// library implementation. -pub fn getRandomBytes(buf: []u8) -> %void { - while (true) { - const err = switch (@compileVar("os")) { - Os.linux => { - if (linking_libc) { - if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0 - } else { - posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)) - } - }, - Os.darwin, Os.macosx, Os.ios => { - if (linking_libc) { - if (posix.getrandom(buf.ptr, buf.len) == -1) *c._errno() else 0 - } else { - posix.getErrno(posix.getrandom(buf.ptr, buf.len)) - } - }, - Os.windows => { - var hCryptProv: windows.HCRYPTPROV = undefined; - if (!windows.CryptAcquireContext(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { - return error.Unexpected; - } - defer _ = windows.CryptReleaseContext(hCryptProv, 0); - - if (!windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr)) { - return error.Unexpected; - } - return; - }, - else => @compileError("Unsupported OS"), - }; - if (err > 0) { - return switch (err) { - errno.EINVAL => unreachable, - errno.EFAULT => unreachable, - errno.EINTR => continue, - else => error.Unexpected, - } - } - return; - } -} - -/// Raises a signal in the current kernel thread, ending its execution. -/// If linking against libc, this calls the abort() libc function. Otherwise -/// it uses the zig standard library implementation. -pub coldcc fn abort() -> noreturn { - if (linking_libc) { - c.abort(); - } - switch (@compileVar("os")) { - Os.linux => { - _ = posix.raise(posix.SIGABRT); - _ = posix.raise(posix.SIGKILL); - while (true) {} - }, - else => @compileError("Unsupported OS"), - } -} diff --git a/std/darwin.zig b/std/os/darwin.zig diff --git a/std/darwin_x86_64.zig b/std/os/darwin_x86_64.zig diff --git a/std/errno.zig b/std/os/errno.zig diff --git a/std/os/index.zig b/std/os/index.zig @@ -0,0 +1,78 @@ +pub const windows = @import("windows.zig"); +pub const darwin = @import("darwin.zig"); +pub const linux = @import("linux.zig"); +pub const posix = switch(@compileVar("os")) { + Os.linux => linux, + Os.darwin, Os.macosx, Os.ios => darwin, + Os.windows => windows, + else => @compileError("Unsupported OS"), +}; + +const errno = @import("errno.zig"); +const linking_libc = @import("../target.zig").linking_libc; +const c = @import("../c/index.zig"); + +error Unexpected; + +/// Fills `buf` with random bytes. If linking against libc, this calls the +/// appropriate OS-specific library call. Otherwise it uses the zig standard +/// library implementation. +pub fn getRandomBytes(buf: []u8) -> %void { + while (true) { + const err = switch (@compileVar("os")) { + Os.linux => { + if (linking_libc) { + if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0 + } else { + posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)) + } + }, + Os.darwin, Os.macosx, Os.ios => { + if (linking_libc) { + if (posix.getrandom(buf.ptr, buf.len) == -1) *c._errno() else 0 + } else { + posix.getErrno(posix.getrandom(buf.ptr, buf.len)) + } + }, + Os.windows => { + var hCryptProv: windows.HCRYPTPROV = undefined; + if (!windows.CryptAcquireContext(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { + return error.Unexpected; + } + defer _ = windows.CryptReleaseContext(hCryptProv, 0); + + if (!windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr)) { + return error.Unexpected; + } + return; + }, + else => @compileError("Unsupported OS"), + }; + if (err > 0) { + return switch (err) { + errno.EINVAL => unreachable, + errno.EFAULT => unreachable, + errno.EINTR => continue, + else => error.Unexpected, + } + } + return; + } +} + +/// Raises a signal in the current kernel thread, ending its execution. +/// If linking against libc, this calls the abort() libc function. Otherwise +/// it uses the zig standard library implementation. +pub coldcc fn abort() -> noreturn { + if (linking_libc) { + c.abort(); + } + switch (@compileVar("os")) { + Os.linux => { + _ = posix.raise(posix.SIGABRT); + _ = posix.raise(posix.SIGKILL); + while (true) {} + }, + else => @compileError("Unsupported OS"), + } +} diff --git a/std/linux.zig b/std/os/linux.zig diff --git a/std/linux_i386.zig b/std/os/linux_i386.zig diff --git a/std/linux_x86_64.zig b/std/os/linux_x86_64.zig diff --git a/std/windows.zig b/std/os/windows.zig diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig @@ -0,0 +1,59 @@ +// This file is in a package which has the root source file exposed as "@root". +// It is included in the compilation unit when exporting an executable. + +const root = @import("@root"); +const std = @import("std"); + +const want_main_symbol = std.target.linking_libc; +const want_start_symbol = !want_main_symbol; + +const exit = std.os.posix.exit; + +var argc: usize = undefined; +var argv: &&u8 = undefined; + +export nakedcc fn _start() -> noreturn { + @setFnVisible(this, want_start_symbol); + if (!want_start_symbol) { + unreachable; + } + + switch (@compileVar("arch")) { + Arch.x86_64 => { + argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize)); + argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8)); + }, + Arch.i386 => { + argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize)); + argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8)); + }, + else => @compileError("unsupported arch"), + } + callMainAndExit() +} + +fn callMain() -> %void { + const args = @alloca([]u8, argc); + for (args) |_, i| { + const ptr = argv[i]; + args[i] = ptr[0...std.cstr.len(ptr)]; + } + return root.main(args); +} + +fn callMainAndExit() -> noreturn { + callMain() %% exit(1); + exit(0); +} + +export fn main(c_argc: i32, c_argv: &&u8) -> i32 { + @setFnVisible(this, want_main_symbol); + if (!want_main_symbol) { + unreachable; + } + + argc = usize(c_argc); + argv = c_argv; + callMain() %% return 1; + return 0; +} diff --git a/std/builtin.zig b/std/special/builtin.zig diff --git a/std/compiler_rt.zig b/std/special/compiler_rt.zig diff --git a/std/panic.zig b/std/special/panic.zig diff --git a/std/test_runner.zig b/std/special/test_runner.zig diff --git a/std/build.zig b/std/target.zig diff --git a/test/cases/import.zig b/test/cases/import.zig @@ -1,5 +1,5 @@ const assert = @import("std").debug.assert; -const a_namespace = @import("cases/import/a_namespace.zig"); +const a_namespace = @import("import/a_namespace.zig"); test "callFnViaNamespaceLookup" { assert(a_namespace.foo() == 1234); diff --git a/test/cases/namespace_depends_on_compile_var/index.zig b/test/cases/namespace_depends_on_compile_var/index.zig @@ -8,6 +8,6 @@ test "namespaceDependsOnCompileVar" { } } const some_namespace = switch(@compileVar("os")) { - Os.linux => @import("cases/namespace_depends_on_compile_var/a.zig"), - else => @import("cases/namespace_depends_on_compile_var/b.zig"), + Os.linux => @import("a.zig"), + else => @import("b.zig"), }; diff --git a/test/cases/pub_enum/index.zig b/test/cases/pub_enum/index.zig @@ -1,4 +1,4 @@ -const other = @import("cases/pub_enum/other.zig"); +const other = @import("other.zig"); const assert = @import("std").debug.assert; test "pubEnum" { diff --git a/test/run_tests.cpp b/test/run_tests.cpp @@ -1254,8 +1254,7 @@ export fn entry() -> usize { @sizeOf(@typeOf(seventh_fib_number)) } const resource = @embedFile("bogus.txt"); export fn entry() -> usize { @sizeOf(@typeOf(resource)) } - )SOURCE", 1, ".tmp_source.zig:2:29: error: unable to find './bogus.txt'"); - + )SOURCE", 2, ".tmp_source.zig:2:29: error: unable to find '", "/bogus.txt'"); add_compile_fail_case("non-const expression in struct literal outside function", R"SOURCE( const Foo = struct {