organize std and make import relative to current file

closes #216
master
Andrew Kelley 2017-03-26 06:39:28 -04:00
parent 7ce753a16b
commit 5bc9feb5cb
30 changed files with 67 additions and 72 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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));

View File

@ -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");

View File

@ -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;

View File

@ -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");

View File

@ -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");

View File

@ -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;

View File

@ -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");

View File

@ -1,12 +1,16 @@
const posix = switch(@compileVar("os")) {
Os.linux => @import("linux.zig"),
Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"),
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 windows = @import("windows.zig");
const errno = @import("errno.zig");
const linking_libc = @import("build.zig").linking_libc;
const c = @import("c/index.zig");
const linking_libc = @import("../target.zig").linking_libc;
const c = @import("../c/index.zig");
error Unexpected;

View File

@ -4,14 +4,10 @@
const root = @import("@root");
const std = @import("std");
const want_main_symbol = std.build.linking_libc;
const want_main_symbol = std.target.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"),
};
const exit = std.os.posix.exit;
var argc: usize = undefined;
var argv: &&u8 = undefined;

View File

@ -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);

View File

@ -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"),
};

View File

@ -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" {

View File

@ -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 {